def VJFindFace(frame): dim = (frame.shape[1]/RATIO, frame.shape[0]/RATIO); resized = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA) gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) fd = FaceDetector('cascades/haarcascade_frontalface_default.xml') faceRects = fd.detect(gray, scaleFactor = 1.1, minNeighbors = 5, minSize = (10, 10)) for (x, y, w, h) in faceRects: # decrease the size of the bounding box x = RATIO*(x+10) y = RATIO*(y+10) w = RATIO*(w-15) h = RATIO*(h-15) # insert the coordinates of each face to the list allRoiPts.append((x, y, x+w, y+h)) # show the detected faces cv2.imshow("Faces", frame) cv2.waitKey(1) return allRoiPts
追溯calHist
功能:计算传入的各个boxes的小图的hsv空间的直方图,并对直方图归一化后返回,
1 2 3 4 5 6 7 8 9 10
def calHist(allRoiPts): global orig allRoiHist = [] for roiPts in allRoiPts: roi = orig[roiPts[1]:roiPts[-1], roiPts[0]:roiPts[2]] roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) roiHist = cv2.calcHist([roi], [0], None, [16], [0, 180]) roiHist = cv2.normalize(roiHist, roiHist, 0, 255, cv2.NORM_MINMAX) allRoiHist.append(roiHist); return allRoiHist
matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(dets, trks) for t, trk in enumerate(self.trackers):#已有搭配的track对象 if t not in unmatched_trks: d = matched[np.where(matched[:, 1] == t)[0], 0] trk.update(dets[d, :][0]) trk.face_addtional_attribute.append(addtional_attribute_list[d[0]]) for i in unmatched_dets:#没有搭配的track则创建新tracker trk = KalmanBoxTracker(dets[i, :]) trk.face_addtional_attribute.append(addtional_attribute_list[i])
整体代码有点复杂,挺多地方看的不是很明白,等以后经验丰富了再回来看吧
ZidanMusk/experimenting-with-sort
入口main.py
1 2 3 4 5 6 7 8 9 10
def main(): detector = GroundTruthDetections() tracker = Sort(use_dlib= use_dlibTracker) #create instance of the SORT tracker with open(out_file, 'w') as f_out: frames = detector.get_total_frames() for frame in range(0, frames): #frame numbers begin at 0! detections = detector.get_detected_items(frame) trackers = tracker.update(detections,img) ` for d in trackers: plot(trackers)
追溯class Sort:创建新tracker或update已有tracker
1 2 3 4 5 6 7 8 9 10 11 12
def update(self,dets,img=None): for t,trk in enumerate(trks): pos = self.trackers[t].predict(img) #for kal! if dets != []: matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(dets,trks) #update matched trackers with assigned detections for t,trk in enumerate(self.trackers): trk.update(dets[d,:][0],img) for dlib re-intialize the trackers ?! #create and initialise new trackers for unmatched detections for i in unmatched_dets: trk = KalmanBoxTracker(dets[i,:]) self.trackers.append(trk)