You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
import numpy as np
|
|
import tensorflow as tf
|
|
import joblib
|
|
from collections import deque
|
|
from ml_gestures_dynamic.feature_extractor import extract_sequence
|
|
|
|
class DynamicGesturePredictor:
|
|
def __init__(self, model_path, classes_path, window_size=30, threshold=0.7):
|
|
self.model = tf.keras.models.load_model(model_path)
|
|
with open(classes_path, 'rb') as f:
|
|
self.classes = joblib.load(f)
|
|
self.window_size = window_size
|
|
self.buffer = deque(maxlen=window_size)
|
|
self.threshold = threshold
|
|
|
|
def add_frame(self, landmarks):
|
|
if landmarks is None or np.isnan(landmarks).any():
|
|
landmarks = np.zeros(99)
|
|
self.buffer.append(landmarks)
|
|
|
|
def predict(self):
|
|
if len(self.buffer) < self.window_size:
|
|
return None
|
|
seq = extract_sequence(list(self.buffer))
|
|
seq = np.expand_dims(seq, axis=0) # (1, window, 99)
|
|
probs = self.model.predict(seq, verbose=0)[0]
|
|
idx = np.argmax(probs)
|
|
if probs[idx] > self.threshold and self.classes[idx] != 'none':
|
|
return self.classes[idx]
|
|
return None
|
|
|
|
def reset(self):
|
|
self.buffer.clear()
|