Saad0KH commited on
Commit
1c0cda0
Β·
verified Β·
1 Parent(s): c11222a

Update SegCloth.py

Browse files
Files changed (1) hide show
  1. SegCloth.py +19 -25
SegCloth.py CHANGED
@@ -4,39 +4,33 @@ import numpy as np
4
  from io import BytesIO
5
  import io
6
  import base64
 
7
  # Initialize segmentation pipeline
8
  segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
9
 
10
 
11
- def encode_image_to_base64(image):
12
- buffered = BytesIO()
13
- image.save(buffered, format="PNG")
14
- return base64.b64encode(buffered.getvalue()).decode('utf-8')
15
-
16
- def segment_clothing(img, clothes=["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
17
  # Segment image
18
  segments = segmenter(img)
19
 
20
- # Dictionary to store masks for each clothing type
21
- masks = {clothing_type: None for clothing_type in clothes}
22
-
23
- # Create individual masks for each clothing type
24
  for s in segments:
25
- if s['label'] in clothes:
26
- mask = np.array(s['mask'])
27
- if masks[s['label']] is None:
28
- masks[s['label']] = mask
29
- else:
30
- masks[s['label']] += mask
31
-
32
- # Create and save images for each clothing type
33
  result_images = []
34
- for clothing_type, mask in masks.items():
35
- if mask is not None:
36
- final_mask = Image.fromarray(np.uint8(mask * 255)) # Normalize mask to 0-255 range
37
- result_image = img.copy()
38
- result_image.putalpha(final_mask)
39
- imageBase64 = encode_image_to_base64(result_image)
40
- result_images.append((clothing_type, imageBase64))
 
 
 
 
 
41
 
42
  return result_images
 
4
  from io import BytesIO
5
  import io
6
  import base64
7
+
8
  # Initialize segmentation pipeline
9
  segmenter = pipeline(model="mattmdjaga/segformer_b2_clothes")
10
 
11
 
12
+ def segment_clothing(img, clothes= ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]):
 
 
 
 
 
13
  # Segment image
14
  segments = segmenter(img)
15
 
16
+ # Create list of masks
17
+ mask_list = []
 
 
18
  for s in segments:
19
+ if(s['label'] in clothes):
20
+ mask_list.append(s['mask'])
21
+
 
 
 
 
 
22
  result_images = []
23
+
24
+ # Paste all masks on top of eachother
25
+ final_mask = np.array(mask_list[0])
26
+ for mask in mask_list:
27
+ current_mask = np.array(mask)
28
+ final_mask_bis = Image.fromarray(current_mask)
29
+ img.putalpha(final_mask_bis)
30
+ imageBase64 = encode_image_to_base64(img)
31
+ result_images.append(('clothing_type', imageBase64))
32
+
33
+
34
+
35
 
36
  return result_images