Как найти позиции наведения мыши (желательно метки времени) на видео? - PullRequest
0 голосов
/ 11 октября 2018

Я пытаюсь сохранить позиции мыши в данном видео.Мне нужно загрузить видео по нажатию кнопки «Загрузить видео».Когда мышь находится за пределами холста, нет (x, y), должны быть сохранены.Я также хочу, чтобы видео передавалось с гораздо меньшей скоростью (скажем, в 4 раза медленнее).В настоящее время у меня есть следующий код: `

import Tkinter as tk
from Tkinter import *
import PIL.Image,PIL.ImageTk 
import time
import cv2


class App:
    def __init__(self, window, window_title, video_source=0):
        self.window = window
        self.window.title(window_title)
        self.video_source = video_source
        self.video_loaded=False

    # open video source (by default this will try to open the computer webcam)
        self.vid = MyVideoCapture(self.video_source)

    # Create a canvas that can fit the above video source size
        self.canvas = tk.Canvas(window, width = self.vid.width, height = 
        self.vid.height)
        self.canvas.pack()

        self.canvas.bind('<Motion>',self.canvas.motion)
       #self.canvas.bind("<Enter>", self.on_enter)
       #self.canvas.bind("<Leave>", self.on_leave)

       # Button that lets the user take a snapshot
        self.btn_snapshot=tk.Button(window, text="Snapshot", width=50,  
        command=self.snapshot)
        self.btn_snapshot.pack(anchor=tk.CENTER, expand=True)

        self.btn_collapse=tk.Button(window, text="Collapse", width=50, 
        command=self.collapse)
        self.btn_collapse.pack(anchor=tk.CENTER, expand=True)

        self.btn_load_video=tk.Button(window, text="Load Video", width=50, 
        command=self.load_video)
        self.btn_load_video.pack(anchor=tk.CENTER, expand=True)

   #if self.video_loaded==True:
     # After it is called once, the update method will be automatically 
     called every delay milliseconds
     self.delay = 15
     self.update()


     self.window.mainloop()

     def load_video(self):
    # open video source (by default this will try to open the computer 
     webcam)
        self.vid = MyVideoCapture(self.video_source)
        self.video_loaded=True

     def snapshot(self):
     # Get a frame from the video source
        ret, frame = self.vid.get_frame()

     if ret:
        cv2.imwrite("frame-" + time.strftime("%d-%m-%Y-%H-%M-%S") +  ".jpg", 
        cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))

     def collapse(self):
         self.window.quit()  

     def motion(self):
         self.x=self.canvas.winfo_pointerx
         self.y=self.canvas.winfo_pointery
         print('{},{}'.format(self.x, self.y))
         #self.canvas.itemconfigure(text='({x},{y})'.format(x = self.x, 
         y=self.y))
         #print('{},{}'.format(self.x, self.y))      
     #def motion(self):
     #    x, y = self.x, self.y
     #   print('{}, {}'.format(x, y))  

     #def on_enter(self, event):
     #    self.l2.configure(text="Hello world")

     #def on_leave(self, enter):
     #    self.l2.configure(text="")       

     def update(self):
     # Get a frame from the video source
         ret, frame = self.vid.get_frame()

     if ret:
        self.photo = PIL.ImageTk.PhotoImage(image = 
         PIL.Image.fromarray(frame))
        self.canvas.create_image(0, 0, image = self.photo, anchor = tk.NW)

        self.window.after(self.delay, self.update)

class MyVideoCapture:
    def __init__(self, video_source=0):
    # Open the video source
        video_source='./videofilename.wmv'
        self.vid = cv2.VideoCapture(video_source)
        if not self.vid.isOpened():
           raise ValueError("Unable to open video source", video_source)

    # Get video source width and height
       self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)
       self.height = self.vid.get(cv2.CAP_PROP_FRAME_HEIGHT)

    def get_frame(self):
        if self.vid.isOpened():
           ret, frame = self.vid.read()
           if ret:
            # Return a boolean success flag and the current frame converted 
            to BGR
              return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
           else:
              return (ret, None)
        else:
           return (ret, None)

    # Release the video source when the object is destroyed
    def __del__(self):
       if self.vid.isOpened():
       self.vid.release()

    # Create a window and pass it to the Application object
    root = tk.Tk()           
App(root, "Tkinter and OpenCV")   

Когда я запускаю это, я получаю следующую ошибку:

Exception in Tkinter callback
Traceback (most recent call last):
File "/home/anaconda2/envs/my_env/lib/python2.7/lib-tk/Tkinter.py", line 
1541, in __call__
return self.func(*args)
TypeError: motion() takes exactly 1 argument (2 given)

Я хочу, чтобы функция motion () возвращала указатель мышипозиции.Ценю помощь.Заранее спасибо.

Вот ссылка , с которой я получил основной код.

1 Ответ

0 голосов
/ 11 октября 2018

Функция bind() отправляет объект события в функцию обратного вызова, но функция motion() принимает только self.Попробуйте:

def motion(self, event):
    self.x=event.x
    self.y=event.y

Связывание функции для сохранения позиций мыши можно выполнить, как в примере ниже.

from tkinter import *

root = Tk()
root.geometry('300x200+800+50')

c = Canvas(root, bg='tan')
c.pack(fill='both', expand='yes')

def motion(event):
    if follow:
        print(event.x, event.y)

follow = False
def follow_motion(event):
    global follow
    follow = not follow

c.bind('<Motion>', motion)
c.bind('<Button-1>', follow_motion)

root.mainloop()

Нажмите левую кнопку мыши на холсте, и функция motion() будет включена.Еще один щелчок отключает его.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...