Skip to content

Instantly share code, notes, and snippets.

@ashish-roopan
Created July 18, 2019 12:53
Show Gist options
  • Save ashish-roopan/9e10bb08d8d21838f586e2a8e5ba0a1b to your computer and use it in GitHub Desktop.
Save ashish-roopan/9e10bb08d8d21838f586e2a8e5ba0a1b to your computer and use it in GitHub Desktop.
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import dlib
import cv2
from face_recognizer import FaceRecognizer
import time
trackers = []
labels = []
cap=cv2.VideoCapture(0)
fce=FaceRecognizer()
fps = FPS().start()
while True:
(ret, frame) = cap.read()
if frame is None:
break
frame = imutils.resize(frame, width=600)
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if len(trackers) == 0:
rect=[]
img,rects,names=fce.get_faces(frame,rect)
print(rects)
print(names)
# loop over the detections
for i in range(len(rects)):
label = 'tracking '+names[i]
# compute the (x, y)-coordinates of the bounding box
# for the object
(startX, startY, endX, endY) = rects[i]
# construct a dlib rectangle object from the bounding
# box coordinates and start the correlation tracker
t = dlib.correlation_tracker()
rect = dlib.rectangle(startX, startY, endX, endY)
t.start_track(rgb, rect)
# update our set of trackers and corresponding class
# labels
labels.append(label)
trackers.append(t)
# grab the corresponding class label for the detection
# and draw the bounding box
cv2.rectangle(frame, (startX, startY), (endX, endY),
(0, 255, 0), 2)
cv2.putText(frame, label, (startX, startY - 15),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
# otherwise, we've already performed detection so let's track
# multiple objects
else:
# loop over each of the trackers
for (t, l) in zip(trackers, labels):
# update the tracker and grab the position of the tracked
# object
t.update(rgb)
pos = t.get_position()
# unpack the position object
startX = int(pos.left())
startY = int(pos.top())
endX = int(pos.right())
endY = int(pos.bottom())
# draw the bounding box from the correlation object tracker
cv2.rectangle(frame, (startX, startY), (endX, endY),(0, 255, 0), 2)
cv2.putText(frame, l, (startX, startY - 15),cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2)
cv2.imshow("Frame", frame)
cv2.imshow('face',img)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# update the FPS counter
fps.update()
# stop the timer and display FPS information
fps.stop()
print("[INFO] elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment