-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (69 loc) · 3.08 KB
/
main.py
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
# Main program for detecting live ASL hand gestures
import cv2
import mediapipe as mp
import pickle
import numpy as np
# Loading the trained model from the pickle file
model_dictionary = pickle.load(open("./model.p", "rb"))
model = model_dictionary["model"]
# Dictionary to map predicted labels to characters (A-Z)
labels_dictionary = {i: chr(65 + i) for i in range(25)}
# Initializing the video capture
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# Initializing MediaPipe Hands module
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
hands = mp_hands.Hands(static_image_mode=True, min_detection_confidence=0.3)
# Continuous loop to process frames from the webcam
while True:
# Lists to store hand landmarks
data_auxiliary = []
xPositions = []
yPositions = []
# Reading a frame from the camera
ret, frame = cap.read()
HEIGHT, WIDTH, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Processing the frame with the Hands model
results = hands.process(frame_rgb)
# Checking if hand landmarks are detected and if only one hand is detected
landmarks = results.multi_hand_landmarks
if landmarks and len(landmarks) <= 1:
# Draws landmarks and connections on the frame
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# Extracts x and y coordinates of hand landmarks
for hand_landmarks in results.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_auxiliary.append(x)
data_auxiliary.append(y)
xPositions.append(x)
yPositions.append(y)
# Calculates bounding box coordinates for the hand region
x1 = int(min(xPositions) * WIDTH) - 10
y1 = int(min(yPositions) * HEIGHT) - 10
x2 = int(max(xPositions) * WIDTH) - 10
y2 = int(max(yPositions) * HEIGHT) - 10
# Makes a character prediction using the trained model
prediction = model.predict([np.asarray(data_auxiliary)])
predicted_character = labels_dictionary[int(prediction[0])]
# Drawing bounding box and predicted character on the frame
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 0), 4)
cv2.putText(frame, predicted_character, (x1, y1 - 10), cv2.FONT_HERSHEY_COMPLEX, 1.3, (0, 0, 0), 3, cv2.LINE_AA)
# Displaying the frame with predictions
cv2.imshow("SignWave", frame)
cv2.waitKey(1)
# Check for the red cancel (X) button click event to exit the loop and close the window
if cv2.getWindowProperty("SignWave", cv2.WND_PROP_VISIBLE) < 1:
break
# Release the camera and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()