Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在视频上找到鼠标悬停位置(最好是时间戳)?_Python 2.7_User Interface_Tkinter_Video Capture - Fatal编程技术网

Python 2.7 如何在视频上找到鼠标悬停位置(最好是时间戳)?

Python 2.7 如何在视频上找到鼠标悬停位置(最好是时间戳)?,python-2.7,user-interface,tkinter,video-capture,Python 2.7,User Interface,Tkinter,Video Capture,我正在尝试保存给定视频中鼠标悬停的位置。我需要按“加载视频”按钮加载视频。当鼠标位于画布外时,不应保存(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, vid

我正在尝试保存给定视频中鼠标悬停的位置。我需要按“加载视频”按钮加载视频。当鼠标位于画布外时,不应保存(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")   
我希望函数motion()返回鼠标悬停位置。谢谢你的帮助。提前谢谢

这里是我获取主代码的地方。

函数
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()
从tkinter导入*
root=Tk()
根几何体('300x200+800+50')
c=画布(根,bg='tan')
c、 打包(填充class='2',扩展class='yes')
def运动(事件):
如有下列情况:
打印(事件x、事件y)
follow=False
def跟车运动(事件):
全球跟踪
跟随=不跟随
c、 绑定(“”,运动)
c、 绑定(“”,跟随运动)
root.mainloop()

在画布上单击鼠标左键,启用功能
motion()
。再单击一次将禁用它

多谢各位。如何关联鼠标单击事件以使用move()开始和停止录制鼠标位置?另外,是否有方法在画布窗口上以比原始视频低得多的帧速率播放视频?那么,您希望它如何工作?您可以创建用于启动和停止的按钮,或将其绑定到键盘键。我对显示视频一无所知:(您好。Th代码现在显示视频,但它立即以原始帧速率开始流式传输。我打算通过单击按钮加载视频,在另一次单击按钮时开始播放视频,也就是以更低的帧速率(~1/4)播放视频)。在鼠标单击时开始/停止录制鼠标位置。在我的实验中,对象在整个视频中都不存在。我只需要在对象出现在视频中时保存鼠标位置。因此,我认为,在视频上单击鼠标以开始和停止鼠标位置录制是最佳解决方案。
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()