如何进行 IP 摄像机连接验证

How can I do IP camera connection verification

我有一个如下所示的代码块,但我无法验证连接。如果无法连接相机,我想打印一个连接错误。你能帮帮我吗

import numpy as np
import cv2
cap = cv2.VideoCapture('rtsp://admin:12345678@192.168.102.114:554/ch01_sub.264')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('ouput.avi', fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        frame = cv2.flip(frame,0)
        out.write(frame)
        
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
           break
    else:
       break

cap.release()
out.release()
cv2.destroyAllWindows()

尝试使用 try。下面的代码可能不完全正确,因为我不太明白它在哪里失败了,但应该足以让你理解这个想法。

    try: 
        while(cap.isOpened()):

           ret, frame = cap.read()
           frame = cv2.flip(frame,0)
           out.write(frame)
        
           cv2.imshow('frame',frame)

           if cv2.waitKey(1) & 0xFF == ord('q'):
             break

    except: 
        print('Connection error')