Upload 5 files
Browse files- Dockerfile +19 -0
- Fer2013.h5 +3 -0
- README.md +22 -5
- app.py +64 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 6 |
+
libglib2.0-0 \
|
| 7 |
+
libsm6 \
|
| 8 |
+
libxext6 \
|
| 9 |
+
libxrender-dev \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
COPY requirements.txt .
|
| 13 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY . .
|
| 16 |
+
|
| 17 |
+
EXPOSE 7860
|
| 18 |
+
|
| 19 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
Fer2013.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bdeb325637a94b721c4eee4493c020a9a5800fc21104d25570024e3b874d0ba9
|
| 3 |
+
size 157500720
|
README.md
CHANGED
|
@@ -1,10 +1,27 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Facial Emotion Recognition
|
| 3 |
+
emoji: 😃
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Facial Emotion Recognition API
|
| 12 |
+
|
| 13 |
+
API لتصنيف المشاعر من صور الوجه باستخدام موديل CNN مدرب على dataset **FER2013**.
|
| 14 |
+
|
| 15 |
+
## Endpoints
|
| 16 |
+
|
| 17 |
+
- `GET /` — health check
|
| 18 |
+
- `POST /predict` — ارفعي صورة (multipart/form-data, field name: `file`) وترجعلك:
|
| 19 |
+
- `predicted_emotion`: التوقع (Angry, Disgust, Fear, Happy, Sad, Surprise, Neutral)
|
| 20 |
+
- `confidence`: نسبة الثقة
|
| 21 |
+
- `all_probabilities`: احتمالات كل الفئات
|
| 22 |
+
|
| 23 |
+
## ملاحظات
|
| 24 |
+
|
| 25 |
+
- الموديل بيتحمل من ملف `Fer2013.h5` الموجود في نفس الـ Space.
|
| 26 |
+
- الصورة بتتحول تلقائيًا Grayscale وتتعمل resize لـ 48x48 قبل التوقع.
|
| 27 |
+
- Swagger UI متاح على `/docs` بعد نشر الـ Space.
|
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
from fastapi import FastAPI, File, UploadFile
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
from tensorflow.keras.models import load_model
|
| 9 |
+
|
| 10 |
+
app = FastAPI(title="Facial Emotion Recognition API")
|
| 11 |
+
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
MODEL_PATH = "Fer2013.h5"
|
| 20 |
+
EMOTION_LABELS = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
|
| 21 |
+
|
| 22 |
+
model = None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@app.on_event("startup")
|
| 26 |
+
def load_fer_model():
|
| 27 |
+
global model
|
| 28 |
+
if not os.path.exists(MODEL_PATH):
|
| 29 |
+
raise FileNotFoundError(
|
| 30 |
+
f"Model file '{MODEL_PATH}' not found. Make sure it is uploaded to the Space root."
|
| 31 |
+
)
|
| 32 |
+
model = load_model(MODEL_PATH, compile=False)
|
| 33 |
+
print("Model loaded successfully.")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def preprocess_image(image_bytes: bytes) -> np.ndarray:
|
| 37 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("L") # grayscale
|
| 38 |
+
image = image.resize((48, 48))
|
| 39 |
+
array = np.array(image, dtype=np.float32) / 255.0
|
| 40 |
+
array = array.reshape(1, 48, 48, 1)
|
| 41 |
+
return array
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@app.get("/")
|
| 45 |
+
def root():
|
| 46 |
+
return {"status": "ok", "message": "Facial Emotion Recognition API is running."}
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.post("/predict")
|
| 50 |
+
async def predict(file: UploadFile = File(...)):
|
| 51 |
+
contents = await file.read()
|
| 52 |
+
input_array = preprocess_image(contents)
|
| 53 |
+
|
| 54 |
+
predictions = model.predict(input_array)[0]
|
| 55 |
+
predicted_idx = int(np.argmax(predictions))
|
| 56 |
+
|
| 57 |
+
result = {
|
| 58 |
+
"predicted_emotion": EMOTION_LABELS[predicted_idx],
|
| 59 |
+
"confidence": float(predictions[predicted_idx]),
|
| 60 |
+
"all_probabilities": {
|
| 61 |
+
EMOTION_LABELS[i]: float(predictions[i]) for i in range(len(EMOTION_LABELS))
|
| 62 |
+
},
|
| 63 |
+
}
|
| 64 |
+
return result
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.110.0
|
| 2 |
+
uvicorn[standard]==0.29.0
|
| 3 |
+
tensorflow-cpu==2.15.0
|
| 4 |
+
numpy==1.26.4
|
| 5 |
+
pillow==10.3.0
|
| 6 |
+
python-multipart==0.0.9
|