Upload 4 files
Browse files- app.py +37 -0
- fer_model.pkl +3 -0
- haarcascade_frontalface_default.xml +0 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from fastai.vision.all import *
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
+
model=load_learner('fer_model.pkl')
|
| 7 |
+
clf=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
| 8 |
+
st.set_page_config('Face Emotion Recognition')
|
| 9 |
+
st.title('Face Emotion Recognition')
|
| 10 |
+
image=st.file_uploader('Upload Image',type=['png','jpg','jpeg','mp4','mpeg'])
|
| 11 |
+
if st.button('Predict'):
|
| 12 |
+
image=PILImage.create(image)
|
| 13 |
+
if image:
|
| 14 |
+
if image.name.split('.')[-1] in ['png','jpg','jpeg']:
|
| 15 |
+
image_ = PILImage.create(image)
|
| 16 |
+
prediction = model.predict(image_)
|
| 17 |
+
st.image(image, use_column_width='auto')
|
| 18 |
+
st.write(f'Image predicted as {prediction[0]}')
|
| 19 |
+
else:
|
| 20 |
+
tfile=tempfile.NamedTemporaryFile(delete=False)
|
| 21 |
+
tfile.write(image.read())
|
| 22 |
+
vf=cv2.VideoCapture(tfile.name)
|
| 23 |
+
stframe=st.empty()
|
| 24 |
+
while vf.isOpened():
|
| 25 |
+
ret,frame=vf.read()
|
| 26 |
+
if ret:
|
| 27 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 28 |
+
faces = clf.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
|
| 29 |
+
for (x, y, w, h) in faces:
|
| 30 |
+
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
|
| 31 |
+
face = frame[y:y + h, x:x + w]
|
| 32 |
+
prediction=model.predict(face)
|
| 33 |
+
frame = cv2.putText(frame,(prediction[0]), (x , y ),
|
| 34 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
|
| 35 |
+
stframe.image(frame)
|
| 36 |
+
else:
|
| 37 |
+
break
|
fer_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b3513cfcdfee5a96fb28e2968a44c514857a252f0d30428f46e90760d80c314d
|
| 3 |
+
size 88242874
|
haarcascade_frontalface_default.xml
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.29.0
|
| 2 |
+
fastai==2.7.12
|
| 3 |
+
opencv-python==4.7.0.72
|
| 4 |
+
numpy==1.23.5
|