简体   繁体   English

在 python 中将 mp4 视频文件转换为灰度并获取 Source is empty 错误

[英]Converting an mp4 video file to gray scale in python and getting a Source is empty error

Unable to import video media folder into program to convert to gray-scale.无法将视频媒体文件夹导入程序以转换为灰度。

import cv2 
import numpy as np

# The video feed is read in as a VideoCapture object
cap = cv2.VideoCapture(r'C:\Users\Mark\Desktop\Research\Source\Video\Traffic01.mp4')

# ret = a boolean return value for getting the frame
ret, first_frame = cap.read()

# Converts frame to grayscale
prev_gray = cv2.cvtColor(cv2.UMat(first_frame), cv2.COLOR_RGB2GRAY)

while(cap.isOpened()):

    # ret = a boolean return value from getting the frame
    ret, frame = cap.read()

    # Converts each frame to grayscale 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Frames are read by intervals of 10 milliseconds. 
    #The programs breaks out of the while loop when the user presses the 'q' key
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed)._src:empty() in function 'cv::cvtColor'错误:OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed)._src:empty() in ZC1C425268E68385D1AB5074C17A9'cvF4' ::cvtColor'

Why is that UMat there?为什么那个UMat在那里? That would seem to answer the question src.empty() from the docs at openCV UMat is/does:- constructs 2D matrix and fills it with the specified value _s.这似乎可以回答 openCV UMat is/does:- 构造 2D 矩阵并用指定值 _s 填充它的文档中的问题 src.empty()。 whereas cvtcolor wants a source image/frame而 cvtcolor 想要一个源图像/帧

Also why is the first frame so important it has to be outside the main loop?另外,为什么第一帧如此重要,它必须在主循环之外?

My advice comment out that first cap.read() and cv2.cvtcolor() lines just before your while loop.我的建议是在您的 while 循环之前注释掉第一行 cap.read() 和 cv2.cvtcolor() 行。 HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM