Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +37 -0
- main.py +116 -0
- requirement.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Gunakan image sistem operasi Linux dengan Python 3.10
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Hugging Face Spaces mewajibkan aplikasi berjalan di bawah user ID 1000
|
| 5 |
+
RUN useradd -m -u 1000 user
|
| 6 |
+
USER user
|
| 7 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 8 |
+
|
| 9 |
+
# Set lokasi direktori kerja di dalam kontainer
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
# Pindah sementara ke akses root untuk menginstal dependensi tingkat OS
|
| 13 |
+
USER root
|
| 14 |
+
RUN apt-get update && apt-get install -y \
|
| 15 |
+
libgl1-mesa-glx \
|
| 16 |
+
libglib2.0-0 \
|
| 17 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
+
|
| 19 |
+
# Kembalikan hak milik direktori ke user 1000
|
| 20 |
+
RUN chown -R user:user /app
|
| 21 |
+
USER user
|
| 22 |
+
|
| 23 |
+
# Salin file requirements dan instal library Python
|
| 24 |
+
COPY --chown=user:user requirements.txt /app/
|
| 25 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 26 |
+
|
| 27 |
+
# Salin seluruh kode aplikasi (main.py)
|
| 28 |
+
COPY --chown=user:user . /app/
|
| 29 |
+
|
| 30 |
+
# Buat folder face_db dan berikan izin tulis (write permission) penuh
|
| 31 |
+
RUN mkdir -p /app/face_db && chmod -R 777 /app/face_db
|
| 32 |
+
|
| 33 |
+
# Buka port wajib dari Hugging Face
|
| 34 |
+
EXPOSE 7860
|
| 35 |
+
|
| 36 |
+
# Perintah untuk menyalakan server uvicorn di port 7860
|
| 37 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
+
from deepface import DeepFace
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
+
import pandas as pd
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="AI Face Recognition Microservice")
|
| 10 |
+
|
| 11 |
+
DB_PATH = "face_db"
|
| 12 |
+
os.makedirs(DB_PATH, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@app.post("/api/register")
|
| 16 |
+
async def register_face(
|
| 17 |
+
name: str = Form(...),
|
| 18 |
+
image: UploadFile = File(...)
|
| 19 |
+
):
|
| 20 |
+
try:
|
| 21 |
+
# Gunakan nama person sebagai nama file citra referensi
|
| 22 |
+
img_path = os.path.join(DB_PATH, f"{name}.jpg")
|
| 23 |
+
|
| 24 |
+
with open(img_path, "wb") as buffer:
|
| 25 |
+
shutil.copyfileobj(image.file, buffer)
|
| 26 |
+
|
| 27 |
+
# Clear cache representations pkl milik DeepFace
|
| 28 |
+
# agar melakukan scan ulang file baru
|
| 29 |
+
for file in os.listdir(DB_PATH):
|
| 30 |
+
if file.endswith(".pkl"):
|
| 31 |
+
os.remove(os.path.join(DB_PATH, file))
|
| 32 |
+
|
| 33 |
+
return JSONResponse(
|
| 34 |
+
content={
|
| 35 |
+
"success": True,
|
| 36 |
+
"message": f"Wajah {name} berhasil didaftarkan."
|
| 37 |
+
}
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return JSONResponse(
|
| 42 |
+
content={
|
| 43 |
+
"success": False,
|
| 44 |
+
"error": str(e)
|
| 45 |
+
},
|
| 46 |
+
status_code=500
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@app.post("/api/recognize")
|
| 51 |
+
async def recognize_face(
|
| 52 |
+
image: UploadFile = File(...)
|
| 53 |
+
):
|
| 54 |
+
temp_path = f"temp_{image.filename}"
|
| 55 |
+
|
| 56 |
+
with open(temp_path, "wb") as buffer:
|
| 57 |
+
shutil.copyfileobj(image.file, buffer)
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
# Melakukan pencarian kesamaan wajah pada direktori face_db
|
| 61 |
+
dfs = DeepFace.find(
|
| 62 |
+
img_path=temp_path,
|
| 63 |
+
db_path=DB_PATH,
|
| 64 |
+
model_name="VGG-Face",
|
| 65 |
+
enforce_detection=False
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if len(dfs) > 0 and not dfs[0].empty:
|
| 69 |
+
best_match = dfs[0].iloc[0]
|
| 70 |
+
|
| 71 |
+
matched_file_path = best_match["identity"]
|
| 72 |
+
distance = float(best_match["distance"])
|
| 73 |
+
|
| 74 |
+
# Ambil kembali nama dari penamaan file
|
| 75 |
+
filename = os.path.basename(matched_file_path)
|
| 76 |
+
detected_name = os.path.splitext(filename)[0]
|
| 77 |
+
|
| 78 |
+
return JSONResponse(
|
| 79 |
+
content={
|
| 80 |
+
"success": True,
|
| 81 |
+
"name": detected_name,
|
| 82 |
+
"distance": distance
|
| 83 |
+
}
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
else:
|
| 87 |
+
return JSONResponse(
|
| 88 |
+
content={
|
| 89 |
+
"success": False,
|
| 90 |
+
"message": "Wajah tidak teridentifikasi di database."
|
| 91 |
+
}
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return JSONResponse(
|
| 96 |
+
content={
|
| 97 |
+
"success": False,
|
| 98 |
+
"error": str(e)
|
| 99 |
+
},
|
| 100 |
+
status_code=500
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
finally:
|
| 104 |
+
if os.path.exists(temp_path):
|
| 105 |
+
os.remove(temp_path)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
import uvicorn
|
| 110 |
+
|
| 111 |
+
uvicorn.run(
|
| 112 |
+
"main:app",
|
| 113 |
+
host="127.0.0.1",
|
| 114 |
+
port=8001,
|
| 115 |
+
reload=True
|
| 116 |
+
)
|
requirement.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
deepface
|
| 4 |
+
tf-keras
|
| 5 |
+
pandas
|
| 6 |
+
python-multipart
|
| 7 |
+
opencv-python-headless
|