[Error]cv2.error: OpenCV error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘

[Error]cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’

问题描述

初始代码

self.cap = cv2.VideoCapture(str('./video1/video1logo.avi'))
    def timer_TimeOut(self):
        frame_count = self.cap.get(cv2.CAP_PROP_FRAME_COUNT)
        #print(frame_count)
        self.n += 1
        if self.n > frame_count:
            self.n = 1
            self.timer1.stop()
        # 视频txt帧播放
        ret, frame = self.cap.read()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)
        self.pm = QPixmap.fromImage(img)
        self.lblpic.setPixmap(self.pm)

首先进行判断视频路径格式、读取的格式都没问题
发现是每次视频读取完毕的时候报错,cv2.cvtcolor中的frame变成了空,所以需要在视频读取结束frame变为空的时候重新循环

在if判断语句的末尾加上

self.cap = cv2.VideoCapture(str('./video1/video1logo.avi'))

让其重新读取视频文件就解决啦

你可能感兴趣的:(python)