1
+ import cv2
2
+ from cvzone .HandTrackingModule import HandDetector
3
+ import json # JSON kütüphanesini içe aktar
4
+
5
+ # JSON haritasını yükle
6
+ map_file_path = r'c:\Users\emirh\OneDrive\Masaüstü\hand_combinations_map.json' # Ham dize olarak tanımla
7
+ try :
8
+ with open (map_file_path , 'r' ) as f :
9
+ hand_map = json .load (f )
10
+ except FileNotFoundError :
11
+ print (f"Hata: Harita dosyası bulunamadı: { map_file_path } " )
12
+ exit ()
13
+ except json .JSONDecodeError :
14
+ print (f"Hata: Harita dosyası geçerli bir JSON değil: { map_file_path } " )
15
+ exit ()
16
+
17
+ cap = cv2 .VideoCapture (0 )
18
+
19
+ # İki eli algılamak için maxHands=2 olarak ayarlandı
20
+ detector = HandDetector (detectionCon = 0.8 , maxHands = 2 )
21
+
22
+ fingerTip = [4 , 8 , 12 , 16 , 20 ]
23
+ last_published_message = None # Son yayınlanan mesajı takip etmek için
24
+
25
+ # 10 parmak için renkler (El 1: 0-4, El 2: 5-9)
26
+ red = (0 , 0 , 255 )
27
+ yellow = (0 , 255 , 255 )
28
+ blue = (255 , 0 , 0 )
29
+ green = (0 , 255 , 0 )
30
+ purple = (255 , 0 , 255 )
31
+ orange = (0 , 165 , 255 )
32
+ pink = (203 , 192 , 255 )
33
+ cyan = (255 , 255 , 0 )
34
+ white = (255 , 255 , 255 )
35
+ lime = (0 , 255 , 127 )
36
+
37
+ colors = [red , yellow , blue , green , purple , # El 1 renkleri
38
+ orange , pink , cyan , white , lime ] # El 2 renkleri
39
+
40
+ while cap .isOpened ():
41
+ success , img = cap .read ()
42
+ if not success :
43
+ print ("Kamera okunamadı." )
44
+ break
45
+
46
+ # Elleri bul (en fazla 2 el)
47
+ hands , img = detector .findHands (img )
48
+
49
+ currentHandsData = ["" ] * 2 # Bu frame'deki el verilerini tutmak için
50
+ currentHandTypes = ["" ] * 2 # Bu frame'deki el tiplerini tutmak için
51
+ num_hands = len (hands )
52
+
53
+ # Algılanan her el için döngü
54
+ for hand_idx , hand in enumerate (hands ):
55
+ lmList = hand ['lmList' ]
56
+ handType = hand ['type' ]
57
+ currentHandTypes [hand_idx ] = handType # El tipini sakla
58
+
59
+ # Mevcut el için parmak değerlerini hesapla
60
+ current_fingerVal = [0 ] * 5
61
+
62
+ # Başparmak (Thumb)
63
+ if handType == "Right" :
64
+ if lmList [fingerTip [0 ]][0 ] > lmList [fingerTip [0 ] - 1 ][0 ]:
65
+ current_fingerVal [0 ] = 1
66
+ else :
67
+ current_fingerVal [0 ] = 0
68
+ else : # Left hand
69
+ if lmList [fingerTip [0 ]][0 ] < lmList [fingerTip [0 ] - 1 ][0 ]:
70
+ current_fingerVal [0 ] = 1
71
+ else :
72
+ current_fingerVal [0 ] = 0
73
+
74
+ # Diğer 4 parmak
75
+ for i in range (1 , 5 ):
76
+ if lmList [fingerTip [i ]][1 ] < lmList [fingerTip [i ] - 2 ][1 ]:
77
+ current_fingerVal [i ] = 1
78
+ else :
79
+ current_fingerVal [i ] = 0
80
+
81
+ # İşaretleri çiz (her parmak için ayrı renk)
82
+ for i in range (5 ):
83
+ if current_fingerVal [i ] == 1 :
84
+ # El indeksine göre renk seçimi (hand_idx * 5 + i)
85
+ color_index = hand_idx * 5 + i
86
+ cv2 .circle (img , (lmList [fingerTip [i ]][0 ], lmList [fingerTip [i ]][1 ]), 15 ,
87
+ colors [color_index ], cv2 .FILLED )
88
+
89
+ # Mevcut elin parmak durumunu string'e çevir
90
+ strVal = '' .join (map (str , current_fingerVal ))
91
+ currentHandsData [hand_idx ] = strVal # Mevcut frame verisine ekle
92
+
93
+ # --- El işleme döngüsü bitti, şimdi haritalama ve yayınlama ---
94
+
95
+ message_to_publish = None
96
+
97
+ if num_hands == 1 :
98
+ hand_type = currentHandTypes [0 ]
99
+ str_val = currentHandsData [0 ]
100
+ message_to_publish = hand_map .get (hand_type , {}).get (str_val )
101
+ elif num_hands == 2 :
102
+ hand0_type = currentHandTypes [0 ]
103
+ hand0_str = currentHandsData [0 ]
104
+ hand1_type = currentHandTypes [1 ]
105
+ hand1_str = currentHandsData [1 ]
106
+
107
+ # Sol ve sağ eli belirle
108
+ left_str = None
109
+ right_str = None
110
+ if hand0_type == 'Left' and hand1_type == 'Right' :
111
+ left_str = hand0_str
112
+ right_str = hand1_str
113
+ elif hand0_type == 'Right' and hand1_type == 'Left' :
114
+ left_str = hand1_str
115
+ right_str = hand0_str
116
+
117
+ if left_str is not None and right_str is not None :
118
+ if left_str == right_str :
119
+ lookup_key = f"{ left_str } _{ left_str } "
120
+ message_to_publish = hand_map .get ("Same" , {}).get (lookup_key )
121
+ else :
122
+ lookup_key = f"{ left_str } _{ right_str } "
123
+ message_to_publish = hand_map .get ("Combined" , {}).get (lookup_key )
124
+
125
+ # Yayınlama Mantığı
126
+ if message_to_publish is not None :
127
+ if message_to_publish != last_published_message :
128
+ print (f"Durum: { message_to_publish } " )
129
+ last_published_message = message_to_publish
130
+ elif num_hands == 0 : # Eller kaybolduysa
131
+ if last_published_message is not None :
132
+ print ("Durum: El algılanmadı" )
133
+ last_published_message = None # Son mesajı sıfırla
134
+
135
+ cv2 .imshow ("Image" , img )
136
+ key = cv2 .waitKey (1 )
137
+ if key == ord ('q' ):
138
+ break
139
+
140
+ cap .release ()
141
+ cv2 .destroyAllWindows ()
0 commit comments