蟒蛇opencv的帧在使用摄像头



我试图设置我的opencv制作热图。我让它为一组。mp4文件工作。但是当我尝试使其有效使用网络摄像头在生活喂它似乎不喜欢它。它的问题是它说"index is out of range"对于以下代码行:

make_video('./frames/', './output.avi')
frame = cv2.imread(os.path.join(image_folder, images[0]))

.py文件如下所示:

import numpy as np
import cv2
import copy
from make_video import make_video
from progress.bar import Bar

def main():
capture = cv2.VideoCapture(0)
background_subtractor = cv2.bgsegm.createBackgroundSubtractorMOG()
length = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
bar = Bar('Processing Frames', max=length)
print(length)
short = length
first_iteration_indicator = 1
for i in range(0, short):
print(i)
ret, frame = capture.read()
# If first frame
if first_iteration_indicator == 1:
first_frame = copy.deepcopy(frame)
height, width = frame.shape[:2]
accum_image = np.zeros((height, width), np.uint8)
first_iteration_indicator = 0
else:
filter = background_subtractor.apply(frame)  # remove the background
cv2.imwrite('./frame.jpg', frame)
cv2.imwrite('./diff-bkgnd-frame.jpg', filter)
threshold = 2
maxValue = 2
ret, th1 = cv2.threshold(filter, threshold, maxValue, cv2.THRESH_BINARY)
# add to the accumulated image
accum_image = cv2.add(accum_image, th1)
cv2.imwrite('./mask.jpg', accum_image)
color_image_video = cv2.applyColorMap(accum_image, cv2.COLORMAP_HOT)
video_frame = cv2.addWeighted(frame, 0.7, color_image_video, 0.7, 0)
name = "./frames/frame%d.jpg" % i
cv2.imwrite(name, video_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
bar.next()
bar.finish()
make_video('./frames/', './output.avi')
color_image = cv2.applyColorMap(accum_image, cv2.COLORMAP_HOT)
result_overlay = cv2.addWeighted(first_frame, 0.7, color_image, 0.7, 0)
# save the final heatmap
cv2.imwrite('diff-overlay.jpg', result_overlay)
# cleanup
capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()

为了将其设置为网络摄像头,我使用capture = cv2.VideoCapture(0),这会导致上面的错误,但是如果我使用预先保存的。mp4文件,如capture = cv2. videoccapture ('vid.mp4'),它可以正常工作。什么好主意吗?

问题:

使用capture = cv2.VideoCapture(0)的网络摄像头时;length = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))这条线没有帮助。因为,当使用网络摄像头时,capture.get(cv2.CAP_PROP_FRAME_COUNT)返回-1.0。这意味着当使用网络摄像头时,帧数并不适用。当用于读取视频时,它是有意义的,因为它可以找出视频中存在的帧数。

解决方案:

我修改了现有的代码来使用网络摄像头,并删除了for循环。我测试了没有make_video的代码。使用cv2.imshow分析它们的结果与预期一致(取消注释以查看自己的结果)。

import cv2
capture = cv2.VideoCapture(0)
background_subtractor = cv2.bgsegm.createBackgroundSubtractorMOG()
threshold = 100
maxValue = 255
# for counting frames
i = 0
while True:
i = i + 1
ret, frame = capture.read()
if frame is None:
break

fgMask = background_subtractor.apply(frame)
ret, th1 = cv2.threshold(fgMask, threshold, maxValue, cv2.THRESH_BINARY)
blur = cv2.GaussianBlur(th1,(11,11), 9)
heat_map = cv2.applyColorMap(blur, cv2.COLORMAP_HOT)
video_hm = cv2.addWeighted(frame, 0.7, heat_map, 0.7, 0)
name = "./frames/frame%d.jpg" % i
cv2.imwrite(name, video_hm)

#cv2.imshow('Frame', frame)
#cv2.imshow('FG Mask', fgMask)
#cv2.imshow('Video OP', video_hm)
#cv2.imshow('Blur', blur)

keyboard = cv2.waitKey(30)
if keyboard == 'q' or keyboard == 27:
break
capture.release()
make_video('./frames/', './output.avi')
cv2.destroyAllWindows()

最新更新