gpt4 book ai didi

python - 无法在 Python (Windows 10) 中使用 OpenCV 打开保存的视频

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:51 26 4
gpt4 key购买 nike

代码读取 train1.mov 文件,显示 cv2.imshow('res', res) 同时保存一个名为 output.avi。问题是,output.avi 大约 40 kb 并且是空的。但是 cv2.imshow('res', res) 显示了整个视频。然后显示如下错误。

Python 代码:

import numpy as np
import cv2

cap = cv2.VideoCapture('C:\\Users\\khan1\\Desktop\\python project\\color_threshold\\train1.mov')

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('C:\\Users\\khan1\\Desktop\\python project\\color_threshold\\output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
_, frame = cap.read()

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_green = np.array([40, 60, 60])
upper_green = np.array([80, 255, 255])

mask = cv2.inRange(hsv, lower_green, upper_green)
mask = cv2.GaussianBlur(mask, (7, 7), 0)
res = cv2.bitwise_and(frame, frame, mask=mask)

out.write(res)

cv2.namedWindow("res", cv2.WINDOW_NORMAL)
cv2.imshow('res', res)
if (cv2.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit
break

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

错误:

OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cv::cvtColor, file C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\color.cpp, line 9815
Traceback (most recent call last):
File "C:/Users/khan1/Desktop/python project/color_threshold/savevid.py", line 19, in <module>
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.error: C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\imgproc\src\color.cpp:9815: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor


Process finished with exit code 1

我该如何解决?

最佳答案

很明显你的错误发生在最后一帧;这就是为什么您的 imshow 一直在工作,但它出错并且没有完成编写 while 循环之后发生的视频。

cap.read() 返回的第一个值是一个 bool 值,表示帧是否存在/是否被读取。所以你可以简单地在你的代码中插入一个 if-else block 来在帧被读取时执行你的处理,并且 quit while 循环如果不,这样你就可以写完框架了。

这应该修补您的代码:

import numpy as np
import cv2

cap = cv2.VideoCapture('C:\\Users\\khan1\\Desktop\\python project\\color_threshold\\train1.mov')

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('C:\\Users\\khan1\\Desktop\\python project\\color_threshold\\output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
ret, frame = cap.read()
if ret:

hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_green = np.array([40, 60, 60])
upper_green = np.array([80, 255, 255])

mask = cv2.inRange(hsv, lower_green, upper_green)
mask = cv2.GaussianBlur(mask, (7, 7), 0)
res = cv2.bitwise_and(frame, frame, mask=mask)

out.write(res)

cv2.namedWindow("res", cv2.WINDOW_NORMAL)
cv2.imshow('res', res)
if (cv2.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit
break
else:
break

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

查看同一过程的示例 here .

关于python - 无法在 Python (Windows 10) 中使用 OpenCV 打开保存的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44639951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com