# import the necessary packages from picamera.array import PiRGBArray from picamera import PiCamera import time import cv2 from PIL import Image import RPi.GPIO as GPIO from scipy.linalg import norm # initialize the camera and grab a reference to the raw camera capture camera = PiCamera() camera.resolution = (320, 240) camera.framerate = 25 rawCapture = PiRGBArray(camera, size=(320, 240)) #camera.resolution = (640, 480) #camera.framerate = 60 #rawCapture = PiRGBArray(camera, size=(640, 480)) # allow the camera to warmup time.sleep(0.1) print "EXIT program: q" brslik=1 start = 1 klik = 0 led0 = 23 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(led0,GPIO.OUT) # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = frame.array if start > 0: start = 0 imageta = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) bkp_imaget = cv2.threshold(imageta, 127, 255, cv2.THRESH_BINARY)[1] kompas = 1 if kompas == 1: imageta = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) imaget = cv2.threshold(imageta, 180, 255, cv2.THRESH_BINARY)[1] # 127 half diff = imaget - bkp_imaget z_norm = norm(diff.ravel(),0) print z_norm if z_norm > 1000: # -------------------------- pixels difference - sensitivity GPIO.output(led0,GPIO.HIGH) else: GPIO.output(led0,GPIO.LOW) bkp_imaget = imaget.copy() # show the frame cv2.imshow("Frame", image) cv2.imshow("Frame2", imaget) # clear the stream in preparation for the next frame rawCapture.truncate(0) # if the `q` key was pressed, break from the loop key = cv2.waitKey(1) & 0xFF if key == ord("q"): break