This is web_app.py file

parent 79aa4d32
import mediapipe as mp
import requests
import json
from tensorflow.keras.models import load_model
import numpy as np
import cv2
data = {0: 'Angry', 1: 'Disgust', 2: 'Fear', 3: 'Happy', 4: 'Sad', 5: 'Surprise', 6: 'Neutral'}
model = load_model('emotion/model.h5')
def pre_processing(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.equalizeHist(img)
img = img / 255
return img
def emo_rec(img):
img = np.asarray(img)
img = cv2.resize(img, (48, 48))
img = pre_processing(img)
img = img.reshape(1, 48, 48, 1)
predict_x = model.predict(img)
classes_x = np.argmax(predict_x, axis=1)
return data[classes_x[0]], str(classes_x[0])
def send_data_to_api(emotion):
url = "http://127.0.0.1:5000/from_web_app"
data = {'emotion': emotion
}
# sending post request and saving response as response object
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
print('pushed')
def web_camera():
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
# BGR 2 RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imshow('Hand Tracking', image)
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
haar_cascade = cv2.CascadeClassifier('Haarcascade_frontalface_default.xml')
faces_rect = haar_cascade.detectMultiScale(gray_img, 1.1, 9)
if len(faces_rect) > 0:
for (x, y, w, h) in faces_rect:
crop_img = image[y:y + h, x:x + w]
emo, emo_index = emo_rec(crop_img)
# send_data_to_api(emo_index)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, emo, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
resized_img = cv2.resize(image, (800, 600))
ret, buffer = cv2.imencode('.jpg', resized_img)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment