Saad0KH commited on
Commit
4459aee
Β·
verified Β·
1 Parent(s): a05ee1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py CHANGED
@@ -4,6 +4,55 @@ import base64
4
  from io import BytesIO
5
  import io
6
  from SegCloth import segment_clothing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
 
9
 
@@ -39,5 +88,15 @@ def classify():
39
  result = segment_image(image,clothes)
40
  return jsonify({'result': result})
41
 
 
 
 
 
 
 
 
 
 
 
42
  if __name__ == "__main__":
43
  app.run(debug=True, host="0.0.0.0", port=7860)
 
4
  from io import BytesIO
5
  import io
6
  from SegCloth import segment_clothing
7
+ import numpy as np
8
+ import cv2
9
+ import insightface
10
+ import onnxruntime as ort
11
+ import huggingface_hub
12
+
13
+ # Charger le modèle
14
+ def load_model():
15
+ path = huggingface_hub.hf_hub_download("public-data/insightface", "models/scrfd_person_2.5g.onnx")
16
+ options = ort.SessionOptions()
17
+ options.intra_op_num_threads = 8
18
+ options.inter_op_num_threads = 8
19
+ session = ort.InferenceSession(
20
+ path, sess_options=options, providers=["CPUExecutionProvider", "CUDAExecutionProvider"]
21
+ )
22
+ model = insightface.model_zoo.retinaface.RetinaFace(model_file=path, session=session)
23
+ return model
24
+
25
+ detector = load_model()
26
+ detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
27
+
28
+ # DΓ©tecter les personnes et extraire les images
29
+ def detect_person(image):
30
+ img = np.array(image)
31
+ img = img[:, :, ::-1] # RGB -> BGR
32
+
33
+ bboxes, kpss = detector.detect(img)
34
+ bboxes = np.round(bboxes[:, :4]).astype(int)
35
+ kpss = np.round(kpss).astype(int)
36
+ kpss[:, :, 0] = np.clip(kpss[:, :, 0], 0, img.shape[1])
37
+ kpss[:, :, 1] = np.clip(kpss[:, :, 1], 0, img.shape[0])
38
+ vbboxes = bboxes.copy()
39
+ vbboxes[:, 0] = kpss[:, 0, 0]
40
+ vbboxes[:, 1] = kpss[:, 0, 1]
41
+ vbboxes[:, 2] = kpss[:, 4, 0]
42
+ vbboxes[:, 3] = kpss[:, 4, 1]
43
+
44
+ person_images = []
45
+ for i in range(bboxes.shape[0]):
46
+ bbox = bboxes[i]
47
+ x1, y1, x2, y2 = bbox
48
+ person_img = img[y1:y2, x1:x2]
49
+
50
+ # Convert numpy array to PIL Image and encode to base64
51
+ pil_img = Image.fromarray(person_img[:, :, ::-1]) # BGR -> RGB
52
+ base64_img = encode_image_to_base64(pil_img)
53
+ person_images.append(base64_img)
54
+
55
+ return person_images
56
 
57
 
58
 
 
88
  result = segment_image(image,clothes)
89
  return jsonify({'result': result})
90
 
91
+ @app.route('/api/detect', methods=['POST'])
92
+ def detect():
93
+ data = request.json
94
+ image_base64 = data['image']
95
+ image = decode_image_from_base64(image_base64)
96
+
97
+ person_images_base64 = detect_person(image)
98
+
99
+ return jsonify({'images': person_images_base64})
100
+
101
  if __name__ == "__main__":
102
  app.run(debug=True, host="0.0.0.0", port=7860)