降低延迟、内存溢出。
import cv2
import queue
import time
import threading
q = queue.Queue()
def img_resize(image):
height, width = image.shape[0], image.shape[1]
# 设置新的图片分辨率框架 640x369 1280×720 1920×1080
width_new = 1920
height_new = 1080
# 判断图片的长宽比率
if width / height >= width_new / height_new:
img_new = cv2.resize(image, (width_new, int(height * width_new / width)))
else:
img_new = cv2.resize(image, (int(width * height_new / height), height_new))
return img_new
def receive():
print("start Reveive")
url = 'rtsp://admin:Bestpower8866@192.168.4.64/Streaming/Channels/1'
cap = cv2.VideoCapture(url)
while cap.isOpened():
q.put(cap.read())
q.get() if q.qsize() > 1 else time.sleep(0.01)
def display():
print("Start Displaying")
while True:
ret, frame = q.get()
# 对获取的视频帧分辨率重处理
img_new = img_resize(frame)
cv2.imshow("window_name", img_new)
del ret
del frame
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == '__main__':
p1 = threading.Thread(target=receive)
p2 = threading.Thread(target=display)
p1.start()
p2.start()
参考:
https://lukeyalvin.blog.csdn.net/article/details/116052377