Advertisement
jackieradallhazik

Take a picture if you smile OPENCV

Jan 7th, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4. face_dascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  5.  
  6. eye_dascade = cv2.CascadeClassifier('haarcascade_eye.xml')
  7. smile_dascade = cv2.CascadeClassifier('haarcascade_smile.xml')
  8.  
  9. cap = cv2.VideoCapture(0)
  10. # temp = cv2.imread('dat.jpg',0)
  11. # temp_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. a = 0
  13. while a != 1:
  14.     ret, img = cap.read()
  15.     hinh = np.copy(img)
  16.     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  17.     faces = face_dascade.detectMultiScale(gray, 1.3, 5)
  18.     for (x,y,w,h) in faces:
  19.         cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
  20.         face_gray = gray[y:y+h, x:x+h]
  21.         face_color = img[y:y+h, x:x+h]
  22.         smile = smile_dascade.detectMultiScale(face_gray, 1.3, 5)
  23.         for (x2,y2,w2,h2) in smile:
  24.             if((((h*1.0*w)/(w2*h2))<11) and len(smile)==len(faces)):
  25.                 cv2.rectangle(face_color, (x2,y2), (x2+w2, y2+h2), (150,0,0), 2)
  26.                 cv2.imwrite('hinh1.jpg',hinh)
  27.                 a = 1
  28.         eyes = eye_dascade.detectMultiScale(face_gray)
  29.         for (xe,ye,we,he) in eyes:
  30.             cv2.rectangle(face_color, (xe,ye), (xe+we, ye+he), (0,0,255), 2)
  31.     cv2.imshow('img',img)
  32.     if cv2.waitKey(1) == 27:
  33.         break
  34. cap.release()
  35. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement