def motion_detection(camera, folder, until): """ Uses 3 frames to look for motion, can't remember where I found it but it gives better result than my first try with comparing 2 frames. """ utils.clear_directory(folder) # Need to get 2 images to start with previous_image = cv2.cvtColor(camera.read()[1], cv2.cv.CV_RGB2GRAY) current_image = cv2.cvtColor(camera.read()[1], cv2.cv.CV_RGB2GRAY) purple = (140, 25, 71) while True: now = datetime.datetime.now() _, image = camera.read() gray_image = cv2.cvtColor(image, cv2.cv.CV_RGB2GRAY) difference1 = cv2.absdiff(previous_image, gray_image) difference2 = cv2.absdiff(current_image, gray_image) result = cv2.bitwise_and(difference1, difference2) # Basic threshold, turn the bitwise_and into a black or white (haha) # result, white (255) being a motion _, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY) # Let's show a square around the detected motion in the original pic low_point, high_point = utils.find_motion_boundaries(result.tolist()) if low_point is not None and high_point is not None: cv2.rectangle(image, low_point, high_point, purple, 3) print 'Motion detected ! Taking picture' utils.save_image(image, folder, now) previous_image = current_image current_image = gray_image if utils.time_over(until, now): break del(camera)
def motion_detection(camera, folder, until): """ Uses 3 frames to look for motion, can't remember where I found it but it gives better result than my first try with comparing 2 frames. """ utils.clear_directory(folder) # Need to get 2 images to start with previous_image = cv2.cvtColor(camera.read()[1], cv2.cv.CV_RGB2GRAY) current_image = cv2.cvtColor(camera.read()[1], cv2.cv.CV_RGB2GRAY) purple = (140, 25, 71) while True: now = datetime.datetime.now() _, image = camera.read() gray_image = cv2.cvtColor(image, cv2.cv.CV_RGB2GRAY) difference1 = cv2.absdiff(previous_image, gray_image) difference2 = cv2.absdiff(current_image, gray_image) result = cv2.bitwise_and(difference1, difference2) # Basic threshold, turn the bitwise_and into a black or white (haha) # result, white (255) being a motion _, result = cv2.threshold(result, 40, 255, cv2.THRESH_BINARY) # Let's show a square around the detected motion in the original pic low_point, high_point = utils.find_motion_boundaries(result.tolist()) if low_point is not None and high_point is not None: cv2.rectangle(image, low_point, high_point, purple, 3) print 'Motion detected ! Taking picture' utils.save_image(image, folder, now) previous_image = current_image current_image = gray_image if utils.time_over(until, now): break del (camera)
def start_camera(camera, folder, interval, until=None): """ Start taking pictures every interval. If until is specified, it will take pictures until that time is reached (24h format). Needs to be of the following format: HH:MM """ utils.clear_directory(folder) number = 0 while True: _, image = camera.read() now = datetime.datetime.now() number += 1 print('Taking picture number %d at %s' % (number, now.isoformat())) utils.save_image(image, folder, now) if utils.time_over(until, now): break time.sleep(interval) del (camera)
def start_camera(camera, folder, interval, until=None): """ Start taking pictures every interval. If until is specified, it will take pictures until that time is reached (24h format). Needs to be of the following format: HH:MM """ utils.clear_directory(folder) number = 0 while True: _, image = camera.read() now = datetime.datetime.now() number += 1 print 'Taking picture number %d at %s' % (number, now.isoformat()) utils.save_image(image, folder, now) if utils.time_over(until, now): break time.sleep(interval) del(camera)