def update_image( self ): # update canvas every time according to time set in the timer. if self.recognize_face_btn.isChecked(): self.ret, self.image = self.capture.read() if self.ret: self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB) image_rc = Image.fromarray(self.image) bboxes, faces = self.face_recognize.align_multi( image_rc, thresholds=[0.5, 0.7, 0.8]) if len(bboxes) != 0: bboxes = bboxes[:, : -1] #shape:[10,4],only keep 10 highest possibiity faces bboxes = bboxes.astype(int) bboxes = bboxes + [-1, -1, 1, 1] # personal choice if self.has_targ: results, score, embs = self.face_recognize.infer( faces, self.targets) for idx, bbox in enumerate(bboxes): draw_box_name(bbox, self.names[results[idx] + 1], self.image) else: for idx, bbox in enumerate(bboxes): draw_box_name(bbox, 'Unknow', self.image) self.display() else: QMessageBox().about(self, "Warring", "Video source not found!") self.recognize_face_btn.setText("Recognize") self.recognize_face_btn.setCheckable(False) self.recognize_face_btn.setCheckable(True) self.stop_timer() if self.video_recording_btn.isChecked(): self.recording()
def generate(names, targets): ret, frame = vid_source.read() # tensor code while ret: img_bg = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(img_bg) try: bboxes, faces = face_recognize.align_multi(image) except: bboxes = [] faces = [] if len(bboxes) != 0: bboxes = bboxes[:,:-1] #shape:[10,4],only keep 10 highest possibiity faces bboxes = bboxes.astype(int) bboxes = bboxes + [-1,-1,1,1] # personal choice results, score, embs = face_recognize.infer(faces, targets) for idx, bbox in enumerate(bboxes): if False: frame = draw_box_name(bbox, names[results[idx] + 1] + '_{:.2f}'.format(score[idx]), frame) else: frame = draw_box_name(bbox, names[results[idx] + 1], frame) payload = cv2.imencode('.jpg', frame)[1].tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + payload + b'\r\n') ret, frame = vid_source.read()
def recognize_webcam(image_np): # image = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) image = Image.fromarray(image_np) # try: bboxes, faces = face_recognize.align_multi(image) # except: # # pass # bboxes = [] # faces = [] if len(bboxes) != 0: bboxes = bboxes[:,:-1] #shape:[10,4],only keep 10 highest possibiity faces bboxes = bboxes.astype(int) bboxes = bboxes + [-1,-1,1,1] # personal choice results, score, embs = face_recognize.infer(faces, targets) for idx, bbox in enumerate(bboxes): is_spoofing = face_recognize.is_spoofing(faces[idx]) status = "attack" if not is_spoofing: status = "genuine" if len(que_name) > 15: que_name.pop() que_name.append(names[results[idx] + 1]+ '_{:.7s}'.format(status)) else: que_name.append(names[results[idx] + 1]+ '_{:.7s}'.format(status)) if False: image_np = draw_box_name(bbox, names[results[idx] + 1] + '_{:.2f}__{:.7s}'.format(score[idx], status), image_np) else: image_np = draw_box_name(bbox, names[results[idx] +1 ], image_np) return image_np
def face_verify(self, avtive_key): while 1: image_np = self.frame_ori[avtive_key].copy() image = Image.fromarray(image_np) try: bboxes, faces = self.face_reg.align_multi(image) except: # pass bboxes = [] faces = [] if len(bboxes) != 0: bboxes = bboxes[:, : -1] #shape:[10,4],only keep 10 highest possibiity faces bboxes = bboxes.astype(int) bboxes = bboxes + [-1, -1, 1, 1] # personal choice results, score, embs = self.face_reg.infer(faces, self.targets) for idx, bbox in enumerate(bboxes): if False: image_np = draw_box_name( bbox, self.names[results[idx] + 1] + '_{:.2f}__{:.7s}'.format(score[idx], status), image_np) else: image_np = draw_box_name(bbox, self.names[results[idx] + 1], image_np) self.frame_process[avtive_key] = {'image': image_np}
def recognize(image_path, targets, names): frame = cv2.imread(image_path) image = Image.fromarray(frame).convert('RGB') try: bboxes, faces = face_recognize.align_multi(image) except: bboxes = [] faces = [] result = {} result['original'] = encode_image(image.copy()) if len(bboxes) != 0: bboxes = bboxes[:,:-1] #shape:[10,4],only keep 10 highest possibiity faces bboxes = bboxes.astype(int) bboxes = bboxes + [-1,-1,1,1] # personal choice results, score, embs = face_recognize.infer(faces, targets) for idx, bbox in enumerate(bboxes): result[names[results[idx] + 1]] = encode_image(faces[idx]) if False: frame = draw_box_name(bbox, names[results[idx]+1] + '_{:.2f}'.format(score[idx]), frame) else: frame = draw_box_name(bbox, names[results[idx]+1], frame) return result
bboxes = bboxes + [-1,-1,1,1] # personal choice results, score, embs = face_recognize.infer(faces, targets) for idx, bbox in enumerate(bboxes): if results[idx] == -1 and args.save_unknow: new_per = "%s/unknow_%s"%(conf.facebank_path, count_unknow) if not os.path.exists(new_per): os.mkdir(new_per) faces[idx].save('%s/%s.jpg'%(new_per, datetime.now().date().strftime('%Y%m%d'))) targets = torch.cat((targets, embs[idx].unsqueeze(0)), dim=0) names =np.append(names, 'unknow_%s'%count_unknow) count_unknow+=1 if args.score: frame = draw_box_name(bbox, names[results[idx]] + '_{:.2f}'.format(score[idx]), frame) else: frame = draw_box_name(bbox, names[results[idx]], frame) video_writer.write(frame) cv2.imshow("face_recognize", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break if args.duration != 0: i += 1 if i % 25 == 0: print('{} second'.format(i // 25)) if i > 25 * args.duration: break cap.release()