import cv2
print("time1")
cap = cv2.VideoCapture("rtsp://192.168.0.1:554/")
print("time2")
ret, frame = cap.read()
cv2.imshow("capture", frame)
cv2.imwrite("test.png', frame)# 存儲為圖像
cap.release()
cv2.destroyAllWindows()
如果192.168.0.1未開啟554端口,VideoCapture函數會等待20多秒才返回。
類似於 telnet 192.168.0.1 554 無法訪問端口,但等待很長時間才返回。
有沒有方法控制等待時間?
import socket
#python實戰練手項目---使用socket探測主機開放的端口 | 酷python
#http://www.coolpython.net/python_senior/miny_pro/find_open_port.html
def portisopen(ip,port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
state = sock.connect_ex((ip, port))
if 0 == state:
#print("port is open")
return True
else :
#print("port is closed")
return False
參考鏈接:
- 【Python】改善 VideoCapture 的影像延遲 · 大專欄
https://www.dazhuanlan.com/jammesliu/topics/923280