-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_gesture.py
129 lines (106 loc) · 5.16 KB
/
test_gesture.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Tests the hand landmarker feature extraction on a video from the test dataset
import mediapipe as mp
import cv2
import time
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
from mediapipe import solutions
from mediapipe.framework.formats import landmark_pb2
import numpy as np
import pandas as pd
class landmarker_and_result():
def __init__(self, type):
self.result = mp.tasks.vision.HandLandmarkerResult
self.landmarker = mp.tasks.vision.HandLandmarker
self.createLandmarker(type)
def createLandmarker(self, type):
# callback function
def update_result(result: mp.tasks.vision.HandLandmarkerResult, output_image: mp.Image, timestamp_ms: int):
self.result = result
options = mp.tasks.vision.HandLandmarkerOptions(
base_options = mp.tasks.BaseOptions(model_asset_path="hand_landmarker.task"), # path to model
running_mode = mp.tasks.vision.RunningMode.IMAGE, # running on a live stream
num_hands = 1, # track both hands
min_hand_detection_confidence = 0.3, # lower than value to get predictions more often
min_hand_presence_confidence = 0.3, # lower than value to get predictions more often
min_tracking_confidence = 0.3) # lower than value to get predictions more often
# initialize landmarker
self.landmarker = self.landmarker.create_from_options(options)
def detect(self, input):
# Load the input image from an image file.
image = mp.Image.create_from_file(input)
hand_landmarker_result = self.landmarker.detect(image)
return image, hand_landmarker_result
def close(self):
# close landmarker
self.landmarker.close()
def draw_landmarks_on_image(rgb_image, detection_result: mp.tasks.vision.HandLandmarkerResult):
"""Taken from https://github.com/googlesamples/mediapipe/blob/main/examples/hand_landmarker/python/hand_landmarker.ipynb"""
try:
if detection_result.hand_landmarks == []:
return rgb_image
else:
hand_landmarks_list = detection_result.hand_landmarks
handedness_list = detection_result.handedness
annotated_image = np.copy(rgb_image)
# Loop through the detected hands to visualize.
for idx in range(len(hand_landmarks_list)):
hand_landmarks = hand_landmarks_list[idx]
# Draw the hand landmarks.
hand_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
hand_landmarks_proto.landmark.extend([
landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in hand_landmarks])
mp.solutions.drawing_utils.draw_landmarks(
annotated_image,
hand_landmarks_proto,
mp.solutions.hands.HAND_CONNECTIONS,
mp.solutions.drawing_styles.get_default_hand_landmarks_style(),
mp.solutions.drawing_styles.get_default_hand_connections_style())
return annotated_image
except:
return rgb_image
def hand_detection_gesture(folder_path, print_image = False):
img_list = []
keypoint_list = [0] * 37
image_landmarker = landmarker_and_result('IMAGE')
for i in range (1, 38):
curr_keypoints = []
image_path = f"{folder_path}/{i:05d}.jpg"
image, detection_result = image_landmarker.detect(image_path)
hand_landmarks_list = detection_result.hand_world_landmarks
#print(hand_landmarks_list)
if len(hand_landmarks_list) == 0:
for j in range(0, 21):
curr_keypoints.extend([0.0, 0.0, 0.0])
else:
for idx in range(len(hand_landmarks_list)):
hand_landmarks = hand_landmarks_list[idx]
for landmarks in hand_landmarks:
curr_keypoints.extend([landmarks.x,landmarks.y, landmarks.z])
keypoint_list[i-1] = curr_keypoints
if(print_image):
annotated_image = draw_landmarks_on_image(image.numpy_view(), detection_result)
img_list.append(annotated_image)
#cv2.imshow('',annotated_image)
#cv2.waitKey(0)
#print(keypoint_list)
if(print_image):
# Calculate the number of rows and columns in the grid
rows = 5 # you can adjust this based on the layout you want
cols = 8 # you can adjust this based on the layout you want
# Create a blank canvas (white background)
canvas = np.ones((rows * img_list[0].shape[0], cols * img_list[0].shape[1], 3), dtype=np.uint8) * 255
# Populate the canvas with images
for i in range(min(len(img_list), rows * cols)):
row_index = i // cols
col_index = i % cols
y_offset = row_index * img_list[0].shape[0]
x_offset = col_index * img_list[0].shape[1]
canvas[y_offset:y_offset + img_list[0].shape[0], x_offset:x_offset + img_list[0].shape[1]] = img_list[i]
# Display the composite image
cv2.imshow('Composite Image', canvas)
cv2.waitKey(0)
cv2.destroyAllWindows()
return(np.array(keypoint_list))
my_list = hand_detection_gesture('Train/11', False)
print(my_list.shape)