Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
# Set the Keras backend to JAX (must be done before importing keras)
|
| 3 |
os.environ["KERAS_BACKEND"] = "jax"
|
| 4 |
|
| 5 |
from fastapi import FastAPI, File, UploadFile
|
|
@@ -11,11 +10,14 @@ from PIL import Image
|
|
| 11 |
import io
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
|
| 14 |
-
|
| 15 |
-
# Initialize FastAPI
|
| 16 |
app = FastAPI()
|
| 17 |
|
| 18 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
model = None
|
| 20 |
desired_emotions = ['happy', 'sad', 'neutral']
|
| 21 |
original_emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
|
|
@@ -24,36 +26,28 @@ desired_indices = [original_emotion_labels.index(emotion) for emotion in desired
|
|
| 24 |
@app.on_event("startup")
|
| 25 |
def load_emotion_model():
|
| 26 |
global model
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
print("β
Model loaded successfully.")
|
| 33 |
-
except Exception as e:
|
| 34 |
-
print("β Failed to load model:", str(e))
|
| 35 |
-
|
| 36 |
|
| 37 |
def preprocess_face(image_bytes):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
return None
|
| 46 |
-
|
| 47 |
-
x, y, w, h = faces[0]
|
| 48 |
-
face = np_img[y:y+h, x:x+w]
|
| 49 |
-
face_resized = cv2.resize(face, (224, 224))
|
| 50 |
-
face_normalized = face_resized / 255.0
|
| 51 |
-
face_expanded = np.expand_dims(face_normalized, axis=0)
|
| 52 |
-
return face_expanded
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print("β Error during preprocessing:", str(e))
|
| 55 |
return None
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.post("/predict")
|
| 58 |
async def predict_emotion(file: UploadFile = File(...)):
|
| 59 |
if model is None:
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
os.environ["KERAS_BACKEND"] = "jax"
|
| 3 |
|
| 4 |
from fastapi import FastAPI, File, UploadFile
|
|
|
|
| 10 |
import io
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
|
|
|
|
|
|
|
| 13 |
app = FastAPI()
|
| 14 |
|
| 15 |
+
# Add root route for testing
|
| 16 |
+
@app.get("/")
|
| 17 |
+
def read_root():
|
| 18 |
+
return {"message": "Facial Emotion API is running π"}
|
| 19 |
+
|
| 20 |
+
# Load model and config
|
| 21 |
model = None
|
| 22 |
desired_emotions = ['happy', 'sad', 'neutral']
|
| 23 |
original_emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
|
|
|
|
| 26 |
@app.on_event("startup")
|
| 27 |
def load_emotion_model():
|
| 28 |
global model
|
| 29 |
+
print("π Downloading model from HuggingFace Hub...")
|
| 30 |
+
model_path = hf_hub_download(repo_id="Shees7/facial_model", filename="emotion_model.keras")
|
| 31 |
+
print("β
Model file downloaded at:", model_path)
|
| 32 |
+
model = keras.saving.load_model(model_path)
|
| 33 |
+
print("β
Model loaded successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def preprocess_face(image_bytes):
|
| 36 |
+
np_img = np.array(Image.open(io.BytesIO(image_bytes)).convert('RGB'))
|
| 37 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 38 |
+
gray = cv2.cvtColor(np_img, cv2.COLOR_RGB2GRAY)
|
| 39 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
|
|
|
| 40 |
|
| 41 |
+
if len(faces) == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return None
|
| 43 |
|
| 44 |
+
x, y, w, h = faces[0]
|
| 45 |
+
face = np_img[y:y+h, x:x+w]
|
| 46 |
+
face_resized = cv2.resize(face, (224, 224))
|
| 47 |
+
face_normalized = face_resized / 255.0
|
| 48 |
+
face_expanded = np.expand_dims(face_normalized, axis=0)
|
| 49 |
+
return face_expanded
|
| 50 |
+
|
| 51 |
@app.post("/predict")
|
| 52 |
async def predict_emotion(file: UploadFile = File(...)):
|
| 53 |
if model is None:
|