簡體   English   中英

OpenCV 太慢了

[英]OpenCV is too slow for this

我想將網絡攝像頭捕獲的視頻疊加在另一個實時視頻上。 所以我嘗試了上面的代碼,但它太慢了。

我應該切換到另一種語言或 lib 還是什么?

任何建議或幫助將不勝感激。

import numpy as np
from cv2 import cv2


def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
     # initialize the dimensions of the image to be resized and
     # grab the image size
     dim = None
     (h, w) = image.shape[:2]

     # if both the width and height are None, then return the
     # original image
     if width is None and height is None:
          return image

     # check to see if the width is None
    if width is None:
         # calculate the ratio of the height and construct the
         # dimensions
         r = height / float(h)
         dim = (int(w * r), height)

         # otherwise, the height is None
    else:
         # calculate the ratio of the width and construct the
         # dimensions
         r = width / float(w)
         dim = (width, int(h * r))

    # resize the image
    resized = cv2.resize(image, dim, interpolation = inter)

    # return the resized image
    return resized



cap2 = cv2.VideoCapture('http://192.168.43.1:8080/video')
cap = cv2.VideoCapture('test.mp4')

# Define the codec and create VideoWriter object 
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('sample3.mp4',fourcc,30, (640,480))


# watermark = logo

# cv2.imshow("watermark",watermark)

while(cap.isOpened()):
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame,cv2.COLOR_BGR2BGRA)

    ret2 ,frame2 = cap2.read()
    frame2 = cv2.cvtColor(frame2,cv2.COLOR_BGR2BGRA)

    watermark = image_resize(frame2,height=177)

    if ret==True:
        frame_h, frame_w, frame_c = frame.shape
        overlay = np.zeros((frame_h, frame_w, 4), dtype='uint8')
        overlay[543:543+177,1044:1044+236] = watermark
        cv2.addWeighted(frame, 0.25, overlay, 1.0, 0, frame)
       
        frame = cv2.cvtColor(frame,cv2.COLOR_BGRA2BGR)

        out.write(frame) 

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

要求是將網絡攝像頭實時視頻平滑地覆蓋在底部的另一個視頻上。

謝謝。

基本上,如果您希望它更快,您可能不會使用 python 循環迭代圖像。 您正在嘗試使用 2 個嵌套循環將按比例縮小的圖像復制到空疊加層中,這非常慢。 我不明白這個條件

             if watermark[i,j][3] != 0:

還有這部分:

                offset = 0
                h_offset = frame_h - watermark_h -offset
                w_offset = frame_w - watermark_w - offset

應該脫離循環——它們都是常數。

但最重要的是,您可以執行以下操作,而不是在圖像上循環:

offset[h_offset:h_offset+watermark_h,w_offset:w_offset+watermark_w] = watermark

在此之后,我從 9 fps 提高到 28 fps。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM