opencv處理圖像延遲高(解決cap.read緩存\不實時問題)

問題描述:

最近做直播系統,利用opencv進行圖像採集處理,直播推出去的流發現延遲高且會累積延遲.

後面發現是opencv的cap.read()會有一定的緩存,不是讀的實時幀.

解決辦法:

多進程進行(python多線程不能利用多核,所以處理線程佔用高的時候,讀取線程會被阻塞)

一個進程進行處理,一個進程進行讀取.直接用 Queue列隊進行通信.參考文章
讀取端進行put,但put之前先判斷有多少緩存,然後刪除緩存再put;

    lenth = que.qsize()
    print ("que lenth: ",lenth)

    if lenth >2:
      for i in range(lenth-2):
        frame = que.get()   #清除緩存
    que.put(img)

處理端get就好.

過程中遇到的問題記錄:

1. 使用 multiprocessing.Queue() 後進程不能停止.
解決辦法:

                manager = multiprocessing.Manager()
                que = manager.Queue()
                ...
                pro_read.terminate()

2. 只讀了第一幀就卡住.
解決辦法:
不把cap=VideoCapture(video)作爲全局變量.放在子進程中.

測試代碼:

# -*- coding:utf-8
import numpy as np
import multiprocessing
from multiprocessing import Process ,Queue
import random
import cv2

def run1(que,cap):

  while 1:
    ret,img = cap.read()
    for i in  range(200):
      for j in range(200):
        for k in  range(3):
          img[j,i,k] = random.randint(0,255)
    

    lenth = que.qsize()
    print ("que lenth: ",lenth)

    if lenth >2:
      for i in range(lenth-2):
        frame = que.get()   #清除緩存
    que.put(img)
    #pipe.send(img_q)
    cv2.imshow("show",img)

    cv2.waitKey(1000/23)
  cv2.destroyAllWindows()
  cap.release()


def test_1(que,cap):
  run1(que)



def test_2(que):
  while 1:
    img = que.get()
    #img = pipe.recv()
    #img = cv2.resize(img,(360,240))
    cv2.imshow("show2",img)
    key = cv2.waitKey(1000/23)
    if key == 27:
      break
  cv2.destroyAllWindows()


def cmd_send():
  pass
if __name__ == "__main__":
    manager = multiprocessing.Manager()
    que = manager.Queue()
    # que = Queue()
    test1_start = 1
    test2_start = 1

    video = "/home/jiteng/桌面/尋夢環遊記.mp4"
    cap= cv2.VideoCapture(video)

    t1 = Process(target=run1,args=(que,cap,))
    # t2 = Process(target=test_2,args=(que,))

    t1.start()
    # t2.start()

    test_2(que)
    # 
    t1.terminate()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章