Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. # USAGE
  2. # python multi_cam_motion.py
  3.  
  4. # import the necessary packages
  5. from __future__ import print_function
  6. from pyimagesearch.basicmotiondetector import BasicMotionDetector
  7. from imutils.video import VideoStream
  8. import numpy as np
  9. import datetime
  10. import imutils
  11. import time
  12. import cv2
  13.  
  14. # initialize the video streams and allow them to warmup
  15. print("[INFO] starting cameras...")
  16. webcam1 = VideoStream(src=0).start()
  17. webcam2 = VideoStream(src=1).start()
  18.  
  19. time.sleep(2.0)
  20.  
  21. # initialize the two motion detectors, along with the total
  22. # number of frames read
  23. camMotion = BasicMotionDetector()
  24. piMotion = BasicMotionDetector()
  25. total = 0
  26.  
  27. # loop over frames from the video streams
  28. while True:
  29. # initialize the list of frames that have been processed
  30. frames = []
  31.  
  32. # loop over the frames and their respective motion detectors
  33. for (stream, motion) in zip((webcam1, webcam2), (camMotion, piMotion)):
  34. # read the next frame from the video stream and resize
  35. # it to have a maximum width of 400 pixels
  36. frame = stream.read()
  37. frame = imutils.resize(frame, width=400)
  38. roomText = "Unoccupied"
  39.  
  40. # convert the frame to grayscale, blur it slightly, update
  41. # the motion detector
  42. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  43. gray = cv2.GaussianBlur(gray, (21, 21), 0)
  44. locs = motion.update(gray)
  45.  
  46. # we should allow the motion detector to "run" for a bit
  47. # and accumulate a set of frames to form a nice average
  48. if total < 32:
  49. frames.append(frame)
  50. continue
  51.  
  52. # otherwise, check to see if motion was detected
  53. if len(locs) > 0:
  54. # initialize the minimum and maximum (x, y)-coordinates,
  55. # respectively
  56. (minX, minY) = (np.inf, np.inf)
  57. (maxX, maxY) = (-np.inf, -np.inf)
  58.  
  59. # loop over the locations of motion and accumulate the
  60. # minimum and maximum locations of the bounding boxes
  61. for l in locs:
  62. (x, y, w, h) = cv2.boundingRect(l)
  63. (minX, maxX) = (min(minX, x), max(maxX, x + w))
  64. (minY, maxY) = (min(minY, y), max(maxY, y + h))
  65.  
  66. # draw the bounding box
  67. cv2.rectangle(frame, (minX, minY), (maxX, maxY),
  68. (0, 0, 255), 3)
  69. roomText = "Occupied"
  70. if roomText == "Occupied":
  71. print("Recording...")
  72. cv2.VideoWriter.write(frames)
  73.  
  74. # update the frames list
  75. frames.append(frame)
  76.  
  77. # increment the total number of frames read and grab the
  78. # current timestamp
  79. total += 1
  80. timestamp = datetime.datetime.now()
  81. ts = timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
  82.  
  83. # loop over the frames a second time
  84. for (frame, name) in zip(frames, ("Camera1", "Camera2")):
  85. # draw the timestamp on the frame and display it
  86. cv2.putText(frame, ts, (10, frame.shape[0] - 10),
  87. cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
  88. cv2.putText(frame, "Room Status: {}".format(roomText), (10, 20),
  89. cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
  90. cv2.imshow(name, frame)
  91.  
  92. # check to see if a key was pressed
  93. key = cv2.waitKey(1) & 0xFF
  94.  
  95. # if the `q` key was pressed, break from the loop
  96. if key == ord("q"):
  97. break
  98.  
  99. # do a bit of cleanup
  100. print("[INFO] cleaning up...")
  101. cv2.destroyAllWindows()
  102. webcam1.stop()
  103. webcam2.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement