From c9f3e333a9e73077947f5c5fe31d0e0fdb1c4c6f Mon Sep 17 00:00:00 2001 From: Joakim Eriksson Date: Wed, 1 Jul 2020 23:09:46 +0200 Subject: [PATCH 1/2] 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 From e9347a8ce7f3eacc772443b5b5c51daaf129c66f Mon Sep 17 00:00:00 2001 From: Joakim Eriksson Date: Thu, 2 Jul 2020 08:07:01 +0200 Subject: [PATCH 2/2] cleaned up webcamera example, added main and refactored drawing of hand and box to functions --- handtracker_webcam.py | 52 ++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/handtracker_webcam.py b/handtracker_webcam.py index 9d342c0..efea474 100644 --- a/handtracker_webcam.py +++ b/handtracker_webcam.py @@ -1,24 +1,15 @@ +# +# Basic example of how to use the handtracker using a web camera as input. +# +# Author: Joakim Eriksson, joakim.eriksson@ri.se +# + 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 - +def draw_hand(frame, kp): + # the points where we go back to the first midpoint of the hand clearpoints = [4,8,12,16,20] lk = None p = 0 @@ -33,10 +24,31 @@ lk = kp[0] p = p + 1 +def draw_box(frame, box): # 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 +if __name__ == '__main__': + 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) + + # Capture video and do the hand-tracking + while True: + ret,frame = cap.read() + image = frame[:,:,::-1] + kp, box = detector(image) + + if kp is not None: + draw_hand(frame, kp) + draw_box(frame, box) + + cv2.imshow('frame',frame) + if cv2.waitKey(1) & 0xFF == ord('q'): + break