Saad0KH commited on
Commit
eb456a2
·
verified ·
1 Parent(s): c89d4d6

Update SegCloth.py

Browse files
Files changed (1) hide show
  1. SegCloth.py +19 -6
SegCloth.py CHANGED
@@ -1,5 +1,5 @@
1
  from transformers import pipeline
2
- from PIL import Image, ImageChops
3
  import numpy as np
4
  from io import BytesIO
5
  import base64
@@ -12,7 +12,7 @@ def encode_image_to_base64(image):
12
  image.save(buffered, format="PNG")
13
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
14
 
15
- def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
16
  # Segmentation de l'image
17
  segments = segmenter(img)
18
 
@@ -36,8 +36,21 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
36
  # Application du masque sur l'image vide
37
  empty_image.paste(segmented_part, mask=mask_image)
38
 
39
- # Encodage de l'image résultante en base64
40
- imageBase64 = encode_image_to_base64(empty_image)
41
- result_images.append((s['label'], imageBase64))
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- return result_images
 
1
  from transformers import pipeline
2
+ from PIL import Image, ImageChops, ImageOps
3
  import numpy as np
4
  from io import BytesIO
5
  import base64
 
12
  image.save(buffered, format="PNG")
13
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
14
 
15
+ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"], margin=10):
16
  # Segmentation de l'image
17
  segments = segmenter(img)
18
 
 
36
  # Application du masque sur l'image vide
37
  empty_image.paste(segmented_part, mask=mask_image)
38
 
39
+ # Déterminer la bounding box du masque
40
+ bbox = mask_image.getbbox()
41
+ if bbox:
42
+ # Ajouter la marge autour de la bounding box
43
+ left, top, right, bottom = bbox
44
+ left = max(0, left - margin)
45
+ top = max(0, top - margin)
46
+ right = min(img.width, right + margin)
47
+ bottom = min(img.height, bottom + margin)
48
+
49
+ # Recadrer l'image à la taille du masque avec la marge
50
+ cropped_image = empty_image.crop((left, top, right, bottom))
51
+
52
+ # Encodage de l'image recadrée en base64
53
+ imageBase64 = encode_image_to_base64(cropped_image)
54
+ result_images.append((s['label'], imageBase64))
55
 
56
+ return result_images