Python Forum

Full Version: Error I don't understand
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Heres is the code below:

from tkinter import *
from PIL import ImageTk, Image
import cv2
import time


data = cv2.VideoCapture("video.mp4")
while not data.isOpened():
    pass
fps = data.get(cv2.CAP_PROP_FPS)
length = int(data.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(data.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(data.get(cv2.CAP_PROP_FRAME_HEIGHT))

print(fps, length, width, height)

data.read()

data.release()
counter = 0
root = Tk()
root.title("VIDEOS CANVAS TOOL")
root.geometry(str(width) + "x" + str(height))
canvas = Canvas(root, width=width, height=height)
canvas.grid(row=0)


def showFrame(frame=0):
    t0 = time.time()
    data.set(cv2.CAP_PROP_POS_FRAMES, frame)
    success, image = data.read()
    #if not success:
        #print("error")
        #return
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    t1 = time.time()
    print("1", t1 - t0)
    image = ImageTk.PhotoImage(image=Image.fromarray(image))
    t2 = time.time()
    print("2", t2 - t1)
    canvas.delete("all")
    canvas.create_image(0, 0, image=image, anchor=NW)
    t3 = time.time()
    print("3", t3 - t2)
    canvas.image = image
    t4 = time.time()
    print("4", t4 - t3)
    print("all", t4 - t0)
    root.after(int(1000//fps), lambda: showFrame(frame+1))
   

showFrame()  # play the video

root.mainloop()
I get this error, but I don't understand what is going wrong here?


Error:
Traceback (most recent call last): File "C:\Users\Finley\Downloads\Biz Project Test.py", line 52, in <module> showFrame() # play the video File "C:\Users\Finley\Downloads\Biz Project Test.py", line 35, in showFrame image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
Any thoughts?
Your src image is empty. Same question was asked here:

https://stackoverflow.com/questions/6068...v2cvtcolor

The error message could certainly be better. I often use assert when developing and testing code, then I go through the working code and replace likely assertion fails with a test that raises an exception. I am surprised something as likely as passing None as the source image is caught/reported using an assertion.

Your image is empty because you released (data.release()) your video. If you take out lines 17-19 your program might work.
Sorted, thanks!