Skip to content

Commit

Permalink
[Feat] gcs에 이미지 추가 #31
Browse files Browse the repository at this point in the history
- frontend에서 이미지 upload 시 gcs에 추가
- 추후 inference 전 이미지 선택 시 gcs 경로의 이미지로 변경 계획
  • Loading branch information
Hyunmin-H committed Aug 14, 2023
1 parent 782f5eb commit b90ef84
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
7 changes: 4 additions & 3 deletions backend/app/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from PIL import Image

import streamlit as st
import base64
import socket

sys.path.append('/opt/ml/level3_cv_finalproject-cv-12/backend/gcp')
from cloud_storage import GCSUploader, load_gcp_config_from_yaml
Expand Down Expand Up @@ -89,8 +89,9 @@ def show_garments_and_checkboxes(category):
cols = st.columns(num_columns)
for i, filename in enumerate(filenames):
im_dir = os.path.join(category_dir, filename)
garment_img = Image.open(im_dir)
garment_byte = read_image_as_bytes(im_dir)
# garment_img = Image.open(im_dir)
# garment_byte = read_image_as_bytes(im_dir)
garment_img = gcs_uploader.read_image_from_gcs(im_dir)
# st.image(garment_img, caption=filename[:-4], width=100)
cols[i % num_columns].image(garment_img, width=100, use_column_width=True, caption=filename[:-4])
# if st.checkbox(filename[:-4]) :
Expand Down
14 changes: 9 additions & 5 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@
from tqdm import tqdm
from transformers import CLIPTextModel, CLIPTokenizer, CLIPVisionModelWithProjection, AutoProcessor
from models.AutoencoderKL import AutoencoderKL

import shutil

sys.path.append('/opt/ml/level3_cv_finalproject-cv-12/backend/gcp')
from cloud_storage import GCSUploader, load_gcp_config_from_yaml

app = FastAPI()
ladi_models = None

db_dir = '/opt/ml/user_db'

gcp_config = load_gcp_config_from_yaml("/opt/ml/level3_cv_finalproject-cv-12/backend/config/gcs.yaml")
gcs_uploader = GCSUploader(gcp_config)
user_name = 'hi'

@app.post("/add_data", description="데이터 저장")
async def add_garment_to_db(files: List[UploadFile] = File(...)):
byte_string = await files[0].read() ##await
Expand All @@ -57,7 +62,8 @@ async def add_garment_to_db(files: List[UploadFile] = File(...)):
garment_image = Image.open(io.BytesIO(garment_bytes))
garment_image = garment_image.convert("RGB")

garment_image.save(os.path.join(db_dir, 'input/garment', category, f'{garment_name}'))
gcs_uploader.upload_blob(garment_bytes, os.path.join(user_name, 'input/garment', category, f'{garment_name}'))
# garment_image.save(os.path.join(db_dir, 'input/garment', category, f'{garment_name}'))

def read_image_as_bytes(image_path):
with open(image_path, "rb") as file:
Expand Down Expand Up @@ -233,8 +239,6 @@ async def make_order(files: List[UploadFile] = File(...)):
garment_image = Image.open(os.path.join(db_dir, 'input/garment', category, filename))
garment_image.save(f'{input_dir}/buffer/garment/{category}.jpg')


inference_allModels(category, db_dir)

return None

return None
Binary file not shown.
30 changes: 23 additions & 7 deletions backend/gcp/cloud_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,36 @@ def upload_blob(self, source_file_data: bytes, destination_blob_name: str) -> st
return user_url

# Uploads image to GCS and returns the URL
def save_image_to_gcs(self, urls: list) -> str:
image_urls = []
for i, (byte_arr, url_name) in enumerate(urls):
url = self.upload_blob(byte_arr, url_name)
image_urls.append(url)
# def save_image_to_gcs(self, urls: list) -> str:
# image_urls = []
# for i, (byte_arr, url_name) in enumerate(urls):
# url = self.upload_blob(byte_arr, url_name)
# image_urls.append(url)

return image_urls
# return image_urls

def list_images_in_folder(self, folder_name):
bucket = self.client.get_bucket(self.bucket_name)
blobs = bucket.list_blobs(prefix=folder_name)

image_names = [blob.name for blob in blobs if blob.name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif'))]
image_names = [blob.name.split("/")[-1] for blob in blobs if blob.name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif'))]

return image_names

def read_image_from_gcs(self, blob_name):
bucket = self.client.get_bucket(self.bucket_name)
blob = bucket.get_blob(blob_name)

if blob is None:
print(f"Blob '{blob_name}' not found in bucket '{self.bucket_name}'.")
return None

image_data = blob.download_as_bytes()
from PIL import Image
from io import BytesIO


return Image.open(BytesIO(image_data))

def load_gcp_config_from_yaml(yaml_path):
with open(yaml_path, 'r') as yaml_file:
Expand Down

0 comments on commit b90ef84

Please sign in to comment.