Saad0KH commited on
Commit
ba27f48
·
verified ·
1 Parent(s): 8280ccf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -22
app.py CHANGED
@@ -38,8 +38,8 @@ def encode_image_to_base64(image):
38
  image.save(buffered, format="PNG")
39
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
40
 
41
- # Détecter les personnes et extraire les images
42
- def detect_person(image):
43
  img = np.array(image)
44
  img = img[:, :, ::-1] # RGB -> BGR
45
 
@@ -57,18 +57,22 @@ def detect_person(image):
57
  bboxes[:, 2] = np.clip(bboxes[:, 2], 0, width) # x2
58
  bboxes[:, 3] = np.clip(bboxes[:, 3], 0, height) # y2
59
 
60
- person_images = []
61
  for i in range(bboxes.shape[0]):
62
  bbox = bboxes[i]
63
  x1, y1, x2, y2 = bbox
64
  person_img = img[y1:y2, x1:x2]
65
 
66
- # Convert numpy array to PIL Image and encode to base64
67
  pil_img = Image.fromarray(person_img[:, :, ::-1]) # BGR -> RGB
68
- base64_img = encode_image_to_base64(pil_img)
69
- person_images.append(base64_img)
70
 
71
- return person_images
 
 
 
 
 
 
72
 
73
  @app.route('/api/detect', methods=['POST'])
74
  def detect():
@@ -77,25 +81,13 @@ def detect():
77
  image_base64 = data['image']
78
  image = decode_image_from_base64(image_base64)
79
 
80
- person_images_base64 = detect_person(image)
 
 
81
 
82
  return jsonify({'images': person_images_base64})
83
  except Exception as e:
84
  return jsonify({'error': str(e)}), 500
85
 
86
- def segment_image(img, clothes):
87
- img = decode_image_from_base64(img)
88
- return segment_clothing(img, clothes)
89
-
90
- # Route pour l'API REST
91
- @app.route('/api/classify', methods=['POST'])
92
- def classify():
93
- data = request.json
94
- print(data)
95
- clothes = ["Upper-clothes", "Skirt", "Pants", "Dress"]
96
- image = data['image']
97
- result = segment_image(image,clothes)
98
- return jsonify({'result': result})
99
-
100
  if __name__ == "__main__":
101
  app.run(debug=True, host="0.0.0.0", port=7860)
 
38
  image.save(buffered, format="PNG")
39
  return base64.b64encode(buffered.getvalue()).decode('utf-8')
40
 
41
+ # Détecter les personnes et segmenter leurs vêtements
42
+ def detect_and_segment_persons(image, clothes):
43
  img = np.array(image)
44
  img = img[:, :, ::-1] # RGB -> BGR
45
 
 
57
  bboxes[:, 2] = np.clip(bboxes[:, 2], 0, width) # x2
58
  bboxes[:, 3] = np.clip(bboxes[:, 3], 0, height) # y2
59
 
60
+ segmented_images = []
61
  for i in range(bboxes.shape[0]):
62
  bbox = bboxes[i]
63
  x1, y1, x2, y2 = bbox
64
  person_img = img[y1:y2, x1:x2]
65
 
66
+ # Convert numpy array to PIL Image
67
  pil_img = Image.fromarray(person_img[:, :, ::-1]) # BGR -> RGB
 
 
68
 
69
+ # Segment clothing for the detected person
70
+ segmented_result = segment_clothing(pil_img, clothes)
71
+
72
+ # Append segmented results
73
+ segmented_images.append(segmented_result)
74
+
75
+ return segmented_images
76
 
77
  @app.route('/api/detect', methods=['POST'])
78
  def detect():
 
81
  image_base64 = data['image']
82
  image = decode_image_from_base64(image_base64)
83
 
84
+ # Détection et segmentation des personnes
85
+ clothes = ["Upper-clothes", "Skirt", "Pants", "Dress"]
86
+ person_images_base64 = detect_and_segment_persons(image, clothes)
87
 
88
  return jsonify({'images': person_images_base64})
89
  except Exception as e:
90
  return jsonify({'error': str(e)}), 500
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  if __name__ == "__main__":
93
  app.run(debug=True, host="0.0.0.0", port=7860)