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