Files changed (1) hide show
  1. app.py +338 -110
app.py CHANGED
@@ -1,115 +1,343 @@
1
- from ultralytics import YOLO
2
- import random
3
- import streamlit as st
4
- from PIL import Image
5
- import os
6
- import gdown
7
-
8
- def extract_user_input(image=None):
9
-
10
-
11
- id = "1e2pHejcp96q-NT9VmIDEY_hCA-tiFNef"
12
- output = "trained_model.pt"
13
- gdown.download(id = id, output = output, quiet=False)
14
-
15
- model = YOLO(output)
16
-
17
- result = model.predict(image)
18
-
19
- names = model.names
20
- det_cls = []
21
- for r in result:
22
- for c in r.boxes.cls:
23
- det_cls.append(names[int(c)])
24
-
25
- return det_cls[0]
26
-
27
-
28
- def ai_choice():
29
- choice = random.randint(0,1000000000)
30
-
31
- if choice <=100000:
32
- choice = "Rock"
33
- elif choice >101 and choice <=1000000:
34
- choice = "Paper"
35
- else:
36
- choice = "Scissors"
37
- return choice
38
-
39
- def winner(user_input,ai_input):
40
-
41
- if user_input == "Rock":
42
- if ai_input == "Scissors":
43
- return "Player 1"
44
- elif ai_input == "Paper":
45
- return "AI"
46
- else:
47
- return "Draw!!!"
48
- if user_input == "Paper":
49
- if ai_input == "Rock":
50
- return "Player 1"
51
- elif ai_input == "Scissors":
52
- return "AI"
53
- else:
54
- return "Draw!!!"
55
- if user_input == "Scissors":
56
- if ai_input == "Rock":
57
- return "AI"
58
- elif ai_input == "Paper":
59
- return "Player 1"
60
- else:
61
- return "Draw!!!"
62
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  def main():
65
- col1, col2 = st.columns(2)
66
- with st.container():
67
- st.header("Welcome to the Computer Vison powered Rock-Paper-Scissors Game!")
68
- st.caption("developed by Indranil Bhattacharyya")
69
-
70
- player_name = st.text_input("Please enter your name, Player!: ")
71
-
72
- image = st.camera_input("Please upload your image: ")
73
-
74
- if image:
75
- st.image(image)
76
-
77
- im = Image.open(image)
78
- rgb_im = im.convert("RGB")
79
-
80
- # exporting the image
81
- rgb_im.save("user_input.jpg")
82
-
83
-
84
- user_input = extract_user_input("user_input.jpg")
85
- user_input_front_end = player_name + " has selected: " + user_input
86
-
87
- st.code(user_input_front_end)
88
- ai_choice_var = ai_choice()
89
- ai_choice_front_end = "AI has selected: " + ai_choice_var
90
- st.code(ai_choice_front_end)
91
- if len(user_input)>0:
92
- winner_name = winner(user_input,ai_choice_var)
93
- if len(winner_name)!=0:
94
- with st.container():
95
- if winner_name == "Player 1":
96
-
97
- winner_front_end = player_name + " Wins!!!"
98
-
99
- st.success(winner_front_end)
100
- else:
101
- if winner_name == "AI":
102
- winner_front_end = winner_name + " Wins!!!"
103
- else:
104
- winner_front_end = "It's a " + winner_name
105
-
106
- st.error(winner_front_end)
107
- else:
108
- winner_name = ''
109
-
110
-
111
 
112
  if __name__ == "__main__":
113
- main()
114
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
 
