I'm trying to use python to capture a photo from my ip cameras every 1 minute and save it as a file for later use. I'm still not good at python and I don't get why some of the times I get the right image and sometimes I get a corrupted gray image. I'm using hikvision api to get the rtsp stream and while the stream is working sometimes the images are still fully gray. Here is the code I wrote:
import cv2
import time
count = 0
while True:
for x in range(1, 9):
count = count +1
RTSP_URL = f'rtsp://user:password@ip:port/ISAPI/Streaming/Channels/{x}01'
cap = cv2.VideoCapture(RTSP_URL, cv2.CAP_FFMPEG)
result, image = cap.read()
if result:
cv2.imwrite(f"pictures/{x}{count}.png", image)
time.sleep(60)
I would be happy to hear suggestions to find the best way to do this task.
time.sleep(60)? For testing, you may addcv2.imshow('image', image)andcv2.waitKey(1)for showing the video.cap.release()afterresult, image = cap.read(). That way you are going to close and reopen the connection every minute. I don't think we can use newcap = cv2.VideoCapturewithout executingcap.release(). In case you want to hold 8 RTSP connections open, use a list of 8capobjects. Open the 8capobjects before thewhile True, and usecap[x-1].read(). Make sure that the CPU load is not too high.