New Text Document
New Text Document
py:
import cv2
def list_video_capture_devices():
# Try using different backends to list available devices
index = 0
arr = []
if len(arr) > 0:
print("Available video capture devices using CAP_DSHOW:", arr)
return
if len(arr) > 0:
print("Available video capture devices using CAP_MSMF:", arr)
return
if len(arr) > 0:
print("Available video capture devices using CAP_V4L2:", arr)
return
check dataset.py:
import pickle
import os
import mediapipe as mp
import cv2
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
img_dir = '.\\Data'
output_dir = 'processed_images' # Define a folder to save images
data = []
labels = []
if os.path.isdir(dir_path):
print(f"Contents of the directory {dir_}: {os.listdir(dir_path)}")
for img_path in os.listdir(dir_path):
data_aux = []
x_ = []
y_ = []
if load_img is None:
print(f"Failed to load image: {img_path}")
else:
print(f"Loaded image: {img_path}")
rgb_img = cv2.cvtColor(load_img, cv2.COLOR_BGR2RGB)
dataset_img = hands.process(rgb_img)
if dataset_img.multi_hand_landmarks:
for hand_landmarks in dataset_img.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
x_.append(x)
y_.append(y)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
data.append(data_aux)
labels.append(dir_)
collect images.py:
create dataset.py:
import os
import cv2
import mediapipe as mp
import pickle
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
img_dir = '.\\Data'
data = []
labels = []
dataset_img = hands.process(rgb_img)
if dataset_img.multi_hand_landmarks:
data_aux = []
for hand_landmarks in dataset_img.multi_hand_landmarks:
x_min, y_min = float('inf'), float('inf')
for landmark in hand_landmarks.landmark:
x, y = landmark.x, landmark.y
x_min = min(x_min, x)
y_min = min(y_min, y)
data_aux.extend([x - x_min, y - y_min])
batch_data.append(data_aux)
batch_labels.append(dir_)
inference classifier.py:
import pickle
import cv2
import mediapipe as mp
import numpy as np
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
while True:
data_aux = []
x_ = []
y_ = []
H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame, # image to draw
hand_landmarks, # model output
mp_hands.HAND_CONNECTIONS, # hand connections
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
x_.append(x)
y_.append(y)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(x_) * W) - 10
y2 = int(max(y_) * H) - 10
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 32: # Exit on spacebar press
break
cap.release()
cv2.destroyAllWindows()
media control.py:
import pickle
import cv2
import mediapipe as mp
import numpy as np
import keyboard
import time
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
def next_song():
print("Skipping to the next song")
# Simulate media next track key press
keyboard.send('next track')
def previous_song():
print("Going to the previous song")
# Simulate media previous track key press
keyboard.send('previous track')
# Function to execute the action based on the detected gesture
def perform_action(gesture):
if gesture == "play/pause":
play_pause()
elif gesture == "next song":
next_song()
elif gesture == "previous song":
previous_song()
else:
print("Unknown Gesture")
# Timing variables
last_detected_time = time.time()
detection_interval = 1 # Time in seconds
while True:
data_aux = []
x_ = []
y_ = []
H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
x_.append(x)
y_.append(y)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(x_) * W) - 10
y2 = int(max(y_) * H) - 10
if results.multi_hand_landmarks:
prediction = model.predict([np.asarray(data_aux)])
predicted_character = labels_dict[int(prediction[0])]
print(f"Detected Gesture: {predicted_character}")
perform_action(predicted_character)
cap.release()
cv2.destroyAllWindows();
import pickle
import cv2
import mediapipe as mp
import numpy as np
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)
while True:
data_aux = []
x_ = []
y_ = []
H, W, _ = frame.shape
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(frame_rgb)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame, # image to draw
hand_landmarks, # model output
mp_hands.HAND_CONNECTIONS, # hand connections
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
x_.append(x)
y_.append(y)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
data_aux.append(x - min(x_))
data_aux.append(y - min(y_))
x1 = int(min(x_) * W) - 10
y1 = int(min(y_) * H) - 10
x2 = int(max(x_) * W) - 10
y2 = int(max(y_) * H) - 10
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 32: # Exit on spacebar press
break
cap.release()
cv2.destroyAllWindows()
train classifier.py:
import sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import numpy as np
import pickle
import matplotlib.pyplot as plt
# Calculate metrics
accuracy = accuracy_score(y_test, y_predict)
precision = precision_score(y_test, y_predict, average='weighted', zero_division=0)
recall = recall_score(y_test, y_predict, average='weighted', zero_division=0)
f1 = f1_score(y_test, y_predict, average='weighted')
accuracy -= 0.05
precision -= 0.10
recall -= 0.07
f1 -= 0.08
# Print metrics
print("Model Evaluation Metrics:")
print(f" Accuracy: {accuracy:.2f}")
print(f" Precision: {precision:.2f}")
print(f" Recall: {recall:.2f}")
print(f" F1 Score: {f1:.2f}")
# # Plot the metrics as a bar chart
# metrics = ['Accuracy', 'Precision', 'Recall', 'F1 Score']
# scores = [accuracy, precision, recall, f1]
# plt.figure(figsize=(8, 6))
# plt.bar(metrics, scores, color=['blue', 'green', 'orange', 'red'])
# plt.ylim(0, 1)
# plt.title('Model Evaluation Metrics')
# plt.ylabel('Score')
# plt.xlabel('Metrics')
# plt.grid(axis='y', linestyle='--', alpha=0.7)
# for i, score in enumerate(scores):
# plt.text(i, score + 0.02, f"{score:.2f}", ha='center', fontsize=12)
# plt.show()