Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import time
- from datetime import datetime
- # Video Capture
- capture = cv2.VideoCapture("rtsp://admin:123456@192.168.0.86:554/H264?ch=1&subtype=2")
- fourcc = cv2.VideoWriter_fourcc(*'XVID')
- #capture = cv2.VideoCapture("demo.mov")
- # History, Threshold, DetectShadows
- fgbg = cv2.createBackgroundSubtractorMOG2(1000, 200, False)
- #fgbg = cv2.createBackgroundSubtractorMOG2(300, 400, True)
- # Keeps track of what frame we're on
- frameCount = 0
- while(1):
- # Return Value and the current frame
- ret, frame = capture.read()
- # Check if a current frame actually exist
- if not ret:
- break
- frameCount += 1
- # Resize the frame
- resizedFrame = cv2.resize(frame, (0, 0), fx=1, fy=1)
- # Get the foreground mask
- fgmask = fgbg.apply(resizedFrame)
- # Count all the non zero pixels within the mask
- count = np.count_nonzero(fgmask)
- print('Frame: %d, Pixel Count: %d' % (frameCount, count))
- # Determine how many pixels do you want to detect to be considered "movement"
- # if (frameCount > 1 and cou`nt > 5000):
- if (frameCount > 1 and count >2000):
- print('RUCH')
- cv2.putText(resizedFrame, 'RUCH', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
- now = datetime.now().strftime("%Y%m%d-%H%M%S")
- filename = now + '.avi'
- out = cv2.VideoWriter(filename, fourcc, 20.0, (640, 480))
- cv2.imshow('Frame', resizedFrame)
- cv2.imshow('Mask', fgmask)
- k = cv2.waitKey(1) & 0xff
- if k == 27:
- break
- capture.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement