Spaces:
Sleeping
Sleeping
Commit
·
6c13449
1
Parent(s):
f111ba0
image cv2
Browse files
main.py
CHANGED
@@ -49,11 +49,13 @@ def get_masks():
|
|
49 |
if image_file.filename == '':
|
50 |
return jsonify({"error": "No image file provided"}), 400
|
51 |
|
52 |
-
|
53 |
-
# Convert the
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
|
58 |
if image is None:
|
59 |
raise ValueError("Image not found or unable to read.")
|
@@ -73,20 +75,20 @@ def get_masks():
|
|
73 |
segmentation[10, -10] or segmentation[-10, -10])
|
74 |
return val
|
75 |
|
76 |
-
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
|
86 |
-
|
87 |
-
|
88 |
|
89 |
-
|
90 |
masks = sorted(masks, key=(lambda x: x['area']), reverse=True)
|
91 |
|
92 |
# Create a zip file in memory
|
|
|
49 |
if image_file.filename == '':
|
50 |
return jsonify({"error": "No image file provided"}), 400
|
51 |
|
52 |
+
# Read image file using OpenCV-style approach (similar to cv2.imread)
|
53 |
+
# Convert the image file to a NumPy array using OpenCV
|
54 |
+
file_bytes = np.fromstring(image_file.read(), np.uint8)
|
55 |
+
image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
56 |
+
|
57 |
+
# Convert BGR to RGB using OpenCV (similar to cv2.cvtColor)
|
58 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
59 |
|
60 |
if image is None:
|
61 |
raise ValueError("Image not found or unable to read.")
|
|
|
75 |
segmentation[10, -10] or segmentation[-10, -10])
|
76 |
return val
|
77 |
|
78 |
+
masks = [mask for mask in masks if not is_background(mask['segmentation'])]
|
79 |
|
80 |
+
for i in range(0, len(masks) - 1)[::-1]:
|
81 |
+
large_mask = masks[i]['segmentation']
|
82 |
+
for j in range(i+1, len(masks)):
|
83 |
+
not_small_mask = np.logical_not(masks[j]['segmentation'])
|
84 |
+
masks[i]['segmentation'] = np.logical_and(large_mask, not_small_mask)
|
85 |
+
masks[i]['area'] = masks[i]['segmentation'].sum()
|
86 |
+
large_mask = masks[i]['segmentation']
|
87 |
|
88 |
+
def sum_under_threshold(segmentation, threshold):
|
89 |
+
return segmentation.sum() / segmentation.size < 0.0015
|
90 |
|
91 |
+
masks = [mask for mask in masks if not sum_under_threshold(mask['segmentation'], 100)]
|
92 |
masks = sorted(masks, key=(lambda x: x['area']), reverse=True)
|
93 |
|
94 |
# Create a zip file in memory
|