Guest User

Untitled

a guest
May 23rd, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. from picamera import PiCamera, PiCameraCircularIO, PiVideoFrameType
  2. from picamera.array import PiRGBArray
  3. from time import sleep
  4. import io
  5. import cv2
  6.  
  7. def diff_image(t0, t1, t2):
  8. d1 = cv2.absdiff(t2, t1)
  9. d2 = cv2.absdiff(t1, t0)
  10. return cv2.bitwise_and(d1, d2)
  11.  
  12. def detect_motion():
  13. for frame in stream.frames:
  14. print(frame)
  15. #print(stream.frames[0])
  16.  
  17. def write_video(stream):
  18. print("writing")
  19. with stream.lock:
  20. # Find the first header frame in the video
  21. for frame in stream.frames:
  22. if frame.frame_type == PiVideoFrameType.sps_header:
  23. stream.seek(frame.position)
  24. break
  25. # Write the rest of the stream to disk
  26. with io.open('video.h264', 'wb') as output:
  27. output.write(stream.read())
  28.  
  29. with PiCamera(resolution="1056x768") as cam:
  30. capture_stream = PiRGBArray(cam)
  31. stream = PiCameraCircularIO(cam, seconds=30)
  32. images = []
  33. try:
  34. cam.start_preview(alpha = 192)
  35. cam.framerate = 10
  36. cam.start_recording(stream, format="h264")
  37. cam.color_effects = (128,128)
  38. print("recording")
  39. while True:
  40. cam.capture(capture_stream, format='bgr')
  41. images.append(cv2.cvtColor(capture_stream.array, cv2.COLOR_RGB2GRAY))
  42. capture_stream.truncate(0)
  43. if len(images) == 3:
  44. #print("diff")
  45. diff = diff_image(images[0], images[1], images[2])
  46. #for i in range(len(images)):
  47. # cv2.imwrite("image{}.png".format(i), images[i])
  48. #cv2.imwrite("diff.png", diff)
  49. if cv2.countNonZero(diff) > 300000:
  50. print("motion")
  51. #print(cv2.countNonZero(diff))
  52. images.pop(0)
  53.  
  54. #cam.wait_recording(0.1)
  55. #input("press enter")
  56. #write_video(stream)
  57. finally:
  58. cam.stop_recording()
  59. cam.stop_preview()
  60. I am
  61. print("finished")
Add Comment
Please, Sign In to add comment