From c1d800f657cf90c2c48692e9294c6b4ce4290816 Mon Sep 17 00:00:00 2001 From: Paolo Tagliaferri Date: Sun, 28 Jan 2018 19:40:26 +0000 Subject: [PATCH] Added bounding box drawing to video capture --- demos/classifier_webcam.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/demos/classifier_webcam.py b/demos/classifier_webcam.py index 122bfb6..c622243 100755 --- a/demos/classifier_webcam.py +++ b/demos/classifier_webcam.py @@ -99,7 +99,7 @@ def getRep(bgrImg): time.time() - start)) # print (reps) - return reps + return (reps,bb) def infer(img, args): @@ -109,7 +109,9 @@ def infer(img, args): else: (le, clf) = pickle.load(f, encoding='latin1') # le - label and clf - classifer - reps = getRep(img) + repsAndBBs = getRep(img) + reps = repsAndBBs[0] + bbs = repsAndBBs[1] persons = [] confidences = [] for rep in reps: @@ -135,7 +137,7 @@ def infer(img, args): dist = np.linalg.norm(rep - clf.means_[maxI]) print(" + Distance from the mean: {}".format(dist)) pass - return (persons, confidences) + return (persons, confidences ,bbs) if __name__ == '__main__': @@ -188,7 +190,7 @@ if __name__ == '__main__': confidenceList = [] while True: ret, frame = video_capture.read() - persons, confidences = infer(frame, args) + persons, confidences, bbs = infer(frame, args) print ("P: " + str(persons) + " C: " + str(confidences)) try: # append with two floating point precision @@ -202,9 +204,13 @@ if __name__ == '__main__': if c <= args.threshold: # 0.5 is kept as threshold for known face. persons[i] = "_unknown" - # Print the person name and conf value on the frame - cv2.putText(frame, "P: {} C: {}".format(persons, confidences), - (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) + # Print the person name and conf value on the frame next to the person + # Also print the bounding box + for idx,person in enumerate(persons): + cv2.rectangle(frame, (bbs[idx].left(), bbs[idx].top()), (bbs[idx].right(), bbs[idx].bottom()), (0, 255, 0), 2) + cv2.putText(frame, "{} @{:.2f}".format(person, confidences[idx]), + (bbs[idx].left(), bbs[idx].bottom()+20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) + cv2.imshow('', frame) # quit the program on the press of key 'q' if cv2.waitKey(1) & 0xFF == ord('q'):