Hello, I’m trying to take picture of a frame using motion detection, basicaly, when motion gets a car rectangle, it starts to take picture of the frame, but when I got detection it takes a lot of pictures 'cause detection continue untill rectangle stop, there is anothe way to take only one picture when are detections ? Thanks.
import cv2
import imutils
import time
cap = cv2.VideoCapture(“rtsp://admin:a10b20c30d40@192.168.0.116:554/cam/realmonitor?channel=1&subtype=2”)
ret, frame1 = cap.read()
ret, frame2 = cap.read()
while cap.isOpened():
ts = time.time()
diff = cv2.absdiff(frame1,frame2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
if cv2.contourArea(contour) < 199900:
continue
cv2.rectangle(frame1, (x,y), (x+w, y+h), (0, 255, 0), 2)
cv2.putText(frame1, "status: {}".format('Movement'), (10, 20), cv2.FONT_HERSHEY_SIMPLEX,
1, (0, 0, 255), 3)
cv2.imwrite('C:\\Users\\Administrador\\Desktop\\picture\\' + str(ts) + '.png', frame1)
resized = imutils.resize(frame1, width=600)
cv2.imshow('feed', resized)
frame1 = frame2
ret, frame2 = cap.read()
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyWindow()