前两篇写了人脸识别和yolo+deepsort

这篇就写下两个方法的结合,在保持一定帧率的前提下进行身份识别和跟踪:

代码直接在yolo+deepsort下的main.py 修改,先备份一个main.py 重命名为 main_track.py:

import numpy as np
import time
from tracker import face_track
from detector import Detector
from lib_face import *
import cv2
from rtsp import myThread
from face_recognize import person_face
import threading
from lib_face import YuNet
thread_lock = threading.Lock()
thread_exit = False
start_tracking = Trueclass detector_service(person_face):def __init__(self,cam=None):super(detector_service, self).__init__()# self.loadface = person_face()self.camera = camself.lib_face = YuNet()X, y, self.names = self.LoadImages()# print('x',X)self.ontrack = Trueself.start_time = 0self.model = cv2.face.EigenFaceRecognizer_create()self.model.train(X, y)self.name_dic = {}self.image = None# self.face_casecade = cv2.CascadeClassifier(r'E:\workspace\person_tracking\data\haarcascade_frontalface_default.xml')def detect_face(self,img):faces = self.face_casecade.detectMultiScale(img, 1.3, 5)for (x, y, w, h) in faces:frame = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)  # 蓝色roi_gray = img[y:y + h, x:x + w]try:# 将图像转换为宽92 高112的图像# resize(原图像,目标大小,(插值方法)interpolation=,)roi_gray = cv2.resize(roi_gray, (92, 112), interpolation=cv2.INTER_LINEAR)params = self.model.predict(roi_gray)print('Label:%s,confidence:%.2f' % (params[0], params[1]))'''putText:给照片添加文字putText(输入图像,'所需添加的文字',左上角的坐标,字体,字体大小,颜色,字体粗细)'''# cv2.putText(frame, self.names[params[0]], (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)if self.names[params[0]] not in self.name_dic.keys():self.name_dic[self.names[params[0]]] = [x, y, x + w, y + h]except:continue# return framedef dlib_detect(self):res = self.face_detecting(self.image)  # 0.032sif res is not None:face, self.all_face_location = resfor i in range(self.face_num):try:[left, right, top, bottom] = self.all_face_location[i]self.face_img = self.image[top:bottom, left:right]gray_img = cv2.cvtColor(self.face_img, cv2.COLOR_BGR2GRAY)roi_gray = cv2.resize(gray_img, (92, 112), interpolation=cv2.INTER_LINEAR)params = self.model.predict(roi_gray)if self.names[params[0]] not in self.name_dic.keys():self.name_dic[self.names[params[0]]] = [left, top, right, bottom]except:continuedef lib_face_det(self):self.lib_face.setInputSize([self.image.shape[1], self.image.shape[0]])face1 = self.lib_face.infer(self.image)for det in face1:try:bbox = det[0:4].astype(np.int32)if int(bbox[2])*int(bbox[3])>2000:print("xxxxxxxxx",bbox[0], bbox[1], bbox[2], bbox[3])self.face_img = self.image[bbox[1]: bbox[1] + bbox[3], bbox[0]:bbox[0] + bbox[2]]gray_img = cv2.cvtColor(self.face_img, cv2.COLOR_BGR2GRAY)roi_gray = cv2.resize(gray_img, (92, 112), interpolation=cv2.INTER_LINEAR)params = self.model.predict(roi_gray)if self.names[params[0]] not in self.name_dic.keys():self.name_dic[self.names[params[0]]] = [bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]]except:continuedef detector_camera_person(self):global start_trackingdetector = Detector()track = face_track()i = 0while self.camera.isOpened() and not self.quit_flag:val, self.image = self.camera.read()if val == False: continue# ref, frame = capture.read()i+=1detection = time.time()self.image = cv2.resize(self.image, (1280, 720))bboxes = detector.detect(self.image) #0.017sif len(bboxes) > 0:# self.dlib_detect()  # 0.032sself.lib_face_det()print("+++++++name+++2++++",self.name_dic)if self.name_dic:list_bboxs, confirmed_name, statue = track.person_update(bboxes, self.image, self.name_dic, confirmed_name=confirmed_name)else:print("无权限--------------------------------------")list_bboxs, confirmed_name, statue = track.person_update(bboxes, self.image, confirmed_name=confirmed_name)output_image_frame = track.draw_bboxes(self.image, list_bboxs, line_thickness=None)print("start_person_update ==use time====", str(time.time() - start_person))else:# 如果画面中 没有bboxoutput_image_frame = self.imageself.get_fps(output_image_frame) #平均12-13帧  cv2.imshow('live', output_image_frame)print(" ==all  use time====", str(time.time() - detection))if cv2.waitKey(1) & 0xFF == ord('q'):breakself.camera.release()cv2.destroyAllWindows()def main():# cam = cv2.VideoCapture('./video/1.mp4')cam = cv2.VideoCapture(0)process = detector_service(cam)process.detector_camera_person()if __name__ == '__main__':# data = r"E:\workspace\person_tracking\data\face"# de = detector_service()# de.detector_rtsp_person()# de.detector_camera_person()main()

