Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

added python code for tracking hand on webcamera videofeed #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions hand_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def detect_hand(self, img_norm):

if candidate_detect.shape[0] == 0:
print("No hands found")
return None, None, None
return None, None
# picking the widest suggestion while NMS is not implemented
max_idx = np.argmax(candidate_detect[:, 3])

Expand Down Expand Up @@ -197,4 +197,4 @@ def __call__(self, img):
kp_orig -= pad[::-1]
box_orig -= pad[::-1]

return kp_orig, box_orig
return kp_orig, box_orig
42 changes: 42 additions & 0 deletions handtracker_webcam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cv2
from hand_tracker import HandTracker
import numpy as np

palm_model_path = "./models/palm_detection.tflite"
landmark_model_path = "./models/hand_landmark.tflite"
anchors_path = "./data/anchors.csv"

cap = cv2.VideoCapture(0)

detector = HandTracker(palm_model_path, landmark_model_path, anchors_path,
box_shift=0.2, box_enlarge=1.3)

while True:
ret,frame = cap.read()
image = frame[:,:,::-1]
kp, box = detector(image)

if kp is None:
continue

clearpoints = [4,8,12,16,20]
lk = None
p = 0

# Draw the hand
for keypoint in kp:
if lk is not None:
cv2.line(frame, (int(keypoint[0]),int(keypoint[1])),(int(lk[0]),int(lk[1])), (255,0,255), 2)
lk = keypoint
cv2.circle(frame, (int(keypoint[0]), int(keypoint[1])), 3, (0,255,255), -1)
if p in clearpoints:
lk = kp[0]
p = p + 1

# draw the box
for i in range(0,4):
cv2.line(frame, (int(box[i][0]),int(box[i][1])),(int(box[(i+1)&3][0]),int(box[(i+1)&3][1])), (255,255,255), 2)

cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break