Spaces:
Sleeping
Sleeping
Alex
commited on
Commit
·
13c1ff0
1
Parent(s):
d56dfe8
input image base64 e update della risposta
Browse files
README.md
CHANGED
@@ -10,3 +10,19 @@ pinned: false
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
13 |
+
|
14 |
+
# Test the endpoint with Curl
|
15 |
+
```
|
16 |
+
curl -X POST -F "file=@test_image.jpg" https://tuo-username-singlefacedetector.hf.space/detect_single_face
|
17 |
+
```
|
18 |
+
|
19 |
+
# Test the endpoint with python
|
20 |
+
|
21 |
+
```
|
22 |
+
import requests
|
23 |
+
|
24 |
+
url = "https://tuo-username-singlefacedetector.hf.space/detect_single_face"
|
25 |
+
with open("test_image.jpg", "rb") as f:
|
26 |
+
response = requests.post(url, files={"file": f})
|
27 |
+
print(response.json())
|
28 |
+
```
|
app.py
CHANGED
@@ -1,13 +1,19 @@
|
|
1 |
-
from fastapi import FastAPI,
|
|
|
2 |
from PIL import Image
|
3 |
from ultralytics import YOLO
|
4 |
import io
|
|
|
5 |
import torch
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
# Carica il modello all'avvio (singleton per efficienza)
|
10 |
-
model = YOLO("
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def preprocess_image(image: Image.Image, size=(320, 320)):
|
13 |
"""Ridimensiona l'immagine per velocizzare l'inferenza"""
|
@@ -16,11 +22,11 @@ def preprocess_image(image: Image.Image, size=(320, 320)):
|
|
16 |
return img
|
17 |
|
18 |
@app.post("/detect_single_face")
|
19 |
-
async def detect_single_face(
|
20 |
try:
|
21 |
-
#
|
22 |
-
|
23 |
-
image = Image.open(io.BytesIO(
|
24 |
image = preprocess_image(image)
|
25 |
|
26 |
# Esegui l'inferenza con YOLOv8
|
@@ -35,10 +41,17 @@ async def detect_single_face(file: UploadFile = File(...)):
|
|
35 |
if int(box.cls) == 0: # "person" in yolov8n
|
36 |
face_count += 1
|
37 |
|
38 |
-
#
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
except Exception as e:
|
41 |
-
|
|
|
42 |
|
43 |
# Avvio del server (Hugging Face lo gestisce automaticamente)
|
44 |
if __name__ == "__main__":
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
from PIL import Image
|
4 |
from ultralytics import YOLO
|
5 |
import io
|
6 |
+
import base64
|
7 |
import torch
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
# Carica il modello all'avvio (singleton per efficienza)
|
12 |
+
model = YOLO("YoloV8-FaceDetection.pt") # Sostituisci con "yolov8n-face.pt" se disponibile
|
13 |
+
|
14 |
+
# Modello Pydantic per validare l'input Base64
|
15 |
+
class ImageRequest(BaseModel):
|
16 |
+
image_base64: str
|
17 |
|
18 |
def preprocess_image(image: Image.Image, size=(320, 320)):
|
19 |
"""Ridimensiona l'immagine per velocizzare l'inferenza"""
|
|
|
22 |
return img
|
23 |
|
24 |
@app.post("/detect_single_face")
|
25 |
+
async def detect_single_face(request: ImageRequest):
|
26 |
try:
|
27 |
+
# Decodifica la stringa Base64
|
28 |
+
image_data = base64.b64decode(request.image_base64)
|
29 |
+
image = Image.open(io.BytesIO(image_data))
|
30 |
image = preprocess_image(image)
|
31 |
|
32 |
# Esegui l'inferenza con YOLOv8
|
|
|
41 |
if int(box.cls) == 0: # "person" in yolov8n
|
42 |
face_count += 1
|
43 |
|
44 |
+
# Logica di risposta
|
45 |
+
if face_count == 1:
|
46 |
+
return {"has_single_face": True}
|
47 |
+
elif face_count > 1:
|
48 |
+
return {"has_single_face": False, "face_count": face_count}
|
49 |
+
else:
|
50 |
+
return {"has_single_face": False}
|
51 |
+
|
52 |
except Exception as e:
|
53 |
+
# In caso di errore (es. Base64 non valido), restituisci False
|
54 |
+
return {"has_single_face": False, "error": str(e)}
|
55 |
|
56 |
# Avvio del server (Hugging Face lo gestisce automaticamente)
|
57 |
if __name__ == "__main__":
|