haydpw commited on
Commit
d0c641b
1 Parent(s): da8cc4f

update code

Browse files
Files changed (3) hide show
  1. main.py +7 -2
  2. requirements.txt +0 -0
  3. utils/helpers.py +13 -2
main.py CHANGED
@@ -10,7 +10,7 @@ import models.face_classifier as classifier
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from PIL import Image
12
  from rembg import remove
13
- from utils.helpers import image_to_base64, calculate_mask_area, process_image
14
 
15
 
16
  dotenv.load_dotenv()
@@ -74,9 +74,14 @@ async def predict_image(file: UploadFile = File(...)):
74
  # add original image base 64 as original image:
75
  image_bg_removed = image_bg_removed.convert("RGB")
76
 
 
 
 
 
77
  response = {
78
  "original_image": image_to_base64(image_bg_removed),
79
- "segmentation_results":results
 
80
  }
81
 
82
  # Kembalikan hasil klasifikasi
 
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from PIL import Image
12
  from rembg import remove
13
+ from utils.helpers import combine_images, image_to_base64, calculate_mask_area, process_image
14
 
15
 
16
  dotenv.load_dotenv()
 
74
  # add original image base 64 as original image:
75
  image_bg_removed = image_bg_removed.convert("RGB")
76
 
77
+ # Combine the original image and masks
78
+ combined_image = combine_images(pil_image, results)
79
+ combined_image_base64 = image_to_base64(combined_image, "PNG")
80
+
81
  response = {
82
  "original_image": image_to_base64(image_bg_removed),
83
+ "segmentation_results":results,
84
+ "combined_image": combined_image_base64
85
  }
86
 
87
  # Kembalikan hasil klasifikasi
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
 
utils/helpers.py CHANGED
@@ -1,4 +1,4 @@
1
- from PIL import Image
2
  import io
3
  import base64
4
  import numpy as np
@@ -28,4 +28,15 @@ def process_image(input_image: Image.Image) -> Image.Image:
28
  # Apply the mask to the alpha channel
29
  data[..., 3][black_areas] = 0
30
 
31
- return Image.fromarray(data)
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image, ImageChops, ImageEnhance
2
  import io
3
  import base64
4
  import numpy as np
 
28
  # Apply the mask to the alpha channel
29
  data[..., 3][black_areas] = 0
30
 
31
+ return Image.fromarray(data)
32
+
33
+ def combine_images(original_image: Image.Image, masks: list) -> Image.Image:
34
+ combined = original_image.copy()
35
+ for mask in masks:
36
+ if mask['label'] == 'background':
37
+ continue
38
+ mask_image = Image.open(io.BytesIO(base64.b64decode(mask['mask'])))
39
+ mask_image = mask_image.convert("L") # Convert mask to grayscale
40
+ mask_image = ImageEnhance.Brightness(mask_image).enhance(0.5) # Adjust the brightness to make it more visible
41
+ combined = ImageChops.add(combined, mask_image)
42
+ return combined