Added bounding box drawing to video capture

This commit is contained in:
Paolo Tagliaferri 2018-01-28 19:40:26 +00:00 committed by Brandon Amos
parent 80ca76eea5
commit c1d800f657
1 changed files with 13 additions and 7 deletions

View File

@ -99,7 +99,7 @@ def getRep(bgrImg):
time.time() - start)) time.time() - start))
# print (reps) # print (reps)
return reps return (reps,bb)
def infer(img, args): def infer(img, args):
@ -109,7 +109,9 @@ def infer(img, args):
else: else:
(le, clf) = pickle.load(f, encoding='latin1') # le - label and clf - classifer (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 = [] persons = []
confidences = [] confidences = []
for rep in reps: for rep in reps:
@ -135,7 +137,7 @@ def infer(img, args):
dist = np.linalg.norm(rep - clf.means_[maxI]) dist = np.linalg.norm(rep - clf.means_[maxI])
print(" + Distance from the mean: {}".format(dist)) print(" + Distance from the mean: {}".format(dist))
pass pass
return (persons, confidences) return (persons, confidences ,bbs)
if __name__ == '__main__': if __name__ == '__main__':
@ -188,7 +190,7 @@ if __name__ == '__main__':
confidenceList = [] confidenceList = []
while True: while True:
ret, frame = video_capture.read() ret, frame = video_capture.read()
persons, confidences = infer(frame, args) persons, confidences, bbs = infer(frame, args)
print ("P: " + str(persons) + " C: " + str(confidences)) print ("P: " + str(persons) + " C: " + str(confidences))
try: try:
# append with two floating point precision # 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. if c <= args.threshold: # 0.5 is kept as threshold for known face.
persons[i] = "_unknown" persons[i] = "_unknown"
# Print the person name and conf value on the frame # Print the person name and conf value on the frame next to the person
cv2.putText(frame, "P: {} C: {}".format(persons, confidences), # Also print the bounding box
(50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) 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) cv2.imshow('', frame)
# quit the program on the press of key 'q' # quit the program on the press of key 'q'
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):