Created
July 19, 2019 06:20
-
-
Save ashish-roopan/b06e040084a3c56b83069a884b449618 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from centroidtracker import CentroidTracker | |
from face_recognizer import FaceRecognizer | |
from imutils.video import FPS | |
import numpy as np | |
import imutils | |
import time | |
import dlib | |
import cv2 | |
class TrackableObject: | |
def __init__(self, objectID, centroid): | |
# store the object ID, then initialize a list of centroids | |
# using the current centroid | |
self.objectID = objectID | |
self.centroids = [centroid] | |
# initialize a boolean used to indicate if the object has | |
# already been counted or not | |
self.counted = False | |
fce=FaceRecognizer() | |
cap=cv2.VideoCapture(2) | |
ct = CentroidTracker(maxDisappeared=70, maxDistance=50) | |
trackers = [] | |
trackableObjects = {} | |
cnt=0 | |
while True: | |
ret,frame = cap.read() | |
if not ret: | |
break | |
frame = imutils.resize(frame, width=500) | |
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
status = "Waiting" | |
rects = [] | |
if cnt%60==0: | |
status = "Detecting" | |
trackers = [] | |
img,boxes=fce.get_faces(frame) | |
for box in boxes: | |
tracker = dlib.correlation_tracker() | |
rect = dlib.rectangle(box[0],box[1],box[2],box[3]) | |
tracker.start_track(rgb, rect) | |
trackers.append(tracker) | |
else: | |
for tracker in trackers: | |
status = "Tracking" | |
# update the tracker and grab the updated position | |
tracker.update(rgb) | |
pos = tracker.get_position() | |
# unpack the position object | |
startX = int(pos.left()) | |
startY = int(pos.top()) | |
endX = int(pos.right()) | |
endY = int(pos.bottom()) | |
rects.append([startX, startY, endX, endY]) | |
#cv2.rectangle(frame,(startX,startY), (endX,endY), (0, 255, 0), 2) | |
print(rects) | |
if rects is not None: | |
for rect in rects: | |
#cv2.putText(frame, , (rect[0],rect[1]), cv2.FONT_HERSHEY_SIMPLEX,0.75, (255, 255, 0), 2) | |
cv2.rectangle(frame,(rect[0],rect[1]), (rect[2],rect[3]), (0, 255, 0), 2) | |
objects = ct.update(rects) | |
for (objectID, centroid) in objects.items(): | |
print( centroid) | |
print(objectID) | |
cv2.putText(frame,str(objectID), (centroid[0],centroid[1]), cv2.FONT_HERSHEY_SIMPLEX,0.75, (255, 255, 0), 2) | |
cv2.imshow('img',img) | |
cv2.imshow('frame',frame) | |
cv2.waitKey(1) | |
cnt+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment