Skip to content

Commit

Permalink
fix image not being masked and annotated while downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
sumn2u committed Jun 7, 2024
1 parent 95740d7 commit 3643af6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
75 changes: 53 additions & 22 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import json
from PIL import Image, ImageDraw
import os
import traceback
from dotenv import load_dotenv

app = Flask(__name__)
Expand Down Expand Up @@ -271,20 +272,25 @@ def download_image_with_annotations():
# Draw polygon with thicker outline
draw.line(scaled_points + [scaled_points[0]], fill=color, width=3) # Change width as desired
elif all(key in region for key in ('x', 'y', 'w', 'h')):
x = float(region['x'][1:-1]) * width # Remove brackets and convert to float
y = float(region['y'][1:-1]) * height
w = float(region['w'][1:-1]) * width
h = float(region['h'][1:-1]) * height
try:
x = float(region['x'][1:-1]) * width if isinstance(region['x'], str) else float(region['x'][0]) * width
y = float(region['y'][1:-1]) * height if isinstance(region['y'], str) else float(region['y'][0]) * height
w = float(region['w'][1:-1]) * width if isinstance(region['w'], str) else float(region['w'][0]) * width
h = float(region['h'][1:-1]) * height if isinstance(region['h'], str) else float(region['h'][0]) * height
except (ValueError, TypeError) as e:
raise ValueError(f"Invalid format in region dimensions: {region}, Error: {e}")
# Draw rectangle with thicker outline
draw.rectangle([x, y, x + w, y + h], outline=color, width=3)

elif all(key in region for key in ('rx', 'ry', 'rw', 'rh')):
rx = float(region['rx'][1:-1]) * width # Remove brackets and convert to float
ry = float(region['ry'][1:-1]) * height
rw = float(region['rw'][1:-1]) * width
rh = float(region['rh'][1:-1]) * height
try:
rx = float(region['rx'][1:-1]) * width if isinstance(region['rx'], str) else float(region['rx'][0]) * width
ry = float(region['ry'][1:-1]) * height if isinstance(region['ry'], str) else float(region['ry'][0]) * height
rw = float(region['rw'][1:-1]) * width if isinstance(region['rw'], str) else float(region['rw'][0]) * width
rh = float(region['rh'][1:-1]) * height if isinstance(region['rh'], str) else float(region['rh'][0]) * height
except (ValueError, TypeError) as e:
raise ValueError(f"Invalid format in region dimensions: {region}, Error: {e}")
# Draw ellipse (circle if rw and rh are equal)
draw.ellipse([rx, ry, rx + rw, ry + rh], outline=color, width=3)
draw.ellipse([rx, ry, rx + rw, ry + rh], outline=color, width=3)



Expand All @@ -294,10 +300,20 @@ def download_image_with_annotations():

return send_file(img_byte_arr, mimetype='image/png', as_attachment=True, download_name=image_info.get("image-name"))

except ValueError as ve:
print('ValueError:', ve)
traceback.print_exc()
return jsonify({'error': str(ve)}), 400
except requests.exceptions.RequestException as re:
print('RequestException:', re)
traceback.print_exc()
return jsonify({'error': 'Error fetching image from URL'}), 500
except Exception as e:
print('Error:', e)
print('General error:', e)
traceback.print_exc()
return jsonify({'error': str(e)}), 500


@app.route('/download_image_mask', methods=['POST'])
@cross_origin(origin=client_url, headers=['Content-Type'])
def download_image_mask():
Expand Down Expand Up @@ -336,28 +352,43 @@ def download_image_mask():
scaled_points = [(int(x * width), int(y * height)) for x, y in points]
draw.polygon(scaled_points, outline=color, fill=color)
elif all(key in region for key in ('x', 'y', 'w', 'h')):
x = float(region['x'][1:-1]) * width # Remove brackets and convert to float
y = float(region['y'][1:-1]) * height
w = float(region['w'][1:-1]) * width
h = float(region['h'][1:-1]) * height
try:
x = float(region['x'][1:-1]) * width if isinstance(region['x'], str) else float(region['x'][0]) * width
y = float(region['y'][1:-1]) * height if isinstance(region['y'], str) else float(region['y'][0]) * height
w = float(region['w'][1:-1]) * width if isinstance(region['w'], str) else float(region['w'][0]) * width
h = float(region['h'][1:-1]) * height if isinstance(region['h'], str) else float(region['h'][0]) * height
except (ValueError, TypeError) as e:
raise ValueError(f"Invalid format in region dimensions: {region}, Error: {e}")
# Draw rectangle for bounding box
draw.rectangle([x, y, x + w, y + h], outline=color, fill=color)
elif all(key in region for key in ('rx', 'ry', 'rw', 'rh')):
rx = float(region['rx'][1:-1]) * width # Remove brackets and convert to float
ry = float(region['ry'][1:-1]) * height
rw = float(region['rw'][1:-1]) * width
rh = float(region['rh'][1:-1]) * height
try:
rx = float(region['rx'][1:-1]) * width if isinstance(region['rx'], str) else float(region['rx'][0]) * width
ry = float(region['ry'][1:-1]) * height if isinstance(region['ry'], str) else float(region['ry'][0]) * height
rw = float(region['rw'][1:-1]) * width if isinstance(region['rw'], str) else float(region['rw'][0]) * width
rh = float(region['rh'][1:-1]) * height if isinstance(region['rh'], str) else float(region['rh'][0]) * height
except (ValueError, TypeError) as e:
raise ValueError(f"Invalid format in region dimensions: {region}, Error: {e}")
# Draw ellipse (circle if rw and rh are equal)
draw.ellipse([rx, ry, rx + rw, ry + rh], outline=color, fill=color)
draw.ellipse([rx, ry, rx + rw, ry + rh], outline=color, width=3)


mask_byte_arr = BytesIO()
mask.save(mask_byte_arr, format='PNG')
mask_byte_arr.seek(0)

return send_file(mask_byte_arr, mimetype='image/png', as_attachment=True, download_name=f"mask_{image_info.get('image-name')}")

except ValueError as ve:
print('ValueError:', ve)
traceback.print_exc()
return jsonify({'error': str(ve)}), 400
except requests.exceptions.RequestException as re:
print('RequestException:', re)
traceback.print_exc()
return jsonify({'error': 'Error fetching image from URL'}), 500
except Exception as e:
print('Error:', e)
print('General error:', e)
traceback.print_exc()
return jsonify({'error': str(e)}), 500

@app.route('/imagesInfo', methods=['GET'])
Expand Down
2 changes: 1 addition & 1 deletion server/db/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def regionType(type):
# return regionData

def saveRegionInDB(self, database, idColumn, uid, data, status): # TODO if region then use one or zero changeSatus, remove in their

print(f"Database: {database}")
index = self.findInfoInDb(database, idColumn, uid)

if index is not None: # set -> diff -> list
Expand Down
4 changes: 2 additions & 2 deletions server/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def test_download_configuration_no_image_name(self):

def test_download_image_with_annotations_no_image_name(self):
response = self.app.post('/download_image_with_annotations', data=json.dumps({}), content_type='application/json')
self.assertEqual(response.status_code, 500)
self.assertEqual(response.status_code, 400)
self.assertIn(b"'image_name' not found", response.data)

def test_download_image_mask_no_image_name(self):
response = self.app.post('/download_image_mask', data=json.dumps({}), content_type='application/json')
self.assertEqual(response.status_code, 500)
self.assertEqual(response.status_code, 400)
self.assertIn(b"'image_name' not found", response.data)

# def test_get_images_info_no_path(self):
Expand Down

0 comments on commit 3643af6

Please sign in to comment.