1
+ import cv2
2
+ import mediapipe as mp
3
+ import pyautogui
4
+ import math
5
+ import time
6
+
7
+
8
+ class HandMouse:
9
+ def __init__(self):
10
+ self.hands = mp.solutions.hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)
11
+ self.mp_draw = mp.solutions.drawing_utils
12
+ self.prev_x, self.prev_y = 0, 0
13
+ self.smooth_factor = 5
14
+ self.last_click_time = 0
15
+ self.last_scroll_time = 0
16
+
17
+
18
+ def find_hands(self, img):
19
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
20
+ results = self.hands.process(img_rgb)
21
+ return results
22
+
23
+
24
+ def get_landmarks(self, img, results):
25
+ lm_list = {}
26
+ if results.multi_hand_landmarks:
27
+ for hand_landmarks in results.multi_hand_landmarks:
28
+ for id, lm in enumerate(hand_landmarks.landmark):
29
+ h, w, _ = img.shape
30
+ cx, cy = int(lm.x * w), int(lm.y * h)
31
+ lm_list[id] = (cx, cy)
32
+ self.mp_draw.draw_landmarks(img, hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS)
33
+ return lm_list
34
+
35
+
36
+ def move_mouse(self, img, lm_list):
37
+ if 8 in lm_list: # Index finger tip
38
+ index_tip = lm_list[8]
39
+ screen_width, screen_height = pyautogui.size()
40
+ img_width, img_height = img.shape[1], img.shape[0]
41
+ x, y = index_tip
42
+
43
+
44
+ # Map coordinates to screen size
45
+ screen_x = int((x / img_width) * screen_width)
46
+ screen_y = int((y / img_height) * screen_height)
47
+
48
+
49
+ # Smooth mouse movement
50
+ smoothed_x = self.prev_x + (screen_x - self.prev_x) / self.smooth_factor
51
+ smoothed_y = self.prev_y + (screen_y - self.prev_y) / self.smooth_factor
52
+
53
+
54
+ pyautogui.moveTo(smoothed_x, smoothed_y)
55
+ self.prev_x, self.prev_y = smoothed_x, smoothed_y
56
+
57
+
58
+ def left_click(self, lm_list):
59
+ if 4 in lm_list and 8 in lm_list: # Thumb and index finger
60
+ thumb_tip = lm_list[4]
61
+ index_tip = lm_list[8]
62
+ distance = math.hypot(thumb_tip[0] - index_tip[0], thumb_tip[1] - index_tip[1])
63
+
64
+
65
+ if distance < 30: # Click threshold
66
+ current_time = time.time()
67
+ if current_time - self.last_click_time > 0.5: # Prevent multiple clicks
68
+ pyautogui.click()
69
+ self.last_click_time = current_time
70
+
71
+
72
+ def right_click(self, lm_list):
73
+ if 8 in lm_list and 12 in lm_list: # Index and middle finger
74
+ index_tip = lm_list[8]
75
+ middle_tip = lm_list[12]
76
+ distance = math.hypot(index_tip[0] - middle_tip[0], index_tip[1] - middle_tip[1])
77
+
78
+
79
+ if distance < 50: # Right-click threshold
80
+ pyautogui.click(button='right')
81
+ time.sleep(0.5)
82
+
83
+
84
+ def scroll(self, lm_list):
85
+ if 12 in lm_list and 0 in lm_list: # Middle finger
86
+ middle_tip = lm_list[12]
87
+ palm_base = lm_list[0]
88
+
89
+
90
+ # Calculate vertical distance
91
+ scroll_distance = middle_tip[1] - palm_base[1]
92
+ current_time = time.time()
93
+
94
+
95
+ if abs(scroll_distance) > 30 and current_time - self.last_scroll_time > 0.1:
96
+ if scroll_distance > 0:
97
+ pyautogui.scroll(-5) # Scroll down
98
+ else:
99
+ pyautogui.scroll(5) # Scroll up
100
+ self.last_scroll_time = current_time
101
+
102
+
103
+ def copy_content(self, lm_list):
104
+ if 4 in lm_list and 8 in lm_list and 12 in lm_list: # Thumb, index, and middle finger
105
+ thumb_tip = lm_list[4]
106
+ index_tip = lm_list[8]
107
+ middle_tip = lm_list[12]
108
+
109
+
110
+ distance_thumb_index = math.hypot(thumb_tip[0] - index_tip[0], thumb_tip[1] - index_tip[1])
111
+ distance_index_middle = math.hypot(index_tip[0] - middle_tip[0], index_tip[1] - middle_tip[1])
112
+
113
+
114
+ if distance_thumb_index < 30 and distance_index_middle < 30:
115
+ pyautogui.hotkey('ctrl', 'c') # Copy content
116
+ time.sleep(0.5)
117
+
118
+
119
+ def detect_gestures(self, img, lm_list):
120
+ self.move_mouse(img, lm_list)
121
+ self.left_click(lm_list)
122
+ self.right_click(lm_list)
123
+ self.scroll(lm_list)
124
+ self.copy_content(lm_list)
125
+
126
 
127
  def main():
