anonymous No title
No License Python
2021年06月23日
Copy Clone
import math
import time

from talon import canvas, ctrl, tap, ui
from talon.skia import Shader
from talon.track.filter import DwellFilter, LowPassFilter, MultiFilter, OneEuroFilter
from talon.types import Point2d
from talon_plugins.eye_mouse import tracker
from talon.track.tobii import EyeFrame

ENABLED = True

screen = ui.main_screen()
size_px = Point2d(screen.width, screen.height)


class LensMouse:
    def __init__(self):
        # tracker.register("gaze", self.on_gaze)

        self.xy_hist = [Point2d(0, 0)]
        self.origin = Point2d(0, 0)

        canvas.register("overlay", self.draw)
        self.enabled = True

    # Shows crazy circles
    def smooth_location(self):
        # Calculate smooth location of point
        x = self.origin.x
        y = self.origin.y
        n = 75
        if len(self.xy_hist) < n:
            n = len(self.xy_hist)
        total = 1
        minimum_group = 50
        for i in range(n):
            x2 = self.xy_hist[-1 - i].x
            y2 = self.xy_hist[-1 - i].y

            # If there are at least 5 points (so there's some smoothness)
            if i > minimum_group:
                # Don't use if points are really far away, so long moves are fast
                if abs(x2 - self.origin.x) > 60:
                    continue
                if abs(y2 - self.origin.y) > 60:
                    continue

            x += x2
            y += y2
            total += 1

        x /= total
        y /= total
        return [x, y]

    def draw(self, canvas):
        if self.origin is None:
            print("Is the tobii disconnected?")
            return

        pos = self.smooth_location()

        # Append the smoothed dot to history > to smooth it some more
        self.xy_hist.append(Point2d(pos[0], pos[1]))
        self.xy_hist.append(Point2d(pos[0], pos[1]))

        if pos is None:
            return

        paint = canvas.paint

        paint.stroke_width = 1
        paint.style = paint.Style.STROKE
        paint.color = "44444444"

        canvas.draw_circle(pos[0], pos[1], 150)

        paint.style = paint.Style.FILL
        paint.color = "99999944"
        canvas.draw_circle(pos[0], pos[1], 150)

    def on_gaze(self, frame):
        x = frame.gaze.x
        y = frame.gaze.y

        # Don't pass edges of screen
        if x < 0:
            x = 0
        if y < 0:
            y = 0

        if x == 0 or y == 0:
            return

        # Multiply by screen width
        x *= size_px.x
        y *= size_px.y

        self.origin = Point2d(x, y)
        self.xy_hist.append(self.origin)
        self.xy_hist = self.xy_hist[-200:]

if ENABLED:
    mouse = LensMouse()
    tracker.register('gaze', mouse.on_gaze)
import math
import time

from talon import canvas, ctrl, tap, ui
from talon.skia import Shader
from talon.track.filter import DwellFilter, LowPassFilter, MultiFilter, OneEuroFilter
from talon.types import Point2d
from talon_plugins.eye_mouse import tracker
from talon.track.tobii import EyeFrame

ENABLED = True

screen = ui.main_screen()
size_px = Point2d(screen.width, screen.height)


class LensMouse:
    def __init__(self):
        # tracker.register("gaze", self.on_gaze)

        self.xy_hist = [Point2d(0, 0)]
        self.origin = Point2d(0, 0)

        canvas.register("overlay", self.draw)
        self.enabled = True

    # Shows crazy circles
    def smooth_location(self):
        # Calculate smooth location of point
        x = self.origin.x
        y = self.origin.y
        n = 75
        if len(self.xy_hist) < n:
            n = len(self.xy_hist)
        total = 1
        minimum_group = 50
        for i in range(n):
            x2 = self.xy_hist[-1 - i].x
            y2 = self.xy_hist[-1 - i].y

            # If there are at least 5 points (so there's some smoothness)
            if i > minimum_group:
                # Don't use if points are really far away, so long moves are fast
                if abs(x2 - self.origin.x) > 60:
                    continue
                if abs(y2 - self.origin.y) > 60:
                    continue

            x += x2
            y += y2
            total += 1

        x /= total
        y /= total
        return [x, y]

    def draw(self, canvas):
        if self.origin is None:
            print("Is the tobii disconnected?")
            return

        pos = self.smooth_location()

        # Append the smoothed dot to history > to smooth it some more
        self.xy_hist.append(Point2d(pos[0], pos[1]))
        self.xy_hist.append(Point2d(pos[0], pos[1]))

        if pos is None:
            return

        paint = canvas.paint

        paint.stroke_width = 1
        paint.style = paint.Style.STROKE
        paint.color = "44444444"

        canvas.draw_circle(pos[0], pos[1], 150)

        paint.style = paint.Style.FILL
        paint.color = "99999944"
        canvas.draw_circle(pos[0], pos[1], 150)

    def on_gaze(self, frame):
        x = frame.gaze.x
        y = frame.gaze.y

        # Don't pass edges of screen
        if x < 0:
            x = 0
        if y < 0:
            y = 0

        if x == 0 or y == 0:
            return

        # Multiply by screen width
        x *= size_px.x
        y *= size_px.y

        self.origin = Point2d(x, y)
        self.xy_hist.append(self.origin)
        self.xy_hist = self.xy_hist[-200:]

if ENABLED:
    mouse = LensMouse()
    tracker.register('gaze', mouse.on_gaze)
No one still commented. Please first comment.
Output