Python OpenCV对此太慢了
我想在现场的另一个视频上覆盖摄像头捕获的视频。所以我尝试了上面的代码,但是速度太慢了 我应该换成另一种语言、lib还是别的什么 如有任何建议或帮助,将不胜感激Python OpenCV对此太慢了,python,opencv,video,webcam,video-processing,Python,Opencv,Video,Webcam,Video Processing,我想在现场的另一个视频上覆盖摄像头捕获的视频。所以我尝试了上面的代码,但是速度太慢了 我应该换成另一种语言、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
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。基本上,如果您希望速度更快,可以不使用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。您确定了管道中的瓶颈吗?@eldesgraciado结果帧速率非常低(1或2 fps),但如果不叠加,它的运行速度将达到30 fps。给出的答案解决了您最初的问题。您已经删除了缓慢的代码,并将其替换为答案。这使你的问题无法理解。此外,您当前问题版本中的代码不再慢。您是否发现了管道中的瓶颈?@eldesgraciado结果帧速率非常低(1或2 fps),但如果不重叠,它的运行速度将达到30 fps。给出的答案解决了您原来的问题。您已经删除了缓慢的代码,并将其替换为答案。这使你的问题无法理解。此外,您当前版本的问题代码不再缓慢。