Prime810 commited on
Commit
58aac12
·
verified ·
1 Parent(s): a5234fe

Update Backend/app.py

Browse files
Files changed (1) hide show
  1. Backend/app.py +25 -25
Backend/app.py CHANGED
@@ -1,6 +1,7 @@
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import JSONResponse
 
4
  from pydantic import BaseModel
5
  import base64
6
  from io import BytesIO
@@ -10,23 +11,33 @@ import cv2
10
  import os
11
  import traceback
12
  from keras.models import load_model
13
- from fastapi.responses import FileResponse
14
- from fastapi.staticfiles import StaticFiles
15
 
16
- # Load the trained model
17
- model_path = os.path.join(os.path.dirname(__file__), 'emotion_model.keras')
18
- model = load_model(model_path)
19
 
20
- # Mount the frontend folder to serve static files
21
  app.mount("/static", StaticFiles(directory="Frontend"), name="static")
22
 
23
- # Emotion and emoji maps
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  emotion_dict = {
25
  0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy",
26
  4: "Neutral", 5: "Sad", 6: "Surprised"
27
  }
28
-
29
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
30
  emoji_map = {
31
  0: os.path.join(BASE_DIR, "emojis", "angry.png"),
32
  1: os.path.join(BASE_DIR, "emojis", "disgusted.png"),
@@ -37,27 +48,16 @@ emoji_map = {
37
  6: os.path.join(BASE_DIR, "emojis", "surprised.png")
38
  }
39
 
40
- # Initialize FastAPI app
41
- app = FastAPI()
42
- app.add_middleware(
43
- CORSMiddleware,
44
- allow_origins=["*"],
45
- allow_credentials=True,
46
- allow_methods=["*"],
47
- allow_headers=["*"],
48
- )
49
-
50
  class ImageData(BaseModel):
51
  image: str
52
 
53
- # Load Haarcascade once
54
- face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
55
-
56
  @app.get("/")
57
  def serve_homepage():
58
  return FileResponse("Frontend/index.html")
59
 
60
-
61
  @app.post("/process_image")
62
  async def process_image(data: ImageData):
63
  try:
@@ -73,7 +73,7 @@ async def process_image(data: ImageData):
73
  raise HTTPException(status_code=400, detail="No face detected")
74
 
75
  for (x, y, w, h) in faces:
76
- roi_gray = gray[y:y + h, x:x + w]
77
  roi = cv2.resize(roi_gray, (48, 48))
78
  roi = roi.astype("float") / 255.0
79
  roi = np.expand_dims(roi, axis=-1)
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import JSONResponse, FileResponse
4
+ from fastapi.staticfiles import StaticFiles
5
  from pydantic import BaseModel
6
  import base64
7
  from io import BytesIO
 
11
  import os
12
  import traceback
13
  from keras.models import load_model
 
 
14
 
15
+ # Initialize FastAPI app
16
+ app = FastAPI()
 
17
 
18
+ # Mount static files from Frontend
19
  app.mount("/static", StaticFiles(directory="Frontend"), name="static")
20
 
21
+ # Add CORS middleware
22
+ app.add_middleware(
23
+ CORSMiddleware,
24
+ allow_origins=["*"],
25
+ allow_credentials=True,
26
+ allow_methods=["*"],
27
+ allow_headers=["*"],
28
+ )
29
+
30
+ # Load model and cascade
31
+ model_path = os.path.join(os.path.dirname(__file__), 'emotion_model.keras')
32
+ model = load_model(model_path)
33
+ face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
34
+
35
+ # Emoji and emotion maps
36
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
37
  emotion_dict = {
38
  0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy",
39
  4: "Neutral", 5: "Sad", 6: "Surprised"
40
  }
 
 
41
  emoji_map = {
42
  0: os.path.join(BASE_DIR, "emojis", "angry.png"),
43
  1: os.path.join(BASE_DIR, "emojis", "disgusted.png"),
 
48
  6: os.path.join(BASE_DIR, "emojis", "surprised.png")
49
  }
50
 
51
+ # Schema
 
 
 
 
 
 
 
 
 
52
  class ImageData(BaseModel):
53
  image: str
54
 
55
+ # Serve homepage
 
 
56
  @app.get("/")
57
  def serve_homepage():
58
  return FileResponse("Frontend/index.html")
59
 
60
+ # Process image
61
  @app.post("/process_image")
62
  async def process_image(data: ImageData):
63
  try:
 
73
  raise HTTPException(status_code=400, detail="No face detected")
74
 
75
  for (x, y, w, h) in faces:
76
+ roi_gray = gray[y:y+h, x:x+w]
77
  roi = cv2.resize(roi_gray, (48, 48))
78
  roi = roi.astype("float") / 255.0
79
  roi = np.expand_dims(roi, axis=-1)