Spaces:
Sleeping
Sleeping
Create app.py
#1
by
NarzullayevMe - opened
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
|
| 2 |
+
from deepface import DeepFace
|
| 3 |
+
import os
|
| 4 |
+
import uuid
|
| 5 |
+
|
| 6 |
+
# FastAPI ilovasini yaratamiz
|
| 7 |
+
app = FastAPI(title="Face Verification API")
|
| 8 |
+
|
| 9 |
+
# Talabalarning asl rasmlari saqlanadigan papka
|
| 10 |
+
STUDENT_IMAGE_DIR = "student_images"
|
| 11 |
+
os.makedirs(STUDENT_IMAGE_DIR, exist_ok=True)
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def read_root():
|
| 15 |
+
return {"message": "Face Verification API ishga tushdi. /verify/ manziliga POST so'rov yuborishingiz mumkin."}
|
| 16 |
+
|
| 17 |
+
@app.post("/verify/")
|
| 18 |
+
async def verify_face(
|
| 19 |
+
image: UploadFile = File(..., description="Kameradan olingan talaba rasmi"),
|
| 20 |
+
pinfl: str = Form(..., description="Talabaning PINFL raqami")
|
| 21 |
+
):
|
| 22 |
+
live_image_path = f"{str(uuid.uuid4())}.jpg"
|
| 23 |
+
try:
|
| 24 |
+
# 1. Talabaning tizimdagi asl rasmini topish
|
| 25 |
+
student_image_path = os.path.join(STUDENT_IMAGE_DIR, f"{pinfl}.jpg")
|
| 26 |
+
if not os.path.exists(student_image_path):
|
| 27 |
+
raise HTTPException(status_code=404, detail=f"{pinfl} raqamli talaba uchun tizimda rasm topilmadi.")
|
| 28 |
+
|
| 29 |
+
# 2. Kelgan rasmni vaqtinchalik saqlash
|
| 30 |
+
with open(live_image_path, "wb") as buffer:
|
| 31 |
+
buffer.write(await image.read())
|
| 32 |
+
|
| 33 |
+
# 3. DeepFace orqali solishtirish
|
| 34 |
+
result = DeepFace.verify(
|
| 35 |
+
img1_path=student_image_path,
|
| 36 |
+
img2_path=live_image_path,
|
| 37 |
+
model_name="VGG-Face",
|
| 38 |
+
detector_backend="mtcnn"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# 4. Natijani JSON formatida qaytarish
|
| 42 |
+
similarity = (1 - result['distance']) * 100
|
| 43 |
+
return {
|
| 44 |
+
"is_match": bool(result["verified"]),
|
| 45 |
+
"similarity_percentage": round(similarity, 2),
|
| 46 |
+
"pinfl": pinfl
|
| 47 |
+
}
|
| 48 |
+
except HTTPException as e:
|
| 49 |
+
raise e
|
| 50 |
+
except Exception as e:
|
| 51 |
+
raise HTTPException(status_code=500, detail=f"Ichki xatolik yuz berdi: {str(e)}")
|
| 52 |
+
finally:
|
| 53 |
+
# Vaqtinchalik faylni o'chirish
|
| 54 |
+
if os.path.exists(live_image_path):
|
| 55 |
+
os.remove(live_image_path)
|