128
+ cap = cv2.VideoCapture(0)
129
+ hand_mouse = HandMouse()
130
+
131
+
132
+ while True:
133
+ success, img = cap.read()
134
+ if not success:
135
+ break
136
+
137
+
138
+ results = hand_mouse.find_hands(img)
139
+ lm_list = hand_mouse.get_landmarks(img, results)
140
+
141
+
142
+ if lm_list:
143
+ hand_mouse.detect_gestures(img, lm_list)
144
+
145
+
146
+ # Show video feed
147
+ cv2.imshow("Hand Tracking Mouse", img)
148
+ if cv2.waitKey(1) & 0xFF == ord('q'):
149
+ break
150
+
151
+
152
+ cap.release()
153
+ cv2.destroyAllWindows()
154
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  if __name__ == "__main__":
157
+ main()
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+ import cv2
176
+
177
+
178
+ ######## destop mouse
179
+ import mediapipe as mp
180
+ import pyautogui
181
+ import math
182
+ import time
183
+
184
+
185
+
186
+
187
+ class HandMouse:
188
+ def __init__(self):
189
+ # Initialize Mediapipe Hand Detection
190
+ self.hands = mp.solutions.hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7)
191
+ self.mp_draw = mp.solutions.drawing_utils
192
+
193
+
194
+ # Smoothing mouse movement
195
+ self.prev_x, self.prev_y = 0, 0
196
+ self.smooth_factor = 5
197
+
198
+
199
+ # Timers to prevent multiple actions
200
+ self.last_click_time = 0
201
+ self.last_right_click_time = 0
202
+ self.last_scroll_time = 0
203
+
204
+
205
+ def find_hands(self, img):
206
+ """Detect hands and return results."""
207
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
208
+ results = self.hands.process(img_rgb)
209
+ return results
210
+
211
+
212
+ def get_landmarks(self, img, results):
213
+ """Extract hand landmarks."""
214
+ lm_list = {}
215
+ if results.multi_hand_landmarks:
216
+ for hand_landmarks in results.multi_hand_landmarks:
217
+ for id, lm in enumerate(hand_landmarks.landmark):
218
+ h, w, _ = img.shape
219
+ cx, cy = int(lm.x * w), int(lm.y * h)
220
+ lm_list[id] = (cx, cy)
221
+ self.mp_draw.draw_landmarks(img, hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS)
222
+ return lm_list
223
+
224
+
225
+ def move_mouse(self, img, lm_list):
226
+ """Move the mouse using the index finger."""
227
+ if 8 in lm_list: # Index finger tip
228
+ index_tip = lm_list[8]
229
+ screen_width, screen_height = pyautogui.size()
230
+ img_width, img_height = img.shape[1], img.shape[0]
231
+ x, y = index_tip
232
+
233
+
234
+ # Map to screen size
235
+ screen_x = int((x / img_width) * screen_width)
236
+ screen_y = int((y / img_height) * screen_height)
237
+
238
+
239
+ # Smooth the movement
240
+ smoothed_x = self.prev_x + (screen_x - self.prev_x) / self.smooth_factor
241
+ smoothed_y = self.prev_y + (screen_y - self.prev_y) / self.smooth_factor
242
+
243
+
244
+ pyautogui.moveTo(smoothed_x, smoothed_y)
245
+ self.prev_x, self.prev_y = smoothed_x, smoothed_y
246
+
247
+
248
+ def left_click(self, lm_list):
249
+ """Perform a left click when thumb and index finger touch."""
250
+ if 4 in lm_list and 8 in lm_list: # Thumb and index finger
251
+ thumb_tip = lm_list[4]
252
+ index_tip = lm_list[8]
253
+ distance = math.hypot(thumb_tip[0] - index_tip[0], thumb_tip[1] - index_tip[1])
254
+
255
+
256
+ if distance < 30: # Click threshold
257
+ current_time = time.time()
258
+ if current_time - self.last_click_time > 0.5: # Prevent multiple clicks
259
+ pyautogui.click()
260
+ self.last_click_time = current_time
261
+
262
+
263
+ def right_click(self, lm_list):
264
+ """Perform a right click when the thumb finger is raised."""
265
+ if 4 in lm_list and 0 in lm_list: # Thumb and palm base
266
+ thumb_tip = lm_list[4]
267
+ palm_base = lm_list[0]
268
+ distance = math.hypot(thumb_tip[0] - palm_base[0], thumb_tip[1] - palm_base[1])
269
+
270
+
271
+ if distance > 50: # Right-click threshold
272
+ current_time = time.time()
273
+ if current_time - self.last_right_click_time > 0.5: # Prevent multiple clicks
274
+ pyautogui.click(button='right')
275
+ self.last_right_click_time = current_time
276
+
277
+
278
+ def scroll(self, lm_list):
279
+ """Scroll up or down using the middle finger."""
280
+ if 12 in lm_list and 0 in lm_list: # Middle finger and palm base
281
+ middle_tip = lm_list[12]
282
+ palm_base = lm_list[0]
283
+
284
+
285
+ # Calculate vertical distance
286
+ scroll_distance = middle_tip[1] - palm_base[1]
287
+ current_time = time.time()
288
+
289
+
290
+ if abs(scroll_distance) > 30 and current_time - self.last_scroll_time > 0.1:
291
+ if scroll_distance > 0:
292
+ pyautogui.scroll(-5) # Scroll down
293
+ else:
294
+ pyautogui.scroll(5) # Scroll up
295
+ self.last_scroll_time = current_time
296
+
297
+
298
+ def detect_gestures(self, img, lm_list):
299
+ """Detect gestures and perform actions."""
300
+ self.move_mouse(img, lm_list)
301
+ self.left_click(lm_list)
302
+ self.right_click(lm_list)
303
+ self.scroll(lm_list)
304
+
305
+
306
+
307
+
308
+ def main():
309
+ cap = cv2.VideoCapture(0)
310
+ hand_mouse = HandMouse()
311
+
312
+
313
+ while True:
314
+ success, img = cap.read()
315
+ if not success:
316
+ break
317
+
318
+
319
+ results = hand_mouse.find_hands(img)
320
+ lm_list = hand_mouse.get_landmarks(img, results)
321
+
322
+
323
+ if lm_list:
324
+ hand_mouse.detect_gestures(img, lm_list)
325
+
326
+
327
+ # Show video feed
328
+ cv2.imshow("Hand Tracking Mouse", img)
329
+ if cv2.waitKey(1) & 0xFF == ord('q'):
330
+ break
331
+
332
+
333
+ cap.release()
334
+ cv2.destroyAllWindows()
335
+
336
+
337
+
338
+
339
+ if __name__ == "__main__":
340
+ main()
341
+
342
+
343