File size: 27,768 Bytes
d19858a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
╔══════════════════════════════════════════════════════════════════════════════╗
β•‘  Models: gesture.py                                                          β•‘
β•‘  Core classes for representing hand gestures                                 β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•

This module contains:
β€’ GestureRanking - The ranking system for gestures
β€’ GestureImage - Represents a captured gesture with its data

πŸ“š WHY SEPARATE FILES?
   In the procedural style, you might put everything in one big file.
   In OOP, we organize related classes into modules (files).
   
   Benefits:
   β€’ Easier to find code (gesture stuff is in gesture.py)
   β€’ Easier to test (can test gesture.py independently)
   β€’ Easier to reuse (import just what you need)
   β€’ Easier to collaborate (different people work on different files)
"""

from dataclasses import dataclass
from typing import List, Optional
from PIL import Image


# ==============================================================================
# CLASS: GestureRanking
# ==============================================================================
# This class holds the RANKING SYSTEM for gestures.
# It's like a rulebook that says "fist comes before one, one comes before peace..."
#
# πŸ’‘ WHY A CLASS? 
#    In procedural code, this would be a dictionary floating around globally.
#    As a class, we can:
#    1. Add METHODS to work with the data (get_rank, get_emoji, compare)
#    2. PROTECT the data from accidental changes
#    3. Keep related functions TOGETHER with the data they use
# ==============================================================================

class GestureRanking:
    """
    Defines the ordering of hand gestures for sorting purposes.
    
    This class encapsulates (bundles together):
    - The ranking of each gesture (which comes first in sorted order)
    - The emoji representation of each gesture
    - Methods to compare gestures
    
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  πŸ“š CONCEPT: Class Attributes vs Instance Attributes                    β”‚
    β”‚                                                                         β”‚
    β”‚  CLASS ATTRIBUTES: Shared by ALL instances (objects) of the class      β”‚
    β”‚      - Defined directly in the class body                               β”‚
    β”‚      - Like a shared resource everyone can read                         β”‚
    β”‚      - Here: RANKINGS and EMOJIS are class attributes                   β”‚
    β”‚                                                                         β”‚
    β”‚  INSTANCE ATTRIBUTES: Unique to EACH instance                           β”‚
    β”‚      - Defined in __init__ using self.attribute_name                    β”‚
    β”‚      - Like personal belongings each person carries                     β”‚
    β”‚      - Here: GestureRanking doesn't have instance attributes            β”‚
    β”‚              (it's a utility class with shared data)                    β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    """
    
    # -------------------------------------------------------------------------
    # Class Attribute: RANKINGS
    # -------------------------------------------------------------------------
    # This dictionary maps gesture names to their rank (sorting order).
    # Lower rank = comes first when sorted in ascending order.
    #
    # πŸ’­ Design Decision: We ordered gestures roughly by "finger count"
    #    fist (0 fingers) β†’ one β†’ peace (2) β†’ three β†’ four β†’ palm (5) β†’ special signs
    # -------------------------------------------------------------------------
    RANKINGS = {
        "fist":     1,   # ✊ Closed fist (0 fingers showing)
        "one":      2,   # ☝️ One finger up
        "two_up":   3,   # ✌️ Two fingers (peace sign)
        "peace":    3,   # ✌️ Alias for two_up (same gesture, different name)
        "three":    4,   # 🀟 Three fingers
        "four":     5,   # πŸ–– Four fingers
        "palm":     6,   # πŸ–οΈ Open palm (5 fingers)
        "stop":     6,   # πŸ–οΈ Alias for palm
        "ok":       7,   # πŸ‘Œ OK sign
        "like":     8,   # πŸ‘ Thumbs up
        "dislike":  9,   # πŸ‘Ž Thumbs down
        "rock":    10,   # 🀘 Rock sign
        "call":    11,   # πŸ€™ Call me sign
        "mute":    12,   # 🀫 Shush/mute gesture
        "no_gesture": 99, # Unknown or no gesture detected
    }
    
    # -------------------------------------------------------------------------
    # Class Attribute: EMOJIS
    # -------------------------------------------------------------------------
    # Visual representation of each gesture.
    # Makes the UI more engaging and helps identify gestures quickly.
    # -------------------------------------------------------------------------
    EMOJIS = {
        "fist":     "✊",
        "one":      "☝️",
        "two_up":   "✌️",
        "peace":    "✌️",
        "three":    "🀟",
        "four":     "πŸ––",
        "palm":     "πŸ–οΈ",
        "stop":     "πŸ–οΈ",
        "ok":       "πŸ‘Œ",
        "like":     "πŸ‘",
        "dislike":  "πŸ‘Ž",
        "rock":     "🀘",
        "call":     "πŸ€™",
        "mute":     "🀫",
        "no_gesture": "❓",
    }
    
    # -------------------------------------------------------------------------
    # Class Method: get_rank
    # -------------------------------------------------------------------------
    # πŸ“š CONCEPT: @classmethod
    # 
    # A classmethod belongs to the CLASS, not to an instance.
    # - Regular method: needs an object to be called (object.method())
    # - Class method: can be called on the class itself (ClassName.method())
    #
    # Use @classmethod when the method needs CLASS data but not INSTANCE data.
    # -------------------------------------------------------------------------
    @classmethod
    def get_rank(cls, gesture_name: str) -> int:
        """
        Get the sorting rank of a gesture.
        
        Args:
            gesture_name: The name of the gesture (e.g., "peace", "fist")
            
        Returns:
            The rank (1-99) of the gesture. Lower = earlier in sorted order.
            Returns 99 if gesture is unknown.
            
        Example:
            >>> GestureRanking.get_rank("peace")
            3
            >>> GestureRanking.get_rank("fist")
            1
        """
        # .get() returns the value if key exists, otherwise the default (99)
        # This prevents crashes if someone passes an unknown gesture name
        return cls.RANKINGS.get(gesture_name.lower(), 99)
    
    @classmethod
    def get_emoji(cls, gesture_name: str) -> str:
        """
        Get the emoji representation of a gesture.
        
        Args:
            gesture_name: The name of the gesture
            
        Returns:
            The emoji string for this gesture, or ❓ if unknown.
        """
        return cls.EMOJIS.get(gesture_name.lower(), "❓")
    
    @classmethod
    def compare(cls, gesture_a: str, gesture_b: str) -> int:
        """
        Compare two gestures for sorting order.
        
        This follows the standard comparison convention:
        - Returns NEGATIVE if a < b (a comes before b)
        - Returns ZERO if a == b (same rank)
        - Returns POSITIVE if a > b (a comes after b)
        
        Args:
            gesture_a: First gesture name
            gesture_b: Second gesture name
            
        Returns:
            Negative, zero, or positive integer.
            
        Example:
            >>> GestureRanking.compare("fist", "peace")
            -2  # Negative: fist comes before peace
            >>> GestureRanking.compare("peace", "fist")
            2   # Positive: peace comes after fist
        """
        return cls.get_rank(gesture_a) - cls.get_rank(gesture_b)
    
    @classmethod
    def get_all_gestures(cls) -> List[str]:
        """
        Get a list of all known gestures, sorted by rank.
        
        Returns:
            List of gesture names in sorted order.
        """
        # Sort the gesture names by their rank value
        # This uses a lambda function as the sorting key
        sorted_gestures = sorted(
            cls.RANKINGS.keys(),
            key=lambda name: cls.RANKINGS[name]
        )
        # Remove duplicates while preserving order
        seen = set()
        unique = []
        for gesture in sorted_gestures:
            if gesture not in seen:
                seen.add(gesture)
                unique.append(gesture)
        return unique


# ==============================================================================
# CLASS: GestureImage (using @dataclass)
# ==============================================================================
"""
╔══════════════════════════════════════════════════════════════════════════════╗
β•‘  πŸ“š CONCEPT: What is a @dataclass?                                           β•‘
╠══════════════════════════════════════════════════════════════════════════════╣
β•‘                                                                              β•‘
β•‘  A @dataclass is a shortcut for creating classes that mainly hold DATA.      β•‘
β•‘                                                                              β•‘
β•‘  WITHOUT @dataclass (the long way):                                          β•‘
β•‘  ─────────────────────────────────                                           β•‘
β•‘      class GestureImage:                                                     β•‘
β•‘          def __init__(self, gesture, rank, emoji, image, capture_id):        β•‘
β•‘              self.gesture = gesture                                          β•‘
β•‘              self.rank = rank                                                β•‘
β•‘              self.emoji = emoji                                              β•‘
β•‘              self.image = image                                              β•‘
β•‘              self.capture_id = capture_id                                    β•‘
β•‘                                                                              β•‘
β•‘          def __repr__(self):                                                 β•‘
β•‘              return f"GestureImage(gesture={self.gesture}, ...)"             β•‘
β•‘                                                                              β•‘
β•‘          def __eq__(self, other):                                            β•‘
β•‘              return self.gesture == other.gesture and ...                    β•‘
β•‘                                                                              β•‘
β•‘  WITH @dataclass (the shortcut):                                             β•‘
β•‘  ───────────────────────────────                                             β•‘
β•‘      @dataclass                                                              β•‘
β•‘      class GestureImage:                                                     β•‘
β•‘          gesture: str                                                        β•‘
β•‘          rank: int                                                           β•‘
β•‘          emoji: str                                                          β•‘
β•‘          image: Image                                                        β•‘
β•‘          capture_id: int                                                     β•‘
β•‘                                                                              β•‘
β•‘  The @dataclass automatically generates __init__, __repr__, __eq__, etc!     β•‘
β•‘                                                                              β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
"""

@dataclass
class GestureImage:
    """
    Represents a captured hand gesture image with its classification.
    
    This is the CORE DATA STRUCTURE of our application.
    Each GestureImage bundles together:
    - The actual image (pixels)
    - The AI's prediction of what gesture it shows
    - A unique ID for tracking (important for stability testing)
    - Visual representations (emoji, rank)
    
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚  πŸ’‘ WHY THIS MATTERS: Encapsulation                                     β”‚
    β”‚                                                                         β”‚
    β”‚  In procedural code, you'd pass around separate variables:              β”‚
    β”‚      process_gesture(image, name, rank, emoji, id)  # 5 parameters!     β”‚
    β”‚                                                                         β”‚
    β”‚  With OOP, you pass ONE object that contains everything:                β”‚
    β”‚      process_gesture(gesture_image)  # 1 parameter!                     β”‚
    β”‚                                                                         β”‚
    β”‚  Benefits:                                                              β”‚
    β”‚  βœ“ Less room for errors (can't mix up parameter order)                 β”‚
    β”‚  βœ“ Easier to add new attributes later                                  β”‚
    β”‚  βœ“ Methods travel WITH the data they operate on                        β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    
    Attributes:
        gesture: The name of the detected gesture (e.g., "peace", "fist")
        rank: Numeric rank for sorting (lower = comes first)
        emoji: Visual emoji representation
        image: The actual PIL Image (can be None if not needed)
        capture_id: Unique ID from capture order (for stability testing)
        thumbnail: Smaller version for display (generated automatically)
    """
    
    # -------------------------------------------------------------------------
    # Dataclass Fields (Attributes)
    # -------------------------------------------------------------------------
    # These define what data each GestureImage object will hold.
    # The type hints (: str, : int, etc.) help document and catch errors.
    # -------------------------------------------------------------------------
    
    gesture: str                           # Name of the gesture
    rank: int                              # Sorting rank (from GestureRanking)
    emoji: str                             # Emoji representation
    capture_id: int                        # Unique ID (for stability tracking)
    image: Optional[Image.Image] = None    # The actual image (optional)
    thumbnail: Optional[Image.Image] = None  # Small version for display
    confidence: float = 0.0                # AI's confidence in the prediction
    
    # -------------------------------------------------------------------------
    # Special Method: __post_init__
    # -------------------------------------------------------------------------
    # This runs AFTER the automatic __init__ created by @dataclass.
    # We use it to create the thumbnail from the full image.
    # -------------------------------------------------------------------------
    def __post_init__(self):
        """
        Called automatically after the object is created.
        Generates a thumbnail if an image is provided.
        """
        if self.image is not None and self.thumbnail is None:
            self._create_thumbnail()
    
    def _create_thumbnail(self, max_size: int = 80):
        """
        Create a smaller version of the image for display.
        
        The underscore prefix (_create_thumbnail) is a Python convention
        meaning "this is an internal method, not meant to be called from outside".
        
        Args:
            max_size: Maximum width/height of the thumbnail
        """
        if self.image is not None:
            # Create a copy so we don't modify the original
            thumb = self.image.copy()
            # Resize while maintaining aspect ratio
            thumb.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
            self.thumbnail = thumb
    
    # -------------------------------------------------------------------------
    # Comparison Methods: Making objects sortable
    # -------------------------------------------------------------------------
    """
    ╔═════════════════════════════════════════════════════════════════════════╗
    β•‘  πŸ“š CONCEPT: Magic Methods (Dunder Methods)                             β•‘
    ╠═════════════════════════════════════════════════════════════════════════╣
    β•‘                                                                         β•‘
    β•‘  Python has special method names surrounded by double underscores.      β•‘
    β•‘  These are called "magic methods" or "dunder methods" (double under).   β•‘
    β•‘                                                                         β•‘
    β•‘  They let your objects work with Python's built-in operations:          β•‘
    β•‘                                                                         β•‘
    β•‘  __lt__(self, other)  β†’  enables: object1 < object2                    β•‘
    β•‘  __le__(self, other)  β†’  enables: object1 <= object2                   β•‘
    β•‘  __eq__(self, other)  β†’  enables: object1 == object2                   β•‘
    β•‘  __gt__(self, other)  β†’  enables: object1 > object2                    β•‘
    β•‘  __ge__(self, other)  β†’  enables: object1 >= object2                   β•‘
    β•‘  __str__(self)        β†’  enables: str(object) or print(object)         β•‘
    β•‘  __repr__(self)       β†’  enables: repr(object) (for debugging)         β•‘
    β•‘                                                                         β•‘
    β•‘  πŸ’‘ WHY THIS MATTERS:                                                   β•‘
    β•‘  With these methods, Python's built-in sorted() function                β•‘
    β•‘  automatically works with our GestureImage objects!                     β•‘
    β•‘                                                                         β•‘
    β•‘      gestures = [gesture1, gesture2, gesture3]                         β•‘
    β•‘      sorted_gestures = sorted(gestures)  # Just works! ✨              β•‘
    β•‘                                                                         β•‘
    β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
    """
    
    def __lt__(self, other: 'GestureImage') -> bool:
        """
        Less than comparison. Enables: gesture1 < gesture2
        
        Compares by rank. If ranks are equal, maintains stability
        by comparing capture_id (earlier captured = smaller).
        """
        if self.rank != other.rank:
            return self.rank < other.rank
        # If same rank, compare by capture_id for stable sorting
        return self.capture_id < other.capture_id
    
    def __le__(self, other: 'GestureImage') -> bool:
        """Less than or equal. Enables: gesture1 <= gesture2"""
        return self.rank <= other.rank
    
    def __gt__(self, other: 'GestureImage') -> bool:
        """Greater than. Enables: gesture1 > gesture2"""
        if self.rank != other.rank:
            return self.rank > other.rank
        return self.capture_id > other.capture_id
    
    def __ge__(self, other: 'GestureImage') -> bool:
        """Greater than or equal. Enables: gesture1 >= gesture2"""
        return self.rank >= other.rank
    
    def __eq__(self, other: object) -> bool:
        """
        Equality comparison. Enables: gesture1 == gesture2
        
        Two gestures are equal if they have the same rank.
        Note: We compare RANKS, not capture_ids, for sorting purposes.
        """
        if not isinstance(other, GestureImage):
            return False
        return self.rank == other.rank
    
    def __hash__(self) -> int:
        """
        Hash function. Required for using objects in sets or as dict keys.
        We hash by capture_id since it's unique.
        """
        return hash(self.capture_id)
    
    # -------------------------------------------------------------------------
    # Display Methods
    # -------------------------------------------------------------------------
    
    def __str__(self) -> str:
        """
        Human-readable string representation.
        Called by print() and str().
        
        Example: "βœŒοΈβ‚" (peace sign, capture #1)
        """
        # Subscript numbers for capture_id
        subscripts = "β‚€β‚β‚‚β‚ƒβ‚„β‚…β‚†β‚‡β‚ˆβ‚‰"
        sub_id = ''.join(subscripts[int(d)] for d in str(self.capture_id))
        return f"{self.emoji}{sub_id}"
    
    def __repr__(self) -> str:
        """
        Developer-friendly representation (for debugging).
        Called by repr() and shown in interactive Python.
        """
        return f"GestureImage(gesture='{self.gesture}', rank={self.rank}, id={self.capture_id})"
    
    def display_label(self) -> str:
        """
        Get a label for UI display.
        Shows emoji, gesture name, and capture ID.
        """
        return f"{self.emoji} {self.gesture} (#{self.capture_id})"
    
    # -------------------------------------------------------------------------
    # Factory Methods
    # -------------------------------------------------------------------------
    """
    ╔═════════════════════════════════════════════════════════════════════════╗
    β•‘  πŸ“š CONCEPT: Factory Methods                                            β•‘
    ╠═════════════════════════════════════════════════════════════════════════╣
    β•‘                                                                         β•‘
    β•‘  A Factory Method is a class method that CREATES instances.             β•‘
    β•‘                                                                         β•‘
    β•‘  Instead of:                                                            β•‘
    β•‘      gesture = GestureImage(                                            β•‘
    β•‘          gesture="peace",                                               β•‘
    β•‘          rank=GestureRanking.get_rank("peace"),                         β•‘
    β•‘          emoji=GestureRanking.get_emoji("peace"),                       β•‘
    β•‘          capture_id=1,                                                  β•‘
    β•‘          image=my_image,                                                β•‘
    β•‘          confidence=0.95                                                β•‘
    β•‘      )                                                                  β•‘
    β•‘                                                                         β•‘
    β•‘  You can use:                                                           β•‘
    β•‘      gesture = GestureImage.create_from_prediction(                     β•‘
    β•‘          gesture_name="peace",                                          β•‘
    β•‘          capture_id=1,                                                  β•‘
    β•‘          image=my_image,                                                β•‘
    β•‘          confidence=0.95                                                β•‘
    β•‘      )                                                                  β•‘
    β•‘                                                                         β•‘
    β•‘  The factory method handles the details of looking up rank/emoji!       β•‘
    β•‘                                                                         β•‘
    β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•
    """
    
    @classmethod
    def create_from_prediction(
        cls,
        gesture_name: str,
        capture_id: int,
        image: Optional[Image.Image] = None,
        confidence: float = 0.0
    ) -> 'GestureImage':
        """
        Factory method to create a GestureImage from an AI prediction.
        
        This is a convenient way to create GestureImage objects without
        needing to manually look up ranks and emojis.
        
        Args:
            gesture_name: The predicted gesture name
            capture_id: Unique identifier for this capture
            image: The original image (optional)
            confidence: AI confidence score (0.0 to 1.0)
            
        Returns:
            A new GestureImage instance
        """
        return cls(
            gesture=gesture_name.lower(),
            rank=GestureRanking.get_rank(gesture_name),
            emoji=GestureRanking.get_emoji(gesture_name),
            capture_id=capture_id,
            image=image,
            confidence=confidence
        )
    
    @classmethod
    def create_manual(
        cls,
        gesture_name: str,
        capture_id: int,
        image: Optional[Image.Image] = None
    ) -> 'GestureImage':
        """
        Create a GestureImage with manual gesture assignment (no AI).
        Same as create_from_prediction but with 100% confidence.
        """
        return cls.create_from_prediction(
            gesture_name=gesture_name,
            capture_id=capture_id,
            image=image,
            confidence=1.0  # Manual assignment = 100% confident
        )