Advertisement
Guest User

Untitled

a guest
Jan 15th, 2022
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. #Code From https://www.youtube.com/watch?v=PulKlAZRoAY&t=3s
  2. #https://github.com/pypower-codes/Emotion-Detection
  3. from keras.models import load_model
  4. from time import sleep
  5. from keras.preprocessing.image import img_to_array
  6. from keras.preprocessing import image
  7. import cv2
  8. import numpy as np
  9.  
  10. face_classifier = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
  11. classifier = load_model('New_One_Pence_Two_Pence_T50.h5')
  12.  
  13. class_labels = ['Two_Pence(0)', 'One_Pence(1)']
  14.  
  15. cap = cv2.VideoCapture(0)
  16.  
  17. while True:
  18.     # Grab a single frame of video
  19.     ret, frame = cap.read()
  20.     labels = []
  21.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  22.     faces = face_classifier.detectMultiScale(gray, 1.3, 5)
  23.  
  24.     for (x, y, w, h) in faces:
  25.         cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
  26.         roi_gray = gray[y:y + h, x:x + w]
  27.         roi_gray = cv2.resize(roi_gray, (255, 255), interpolation=cv2.INTER_AREA)
  28.         #roi_gray = cv2.resize(roi_gray, (255, 255), interpolation=cv2.INTER_AREA)
  29.  
  30.  
  31.  
  32.         if np.sum([roi_gray]) != 0:
  33.             roi = roi_gray.astype('float') / 255.0
  34.             roi = img_to_array(roi)
  35.             roi = np.expand_dims(roi, axis=0)
  36.  
  37.             # make a prediction on the ROI, then lookup the class
  38.  
  39.             preds = classifier.predict(roi)[0]
  40.             print("\nprediction = ", "Folder_1_Photos", preds,"Folder_2_Photos")
  41.             label = class_labels[preds.argmax()]
  42.             print("\nprediction max = ", preds.argmax())
  43.             print("\nlabel = ", label)
  44.             label_position = (x, y)
  45.             cv2.putText(frame, label, label_position, cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
  46.  
  47.  
  48.         else:
  49.             cv2.putText(frame, 'No Face Found', (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
  50.  
  51.         print("\n\n")
  52.         cv2.imshow('Emotion Detector', frame)
  53.         if cv2.waitKey(1) & 0xFF == ord('q'):
  54.             break
  55.  
  56.  
  57.  
  58.  
  59.  
  60.     cv2.imshow('Emotion Detector', frame)
  61.     if cv2.waitKey(1) & 0xFF == ord('q'):
  62.         break
  63.  
  64. cap.release()
  65. cv2.destroyAllWindows()
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement