cv2的VideoCapture訪問不存在的rtsp端口等待時間過長


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

參考鏈接:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM