Saad0KH commited on
Commit
8da9807
·
verified ·
1 Parent(s): 2b9a06d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -44
app.py CHANGED
@@ -2,14 +2,14 @@ from flask import Flask, request, jsonify
2
  from PIL import Image
3
  import base64
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")
@@ -25,12 +25,27 @@ def load_model():
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])
@@ -54,49 +69,18 @@ def detect_person(image):
54
 
55
  return person_images
56
 
57
-
58
-
59
- # Créer une instance FastAPI
60
- app = Flask(__name__)
61
-
62
- def segment_image(img, clothes):
63
- img = decode_image_from_base64(img)
64
- return segment_clothing(img, clothes)
65
-
66
- # Fonction pour décoder une image encodée en base64 en objet PIL.Image.Image
67
- def decode_image_from_base64(image_data):
68
- image_data = base64.b64decode(image_data)
69
- image = Image.open(io.BytesIO(image_data))
70
- return image
71
-
72
- def encode_image_to_base64(image):
73
- buffered = BytesIO()
74
- image.save(buffered, format="PNG")
75
- return base64.b64encode(buffered.getvalue()).decode('utf-8')
76
-
77
- @app.get("/")
78
- def root():
79
- return "Welcome to Fashion Clothing Detectiony API!"
80
-
81
- # Route pour l'API REST
82
- @app.route('/api/classify', methods=['POST'])
83
- def classify():
84
- data = request.json
85
- print(data)
86
- clothes = ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress"]
87
- image = data['image']
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)
 
2
  from PIL import Image
3
  import base64
4
  from io import BytesIO
 
 
5
  import numpy as np
6
  import cv2
7
  import insightface
8
  import onnxruntime as ort
9
  import huggingface_hub
10
 
11
+ app = Flask(__name__)
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")
 
25
  detector = load_model()
26
  detector.prepare(-1, nms_thresh=0.5, input_size=(640, 640))
27
 
28
+ # Fonction pour décoder une image encodée en base64 en objet PIL.Image.Image
29
+ def decode_image_from_base64(image_data):
30
+ image_data = base64.b64decode(image_data)
31
+ image = Image.open(BytesIO(image_data)).convert("RGB") # Convertir en RGB pour éviter les problèmes de canal alpha
32
+ return image
33
+
34
+ # Fonction pour encoder une image PIL en base64
35
+ def encode_image_to_base64(image):
36
+ buffered = BytesIO()
37
+ image.save(buffered, format="PNG")
38
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
39
+
40
  # Détecter les personnes et extraire les images
41
  def detect_person(image):
42
  img = np.array(image)
43
  img = img[:, :, ::-1] # RGB -> BGR
44
 
45
  bboxes, kpss = detector.detect(img)
46
+ if bboxes.shape[0] == 0: # Aucun visage détecté
47
+ return []
48
+
49
  bboxes = np.round(bboxes[:, :4]).astype(int)
50
  kpss = np.round(kpss).astype(int)
51
  kpss[:, :, 0] = np.clip(kpss[:, :, 0], 0, img.shape[1])
 
69
 
70
  return person_images
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  @app.route('/api/detect', methods=['POST'])
73
  def detect():
74
+ try:
75
+ data = request.json
76
+ image_base64 = data['image']
77
+ image = decode_image_from_base64(image_base64)
78
+
79
+ person_images_base64 = detect_person(image)
80
+
81
+ return jsonify({'images': person_images_base64})
82
+ except Exception as e:
83
+ return jsonify({'error': str(e)}), 500
84
 
85
  if __name__ == "__main__":
86
+ app.run(debug=True, host="0.0.0.0", port=7860)