doublelotus commited on
Commit
6c13449
·
1 Parent(s): f111ba0
Files changed (1) hide show
  1. main.py +18 -16
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
- raw_image = Image.open(image_file).convert("RGB")
53
- # Convert the PIL Image to a NumPy array
54
- image_array = np.array(raw_image)
55
- # Since OpenCV expects BGR, convert RGB to BGR
56
- image = image_array[:, :, ::-1]
 
 
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
- # masks = [mask for mask in masks if not is_background(mask['segmentation'])]
77
 
78
- # for i in range(0, len(masks) - 1)[::-1]:
79
- # large_mask = masks[i]['segmentation']
80
- # for j in range(i+1, len(masks)):
81
- # not_small_mask = np.logical_not(masks[j]['segmentation'])
82
- # masks[i]['segmentation'] = np.logical_and(large_mask, not_small_mask)
83
- # masks[i]['area'] = masks[i]['segmentation'].sum()
84
- # large_mask = masks[i]['segmentation']
85
 
86
- # def sum_under_threshold(segmentation, threshold):
87
- # return segmentation.sum() / segmentation.size < 0.0015
88
 
89
- # masks = [mask for mask in masks if not sum_under_threshold(mask['segmentation'], 100)]
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