Update app.py
Browse files
app.py
CHANGED
@@ -39,12 +39,26 @@ if api_key:
|
|
39 |
|
40 |
def segment_image(image_path):
|
41 |
try:
|
42 |
-
|
|
|
|
|
43 |
|
|
|
|
|
|
|
|
|
44 |
image = cv2.imread(image_path)
|
45 |
image = cv2.resize(image, (800, 600))
|
46 |
mask = np.zeros(image.shape, dtype=np.uint8)
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
if 'predictions' in results:
|
49 |
for prediction in results['predictions']:
|
50 |
points = prediction['points']
|
@@ -55,7 +69,7 @@ if api_key:
|
|
55 |
pts = pts.astype(np.int32)
|
56 |
pts = pts.reshape((-1, 1, 2))
|
57 |
cv2.fillPoly(mask, [pts], color=(255, 255, 255)) # White mask
|
58 |
-
|
59 |
segmented_image = cv2.bitwise_and(image, mask)
|
60 |
else:
|
61 |
st.warning("No predictions found in the image. Returning original image.")
|
@@ -64,8 +78,8 @@ if api_key:
|
|
64 |
return Image.fromarray(cv2.cvtColor(segmented_image, cv2.COLOR_BGR2RGB))
|
65 |
except Exception as e:
|
66 |
st.error(f"Error in segmentation: {str(e)}")
|
67 |
-
|
68 |
-
|
69 |
def get_image_embedding(image):
|
70 |
image_tensor = preprocess_val(image).unsqueeze(0).to(device)
|
71 |
with torch.no_grad():
|
|
|
39 |
|
40 |
def segment_image(image_path):
|
41 |
try:
|
42 |
+
# 이미지 파일 읽기
|
43 |
+
with open(image_path, "rb") as image_file:
|
44 |
+
image_data = image_file.read()
|
45 |
|
46 |
+
# 이미지를 base64로 인코딩
|
47 |
+
encoded_image = base64.b64encode(image_data).decode('utf-8')
|
48 |
+
|
49 |
+
# 원본 이미지 로드 (여기로 이동)
|
50 |
image = cv2.imread(image_path)
|
51 |
image = cv2.resize(image, (800, 600))
|
52 |
mask = np.zeros(image.shape, dtype=np.uint8)
|
53 |
|
54 |
+
try:
|
55 |
+
# Roboflow API 호출
|
56 |
+
results = CLIENT.infer(encoded_image, model_id="closet/1")
|
57 |
+
except HTTPCallErrorError as api_error:
|
58 |
+
st.error(f"API Error: {api_error.description}")
|
59 |
+
st.error(f"API Message: {api_error.api_message}")
|
60 |
+
return Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
61 |
+
|
62 |
if 'predictions' in results:
|
63 |
for prediction in results['predictions']:
|
64 |
points = prediction['points']
|
|
|
69 |
pts = pts.astype(np.int32)
|
70 |
pts = pts.reshape((-1, 1, 2))
|
71 |
cv2.fillPoly(mask, [pts], color=(255, 255, 255)) # White mask
|
72 |
+
|
73 |
segmented_image = cv2.bitwise_and(image, mask)
|
74 |
else:
|
75 |
st.warning("No predictions found in the image. Returning original image.")
|
|
|
78 |
return Image.fromarray(cv2.cvtColor(segmented_image, cv2.COLOR_BGR2RGB))
|
79 |
except Exception as e:
|
80 |
st.error(f"Error in segmentation: {str(e)}")
|
81 |
+
# 원본 이미지를 다시 읽어 반환
|
82 |
+
return Image.open(image_path)
|
83 |
def get_image_embedding(image):
|
84 |
image_tensor = preprocess_val(image).unsqueeze(0).to(device)
|
85 |
with torch.no_grad():
|