def getFeature(net, dataloader, device, flip = True): ### Calculate the features ### featureLs = None featureRs = None count = 0 for det in dataloader: for i in range(len(det)): det[i] = det[i].to(device) count += det[0].size(0) # print('extracing deep features from the face pair {}...'.format(count)) with torch.no_grad(): res = [net(d).data.cpu() for d in det] if flip: featureL = l2_norm(res[0] + res[1]) featureR = l2_norm(res[2] + res[3]) else: featureL = res[0] featureR = res[2] if featureLs is None: featureLs = featureL else: featureLs = torch.cat((featureLs, featureL), 0) if featureRs is None: featureRs = featureR else: featureRs = torch.cat((featureRs, featureR), 0) return featureLs, featureRs
def prepare_facebank(model, path='facebank', tta=True): model.eval() embeddings = [] names = [''] data_path = Path(path) for doc in data_path.iterdir(): if doc.is_file(): continue else: embs = [] for files in listdir_nohidden(doc): try: image_path = os.path.join(doc, files) img = cv2.imread(image_path) if img.shape != (112, 112, 3): bboxes, landmarks = create_mtcnn_net( img, 12, device, p_model_path='MTCNN/weights/pnet_Weights', r_model_path='MTCNN/weights/rnet_Weights', o_model_path='MTCNN/weights/onet_Weights') img = Face_alignment(img, default_square=True, landmarks=landmarks) if img == []: continue with torch.no_grad(): if tta: mirror = cv2.flip(img[0], 1) emb = model( test_transform(img[0]).to(device).unsqueeze(0)) emb_mirror = model( test_transform(mirror).to(device).unsqueeze(0)) embs.append(l2_norm(emb + emb_mirror)) else: embs.append( model( test_transform( img[0]).to(device).unsqueeze(0))) except: continue if len(embs) == 0: continue embedding = torch.cat(embs).mean(0, keepdim=True) embeddings.append(embedding) names.append(doc.name) embeddings = torch.cat(embeddings) names = np.array(names) print(names) torch.save(embeddings, os.path.join(path, 'facebank.pth')) np.save(os.path.join(path, 'names'), names) return embeddings, names
t1 = time.time() - t t = time.time() faces = Face_alignment(image, default_square = True,landmarks = landmarks) embs = [] test_transform = trans.Compose([ trans.ToTensor(), trans.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])]) for img in faces: if args.tta: mirror = cv2.flip(img,1) emb = detect_model(test_transform(img).to(device).unsqueeze(0)) emb_mirror = detect_model(test_transform(mirror).to(device).unsqueeze(0)) embs.append(l2_norm(emb + emb_mirror)) else: embs.append(detect_model(test_transform(img).to(device).unsqueeze(0))) source_embs = torch.cat(embs) # number of detected faces x 512 diff = source_embs.unsqueeze(-1) - targets.transpose(1, 0).unsqueeze(0) # i.e. 3 x 512 x 1 - 1 x 512 x 2 = 3 x 512 x 2 dist = torch.sum(torch.pow(diff, 2), dim=1) # number of detected faces x numer of target faces minimum, min_idx = torch.min(dist, dim=1) # min and idx for each row min_idx[minimum > ((args.threshold-156)/(-80))] = -1 # if no match, set idx to -1 score = minimum results = min_idx # convert distance to score dis(0.7,1.2) to score(100,60) score_100 = torch.clamp(score*-80+156,0,100) image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
def get_frame(self): #여기다가 face detection, recognition기능을 넣으면 문제없음. frame = self.frame #thread가 update하는 이미지를 가져 옴. if frame is not None and self.flag is True: try: start_time=time.time() input=resize_image(frame,self.scale)# input size를 줄여줌으로 speed up 가능 #print('get bboxes') # bboxes, landmarks = create_mtcnn_net(input, self.min_face, self.device, p_model_path='MTCNN/weights/pnet_Weights', # r_model_path='MTCNN/weights/rnet_Weights', # o_model_path='MTCNN/weights/onet_Weights') #print('sucess bbox') bboxes,landmarks=self.face_detector.detect_all_net(image=input,mini_face=self.min_face) if bboxes != []: bboxes=bboxes/self.scale landmarks=landmarks/self.scale faces= Face_alignment(frame,default_square=True,landmarks=landmarks) embs=[] test_transform = trans.Compose([ trans.ToTensor(), trans.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])]) for img in faces: if self.tta: mirror = cv2.flip(img,1) emb = self.detect_model(test_transform(img).to(self.device).unsqueeze(0)) emb_mirror = self.detect_model(test_transform(mirror).to(self.device).unsqueeze(0)) embs.append(l2_norm(emb + emb_mirror)) else: embs.append(self.detect_model(test_transform(img).to(self.device).unsqueeze(0))) source_embs=torch.cat(embs) diff=source_embs.unsqueeze(-1) - self.targets.transpose(1, 0).unsqueeze(0) # i.e dist = torch.sum(torch.pow(diff, 2), dim=1) # number of detected faces x numer of target faces minimum, min_idx = torch.min(dist, dim=1) # min and idx for each row min_idx[minimum > ((self.threshold-156)/(-80))] = -1 # if no match, set idx to -1 score = minimum results = min_idx score_100 = torch.clamp(score*-80+156,0,100) FPS=1.0/(time.time()-start_time) cv2.putText(frame,'FPS : {:.1f}'.format(FPS),(10,15),cv2.FONT_HERSHEY_DUPLEX,0.75,(255,0,255)) for i,b in enumerate(bboxes): b=b.astype('uint32') cv2.rectangle(frame,(b[0],b[1]),(b[2],b[3]),(0,255,0),1) try: if self.names[results[i]+1]=='Unknown': #mosic func #print('detect unknwon') face_region=frame[b[1]:b[3],b[0]:b[2]] face_region=cv2.blur(face_region,(30,30)) frame[b[1]:b[3],b[0]:b[2]]=face_region except: pass # development version # if self.score: # cv2.putText(frame,self.names[results[i]+1]+' score:{:.0f}'.format(score_100[i]),(int(b[0]),int(b[1]-25)),cv2.FONT_ITALIC,1,(255,255,0)) # else: # cv2.putText(frame,self.names[results[i]+1],(int(b[0]),int(b[1]-25)),cv2.FONT_ITALIC,1,(255,255,0)) except: pass _, jpeg = cv2.imencode('.jpg',frame) return jpeg.tobytes()