【发布时间】:2020-08-26 00:27:45
【问题描述】:
我想取一帧中所有行的平均值并打印出如下所示的列表。
import numpy as np
import cv2, time
from numba import jit
cap = cv2.VideoCapture(0)
@jit(nopython=True)
def test(frame):
print(frame.mean(axis=1)) # to take the mean of each row
fc = 0
frame_rate = 30
prev = 0
while(cap.isOpened()):
ret, frame = cap.read()
time_elapsed = time.time() - prev
if time_elapsed > 1/frame_rate:
prev = time.time()
if ret == True:
cv2.imshow("Video", frame)
print(frame.shape) #(480, 640, 3)
test(frame)
if cv2.waitKey(27) & 0xFF == ord('q'):
break
# if no frame found
else:
break
cap.release()
cv2.destroyAllWindows()
但我不断收到以下错误。 Numba 应该可以很好地处理数值计算,比如平均一个 numpy 数组,所以我对我的函数的问题有点困惑。
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
- Resolution failure for literal arguments:
AssertionError()
- Resolution failure for non-literal arguments:
AssertionError()
During: resolving callee type: BoundFunction(array.mean for array(uint8, 3d, C))
During: typing of call at <ipython-input-2-3d43e63d267e> (11)
File "<ipython-input-2-3d43e63d267e>", line 11:
def test(frame):
print(frame.mean(axis=1)) # to take the mean of each row
【问题讨论】:
-
您可以尝试
np.mean(frame,axis=1),而不是错误消息更有意义(不支持轴关键字)。所以你必须自己实现它。打印和测量性能也没有意义(打印成本很高)。我猜你的最后一个维度总是大小为 3,对吧?这是优化解决方案的重要一点。 -
我想我应该手动遍历帧的每一行并将其平均