First I would like to mention a possible problem.
Always check whether the frame
returns or not. Otherwise your application will crash.
ret, frame = cap.read()
if ret:
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
Now resize
your frame
# function for video streaming
ret, frame = cap.read()
if ret:
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
cv2image = cv2.resize(cv2image, (500, 500))
Correct Code:
from tkinter import *
from PIL import ImageTk, Image
import cv2
root = Tk()
main_label = Label(root)
main_label.grid()
# Capture from camera
cap = cv2.VideoCapture("video.mp4")
# function for video streaming
def video_stream():
ret, frame = cap.read()
if ret:
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
cv2image = cv2.resize(cv2image, (500, 500))
img = Image.fromarray(cv2image)
tk_img = ImageTk.PhotoImage(image=img)
main_label.configure(image=tk_img)
main_label.tk_img = tk_img
main_label.after(20, video_stream)
video_stream()
root.mainloop()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…