Zain commited on
Commit
4720852
1 Parent(s): 5de4f22

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +25 -0
  2. face_expression_detection_model3.h5 +3 -0
  3. main.py +89 -0
  4. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## use the offical python 3.9
2
+
3
+ FROM python:3.9
4
+
5
+ ## Set the working directory to /code
6
+ WORKDIR /code
7
+
8
+ ## Copy the current directory content into the container at /code
9
+ COPY ./requirements.txt /code/requirements.txt
10
+
11
+
12
+ ## install the requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
14
+
15
+ RUN useradd user
16
+ USER user
17
+
18
+ ENV HOME=/home/user \
19
+ PATH=/home/user/.local/bin:$PATH
20
+
21
+ WORKDIR $HOME/app
22
+ COPY --chown=user . $HOME/app
23
+
24
+
25
+ CMD ["uvicorn","app:main","--host","0.0.0.0","--port","7860"]
face_expression_detection_model3.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a68ca8cc7c18e6faf820eefeb5f9829984d823b2f3d6b3ca1ecd5a49b2d531ff
3
+ size 86587288
main.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from tensorflow.keras.models import load_model
3
+ from tensorflow.keras.utils import img_to_array
4
+ os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
5
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
6
+
7
+ import tensorflow as tf
8
+
9
+ from fastapi import FastAPI, File, UploadFile
10
+ from fastapi.responses import JSONResponse
11
+ from fastapi.middleware.cors import CORSMiddleware
12
+ # from tensorflow.keras.preprocessing import image
13
+ import numpy as np
14
+
15
+ import uvicorn
16
+ from PIL import Image
17
+ import io
18
+ import logging
19
+
20
+
21
+
22
+
23
+ # Suppress TensorFlow warnings
24
+ #tf.get_logger().setLevel(logging.ERROR)
25
+
26
+ app = FastAPI()
27
+
28
+ # Add CORS middleware
29
+ app.add_middleware(
30
+ CORSMiddleware,
31
+ allow_origins=["*"], # Adjust as needed
32
+ allow_credentials=True,
33
+ allow_methods=["*"],
34
+ allow_headers=["*"],
35
+ )
36
+
37
+ # Replace with your actual model path
38
+ model_path = 'face_expression_detection_model3.h5' # Update with your new model's path
39
+ model = load_model(model_path)
40
+
41
+
42
+ # def preprocess_image(img: Image.Image, target_size=(224, 224)):
43
+ # img = img.resize(target_size)
44
+ # x = image.img_to_array(img)
45
+ # x = np.expand_dims(x, axis=0)
46
+ # x = x / 255.0 # Assuming normalization was done during training
47
+ # return x
48
+
49
+ def preprocess_image(img: Image.Image, target_size=(224, 224)):
50
+ img = img.resize(target_size)
51
+ x = img_to_array(img)
52
+ x = np.expand_dims(x, axis=0)
53
+ x = x / 255.0 # Assuming normalization was done during training
54
+ return x
55
+
56
+ @app.get("/")
57
+ async def read_root():
58
+ return {"message": "Welcome to the Emotion Classification API"}
59
+
60
+
61
+ @app.post("/predict")
62
+ async def predict(file: UploadFile = File(...)):
63
+ contents = await file.read()
64
+ img = Image.open(io.BytesIO(contents))
65
+ x = preprocess_image(img)
66
+ prediction = model.predict(x)
67
+ predicted_class = np.argmax(prediction[0])
68
+
69
+ class_names = ['angry', 'happy', 'sad', 'surprised', 'neutral', 'disgusted', 'fearful']
70
+ stress_levels = {
71
+ 'angry': 'High',
72
+ 'happy': 'Normal',
73
+ 'sad': 'Low',
74
+ 'surprised': 'Low',
75
+ 'neutral': 'Low',
76
+ 'disgusted': 'Low',
77
+ 'fearful': 'Low'
78
+ }
79
+
80
+ emotion = class_names[predicted_class]
81
+ stress_level = stress_levels[emotion]
82
+
83
+ result = {
84
+ #"predicted_class": emotion,
85
+ "predicted_class": stress_level
86
+ }
87
+ return JSONResponse(content=result)
88
+
89
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ tensorflow==2.12.0
4
+ pillow