Guest User

เขียนโปรแกรมเตือนให้พักสายตาด้วย Python

a guest
Apr 21st, 2017
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import cv2
  2. import time
  3. import easygui
  4.  
  5. face_cascade = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml')
  6. eye_cascade = cv2.CascadeClassifier('data/haarcascades/haarcascade_eye_tree_eyeglasses.xml')
  7.  
  8. cap = cv2.VideoCapture(0)
  9.  
  10. last_time = time.time()
  11. elapse = 0
  12.  
  13. while True:
  14.     if elapse <= 20*60:
  15.         ret, img = cap.read()
  16.         gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  17.         faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  18.  
  19.         for (x,y,w,h) in faces:
  20.             cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
  21.  
  22.             roi_gray = gray[y:y+h, x:x+w]
  23.             roi_color = img[y:y+h, x:x+w]
  24.             eyes = eye_cascade.detectMultiScale(roi_gray)
  25.             for (ex,ey,ew,eh) in eyes:
  26.                 cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
  27.                 if (ex,ey,ew,eh) != None:
  28.                     elapse = time.time() - last_time
  29.     else :
  30.         print('Rest your eye!')
  31.         easygui.msgbox('Rest your eye!', 'Eye love you.')
  32.         time.sleep(30)
  33.         elapse = 0
  34.         last_time = time.time()
Advertisement
Add Comment
Please, Sign In to add comment