Advertisement
Guest User

Untitled

a guest
Aug 8th, 2021
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import time
  4. from datetime import datetime
  5. from timer import Timer
  6. t = Timer()
  7.  
  8. # Video Capture
  9. capture = cv2.VideoCapture("rtsp://admin:123456@192.168.0.86:554/H264?ch=1&subtype=2")
  10. fourcc = cv2.VideoWriter_fourcc(*'XVID')
  11. # capture = cv2.VideoCapture("demo.mov")
  12.  
  13. # History, Threshold, DetectShadows
  14. fgbg = cv2.createBackgroundSubtractorMOG2(1000, 200, False)
  15. # fgbg = cv2.createBackgroundSubtractorMOG2(300, 400, True)
  16.  
  17. # Keeps track of what frame we're on
  18. frameCount = 0
  19.  
  20. while (1):
  21.     # Return Value and the current frame
  22.     ret, frame = capture.read()
  23.  
  24.     #  Check if a current frame actually exist
  25.     if not ret:
  26.         break
  27.  
  28.     frameCount += 1
  29.     # Resize the frame
  30.     resizedFrame = cv2.resize(frame, (0, 0), fx=1, fy=1)
  31.  
  32.     # Get the foreground mask
  33.     fgmask = fgbg.apply(resizedFrame)
  34.  
  35.     # Count all the non zero pixels within the mask
  36.     count = np.count_nonzero(fgmask)
  37.  
  38.     print('Frame: %d, Pixel Count: %d' % (frameCount, count))
  39.  
  40.     # Determine how many pixels do you want to detect to be considered "movement"
  41.     # if (frameCount > 1 and cou`nt > 5000):
  42.     if (frameCount > 1 and count > 2000):
  43.         x = t.start()
  44.         while t.stop() - x < 20:
  45.             print('RUCH')
  46.             cv2.putText(resizedFrame, 'RUCH', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
  47.             now = datetime.now().strftime("%Y%m%d-%H%M%S")
  48.             filename = now + '.avi'
  49.             out = cv2.VideoWriter(filename, fourcc, 20.0, (640, 480))
  50.  
  51.     cv2.imshow('Frame', resizedFrame)
  52.     cv2.imshow('Mask', fgmask)
  53.  
  54.     k = cv2.waitKey(1) & 0xff
  55.     if k == 27:
  56.         break
  57.  
  58. capture.release()
  59. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement