-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandDetect.py
139 lines (109 loc) · 4.68 KB
/
HandDetect.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
130
131
132
133
134
135
136
137
138
139
import mediapipe as mp
import cv2
import torch
import math
import pyrealsense2 as rs
import numpy as np
import os
import time
class RealsenseCamera():
def __init__(self):
self.pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
self.pipeline.start(config)
self.center = [0, 0, 0]
self.xc = 0
self.yc = 0
self.zc = 0
self.yaw_angle = 0
self.pitch_angle = 0
def pixel2point(self, color_frame, depth_frame, u, v):
color_intrinsics = color_frame.profile.as_video_stream_profile().intrinsics
point = [u, v]
depth = depth_frame.get_distance(int(point[0]/2), int(point[1]/2))
point = np.append(point, depth)
if depth != 0:
x = point[0]
y = point[1]
z = point[2]
x, y, z = rs.rs2_deproject_pixel_to_point(color_intrinsics, [x, y], z)
self.center = [x, y, z]
return self.center
def get_images(self):
align_to = rs.stream.color
align = rs.align(align_to)
frames = self.pipeline.wait_for_frames()
aligned_frames = align.process(frames)
self.depth_frame = aligned_frames.get_depth_frame()
self.color_frame = aligned_frames.get_color_frame()
return self.color_frame, self.depth_frame
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
# For webcam input:
hands = mp_hands.Hands(
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
class HandLandMarkDetect():
def __init__(self):
self.camera = RealsenseCamera()
self.hand_point = None
self.hand_pixel = None
self.hand_landmarks = None
self.color_image = None
def get_HandLandMarks(self):
color_frame, depth_frame = self.camera.get_images()
self.color_image = np.asanyarray(color_frame.get_data())
self.color_image = cv2.cvtColor(cv2.flip(self.color_image, 1), cv2.COLOR_BGR2RGB)
image_width, image_height = self.color_image.shape[0], self.color_image.shape[1]
results = hands.process(self.color_image)
self.color_image = cv2.cvtColor(self.color_image, cv2.COLOR_RGB2BGR)
# print('0=0=0=0=0', self.color_image)
hand_point, hand_pixel = None, None
if results.multi_hand_landmarks:
self.hand_landmarks = results.multi_hand_landmarks
for hand_landmarks in results.multi_hand_landmarks:
hand_point = {
0: [],
1: [],
2: [],
3: [],
4: [],
5: []
}
hand_pixel = {
0: [],
1: [],
2: [],
3: [],
4: [],
5: []
}
hand_x = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].x * image_width
hand_y = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].y * image_height
hand_z = hand_landmarks.landmark[mp_hands.HandLandmark.WRIST].z
hand_pixel[0].append([hand_x, hand_y, hand_z])
hand_point_xyz = self.camera.pixel2point(color_frame, depth_frame, hand_x, hand_y)
hand_point[0].append(hand_point_xyz)
count = 0
landmark_idx = 1
for index in range(1, 21):
hand_x = hand_landmarks.landmark[index].x * image_width
hand_y = hand_landmarks.landmark[index].y * image_height
hand_z = hand_landmarks.landmark[index].z
hand_pixel[landmark_idx].append([hand_x, hand_y, hand_z])
hand_point_xyz = self.camera.pixel2point(color_frame,depth_frame, hand_x, hand_y)
hand_point[landmark_idx].append(hand_point_xyz)
count = count + 1
if count == 4:
count = 0
landmark_idx = landmark_idx + 1
self.hand_point, self.hand_pixel = hand_point, hand_pixel
return self.hand_point, self.hand_pixel
def show_hand(self):
if self.hand_landmarks:
for hand_landmark in self.hand_landmarks:
mp_drawing.draw_landmarks(self.color_image, hand_landmark, mp_hands.HAND_CONNECTIONS)
cv2.imshow('robotCamera', self.color_image)
cv2.waitKey(5)