Saad0KH commited on
Commit
c8c83ea
·
verified ·
1 Parent(s): 7046d86

Update SegCloth.py

Browse files
Files changed (1) hide show
  1. SegCloth.py +11 -26
SegCloth.py CHANGED
@@ -1,26 +1,12 @@
1
  from transformers import pipeline
2
- from PIL import Image, ImageChops
3
  import numpy as np
4
  from io import BytesIO
5
  import base64
6
- from transparent_background import Remover
7
 
8
  # Initialisation du pipeline de segmentation
9
  segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
10
 
11
- # Fonction pour supprimer l'arrière-plan
12
- def remove_background(image):
13
- remover = Remover()
14
- if isinstance(image, Image.Image):
15
- output = remover.process(image)
16
- elif isinstance(image, np.ndarray):
17
- image_pil = Image.fromarray(image)
18
- output = remover.process(image_pil)
19
- else:
20
- raise TypeError("Unsupported image type")
21
- return output
22
-
23
- # Fonction pour encoder une image en base64
24
  def encode_image_to_base64(image):
25
  buffered = BytesIO()
26
  image.save(buffered, format="PNG")
@@ -35,16 +21,17 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
35
 
36
  for s in segments:
37
  if s['label'] in clothes:
 
 
 
38
  # Création d'une image vide avec transparence
39
  empty_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
40
 
41
- # Conversion du masque en tableau NumPy et ajustement de l'image
42
- mask_array = np.array(s['mask'])
43
- mask_image = Image.fromarray(mask_array).convert("L") # Convertir le masque en niveau de gris
44
-
45
- # Appliquer le masque à l'image d'origine (en RGBA)
46
- mask_rgba = Image.merge("RGBA", [mask_image, mask_image, mask_image, mask_image])
47
- segmented_part = ImageChops.multiply(img.convert("RGBA"), mask_rgba)
48
 
49
  # Application du masque sur l'image vide
50
  empty_image.paste(segmented_part, mask=mask_image)
@@ -62,11 +49,9 @@ def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dr
62
  # Recadrer l'image à la taille du masque avec la marge
63
  cropped_image = empty_image.crop((left, top, right, bottom))
64
 
65
- # Supprimer l'arrière-plan
66
- image_rm_background = remove_background(cropped_image)
67
-
68
  # Encodage de l'image recadrée en base64
69
- imageBase64 = encode_image_to_base64(image_rm_background)
 
70
  result_images.append(imageBase64)
71
 
72
  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
 
6
 
7
  # Initialisation du pipeline de segmentation
8
  segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def encode_image_to_base64(image):
11
  buffered = BytesIO()
12
  image.save(buffered, format="PNG")
 
21
 
22
  for s in segments:
23
  if s['label'] in clothes:
24
+ # Conversion du masque en tableau NumPy
25
+ mask_array = np.array(s['mask'])
26
+
27
  # Création d'une image vide avec transparence
28
  empty_image = Image.new("RGBA", img.size, (0, 0, 0, 0))
29
 
30
+ # Conversion du masque en image PIL (niveau de gris)
31
+ mask_image = Image.fromarray(mask_array).convert("L")
32
+
33
+ # Extraction de la partie de l'image correspondant au masque
34
+ segmented_part = ImageChops.multiply(img.convert("RGBA"), Image.merge("RGBA", [mask_image, mask_image, mask_image, mask_image]))
 
 
35
 
36
  # Application du masque sur l'image vide
37
  empty_image.paste(segmented_part, mask=mask_image)
 
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
  result_images.append(imageBase64)
56
 
57
  return result_images