Advertisement
nonkung51

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

Apr 22nd, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. # by nonkung51 for this https://medium.com/@nonthakon/เขียนโปรแกรมเตือนให้พักสายตาด้วย-python-6f61d9f56cf7
  2. import cv2
  3. import time
  4. import easygui
  5.  
  6. face_cascade = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml')
  7.  
  8. cap = cv2.VideoCapture(0)
  9.  
  10. last_time = time.time()
  11. elapse = 0
  12.  
  13. while True:
  14.     if elapse <= 18*60:
  15.         ret, img = cap.read()
  16.         gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  17.         faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  18.         if faces != ():
  19.             elapsePerFrame = time.time() - last_time
  20.             elapse += elapsePerFrame
  21.             print('You stared at the screen for {} seconds now.'.format(round(elapse, 2)))
  22.             last_time = time.time()
  23.         else:
  24.             print('eyes not detect')
  25.             last_time = time.time()
  26.         time.sleep(1)
  27.     else :
  28.         print('Rest your eye!')
  29.         easygui.msgbox('Rest your eye!', 'Eye love you!')
  30.         time.sleep(30)
  31.         easygui.msgbox('It\'s OK now.', 'Eye love you!')
  32.         elapse = 0
  33.         last_time = time.time()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement