Advertisement
Guest User

python move detection to file

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