为了方便测试 我把10种人脸检测的方法,挑选了部分加进去了,有兴趣的朋友可以了解下这篇文章:10种轻量级人脸检测算法的比拼_nihate的博客-CSDN博客_轻量级人脸检测

有libface:

import numpy as np
import cv2
import time
import argparse
from itertools import product
from _testcapi import FLT_MIN
# from face_recognize import person_faceclass YuNet:def __init__(self, inputSize=[320, 320], confThreshold=0.9, nmsThreshold=0.3, topK=5000, keepTopK=750):modelPath = r'xxx'self._model = cv2.dnn.readNet(modelPath)self._inputNames = ''self._outputNames = ['loc', 'conf', 'iou']self._inputSize = inputSize # [w, h]self._confThreshold = confThresholdself._nmsThreshold = nmsThresholdself._topK = topKself._keepTopK = keepTopKself._min_sizes = [[10, 16, 24], [32, 48], [64, 96], [128, 192, 256]]self._steps = [8, 16, 32, 64]self._variance = [0.1, 0.2]self.start_time = 0# Generate priorsself._priorGen()@propertydef name(self):return self.__class__.__name__def get_fps(self,img):now = time.time()time_period = now - self.start_timeself.fps = 1.0 / time_periodself.start_time = nowcolor = (0,255,0)if self.fps < 15:color = (0,0,255)cv2.putText(img, str(self.fps.__round__(2)), (20, 50), cv2.FONT_HERSHEY_DUPLEX, 1, color)def setBackend(self, backend):self._model.setPreferableBackend(backend)def setTarget(self, target):self._model.setPreferableTarget(target)def setInputSize(self, input_size):self._inputSize = input_size # [w, h]# Regenerate priorsself._priorGen()def infer(self, image):assert image.shape[0] == self._inputSize[1], '{} (height of input image) != {} (preset height)'.format(image.shape[0], self._inputSize[1])assert image.shape[1] == self._inputSize[0], '{} (width of input image) != {} (preset width)'.format(image.shape[1], self._inputSize[0])# PreprocessinputBlob = cv2.dnn.blobFromImage(image)# Forwardself._model.setInput(inputBlob, self._inputNames)outputBlob = self._model.forward(self._outputNames)# Postprocessresults = self._postprocess(outputBlob)return resultsdef _postprocess(self, outputBlob):# Decodedets = self._decode(outputBlob)# NMSkeepIdx = cv2.dnn.NMSBoxes(bboxes=dets[:, 0:4].tolist(),scores=dets[:, -1].tolist(),score_threshold=self._confThreshold,nms_threshold=self._nmsThreshold,top_k=self._topK) # box_num x class_numif len(keepIdx) > 0:dets = dets[keepIdx]# dets = np.squeeze(dets, axis=1)return dets[:self._keepTopK]else:return np.empty(shape=(0, 15))def _priorGen(self):w, h = self._inputSizefeature_map_2th = [int(int((h + 1) / 2) / 2),int(int((w + 1) / 2) / 2)]feature_map_3th = [int(feature_map_2th[0] / 2),int(feature_map_2th[1] / 2)]feature_map_4th = [int(feature_map_3th[0] / 2),int(feature_map_3th[1] / 2)]feature_map_5th = [int(feature_map_4th[0] / 2),int(feature_map_4th[1] / 2)]feature_map_6th = [int(feature_map_5th[0] / 2),int(feature_map_5th[1] / 2)]feature_maps = [feature_map_3th, feature_map_4th,feature_map_5th, feature_map_6th]priors = []for k, f in enumerate(feature_maps):min_sizes = self._min_sizes[k]for i, j in product(range(f[0]), range(f[1])): # i->h, j->wfor min_size in min_sizes:s_kx = min_size / ws_ky = min_size / hcx = (j + 0.5) * self._steps[k] / wcy = (i + 0.5) * self._steps[k] / hpriors.append([cx, cy, s_kx, s_ky])self.priors = np.array(priors, dtype=np.float32)def _decode(self, outputBlob):loc, conf, iou = outputBlob# get scorecls_scores = conf[:, 1]iou_scores = iou[:, 0]# clamp_idx = np.where(iou_scores < 0.)iou_scores[_idx] = 0._idx = np.where(iou_scores > 1.)iou_scores[_idx] = 1.scores = np.sqrt(cls_scores * iou_scores)scores = scores[:, np.newaxis]scale = np.array(self._inputSize)# get bboxesbboxes = np.hstack(((self.priors[:, 0:2] + loc[:, 0:2] * self._variance[0] * self.priors[:, 2:4]) * scale,(self.priors[:, 2:4] * np.exp(loc[:, 2:4] * self._variance)) * scale))# (x_c, y_c, w, h) -> (x1, y1, w, h)bboxes[:, 0:2] -= bboxes[:, 2:4] / 2# get landmarkslandmarks = np.hstack(((self.priors[:, 0:2] + loc[:,  4: 6] * self._variance[0] * self.priors[:, 2:4]) * scale,(self.priors[:, 0:2] + loc[:,  6: 8] * self._variance[0] * self.priors[:, 2:4]) * scale,(self.priors[:, 0:2] + loc[:,  8:10] * self._variance[0] * self.priors[:, 2:4]) * scale,(self.priors[:, 0:2] + loc[:, 10:12] * self._variance[0] * self.priors[:, 2:4]) * scale,(self.priors[:, 0:2] + loc[:, 12:14] * self._variance[0] * self.priors[:, 2:4]) * scale))dets = np.hstack((bboxes, landmarks, scores))return detsdef visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None):output = image.copy()landmark_color = [(255,   0,   0), # right eye(  0,   0, 255), # left eye(  0, 255,   0), # nose tip(255,   0, 255), # right mouth corner(  0, 255, 255)  # left mouth corner]if fps is not None:cv2.putText(output, 'FPS: {:.2f}'.format(fps), (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, text_color)for det in results:bbox = det[0:4].astype(np.int32)cv2.rectangle(output, (bbox[0], bbox[1]), (bbox[0]+bbox[2], bbox[1]+bbox[3]), box_color, 2)conf = det[-1]cv2.putText(output, '{:.4f}'.format(conf), (bbox[0], bbox[1]+12), cv2.FONT_HERSHEY_DUPLEX, 0.5, text_color)landmarks = det[4:14].astype(np.int32).reshape((5,2))for idx, landmark in enumerate(landmarks):cv2.circle(output, landmark, 2, landmark_color[idx], 2)return outputclass SFace:def __init__(self):modelPath ='weights/face_detection_yunet.onnx'self._model = cv2.dnn.readNet(modelPath)self._input_size = [112, 112]self._dst = np.array([[38.2946, 51.6963],[73.5318, 51.5014],[56.0252, 71.7366],[41.5493, 92.3655],[70.7299, 92.2041]], dtype=np.float32)self._dst_mean = np.array([56.0262, 71.9008], dtype=np.float32)@propertydef name(self):return self.__class__.__name__def setBackend(self, backend_id):self._model.setPreferableBackend(backend_id)def setTarget(self, target_id):self._model.setPreferableTarget(target_id)def _preprocess(self, image, bbox):aligned_image = self._alignCrop(image, bbox)return cv2.dnn.blobFromImage(aligned_image)def infer(self, image, bbox):# Preprocess# inputBlob = self._preprocess(image, bbox)inputBlob = cv2.dnn.blobFromImage(self._alignCrop(image, bbox))# Forwardself._model.setInput(inputBlob)outputBlob = self._model.forward()# Postprocessresults = outputBlob / cv2.norm(outputBlob)return resultsdef match(self, image1, face1, image2, face2, dis_type=0):feature1 = self.infer(image1, face1)feature2 = self.infer(image2, face2)if dis_type == 0: # COSINEreturn np.sum(feature1 * feature2)elif dis_type == 1: # NORM_L2return cv2.norm(feature1, feature2)else:raise NotImplementedError()def _alignCrop(self, image, face):# Retrieve landmarksif face.shape[-1] == (4 + 5 * 2):landmarks = face[4:].reshape(5, 2)else:raise NotImplementedError()warp_mat = self._getSimilarityTransformMatrix(landmarks)aligned_image = cv2.warpAffine(image, warp_mat, self._input_size, flags=cv2.INTER_LINEAR)return aligned_imagedef _getSimilarityTransformMatrix(self, src):# compute the mean of src and dstsrc_mean = np.array([np.mean(src[:, 0]), np.mean(src[:, 1])], dtype=np.float32)dst_mean = np.array([56.0262, 71.9008], dtype=np.float32)# subtract the means from src and dstsrc_demean = src.copy()src_demean[:, 0] = src_demean[:, 0] - src_mean[0]src_demean[:, 1] = src_demean[:, 1] - src_mean[1]dst_demean = self._dst.copy()dst_demean[:, 0] = dst_demean[:, 0] - dst_mean[0]dst_demean[:, 1] = dst_demean[:, 1] - dst_mean[1]A = np.array([[0., 0.], [0., 0.]], dtype=np.float64)for i in range(5):A[0][0] += dst_demean[i][0] * src_demean[i][0]A[0][1] += dst_demean[i][0] * src_demean[i][1]A[1][0] += dst_demean[i][1] * src_demean[i][0]A[1][1] += dst_demean[i][1] * src_demean[i][1]A = A / 5d = np.array([1.0, 1.0], dtype=np.float64)if A[0][0] * A[1][1] - A[0][1] * A[1][0] < 0:d[1] = -1T = np.array([[1.0, 0.0, 0.0],[0.0, 1.0, 0.0],[0.0, 0.0, 1.0]], dtype=np.float64)s, u, vt = cv2.SVDecomp(A)smax = s[0][0] if s[0][0] > s[1][0] else s[1][0]tol = smax * 2 * FLT_MINrank = int(0)if s[0][0] > tol:rank += 1if s[1][0] > tol:rank += 1det_u = u[0][0] * u[1][1] - u[0][1] * u[1][0]det_vt = vt[0][0] * vt[1][1] - vt[0][1] * vt[1][0]if rank == 1:if det_u * det_vt > 0:uvt = np.matmul(u, vt)T[0][0] = uvt[0][0]T[0][1] = uvt[0][1]T[1][0] = uvt[1][0]T[1][1] = uvt[1][1]else:temp = d[1]d[1] = -1D = np.array([[d[0], 0.0], [0.0, d[1]]], dtype=np.float64)Dvt = np.matmul(D, vt)uDvt = np.matmul(u, Dvt)T[0][0] = uDvt[0][0]T[0][1] = uDvt[0][1]T[1][0] = uDvt[1][0]T[1][1] = uDvt[1][1]d[1] = tempelse:D = np.array([[d[0], 0.0], [0.0, d[1]]], dtype=np.float64)Dvt = np.matmul(D, vt)uDvt = np.matmul(u, Dvt)T[0][0] = uDvt[0][0]T[0][1] = uDvt[0][1]T[1][0] = uDvt[1][0]T[1][1] = uDvt[1][1]var1 = 0.0var2 = 0.0for i in range(5):var1 += src_demean[i][0] * src_demean[i][0]var2 += src_demean[i][1] * src_demean[i][1]var1 /= 5var2 /= 5scale = 1.0 / (var1 + var2) * (s[0][0] * d[0] + s[1][0] * d[1])TS = [T[0][0] * src_mean[0] + T[0][1] * src_mean[1],T[1][0] * src_mean[0] + T[1][1] * src_mean[1]]T[0][2] = dst_mean[0] - scale * TS[0]T[1][2] = dst_mean[1] - scale * TS[1]T[0][0] *= scaleT[0][1] *= scaleT[1][0] *= scaleT[1][1] *= scalereturn np.array([[T[0][0], T[0][1], T[0][2]],[T[1][0], T[1][1], T[1][2]]], dtype=np.float64)if __name__=='__main__':cap = cv2.VideoCapture(0)img1path='telangpu.png'img2path='telangpu2.png'dis_type = 0 #  0: cosine, 1: norm_l1detector = YuNet()# recognizer = SFace()#摄像头入口人脸检测# img2 = cv2.imread(img2path)# face2 = detector.infer(img2)while True:ret, img = cap.read()srcimg = cv2.resize(img,(640,480))print(srcimg)# srcimg_1 = cv2.flip(srcimg,1)detector.setInputSize([srcimg.shape[1], srcimg.shape[0]])face1 = detector.infer(srcimg)for det in face1:bbox = det[0:4].astype(np.int32)cv2.rectangle(srcimg, (bbox[0], bbox[1]), (bbox[0] + bbox[2], bbox[1] + bbox[3]), (0, 255, 0), 2)# srcimg = visualize(srcimg,face1)winName = 'Deep learning object detection in OpenCV'detector.get_fps(srcimg)cv2.namedWindow(winName, 0)cv2.imshow(winName, srcimg)# distance = recognizer.match(srcimg_1, face1[0][:-1], img2, face2[0][:-1], dis_type)# if dis_type == 0:#     dis_type = 'Cosine'#     threshold = 0.363#     result = 'same identity' if distance >= threshold else 'different identity'# else:#     dis_type = 'Norm-L2'#     threshold = 1.128#     result = 'same identity' if distance <= threshold else 'different identity'if cv2.waitKey(100) & 0xFF == ord('q'):breakcap.release()cv2.destroyAllWindows()# print('Using {} distance, threshold {} :distance{} ,{}.'.format(dis_type, threshold, distance, result))print(face1)

有dlib face: 代码部分加下cv2的摄像头读取就可以查看了,就不再写了,自行更改

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 27 11:43:47 2018@author: Administrator
"""
'''
调用opencv的库实现人脸识别
'''import time
import cv2,dlib
import numpy as np
import os
import threading
from rtsp import myThread
import config
thread_lock = threading.Lock()
thread_exit=False
from lib_face import YuNet
import shutil
# from camera_catch import camera_service
class person_face():def __init__(self, name="y", xml_path=None, root_dir=None):self.name =nameself.root_dir = r"E:\workspace\person_tracking\data\face"self.face_dir =os.path.join(self.root_dir,name)self.detector = dlib.get_frontal_face_detector()self.fps = 0  # 帧率self.image = Noneself.face_img = Nonedef face_detecting(self,img):face_location = []all_face_location = []faces = self.detector(img, 0)self.face_num = len(faces)if len(faces) != 0:self.face_flag = Truefor i, face in enumerate(faces):face_location.append(face)w, h = (face.right() - face.left()), (face.bottom() - face.top())left, right, top, bottom = face.left() - w // 4, face.right() + w // 4, face.top() - h // 2, face.bottom() + h // 4all_face_location.append([left, right, top, bottom])return face_location, all_face_locationelse:self.face_flag = Falsereturn Noneif __name__ == '__main__':data = r"E:\workspace\person_tracking\data\face"serve= person_face()serve.face_detecting()

对人脸检测这块就不再赘述:目前方法非常多也很成熟

人脸识别部分:从main_track.py 种可以看出用的是

model = cv2.face.EigenFaceRecognizer_create()

具体的先采集人脸,再测试人脸,检测和识别都再此代码中。:

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 27 11:43:47 2018@author: Administrator
"""
'''
调用opencv的库实现人脸识别
'''import time
import cv2,dlib
import numpy as np
import os
import threading
from rtsp import myThread
import config
thread_lock = threading.Lock()
thread_exit=False
from lib_face import YuNet
import shutil
# from camera_catch import camera_service
class person_face():def __init__(self, name="aaa", xml_path=None, root_dir=None):self.name =name #需要采集的人名self.root_dir = r"E:\workspace\person_tracking\data\face"self.face_dir =os.path.join(self.root_dir,name)# 采集图像保存地址self.lib_face = YuNet()self.xml_path =r'E:\workspace\person_tracking\data\haarcascade_frontalface_default.xml'self.detector = dlib.get_frontal_face_detector()self.face_num = 0  # 这一帧的人脸个数self.last_face_num = 0  # 上一帧的人脸个数self.face_num_change_flag = False  # 当前帧人脸数量变化的标志位,用于后续人脸识别提高帧率self.quit_flag = False  # 退出程序标志位self.img_num = 0  # 人脸数据文件夹内的图像个数self.collect_face_data = True  # 是否进行人脸数据的采集,只有为真时才会进行采集self.image = Noneself.face_img = Nonedef lib_detect(self,img):face_bbox = []self.lib_face.setInputSize([img.shape[1], img.shape[0]])face1 = self.lib_face.infer(img)for det in face1:bbox = det[0:4].astype(np.int32)face_bbox.append([bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]])return face_bbox# 采集自己的人脸数据def generator_camera(self):'''打开摄像头,读取帧,检测该帧图像中的人脸,并进行剪切、缩放生成图片满足以下格式:1.灰度图,后缀为 .png2.图像大小相同params:data:指定生成的人脸数据的保存路径'''img_height = 480img_width = 720capture = cv2.VideoCapture(0)if not os.path.exists(self.face_dir):os.mkdir(self.face_dir)while True:ref, frame = capture.read()#人脸检测器# face = self.detector(frame, 0)#dlibface = self.lib_detect(frame)#libfaceprint(face)for i,[left, top, right, bottom] in enumerate(face):w = int(right-left)h = int(bottom-top)area = w*hprint(area)if area<600:continue# 在原图上绘制矩形# w, h = (face.right() - face.left()), (face.bottom() - face.top())# left, right, top, bottom = face.left() - w // 4, face.right() + w // 4, face.top() - h // 2, face.bottom() + h // 4# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)else:cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# cv2.rectangle(frame, (face[0], face[1]), (face[2], face[3]), (0, 0, 255), 2)# 调整图像大小# new_frame = cv2.resize(frame[y:y + h, x:x + w], (92, 112))# new_frame = frame[top+2:bottom-2, left+2:right-2]new_frame = cv2.resize(frame[top+2:bottom-2, left+2:right-2], (92, 112))# 保存人脸cv2.imwrite('%s/%s.png' % (self.face_dir, str(time.time())), new_frame)cv2.imshow('Dynamic', frame)# 按下q键退出if cv2.waitKey(100) & 0xff == ord('q'):breakcv2.destroyAllWindows()# 载入图像   读取ORL人脸数据库,准备训练数据def LoadImages(self):'''加载图片数据用于训练params:data:训练数据所在的目录,要求图片尺寸一样ret:images:[m,height,width]  m为样本数,height为高,width为宽names:名字的集合labels:标签'''images = []names = []labels = []label = 0# 遍历所有文件夹for subdir in os.listdir(self.root_dir):subpath = os.path.join(self.root_dir, subdir)# print('path',subpath)# 判断文件夹是否存在if os.path.isdir(subpath):# 在每一个文件夹中存放着一个人的许多照片names.append(subdir)# 遍历文件夹中的图片文件for filename in os.listdir(subpath):imgpath = os.path.join(subpath, filename)img = cv2.imread(imgpath, cv2.IMREAD_COLOR)gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# cv2.imshow('1',img)# cv2.waitKey(0)images.append(gray_img)labels.append(label)label += 1images = np.asarray(images)# names=np.asarray(names)labels = np.asarray(labels)return images, labels, names# 检验训练结果def FaceRec(self):# 加载训练的数据X, y, names = self.LoadImages()# print('x',X)model = cv2.face.EigenFaceRecognizer_create()model.train(X, y)# 打开摄像头cap = cv2.VideoCapture(0)cv2.namedWindow('Dynamic')# 创建级联分类器face_casecade = cv2.CascadeClassifier(self.xml_path)while (True):# 读取一帧图像# ret:图像是否读取成功# frame:该帧图像ret, frame = cap.read()frame = cv2.resize(frame,(1280,720))# 判断图像是否读取成功# print('ret',ret)if ret:# 转换为灰度图gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# 利用级联分类器鉴别人脸faces = face_casecade.detectMultiScale(gray_img, 1.3, 5)# faces = self.lib_detect(frame)# 遍历每一帧图像,画出矩形for (x, y, w, h) in faces:frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)  # 蓝色# frame = cv2.rectangle(frame, (x, y), (w,  h), (255, 0, 0), 2)  # 蓝色roi_gray = gray_img[y:y + h, x:x + w]try:# 将图像转换为宽92 高112的图像# resize(原图像,目标大小,(插值方法)interpolation=,)roi_gray = cv2.resize(roi_gray, (92, 112), interpolation=cv2.INTER_LINEAR)params = model.predict(roi_gray)print('Label:%s,confidence:%.2f' % (params[0], params[1]))'''putText:给照片添加文字putText(输入图像,'所需添加的文字',左上角的坐标,字体,字体大小,颜色,字体粗细)'''cv2.putText(frame, names[params[0]], (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)except:continuecv2.imshow('Dynamic', frame)# 按下q键退出if cv2.waitKey(100) & 0xff == ord('q'):breakcap.release()cv2.destroyAllWindows()if __name__ == '__main__':data = r"E:\workspace\person_tracking\data\face"serve= person_face()# LoadImages(data)# image = camera_service()# generator(image,"ybj")serve.generator_camera()# serve.FaceRec()

人脸部分到底结束,前面两篇也具体介绍了,此部分为扩展方法。

接下来回到main_track.py中

通过人脸检测和人脸识别,类似于门禁一样先对人脸进行识别,并把身份id和人脸的bbox进行保存,再结合人和人脸直接bbox之间的关系,到deepsort中直接对track_id 进行身份id的赋值:

到deep_sort.py中的25行

 def update(self, bbox_xywh, confidences, ori_img, name_list=None):self.height, self.width = ori_img.shape[:2]# generate detectionsfeatures = self._get_features(bbox_xywh, ori_img, name_list=name_list)bbox_tlwh = self._xywh_to_tlwh(bbox_xywh)detections = [Detection(bbox_tlwh[i], conf, list(features.values())[i], list(features.keys())[i]) for i, conf in enumerate(confidences) if conf>self.min_confidence]# run on non-maximum supressionboxes = np.array([d.tlwh for d in detections])scores = np.array([d.confidence for d in detections])indices = non_max_suppression(boxes, self.nms_max_overlap, scores)detections = [detections[i] for i in indices]# update trackerself.tracker.predict()self.tracker.update(detections)# output bbox identitiesoutputs = []out_name = []for track in self.tracker.tracks:if not track.is_confirmed() or track.time_since_update > 1:continuebox = track.to_tlwh()x1, y1, x2, y2 = self._tlwh_to_xyxy(box)print("==========track.track_id=========:",track.track_id)track_id = track.track_idoutputs.append(np.array([x1, y1, x2, y2], dtype=np.int))out_name.append(track_id)if len(outputs) > 0:outputs = np.stack(outputs, axis=0)return outputs, out_name

将name dic和bbox的对应关系转为name:bbox的dic 传入features中,根据features中对bbox区域提取特征后再对应为 name:features 的dic

再将name和features 传入Detection类中 Detection中加 self.name = name

本身tracker是对Detection信息 做处理,因此在deep_sort/deep_sort/sort/tracker.py中第58行update中 备注的地方加入self._initiate_track(detections[detection_idx]),进行身份的更新。

    def update(self, detections):"""Perform measurement update and track management.Parameters----------detections : List[deep_root.detection.Detection]A list of detections at the current time step."""# Run matching cascade.matches, unmatched_tracks, unmatched_detections = self._match(detections)# Update track set.for track_idx, detection_idx in matches:#添加下行 对每每帧都进行name对track_id的重新赋值self._initiate_track(detections[detection_idx])self.tracks[track_idx].update(self.kf, detections[detection_idx])for track_idx in unmatched_tracks:self.tracks[track_idx].mark_missed()for detection_idx in unmatched_detections:self._initiate_track(detections[detection_idx])self.tracks = [t for t in self.tracks if not t.is_deleted()]# Update distance metric.active_targets = [t.track_id for t in self.tracks if t.is_confirmed()]features, targets = [], []for track in self.tracks:if not track.is_confirmed():continuefeatures += track.featurestargets += [track.track_id for _ in track.features]track.features = []self.metric.partial_fit(np.asarray(features), np.asarray(targets), active_targets)

def _initiate_track(): 如下

    def _initiate_track(self, detection):mean, covariance = self.kf.initiate(detection.to_xyah())print("==========detection.name=========",detection.name)# if len(name_dic)>=self._next_id:#     name = name_dic[self._next_id]# else:#     name = self._next_id# if isinstance(detection.name, int):#     detection.name = self._next_idself.tracks.append(Track(mean, covariance, detection.name, self.n_init, self.max_age,detection.feature))# self.tracks.append(Track(#         mean, covariance, self._next_id, self.n_init, self.max_age,#         detection.feature))# self._next_id += 1

至此,遍初步完成对身份的定义和跟踪,此方法目前还不完美,有许多bug的地方。但是目前好像还没有在博客中看到 做人脸识别+行人跟踪的,还在改进中,希望有大佬能有更好的方法能告知我,万分感谢。

​​​​​​​

行人跟踪之身份识别(三)相关推荐

  1. 行人跟踪之身份识别(er)

    er 不是二 我故意的!别讲我二.留言二的那个,你别跑! 人脸识别demo完成,搞一搞yolov5+deepsort 环境还是上个环境,装了就没问题 直接上百度盘链接 链接:https://pan.b ...

  2. 基于深度学习实现行人跟踪相关论文总结

    基于深度学习实现行人跟踪相关论文总结 [1] Recurrent YOLO and LSTM-based IR single pedestrian tracking 本文提出了一种基于空间监督的递归卷 ...

  3. 密集场景下的行人跟踪替代算法,头部跟踪算法 | CVPR 2021

    点击下方"AI算法与图像处理",一起进步!重磅干货,第一时间送达 报告链接:https://www.bilibili.com/video/BV1Ug411M7Kt/ https:/ ...

  4. 行人检测与重识别!SOTA算法

    行人检测与重识别!SOTA算法 A Simple Baseline for Multi-Object Tracking, Yifu Zhang, Chunyu Wang, Xinggang Wang, ...

  5. OWASP A7 Identification and Authentication Failures(身份识别和身份验证错误)

    Identification and Authentication Failures(身份识别和身份验证错误) 从 2017 的第二名下降到了 2021的第7名 我在 TOP1 中提出过水平越权和垂直 ...

  6. 2020电赛F题回顾——简易无接触温度测量与身份识别装置

    2020电赛F题回顾--简易无接触温度测量与身份识别装置 第一次参加电赛,已经大三了,这也有可能是我的最后一次,不禁感慨时间过得真快.在实验室一起奋斗的夜晚既辛苦又幸福,感谢陪伴在我身边一起做电赛的同 ...

  7. 硕士论文阅读——基于机器视觉和深度学习的工人安全帽检测与身份识别方法

    文章目录 零.摘要 一.绪论 1.背景与研究意义 2.国内外研究现状 (1)安全帽佩戴检测研究现状与不足 (2)身份识别研究现状与不足 (3)基于深度学习的目标检测 二.深度学习目标检测理论 1.卷积 ...

  8. 2020年TI杯电子设计大赛F题及2022年山西省电子设计大赛E题简易无接触温度测量与身份识别装置整体思路及代码

    在2022年山西省电子设计大赛选题刚刚公布的时候,我们组本来是想做电源题的,奈何学艺不精,最后转做了这道仪器仪表题,经过几天紧张的制作,写报告,最终也是获得了省一,想要代码和报告文档的可以私聊我或者从 ...

  9. Schnorr身份识别方案

    Schnorr身份识别协议是又一个零知识证明协议,相比Fiamt协议有两点不同,一是其安全性依赖于离散对数的困难性,二是该方案使用乘法群,从而可以提前计算了一些参数,减小了证明者实时计算开销,特别适合 ...

最新文章

  1. 【知识图谱】知识图谱概论
  2. php中的exception与自定义异常类
  3. Linux下搭建CACTI的时候总结的一些小知识
  4. 软件基本功:不会代码共用,因为没有设计能力;代码共用都不会,谈什么设计
  5. arcgis下载地址
  6. 分享李刚javaweb跟webservice视频教程
  7. 使用hMailServer邮箱服务器收发邮件
  8. 【IoT】STM32 文件系统 fatfs 移植笔记详解
  9. 【PCI】PCI驱动匹配(四)
  10. c++正则表达式regex
  11. 人像分割之ExtremeC3Net
  12. 解决 ClickOnce 发布出现: 清单中的引用与下载的程序集 *.exe 的标识不匹配 问题
  13. mac版Mysql可视化工具 - Sequel Pro
  14. 0基础如何入门人工智能?
  15. 2011年襄阳市高一下学期市统考数学试题
  16. SuMA++论文重点总结
  17. 迎风破局·守正创新,2021未来商业生态链接大会暨第六届金陀螺奖颁奖典礼成功举办!...
  18. 【软件测试】你肯定不知道的七大事实
  19. D-Link DIR-859的RCE漏洞 CVE-2019–17621
  20. 3DES的算法原理浅析

热门文章

  1. uni-app 动画
  2. 4g手机关闭4g信号显示无服务器,手机4G信号栏为什么突然出现“HD”?原来开通了这个业务,望周知...
  3. unity 转向和角度
  4. 美狐美颜SDK开源代码浅析
  5. 分享html代码的博客,简单实用的HTML代码分享
  6. CSS——网易云音乐首页之热门推荐歌单的制作
  7. 数字音频接口之TDM
  8. 微信自定义菜单使用特殊字符出现的问题
  9. mysql用update方法更改用户密码
  10. xmanager linux 远程桌面,Windows系统下通过xmanager远程桌面控制Linux