Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. # import the necessary packages
  2. import argparse
  3. import datetime
  4. import imutils
  5. import time
  6. import cv2
  7. import numpy as np
  8.  
  9. # construct the argument parser and parse the arguments
  10.  
  11. ap = argparse.ArgumentParser()
  12. ap.add_argument("-v", "--video", help="path to the video file")
  13. ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
  14. args = vars(ap.parse_args())
  15.  
  16. # if the video argument is None, then we are reading from webcam
  17. if args.get("video", None) is None:
  18.     camera = cv2.VideoCapture(0)
  19.     time.sleep(0.25)
  20.  
  21. # otherwise, we are reading from a video file
  22. else:
  23.     camera = cv2.VideoCapture(args["video"])
  24.  
  25. # initialize the first frame in the video stream
  26. firstFrame = None
  27.  
  28. # loop over the frames of the video
  29. while True:
  30.     # grab the current frame and initialize the occupied/unoccupied
  31.     # text
  32.     (grabbed, frame) = camera.read()
  33.     text = "Unoccupied"
  34.  
  35.     # if the frame could not be grabbed, then we have reached the end
  36.     # of the video
  37.     if not grabbed:
  38.         break
  39.  
  40.     # resize the frame, convert it to grayscale, and blur it
  41.     frame = imutils.resize(frame, width=500)
  42.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  43.     # gray = cv2.GaussianBlur(gray, (21, 21), 0)
  44.  
  45.     # if the first frame is None, initialize it
  46.     if firstFrame is None:
  47.         firstFrame = gray
  48.         continue
  49.  
  50.     # compute the absolute difference between the current frame and
  51.     # first frame
  52.     frameDelta = cv2.absdiff(firstFrame, gray)
  53.     thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
  54.  
  55.     # dilate the thresholded image to fill in holes, then find contours
  56.     # on thresholded image
  57.     # thresh = cv2.dilate(thresh, None, iterations=2)
  58.  
  59.     # lower mask (0-10)
  60.     lower_red = np.array([0, 50, 50])
  61.     upper_red = np.array([180, 255, 255])
  62.     mask0 = cv2.inRange(thresh.copy(), lower_red, upper_red)
  63.  
  64.     # upper mask (170-180)
  65.     lower_red = np.array([170, 50, 50])
  66.     upper_red = np.array([180, 255, 255])
  67.     mask1 = cv2.inRange(thresh.copy(), lower_red, upper_red)
  68.  
  69.     mask = mask0
  70.  
  71.     output_hsv = thresh.copy()
  72.     output_hsv[np.where(mask == 0)] = 0
  73.  
  74.     # _, cnts, _= cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
  75.     #                              cv2.CHAIN_APPROX_SIMPLE)
  76.     #
  77.     # # loop over the contours
  78.     # for c in cnts:
  79.     #     # if the contour is too small, ignore it
  80.     #     if cv2.contourArea(c) < args["min_area"]:
  81.     #         continue
  82.     #
  83.     #     # compute the bounding box for the contour, draw it on the frame,
  84.     #     # and update the text
  85.     #     (x, y, w, h) = cv2.boundingRect(c)
  86.     #     cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  87.     #     text = "Occupied"
  88.  
  89.     # draw the text and timestamp on the frame
  90.     cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
  91.                 cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
  92.     cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
  93.                 (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
  94.  
  95.     # show the frame and record if the user presses a key
  96.     cv2.imshow("Security Feed", frame)
  97.     cv2.imshow("Thresh", output_hsv)
  98.     cv2.imshow("Frame Delta", frameDelta)
  99.     key = cv2.waitKey(1) & 0xFF
  100.  
  101.     # if the `q` key is pressed, break from the lop
  102.     if key == ord("q"):
  103.         break
  104.  
  105. # cleanup the camera and close any open windows
  106. camera.release()
  107. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement