AkashKumarave commited on
Commit
12e9f72
·
verified ·
1 Parent(s): 0271b14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -12,28 +12,30 @@ pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code
12
 
13
  # Function to remove background from an image
14
  def remove_background(image):
15
- # Apply mask to the original image
16
  pillow_image = pipe(image)
17
  return pillow_image
18
 
19
  @app.route('/remove-background', methods=['POST'])
20
  def remove_background_endpoint():
21
- if 'image' not in request.files:
22
- return jsonify({'error': 'No image provided'}), 400
23
-
24
- file = request.files['image']
25
  try:
26
- # Open the image
27
- image = Image.open(file).convert('RGB')
28
-
 
 
 
 
 
 
 
29
  # Process the image
30
  result_image = remove_background(image)
31
-
32
  # Save the result to a bytes buffer
33
  img_io = io.BytesIO()
34
  result_image.save(img_io, format='PNG')
35
  img_io.seek(0)
36
-
37
  # Return the image as a response
38
  return send_file(img_io, mimetype='image/png', as_attachment=True, download_name='result.png')
39
  except Exception as e:
 
12
 
13
  # Function to remove background from an image
14
  def remove_background(image):
 
15
  pillow_image = pipe(image)
16
  return pillow_image
17
 
18
  @app.route('/remove-background', methods=['POST'])
19
  def remove_background_endpoint():
 
 
 
 
20
  try:
21
+ # Handle raw image data in the request body
22
+ if request.data:
23
+ image = Image.open(io.BytesIO(request.data)).convert('RGB')
24
+ # Fallback for multipart/form-data (if needed later)
25
+ elif 'image' in request.files:
26
+ file = request.files['image']
27
+ image = Image.open(file).convert('RGB')
28
+ else:
29
+ return jsonify({'error': 'No image data provided'}), 400
30
+
31
  # Process the image
32
  result_image = remove_background(image)
33
+
34
  # Save the result to a bytes buffer
35
  img_io = io.BytesIO()
36
  result_image.save(img_io, format='PNG')
37
  img_io.seek(0)
38
+
39
  # Return the image as a response
40
  return send_file(img_io, mimetype='image/png', as_attachment=True, download_name='result.png')
41
  except Exception as e: