prav2020/Computer_vision
0
1import cv2
2import mediapipe as mp
3import gradio as gr
4from collections import deque
5import time
6
7# MediaPipe setup
8mp_hands = mp.solutions.hands
9mp_drawing = mp.solutions.drawing_utils
10hands = mp_hands.Hands(max_num_hands=2, min_detection_confidence=0.7)
11
12prev_wrist_positions = deque(maxlen=10)
13last_sign_time = 0
14cooldown = 1.0
15last_sign = None
16
17bot_responses = {
18 "I love you": "Love you too !",
19 "Hi": "Hello! ๐",
20 "Bye": "Goodbye! ๐",
21 "You": "Yes, you!",
22 "Help": "I'm here to help!"
23}
24
25def fingers_up(hand_landmarks):
26 tips_ids = [4, 8, 12, 16, 20]
27 fingers = []
28
29 if hand_landmarks.landmark[4].x < hand_landmarks.landmark[3].x:
30 fingers.append(1)
31 else:
32 fingers.append(0)
33
34 for tip_id in tips_ids[1:]:
35 if hand_landmarks.landmark[tip_id].y < hand_landmarks.landmark[tip_id-2].y:
36 fingers.append(1)
37 else:
38 fingers.append(0)
39 return fingers
40
41def detect_sign(hand_landmarks):
42 global prev_wrist_positions
43 fingers = fingers_up(hand_landmarks)
44 wrist = hand_landmarks.landmark[0]
45
46 prev_wrist_positions.append(wrist.x)
47 movement = max(prev_wrist_positions) - min(prev_wrist_positions)
48
49 if fingers == [1,1,0,0,1]:
50 return "I love you"
51 elif fingers == [1,0,0,0,0]:
52 return "Help"
53 elif fingers == [0,1,0,0,0]:
54 return "You"
55 elif fingers == [1,1,1,1,1] and movement < 0.03:
56 return "Hi"
57 elif fingers == [1,1,1,1,1] and movement >= 0.03:
58 return "Bye"
59 return None
60
61def process_frame(frame):
62 global last_sign, last_sign_time
63 if frame is None:
64 return None
65
66 frame = cv2.flip(frame, 1)
67 rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
68 results = hands.process(rgb_frame)
69
70 current_time = time.time()
71
72 if results.multi_hand_landmarks:
73 for hand_landmarks in results.multi_hand_landmarks:
74 mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
75 temp_sign = detect_sign(hand_landmarks)
76 if temp_sign and current_time - last_sign_time > cooldown:
77 last_sign = temp_sign
78 last_sign_time = current_time
79 break
80 else:
81 if current_time - last_sign_time > 2:
82 last_sign = None
83
84 display_sign = last_sign if last_sign else "No sign detected"
85 display_bot = bot_responses[last_sign] if last_sign else ""
86
87 cv2.putText(frame, f"Sign: {display_sign}", (10,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
88 if display_bot:
89 cv2.putText(frame, f"Bot: {display_bot}", (10,100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
90
91 return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
92
93# Gradio interface
94iface = gr.Interface(
95 fn=process_frame,
96 inputs=gr.Image(source="webcam", streaming=True),
97 outputs="image",
98 live=True,
99 title="ASL Chatbot",
100 description="Detects ASL signs from webcam and responds with a chatbot-like reply."
101)
102
103if __name__ == "__main__":
104 iface.launch()
105 