Я должен добавить трекбары видеоплеера в окне Tkinter вместо использования треков cv2.
Это то, что я пытался сделать.
import tkinter as tk
from tkinter import *
from tkinter import ttk
master = Tk()
import cv2
import numpy as np
# function called by trackbar, sets the next frame to be read
# open video
video = cv2.VideoCapture("test.mp4")
# get total number of frames
nr_of_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
# create display window
cv2.namedWindow("Video")
# set wait for each frame, determines playbackspeed
playSpeed = 50
def show_values(a):
print(a)
global video
video.set(cv2.CAP_PROP_POS_FRAMES, int(a))
# function called by trackbar, sets the speed of playback
def show_values1(a):
global playSpeed
playSpeed = max(int(a),1)
while 1:
# Get the next videoframe
ret, frame = video.read()
# show frame, break the loop if no frame is found
if ret:
cv2.imshow("Video", frame)
# update slider position on trackbar
# NOTE: this is an expensive operation, remove to greatly increase max playback speed
w2 = Scale(master, from_=0, to=nr_of_frames, orient=HORIZONTAL, command = show_values)
w2.set(int(video.get(cv2.CAP_PROP_POS_FRAMES)))
w1 = Scale(master, from_=0, to=100, orient=HORIZONTAL,command =show_values1)
else:
break
# display frame for 'playSpeed' ms, detect key input
key = cv2.waitKey(playSpeed)
w1.set(0)
w1.pack()
w2.pack()
cv2.setTrackbarPos("Frame","Video", int(video.get(cv2.CAP_PROP_POS_FRAMES)))
# stop playback when q is pressed
if key == ord('q'):
break
mainloop()
# release resources
video.release()
cv2.destroyAllWindows()
В настоящее время отображаются 2 окна, т.е. окно трекбара Tkinter и окно видео CV2.Я хочу использовать трекбар для перехода к определенному фрагменту видео, и видео также должно быть запущено одновременно.