Guest User

Untitled

a guest
Jan 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. from picamera.array import PiRGBArray
  2.  
  3. from tkinter import *
  4.  
  5. from tkinter import messagebox
  6.  
  7. import RPi.GPIO as gpio
  8.  
  9. from picamera import PiCamera
  10.  
  11. import time
  12.  
  13. import cv2
  14.  
  15. import threading
  16.  
  17.  
  18.  
  19.  
  20.  
  21. window = Tk()
  22.  
  23. window.title("Message Box")
  24.  
  25. window.geometry("200x100+0+0")
  26.  
  27. window.resizable(0,0)
  28.  
  29.  
  30.  
  31. Sec = 0
  32.  
  33.  
  34.  
  35. def popup():
  36.  
  37. test_button = Button(window, text = "Open Your Eyes!!",)
  38.  
  39. test_button.pack()
  40.  
  41. window.mainloop()
  42.  
  43.  
  44.  
  45. RELAY = 17
  46.  
  47. gpio.setmode(gpio.BCM)
  48.  
  49. gpio.setup(RELAY, gpio.OUT, initial=gpio.LOW)
  50.  
  51. camera = PiCamera()
  52.  
  53. camera.resolution = (640, 480)
  54.  
  55. camera.framerate = 40
  56.  
  57. rawCapture = PiRGBArray(camera, size=(640, 480))
  58.  
  59.  
  60.  
  61. faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
  62.  
  63. eyesCascade = cv2.CascadeClassifier("haarcascade_eye_tree_eyeglasses.xml")
  64.  
  65.  
  66.  
  67. time.sleep(0.1)
  68.  
  69.  
  70.  
  71. for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
  72.  
  73. image = frame.array
  74.  
  75.  
  76.  
  77. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  78.  
  79. faces = faceCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50))
  80.  
  81.  
  82.  
  83. for (x, y, w, h) in faces:
  84.  
  85. cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
  86.  
  87. roi_gray = gray[y:y + h, x:x + w]
  88.  
  89. roi_color = image[y:y + h, x:x + w]
  90.  
  91. eyes = eyesCascade.detectMultiScale(roi_gray)
  92.  
  93.  
  94.  
  95. for (ex, ey, ew, eh) in eyes:
  96.  
  97. print (eyes)
  98.  
  99. cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (100, 255, 255), 2)
  100.  
  101.  
  102.  
  103.  
  104.  
  105. if len(faces) >= 1 and len(eyes) >= 2:
  106.  
  107. Sec = 0
  108.  
  109. # cv2.putText(image, 'WARNING!', (10, 500), cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 255, 255), 2)
  110.  
  111. gpio.output(RELAY, False)
  112.  
  113.  
  114.  
  115. else:
  116.  
  117. Sec += 1
  118.  
  119. print(str(Sec) + " Sec")
  120.  
  121. time.sleep(1)
  122.  
  123. if Sec == 3:
  124.  
  125. popup()
  126.  
  127. gpio.output(RELAY, True)
  128.  
  129.  
  130.  
  131. cv2.imshow("OPEN CV", image)
  132.  
  133. key = cv2.waitKey(1) & 0xFF
  134.  
  135. rawCapture.truncate(0)
  136.  
  137.  
  138.  
  139. if key == ord("q"):
  140.  
  141. break
  142.  
  143. gpio.output(RELAY, False)
  144.  
  145. gpio.cleanup()
  146.  
  147. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment