diff options
author | Théo de la Hogue | 2022-12-19 11:44:18 +0100 |
---|---|---|
committer | Théo de la Hogue | 2022-12-19 11:44:18 +0100 |
commit | 5be918dcc86c67640253f6bd7e1b4f7dc9bc5544 (patch) | |
tree | fac92e83058d4f493af73752c155864ae0b01c12 /src | |
parent | 270accd74d69ffb1fbb9bd9065dece4a2e79873f (diff) | |
download | argaze-5be918dcc86c67640253f6bd7e1b4f7dc9bc5544.zip argaze-5be918dcc86c67640253f6bd7e1b4f7dc9bc5544.tar.gz argaze-5be918dcc86c67640253f6bd7e1b4f7dc9bc5544.tar.bz2 argaze-5be918dcc86c67640253f6bd7e1b4f7dc9bc5544.tar.xz |
Allowing to disable precision circle drawing. Loading time stamped gaze positions from json file
Diffstat (limited to 'src')
-rw-r--r-- | src/argaze/GazeFeatures.py | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/argaze/GazeFeatures.py b/src/argaze/GazeFeatures.py index c3778bb..f3fa45a 100644 --- a/src/argaze/GazeFeatures.py +++ b/src/argaze/GazeFeatures.py @@ -3,6 +3,7 @@ from typing import TypeVar, Tuple from dataclasses import dataclass, field import math +import ast import json from argaze import DataStructures @@ -66,7 +67,7 @@ class GazePosition(): else: return dist < self.precision - def draw(self, frame, color=(0, 255, 255)): + def draw(self, frame, color=(0, 255, 255), draw_precision=True): """Draw gaze position point and precision circle.""" if self.valid: @@ -77,8 +78,8 @@ class GazePosition(): cv.circle(frame, int_value, 2, color, -1) # Draw precision circle - if self.precision > 0: - cv.circle(frame, int_value, round(self.precision), color, 1) + if self.precision > 0 and draw_precision: + cv.circle(frame, int_value, round(self.precision), color_precision, 1) class UnvalidGazePosition(GazePosition): """Unvalid gaze position.""" @@ -89,6 +90,9 @@ class UnvalidGazePosition(GazePosition): super().__init__((None, None), precision=None) +TimeStampedGazePositionsType = TypeVar('TimeStampedGazePositions', bound="TimeStampedGazePositions") +# Type definition for type annotation convenience + class TimeStampedGazePositions(DataStructures.TimeStampedBuffer): """Define timestamped buffer to store gaze positions.""" @@ -98,14 +102,30 @@ class TimeStampedGazePositions(DataStructures.TimeStampedBuffer): # Convert dict into GazePosition if type(value) == dict: - assert(set(["value", "precision"]).issubset(value.keys())) + assert(set(['value', 'precision']).issubset(value.keys())) + + if 'message' in value.keys(): + + value = UnvalidGazePosition(value['message']) + + else: - value = GazePosition(value["value"], precision=value["precision"]) + value = GazePosition(value['value'], precision=value['precision']) assert(type(value) == GazePosition or type(value) == UnvalidGazePosition) super().__setitem__(key, value) + @classmethod + def from_json(self, json_filepath: str) -> TimeStampedGazePositionsType: + """Create a TimeStampedGazePositionsType from .json file.""" + + with open(json_filepath, encoding='utf-8') as ts_buffer_file: + + json_buffer = json.load(ts_buffer_file) + + return TimeStampedGazePositions({ast.literal_eval(ts_str): json_buffer[ts_str] for ts_str in json_buffer}) + GazeMovementType = TypeVar('GazeMovement', bound="GazeMovement") # Type definition for type annotation convenience |