我们如何在Tkinter窗口中添加cv2跟踪栏?

时间:2019-02-14 14:51:02

标签: python tkinter opencv-python

我必须在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视频窗口。我想使用轨迹栏跳到视频的特定部分,并且视频也应该同时运行。

0 个答案:

没有答案