-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandpointer.py
110 lines (80 loc) · 2.79 KB
/
handpointer.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
from utils import detector_utils as detector_utils
import cv2
import tensorflow as tf
import datetime
import argparse
import numpy as np
from collections import deque
pts = deque(maxlen = 64)
# load the model
detection_graph, sess = detector_utils.load_inference_graph()
score_thresh = 0.3
fps = 1
# 0 for internal web cam
video_source = 0
# caputer frame
cap = cv2.VideoCapture(video_source)
start_time = datetime.datetime.now()
num_frames = 0
# get dimenstions
im_width, im_height = (cap.get(3), cap.get(4))
# max number of hands we want to detect/track
num_hands_detect = 1
cv2.namedWindow('Hand Pointer', cv2.WINDOW_NORMAL)
while True:
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
ret, image_np = cap.read()
image_np = cv2.flip(image_np, 1)
try:
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
except:
print("Error converting to RGB")
# actual detection
boxes, scores = detector_utils.detect_objects(
image_np, detection_graph, sess)
# draw bounding boxes
# detector_utils.draw_box_on_image(
# num_hands_detect, score_thresh, scores, boxes, im_width, im_height, image_np)
# Draw Pointer trails
# If hand detected
if(scores[0] > score_thresh):
# calculate centroid
(left, right, top, bottom) = (boxes[0][1] * im_width, boxes[0][3] * im_width, boxes[0][0] * im_height, boxes[0][2] * im_height)
# calculate centroid points
y = (bottom +top)/2
x = (right + left)/2
# For detecting a fist OR open hand
fist = (top-bottom)/(left-right)
# use y insted of top to get the pointer in center of hand (on plam)
center = (int(x), int(top))
cv2.circle(image_np, center, 5, (255, 25, 0), 10)
# if it is not a fist OR expanded hand
if fist>1.2:
pts.appendleft(center)
else:
pts.appendleft(None)
else:
pts.appendleft(None)
# Draw trails
for i in range (1,len(pts)):
if pts[i-1]is None or pts[i] is None:
continue
thick = int(np.sqrt(len(pts) / float(i + 1)) * 2.5)
cv2.line(image_np, pts[i-1],pts[i],(255,0,0),thick)
# Calculate Frames per second (FPS)
num_frames += 1
elapsed_time = (datetime.datetime.now() -
start_time).total_seconds()
fps = num_frames / elapsed_time
# Display FPS on frame
if (fps > 0):
detector_utils.draw_fps_on_image(
"FPS : " + str(int(fps)), image_np)
cv2.imshow('Hand Pointer', cv2.cvtColor(
image_np, cv2.COLOR_RGB2BGR))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
# cleanup the camera and close any open windows
cap.release()
cv2.destroyAllWindows()