| import streamlit as st |
| from fastai.vision.all import * |
| import cv2 |
| import numpy as np |
| import tempfile |
| model=load_learner('fer_model.pkl') |
| clf=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') |
| st.set_page_config('Face Emotion Recognition') |
| st.title('Face Emotion Recognition') |
| image_up=st.file_uploader('Upload Image',type=['png','jpg','jpeg','mp4','mpeg']) |
| if st.button('Predict'): |
| if image_up: |
| if image_up.name.split('.')[-1] in ['png','jpg','jpeg']: |
| image=PILImage.create(image_up) |
| prediction = model.predict(image) |
| st.image(image, use_column_width='auto') |
| st.write(f'Image predicted as {prediction[0]}') |
| else: |
| tfile=tempfile.NamedTemporaryFile(delete=False) |
| tfile.write(image_up.read()) |
| vf=cv2.VideoCapture(tfile.name) |
| stframe=st.empty() |
| while vf.isOpened(): |
| ret,frame=vf.read() |
| if ret: |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| faces = clf.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) |
| for (x, y, w, h) in faces: |
| cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) |
| face = frame[y:y + h, x:x + w] |
| prediction=model.predict(face) |
| frame = cv2.putText(frame,(prediction[0]), (x , y ), |
| cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) |
| stframe.image(frame) |
| else: |
| break |