Spaces:
Sleeping

File size: 8,825 Bytes
f4586bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Rule-based gesture classifier using finger openness.

No training required - works immediately based on geometric rules:
- ROCK: All fingers closed (low openness)
- PAPER: All fingers open (high openness)
- SCISSORS: Index and middle open, others closed

Works best with MediaPipeEnhancedExtractor which provides finger_openness in metadata.
Can also work with raw 63D landmarks by computing openness on-the-fly.
"""

import time
import numpy as np
from core.base_classifier import BaseClassifier
from core.feature_vector import FeatureVector
from core.classification_result import ClassificationResult, GestureClass
from core.data_object import DataObject
from typing import Dict, Optional, List


class RuleBasedGestureClassifier(BaseClassifier):
    """
    Rule-based classifier using finger openness values.

    Finger indices:
        0: Thumb
        1: Index
        2: Middle
        3: Ring
        4: Pinky

    Classification rules:
        ROCK: All fingers closed (openness < threshold)
        PAPER: All fingers open (openness > threshold)
        SCISSORS: Index + Middle open, others closed
    """

    def __init__(
        self,
        open_threshold: float = 0.65,
        closed_threshold: float = 0.45,
        confidence_base: float = 0.7
    ):
        """
        Initialize rule-based classifier.

        Args:
            open_threshold: Openness value above which finger is considered "open"
            closed_threshold: Openness value below which finger is considered "closed"
            confidence_base: Base confidence for classifications
        """
        super().__init__(name="Rule_Based_Gesture")
        self.open_threshold = open_threshold
        self.closed_threshold = closed_threshold
        self.confidence_base = confidence_base

    def _process(self, input: DataObject) -> ClassificationResult:
        start_time = time.perf_counter()

        features = input  # type: FeatureVector

        # Check if hand was detected
        if features.metadata and not features.metadata.get('hand_detected', True):
            return self._create_unknown_result(start_time, "No hand detected")

        # Get finger openness values
        finger_openness = self._get_finger_openness(features)

        if finger_openness is None:
            return self._create_unknown_result(start_time, "Could not extract finger openness")

        # Classify based on rules
        predicted_class, confidence, probabilities = self._classify_by_rules(finger_openness)

        end_time = time.perf_counter()
        processing_time = (end_time - start_time) * 1000

        return ClassificationResult(
            predicted_class=predicted_class,
            confidence=confidence,
            class_probabilities=probabilities,
            processing_time_ms=processing_time,
            classifier_name=self.name,
            metadata={
                'finger_openness': finger_openness.tolist(),
                'open_threshold': self.open_threshold,
                'closed_threshold': self.closed_threshold
            }
        )

    def _get_finger_openness(self, features: FeatureVector) -> Optional[np.ndarray]:
        """
        Extract finger openness values from features.

        First tries metadata (from MediaPipeEnhancedExtractor),
        then tries named_features, otherwise returns None.
        """
        # Try metadata first (MediaPipeEnhancedExtractor provides this)
        if features.metadata and 'finger_openness' in features.metadata:
            return np.array(features.metadata['finger_openness'], dtype=np.float32)

        # Try named_features
        if features.named_features:
            finger_names = ['thumb', 'index', 'middle', 'ring', 'pinky']
            openness = []
            for name in finger_names:
                key = f'{name}_openness'
                if key in features.named_features:
                    openness.append(features.named_features[key])
                else:
                    return None
            return np.array(openness, dtype=np.float32)

        return None

    def _classify_by_rules(
        self,
        finger_openness: np.ndarray
    ) -> tuple[GestureClass, float, Dict[GestureClass, float]]:
        """
        Classify gesture based on finger openness rules.

        Returns:
            (predicted_class, confidence, class_probabilities)
        """
        thumb, index, middle, ring, pinky = finger_openness

        # Compute scores for each gesture
        rock_score = self._compute_rock_score(finger_openness)
        paper_score = self._compute_paper_score(finger_openness)
        scissors_score = self._compute_scissors_score(finger_openness)

        # Normalize to probabilities
        total = rock_score + paper_score + scissors_score + 1e-6
        probabilities = {
            GestureClass.ROCK: rock_score / total,
            GestureClass.PAPER: paper_score / total,
            GestureClass.SCISSORS: scissors_score / total,
            GestureClass.UNKNOWN: 0.0
        }

        # Find winner
        if rock_score >= paper_score and rock_score >= scissors_score:
            predicted = GestureClass.ROCK
            confidence = probabilities[GestureClass.ROCK]
        elif paper_score >= scissors_score:
            predicted = GestureClass.PAPER
            confidence = probabilities[GestureClass.PAPER]
        else:
            predicted = GestureClass.SCISSORS
            confidence = probabilities[GestureClass.SCISSORS]

        # Adjust confidence based on clarity of decision
        confidence = min(confidence * 1.2, 0.99)  # Boost but cap at 99%

        return predicted, confidence, probabilities

    def _compute_rock_score(self, openness: np.ndarray) -> float:
        """
        ROCK: All fingers closed.
        Score based on how closed all fingers are.
        """
        # All fingers should be below closed_threshold
        closed_scores = []
        for val in openness:
            if val < self.closed_threshold:
                closed_scores.append(1.0)
            elif val < self.open_threshold:
                # Partial score in the middle zone
                closed_scores.append(0.5)
            else:
                closed_scores.append(0.0)

        return np.mean(closed_scores)

    def _compute_paper_score(self, openness: np.ndarray) -> float:
        """
        PAPER: All fingers open.
        Score based on how open all fingers are.
        """
        open_scores = []
        for val in openness:
            if val > self.open_threshold:
                open_scores.append(1.0)
            elif val > self.closed_threshold:
                open_scores.append(0.5)
            else:
                open_scores.append(0.0)

        return np.mean(open_scores)

    def _compute_scissors_score(self, openness: np.ndarray) -> float:
        """
        SCISSORS: Index and middle open, thumb/ring/pinky closed.
        """
        thumb, index, middle, ring, pinky = openness

        scores = []

        # Index should be open
        if index > self.open_threshold:
            scores.append(1.0)
        elif index > self.closed_threshold:
            scores.append(0.5)
        else:
            scores.append(0.0)

        # Middle should be open
        if middle > self.open_threshold:
            scores.append(1.0)
        elif middle > self.closed_threshold:
            scores.append(0.5)
        else:
            scores.append(0.0)

        # Ring should be closed
        if ring < self.closed_threshold:
            scores.append(1.0)
        elif ring < self.open_threshold:
            scores.append(0.5)
        else:
            scores.append(0.0)

        # Pinky should be closed
        if pinky < self.closed_threshold:
            scores.append(1.0)
        elif pinky < self.open_threshold:
            scores.append(0.5)
        else:
            scores.append(0.0)

        # Thumb is flexible for scissors (can be open or closed)
        # Give partial score regardless
        scores.append(0.5)

        return np.mean(scores)

    def _create_unknown_result(
        self,
        start_time: float,
        reason: str
    ) -> ClassificationResult:
        """Create an UNKNOWN result with zero confidence."""
        end_time = time.perf_counter()
        processing_time = (end_time - start_time) * 1000

        return ClassificationResult(
            predicted_class=GestureClass.UNKNOWN,
            confidence=0.0,
            class_probabilities={
                GestureClass.ROCK: 0.0,
                GestureClass.PAPER: 0.0,
                GestureClass.SCISSORS: 0.0,
                GestureClass.UNKNOWN: 1.0
            },
            processing_time_ms=processing_time,
            classifier_name=self.name,
            metadata={'reason': reason}
        )