Update app.py
Browse files
app.py
CHANGED
@@ -38,25 +38,33 @@ if api_key:
|
|
38 |
CLIENT = setup_roboflow_client(api_key)
|
39 |
|
40 |
def segment_image(image_path):
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
def get_image_embedding(image):
|
62 |
image_tensor = preprocess_val(image).unsqueeze(0).to(device)
|
|
|
38 |
CLIENT = setup_roboflow_client(api_key)
|
39 |
|
40 |
def segment_image(image_path):
|
41 |
+
try:
|
42 |
+
results = CLIENT.infer(image_path, model_id="closet/1")
|
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']
|
51 |
+
pts = np.array([[p['x'], p['y']] for p in points], np.int32)
|
52 |
+
scale_x = image.shape[1] / results['image']['width']
|
53 |
+
scale_y = image.shape[0] / results['image']['height']
|
54 |
+
pts = pts * [scale_x, scale_y]
|
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.")
|
62 |
+
segmented_image = image
|
63 |
+
|
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 |
+
return Image.open(image_path) # Return original image if segmentation fails
|
68 |
|
69 |
def get_image_embedding(image):
|
70 |
image_tensor = preprocess_val(image).unsqueeze(0).to(device)
|