[英]Cannot save video using python and opencv
I'm trying to change a video to grayscale and save it but the video only shows up during the run process after it finishes running I click on the saved file but I only get a cannot render file message我正在尝试将视频更改为灰度并保存它,但视频仅在完成运行后在运行过程中显示我单击保存的文件但我只收到无法渲染文件消息
import cv2
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import cv2
cap = cv2.VideoCapture ('DJI_0055.mov')
ret, frame = cap.read()
print('ret =', ret, 'W =', frame.shape[1], 'H =', frame.shape[0],
'channel =', frame.shape[2])
FPS= 60.0
FrameSize=(frame.shape[1], frame.shape[0])
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('Video_output3.mov', fourcc, FPS, FrameSize,
0)
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = gray
# Save the video
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Aside from the cannot render file message I also get this message after the video finishes running on IDLE除了无法呈现文件消息之外,我还在视频在 IDLE 上完成运行后收到此消息
Traceback (most recent call last):
File "C:\Users\fido\Desktop\Practice\E-1-00pm-12m-1mps-
8wp\BlackAndWhite.py", line 25, in <module>
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.1) C:\projects\opencv-
python\opencv\modules\imgproc\src\color.cpp:182: error:
(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
after the last frame, ret ---> False and Frame ---> [], this is what you will get.在最后一帧之后,ret ---> False 和 Frame ---> [],这就是你会得到的。 That is the reason cvtColor is showing this error.
这就是 cvtColor 显示此错误的原因。
So, if you add所以,如果你添加
if not ret:
break
It will get out of the loop properly and your video will be saved.它将正确退出循环,您的视频将被保存。
Edit your code like this:像这样编辑您的代码:
while(cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = gray
# Save the video
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.