From c9f3e333a9e73077947f5c5fe31d0e0fdb1c4c6f Mon Sep 17 00:00:00 2001 From: Joakim Eriksson Date: Wed, 1 Jul 2020 23:09:46 +0200 Subject: [PATCH] added python code for tracking hand on webcamera videofeed --- hand_tracker.py | 4 ++-- handtracker_webcam.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 handtracker_webcam.py diff --git a/hand_tracker.py b/hand_tracker.py index c79c73c..8a9bd42 100644 --- a/hand_tracker.py +++ b/hand_tracker.py @@ -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]) @@ -197,4 +197,4 @@ def __call__(self, img): kp_orig -= pad[::-1] box_orig -= pad[::-1] - return kp_orig, box_orig \ No newline at end of file + return kp_orig, box_orig diff --git a/handtracker_webcam.py b/handtracker_webcam.py new file mode 100644 index 0000000..9d342c0 --- /dev/null +++ b/handtracker_webcam.py @@ -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