-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_advanced.py
More file actions
549 lines (443 loc) · 19 KB
/
ml_advanced.py
File metadata and controls
549 lines (443 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
"""
Advanced ML models for improved user experience.
Includes adaptive smoothing, polynomial prediction, gesture classification, and learning capabilities.
"""
import numpy as np
from collections import deque
from typing import Tuple, Optional, Dict, List
import time
import json
import os
import config
class AdaptiveCursorPredictor:
"""
Advanced ML predictor with adaptive smoothing based on movement context.
Automatically adjusts smoothing based on movement speed, direction changes, and user patterns.
"""
def __init__(self, window_size: int = 8):
"""
Initialize adaptive predictor.
Args:
window_size: Number of positions to track (increased for better prediction)
"""
self.window_size = window_size
self.position_history: deque = deque(maxlen=window_size)
self.time_history: deque = deque(maxlen=window_size)
self.velocity_history: deque = deque(maxlen=window_size)
# Adaptive parameters
self.base_smoothing = config.ML_SMOOTHING_FACTOR
self.prediction_horizon = config.ML_PREDICTION_HORIZON
# Learning state
self.prediction_errors: deque = deque(maxlen=50) # Track prediction accuracy
self.adaptive_factor = 1.0 # Learned adaptation factor
# Movement context
self.last_speed = 0.0
self.last_direction_change = 0.0
def add_position(self, x: float, y: float, timestamp: float):
"""Add position and calculate velocity."""
if len(self.position_history) > 0:
prev_pos, prev_time = self.position_history[-1], self.time_history[-1]
dt = timestamp - prev_time
if dt > 0:
vx = (x - prev_pos[0]) / dt
vy = (y - prev_pos[1]) / dt
speed = np.sqrt(vx * vx + vy * vy)
self.velocity_history.append((vx, vy, speed))
self.last_speed = speed
self.position_history.append((x, y))
self.time_history.append(timestamp)
def _calculate_adaptive_smoothing(self) -> float:
"""
Calculate adaptive smoothing factor based on movement context.
Returns:
Adaptive smoothing factor (0.0 to 1.0)
"""
if len(self.velocity_history) < 2:
return self.base_smoothing
# Get current movement characteristics
current_speed = self.last_speed
recent_speeds = [v[2] for v in list(self.velocity_history)[-3:]]
avg_speed = np.mean(recent_speeds) if recent_speeds else current_speed
# Calculate direction change (curvature)
if len(self.position_history) >= 3:
positions = list(self.position_history)[-3:]
# Calculate angle change
v1 = np.array(positions[1]) - np.array(positions[0])
v2 = np.array(positions[2]) - np.array(positions[1])
if np.linalg.norm(v1) > 0 and np.linalg.norm(v2) > 0:
cos_angle = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
direction_change = 1.0 - cos_angle # 0 = straight, 1 = 90° turn
else:
direction_change = 0.0
else:
direction_change = 0.0
# Adaptive smoothing rules:
# - Fast movement: less smoothing (more direct)
# - Slow movement: more smoothing (reduce jitter)
# - Curved movement: less smoothing (follow curves)
# - Straight movement: more smoothing (predict ahead)
speed_factor = 1.0
if avg_speed > config.ML_FAST_SPEED_THRESHOLD:
speed_factor = 0.6 # Less smoothing for fast movement
elif avg_speed < config.ML_SLOW_SPEED_THRESHOLD:
speed_factor = 1.4 # More smoothing for slow movement
curve_factor = 1.0
if direction_change > 0.3: # Significant direction change
curve_factor = 0.7 # Less smoothing for curves
# Apply learned adaptation
adaptive_smoothing = self.base_smoothing * speed_factor * curve_factor * self.adaptive_factor
# Clamp to reasonable range
return max(0.0, min(0.8, adaptive_smoothing))
def predict_next_position_polynomial(self) -> Optional[Tuple[float, float]]:
"""
Predict using polynomial regression (better for curves).
Enhanced with velocity and acceleration terms for better accuracy.
Returns:
Predicted (x, y) position
"""
if len(self.position_history) < 4:
return None
positions = np.array(list(self.position_history))
times = np.array(list(self.time_history))
times = times - times[-1] # Normalize
try:
# Use polynomial regression (degree 2 for curves)
x_coords = positions[:, 0]
y_coords = positions[:, 1]
# Fit polynomial: position = a*t² + b*t + c
x_coef = np.polyfit(times, x_coords, min(2, len(times) - 1))
y_coef = np.polyfit(times, y_coords, min(2, len(times) - 1))
predicted_x = np.polyval(x_coef, self.prediction_horizon)
predicted_y = np.polyval(y_coef, self.prediction_horizon)
# Enhance with velocity-based correction
if len(self.velocity_history) >= 2:
recent_velocities = list(self.velocity_history)[-2:]
avg_vx = np.mean([v[0] for v in recent_velocities])
avg_vy = np.mean([v[1] for v in recent_velocities])
# Add velocity-based prediction component
velocity_pred_x = positions[-1][0] + avg_vx * self.prediction_horizon
velocity_pred_y = positions[-1][1] + avg_vy * self.prediction_horizon
# Blend polynomial and velocity predictions
predicted_x = 0.7 * predicted_x + 0.3 * velocity_pred_x
predicted_y = 0.7 * predicted_y + 0.3 * velocity_pred_y
return (float(predicted_x), float(predicted_y))
except:
return None
def smooth_position_adaptive(
self,
current_x: float,
current_y: float
) -> Tuple[float, float]:
"""
Smooth position with adaptive smoothing factor.
Args:
current_x: Current X position
current_y: Current Y position
Returns:
Smoothed (x, y) position
"""
predicted = self.predict_next_position_polynomial()
if predicted is None:
return (current_x, current_y)
predicted_x, predicted_y = predicted
# Get adaptive smoothing factor
smoothing = self._calculate_adaptive_smoothing()
# Blend with adaptive smoothing
smoothed_x = current_x * (1.0 - smoothing) + predicted_x * smoothing
smoothed_y = current_y * (1.0 - smoothing) + predicted_y * smoothing
return (smoothed_x, smoothed_y)
def update_learning(self, actual_x: float, actual_y: float):
"""
Learn from prediction errors to improve over time.
Args:
actual_x: Actual cursor X position
actual_y: Actual cursor Y position
"""
if len(self.position_history) < 2:
return
# Get last prediction
predicted = self.predict_next_position_polynomial()
if predicted is None:
return
# Calculate prediction error
error = np.sqrt(
(actual_x - predicted[0]) ** 2 +
(actual_y - predicted[1]) ** 2
)
self.prediction_errors.append(error)
# Adapt based on error history
if len(self.prediction_errors) >= 10:
avg_error = np.mean(list(self.prediction_errors))
# If errors are high, reduce smoothing (be more direct)
# If errors are low, can increase smoothing (more prediction)
if avg_error > config.ML_ERROR_THRESHOLD_HIGH:
self.adaptive_factor *= 0.95 # Reduce smoothing
elif avg_error < config.ML_ERROR_THRESHOLD_LOW:
self.adaptive_factor *= 1.02 # Slightly increase smoothing
# Clamp adaptation factor
self.adaptive_factor = max(0.5, min(1.5, self.adaptive_factor))
def clear_history(self):
"""Clear all history."""
self.position_history.clear()
self.time_history.clear()
self.velocity_history.clear()
self.prediction_errors.clear()
self.adaptive_factor = 1.0
class GestureClassifier:
"""
ML-based gesture classifier for improved gesture recognition.
Uses pattern matching and learning to classify gestures more accurately.
"""
def __init__(self):
"""Initialize gesture classifier."""
self.gesture_patterns: Dict[str, List] = {
'pinch': [],
'scroll': [],
'point': []
}
self.learning_enabled = config.ML_GESTURE_LEARNING
def classify_pinch(
self,
thumb_pos: Tuple[float, float],
index_pos: Tuple[float, float],
hand_size: float
) -> Tuple[bool, float]:
"""
Classify pinch gesture with ML-enhanced threshold.
Args:
thumb_pos: Thumb tip position
index_pos: Index finger tip position
hand_size: Hand size for normalization
Returns:
(is_pinch, confidence)
"""
distance = np.sqrt(
(thumb_pos[0] - index_pos[0]) ** 2 +
(thumb_pos[1] - index_pos[1]) ** 2
)
# Base threshold
base_threshold = config.PINCH_THRESHOLD * hand_size
# ML-enhanced threshold (learned from patterns)
if self.learning_enabled and len(self.gesture_patterns['pinch']) > 5:
# Calculate average pinch distance from learned patterns
learned_distances = [p['distance'] for p in self.gesture_patterns['pinch']]
learned_threshold = np.percentile(learned_distances, 75) # 75th percentile
# Blend learned with base
adaptive_threshold = 0.7 * base_threshold + 0.3 * learned_threshold
else:
adaptive_threshold = base_threshold
is_pinch = distance < adaptive_threshold
# Calculate confidence based on how close to threshold
if is_pinch:
confidence = 1.0 - (distance / adaptive_threshold)
else:
confidence = 0.0
return (is_pinch, confidence)
def learn_pinch_pattern(
self,
thumb_pos: Tuple[float, float],
index_pos: Tuple[float, float],
hand_size: float,
was_click: bool
):
"""
Learn from successful pinch gestures.
Args:
thumb_pos: Thumb position
index_pos: Index position
hand_size: Hand size
was_click: Whether this resulted in a click
"""
if not self.learning_enabled:
return
distance = np.sqrt(
(thumb_pos[0] - index_pos[0]) ** 2 +
(thumb_pos[1] - index_pos[1]) ** 2
)
# Only learn from successful gestures
if was_click:
pattern = {
'distance': distance,
'hand_size': hand_size,
'normalized_distance': distance / hand_size if hand_size > 0 else 0
}
self.gesture_patterns['pinch'].append(pattern)
# Keep only recent patterns
if len(self.gesture_patterns['pinch']) > 100:
self.gesture_patterns['pinch'] = self.gesture_patterns['pinch'][-100:]
class IntentPredictor:
"""
AI-powered intent prediction for smarter cursor control.
Predicts user intent (click, scroll, move) before gestures complete.
"""
def __init__(self):
"""Initialize intent predictor."""
self.movement_patterns: deque = deque(maxlen=20)
self.gesture_history: deque = deque(maxlen=10)
# Intent probabilities
self.intent_click_prob = 0.0
self.intent_scroll_prob = 0.0
self.intent_move_prob = 1.0
def update_movement(
self,
position: Tuple[float, float],
velocity: Tuple[float, float],
timestamp: float
):
"""
Update movement pattern for intent prediction.
Args:
position: Current position
velocity: Current velocity
timestamp: Current time
"""
speed = np.sqrt(velocity[0] ** 2 + velocity[1] ** 2)
self.movement_patterns.append({
'position': position,
'velocity': velocity,
'speed': speed,
'timestamp': timestamp
})
def predict_intent(self) -> Dict[str, float]:
"""
Predict user intent based on movement patterns.
Returns:
Dictionary with intent probabilities
"""
if len(self.movement_patterns) < 3:
return {
'click': 0.0,
'scroll': 0.0,
'move': 1.0
}
recent = list(self.movement_patterns)[-5:]
# Analyze movement characteristics
speeds = [p['speed'] for p in recent]
avg_speed = np.mean(speeds)
speed_variance = np.var(speeds)
# Calculate deceleration (slowing down = likely to click)
if len(recent) >= 2:
speed_change = speeds[-1] - speeds[0]
deceleration = speed_change < -config.ML_DECELERATION_THRESHOLD
else:
deceleration = False
# Movement toward target (if intent engine active)
# This would integrate with intent engine
# Predict intent probabilities
click_prob = 0.0
if deceleration and avg_speed < config.ML_CLICK_SPEED_THRESHOLD:
click_prob = min(1.0, (config.ML_CLICK_SPEED_THRESHOLD - avg_speed) / config.ML_CLICK_SPEED_THRESHOLD)
scroll_prob = 0.0
if speed_variance > config.ML_SCROLL_VARIANCE_THRESHOLD:
scroll_prob = min(1.0, speed_variance / config.ML_SCROLL_VARIANCE_THRESHOLD)
move_prob = 1.0 - click_prob - scroll_prob
move_prob = max(0.0, move_prob)
self.intent_click_prob = click_prob
self.intent_scroll_prob = scroll_prob
self.intent_move_prob = move_prob
return {
'click': click_prob,
'scroll': scroll_prob,
'move': move_prob
}
def get_optimal_smoothing(self) -> float:
"""
Get optimal smoothing factor based on predicted intent.
Returns:
Optimal smoothing factor
"""
# If likely to click: less smoothing (more precise)
# If likely to scroll: moderate smoothing
# If likely to move: more smoothing (smooth movement)
if self.intent_click_prob > 0.5:
return config.ML_SMOOTHING_FACTOR * 0.5 # Less smoothing for precision
elif self.intent_scroll_prob > 0.5:
return config.ML_SMOOTHING_FACTOR * 0.8
else:
return config.ML_SMOOTHING_FACTOR # Normal smoothing
class UserAdaptation:
"""
Learn and adapt to individual user patterns for personalized experience.
"""
def __init__(self, user_profile_path: str = "user_profile.json"):
"""
Initialize user adaptation.
Args:
user_profile_path: Path to save/load user profile
"""
self.profile_path = user_profile_path
self.profile = self._load_profile()
# Track user patterns
self.movement_speeds: deque = deque(maxlen=100)
self.gesture_timings: deque = deque(maxlen=50)
self.preferred_settings = {}
def _load_profile(self) -> Dict:
"""Load user profile from file."""
if os.path.exists(self.profile_path):
try:
with open(self.profile_path, 'r') as f:
return json.load(f)
except:
return {}
return {}
def _save_profile(self):
"""Save user profile to file."""
try:
with open(self.profile_path, 'w') as f:
json.dump(self.profile, f, indent=2)
except:
pass
def learn_movement_pattern(self, speed: float, timestamp: float):
"""Learn from user's movement patterns."""
self.movement_speeds.append(speed)
if len(self.movement_speeds) >= 20:
avg_speed = np.mean(list(self.movement_speeds))
self.profile['avg_movement_speed'] = float(avg_speed)
self._save_profile()
def learn_gesture_timing(self, gesture_type: str, duration: float):
"""Learn from gesture timings."""
if gesture_type not in self.profile:
self.profile[gesture_type] = []
self.profile[gesture_type].append(duration)
# Keep only recent timings
if len(self.profile[gesture_type]) > 50:
self.profile[gesture_type] = self.profile[gesture_type][-50:]
self._save_profile()
def get_adaptive_threshold(self, base_threshold: float, gesture_type: str) -> float:
"""
Get adaptive threshold based on learned patterns.
Args:
base_threshold: Base threshold value
gesture_type: Type of gesture ('pinch', 'scroll', etc.)
Returns:
Adaptive threshold
"""
if gesture_type in self.profile and len(self.profile[gesture_type]) > 5:
learned_timings = self.profile[gesture_type]
avg_timing = np.mean(learned_timings)
# Adjust threshold based on user's typical timing
# Users who pinch quickly need lower threshold
# Users who pinch slowly need higher threshold
if gesture_type == 'pinch':
if avg_timing < 0.2: # Fast pincher
return base_threshold * 0.9
elif avg_timing > 0.4: # Slow pincher
return base_threshold * 1.1
return base_threshold
def get_optimal_settings(self) -> Dict:
"""
Get optimal settings based on learned patterns.
Returns:
Dictionary of optimal settings
"""
settings = {}
# Optimal smoothing based on movement speed
if 'avg_movement_speed' in self.profile:
avg_speed = self.profile['avg_movement_speed']
if avg_speed > 500: # Fast mover
settings['smoothing'] = 0.1
elif avg_speed < 100: # Slow mover
settings['smoothing'] = 0.3
else:
settings['smoothing'] = 0.2
return settings