doublelotus commited on
Commit
05e5c9d
·
1 Parent(s): eb81225
Files changed (1) hide show
  1. main.py +33 -45
main.py CHANGED
@@ -14,9 +14,10 @@ CORS(app)
14
  cudaOrNah = "cuda" if torch.cuda.is_available() else "cpu"
15
  print(cudaOrNah)
16
 
17
- # Global model setup
18
- # Adjust the model type and checkpoint as needed
19
- # For example, using the "vit_l" model
 
20
  checkpoint = "sam_vit_l_0b3195.pth"
21
  model_type = "vit_l"
22
  sam = sam_model_registry[model_type](checkpoint=checkpoint)
@@ -29,7 +30,7 @@ print('Setup SAM model')
29
 
30
  @app.route('/')
31
  def hello():
32
- return {"hei": "Shredded to pieces"}
33
 
34
  @app.route('/health', methods=['GET'])
35
  def health_check():
@@ -39,45 +40,27 @@ def health_check():
39
  @app.route('/get-masks', methods=['POST'])
40
  def get_masks():
41
  try:
42
- print('Received image from frontend')
43
  # Get the image file from the request
44
  if 'image' not in request.files:
45
  return jsonify({"error": "No image file provided"}), 400
46
-
47
  image_file = request.files['image']
48
  if image_file.filename == '':
49
  return jsonify({"error": "No image file provided"}), 400
50
 
51
- # **Modified Section Starts Here**
52
-
53
- # Load the image with alpha channel to preserve transparency
54
- raw_image = Image.open(image_file).convert("RGBA")
55
- # Convert the PIL Image to a NumPy array (shape: H x W x 4)
56
  image_array = np.array(raw_image)
57
-
58
- # Extract the alpha channel to create a transparency mask
59
- alpha_channel = image_array[:, :, 3]
60
- transparency_mask = alpha_channel > 0 # True where pixel is opaque
61
-
62
- # Apply the transparency mask to the RGB channels
63
- # Set transparent pixels to black (or any background color)
64
- image_array[~transparency_mask, :3] = [0, 0, 0]
65
-
66
- # Discard the alpha channel as it's no longer needed
67
- image_array = image_array[:, :, :3]
68
-
69
- # Since OpenCV expects BGR format, convert RGB to BGR
70
  image = image_array[:, :, ::-1]
71
 
72
- # **Modified Section Ends Here**
73
-
74
  if image is None:
75
  raise ValueError("Image not found or unable to read.")
76
-
77
  if cudaOrNah == "cuda":
78
  torch.cuda.empty_cache()
79
-
80
- # Generate masks using the preprocessed image
81
  masks = mask_generator.generate(image)
82
 
83
  if cudaOrNah == "cuda":
@@ -85,23 +68,28 @@ def get_masks():
85
 
86
  masks = sorted(masks, key=(lambda x: x['area']), reverse=True)
87
 
88
- # Optional: Remove overlapping masks and small regions
89
- for i in range(len(masks) - 1, -1, -1):
90
- large_mask = masks[i]['segmentation']
91
- for j in range(i + 1, len(masks)):
92
- not_small_mask = np.logical_not(masks[j]['segmentation'])
93
- masks[i]['segmentation'] = np.logical_and(large_mask, not_small_mask)
94
- masks[i]['area'] = masks[i]['segmentation'].sum()
95
- large_mask = masks[i]['segmentation']
96
 
97
- # Filter out very small masks based on area threshold
98
- def sum_under_threshold(segmentation, threshold):
99
- return segmentation.sum() / segmentation.size < threshold
100
 
101
- masks = [mask for mask in masks if not sum_under_threshold(mask['segmentation'], 0.0015)]
 
 
 
 
 
 
 
 
 
 
 
102
  masks = sorted(masks, key=(lambda x: x['area']), reverse=True)
103
 
104
- # Create a zip file in memory containing the mask images
105
  zip_buffer = io.BytesIO()
106
  with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
107
  for idx, mask in enumerate(masks):
@@ -110,10 +98,10 @@ def get_masks():
110
  mask_io = io.BytesIO()
111
  mask_image.save(mask_io, format="PNG")
112
  mask_io.seek(0)
113
- zip_file.writestr(f'mask_{idx + 1}.png', mask_io.read())
114
 
115
  zip_buffer.seek(0)
116
-
117
  return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='masks.zip')
118
  except Exception as e:
119
  # Log the error message if needed
@@ -122,4 +110,4 @@ def get_masks():
122
  return jsonify({"error": "Error processing the image", "details": str(e)}), 400
123
 
124
  if __name__ == '__main__':
125
- app.run(debug=True)
 
14
  cudaOrNah = "cuda" if torch.cuda.is_available() else "cpu"
15
  print(cudaOrNah)
16
 
17
+ # Global model setup
18
+ # running out of memory adjusted
19
+ # checkpoint = "sam_vit_h_4b8939.pth"
20
+ # model_type = "vit_h"
21
  checkpoint = "sam_vit_l_0b3195.pth"
22
  model_type = "vit_l"
23
  sam = sam_model_registry[model_type](checkpoint=checkpoint)
 
30
 
31
  @app.route('/')
32
  def hello():
33
+ return {"hei": "Shredded to peices"}
34
 
35
  @app.route('/health', methods=['GET'])
36
  def health_check():
 
40
  @app.route('/get-masks', methods=['POST'])
41
  def get_masks():
42
  try:
43
+ print('received image from frontend')
44
  # Get the image file from the request
45
  if 'image' not in request.files:
46
  return jsonify({"error": "No image file provided"}), 400
47
+
48
  image_file = request.files['image']
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.")
60
+
61
  if cudaOrNah == "cuda":
62
  torch.cuda.empty_cache()
63
+
 
64
  masks = mask_generator.generate(image)
65
 
66
  if cudaOrNah == "cuda":
 
68
 
69
  masks = sorted(masks, key=(lambda x: x['area']), reverse=True)
70
 
71
+ def is_background(segmentation):
72
+ val = (segmentation[10, 10] or segmentation[-10, 10] or
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
93
  zip_buffer = io.BytesIO()
94
  with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
95
  for idx, mask in enumerate(masks):
 
98
  mask_io = io.BytesIO()
99
  mask_image.save(mask_io, format="PNG")
100
  mask_io.seek(0)
101
+ zip_file.writestr(f'mask_{idx+1}.png', mask_io.read())
102
 
103
  zip_buffer.seek(0)
104
+
105
  return send_file(zip_buffer, mimetype='application/zip', as_attachment=True, download_name='masks.zip')
106
  except Exception as e:
107
  # Log the error message if needed
 
110
  return jsonify({"error": "Error processing the image", "details": str(e)}), 400
111
 
112
  if __name__ == '__main__':
113
+ app.run(debug=True)