Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the test camera script and optimize the create dataset. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion collect_imgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
number_of_classes = 3
dataset_size = 100

cap = cv2.VideoCapture(2)
cap = cv2.VideoCapture(0)
for j in range(number_of_classes):
if not os.path.exists(os.path.join(DATA_DIR, str(j))):
os.makedirs(os.path.join(DATA_DIR, str(j)))
Expand Down
76 changes: 48 additions & 28 deletions create_dataset.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
import os
import pickle

import mediapipe as mp
import cv2
import mediapipe as mp
import matplotlib.pyplot as plt


# Initialize MediaPipe Hands
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)

# Define data directory
DATA_DIR = './data'

# Initialize lists for data and labels
data = []
labels = []
for dir_ in os.listdir(DATA_DIR):
for img_path in os.listdir(os.path.join(DATA_DIR, dir_)):
data_aux = []

x_ = []
y_ = []
# Function to process images
def process_image(img_path):
data_aux = []
x_ = []
y_ = []

img = cv2.imread(os.path.join(DATA_DIR, dir_, img_path))
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.imread(img_path)
if img is None:
return None, None
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

results = hands.process(img_rgb)
if results.multi_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
results = hands.process(img_rgb)
if results.multi_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
x_.append(x)
y_.append(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_))

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_))
return data_aux, True
return None, False

data.append(data_aux)
# Process each image in the data directory
for dir_ in os.listdir(DATA_DIR):
class_dir = os.path.join(DATA_DIR, dir_)
if not os.path.isdir(class_dir):
continue

for img_name in os.listdir(class_dir):
img_path = os.path.join(class_dir, img_name)
processed_data, success = process_image(img_path)

if success:
data.append(processed_data)
labels.append(dir_)

f = open('data.pickle', 'wb')
pickle.dump({'data': data, 'labels': labels}, f)
f.close()
# Save data to a pickle file
with open('data.pickle', 'wb') as f:
pickle.dump({'data': data, 'labels': labels}, f)

# Release resources
hands.close()

print("Data processing completed and saved to 'data.pickle'.")
2 changes: 1 addition & 1 deletion inference_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
model_dict = pickle.load(open('./model.p', 'rb'))
model = model_dict['model']

cap = cv2.VideoCapture(2)
cap = cv2.VideoCapture(0)

mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
Expand Down
12 changes: 12 additions & 0 deletions test_camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import cv2

def test_cameras():
for i in range(5): # Test indices 0 to 4
cap = cv2.VideoCapture(i)
if cap.isOpened():
print(f"Camera {i} is available.")
cap.release()
else:
print(f"Camera {i} is not available.")

test_cameras()