-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (46 loc) · 1.62 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import uvicorn
import numpy as np
import cv2 as cv
import tensorflow.keras as keras
from PIL import Image
from fastapi import FastAPI
from starlette.responses import FileResponse
from pydantic import BaseModel
import io
import base64
app = FastAPI()
CLASSES = {0: 'angry',
1: 'disgust',
2: 'fear',
3: 'happy',
4: 'neutral',
5: 'sad',
6: 'surprise'}
class Item(BaseModel):
img: str
model = keras.models.load_model('my_arch64.h5')
@app.post('/')
async def GetMood(item: Item):
b = base64.b64decode(item.img)
img = np.array(Image.open(io.BytesIO(b)))
fc = cv.CascadeClassifier(r'haarcascade_frontalface_default.xml')
detection_result, rejectLevels, levelWeights = fc.detectMultiScale3(img, outputRejectLevels=True,
scaleFactor=1.1, minNeighbors=3,
minSize=(48, 48))
try:
levelWeights = levelWeights.reshape(len(levelWeights))
max_index = list(levelWeights).index(levelWeights.max())
x, y, w, h = detection_result[max_index]
face = img[y:y + h, x:x + w]
gray = cv.cvtColor(face, cv.COLOR_BGR2GRAY)
img = cv.resize(gray, (128, 128))
except:
return "Couldn't detect a face"
input_arr = np.array([img])
predictions = model.predict(input_arr)
predictions = list(predictions.reshape(7))
prediction = CLASSES[predictions.index(max(predictions))]
return prediction
@app.get('/')
async def Welcome():
return 'Welcome to Mood Classifier API'