smile_detection.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import cv2
  2. faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
  3. smileCascade= cv2.CascadeClassifier('haarcascade_smile.xml')
  4. # grab the reference to the webcam
  5. vs = cv2.VideoCapture(0)
  6. # keep looping
  7. while True:
  8. # grab the current frame
  9. ret, frame = vs.read()
  10. # if we are viewing a video and we did not grab a frame,
  11. # then we have reached the end of the video
  12. if frame is None:
  13. break
  14. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  15. gray = cv2.equalizeHist(gray)
  16. faces = faceCascade.detectMultiScale(frame, scaleFactor=1.05, minNeighbors=5, minSize=(45, 45))
  17. for (x,y,w,h) in faces:
  18. #cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
  19. face_gray = gray[y:y+h, x:x+w]
  20. face_color = frame[y:y+h, x:x+w]
  21. smiles = smileCascade.detectMultiScale(face_gray, scaleFactor=1.7, minNeighbors=3, minSize=(15, 15))
  22. for (ex,ey,ew,eh) in smiles:
  23. cv2.rectangle(face_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
  24. # show the frame to our screen
  25. cv2.imshow("Video", frame)
  26. key = cv2.waitKey(1) & 0xFF
  27. # if the 'q' or ESC key is pressed, stop the loop
  28. if key == ord("q") or key == 27:
  29. break
  30. # close all windows
  31. cv2.destroyAllWindows()