def test_net(prefix=['model/pnet', 'model/rnet', 'model/onet'], epoch=[16, 16, 16], batch_size=[2048, 256, 16], ctx=mx.cpu(0), thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, camera_path='0'): # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) PNet = FcnDetector(P_Net("test"), ctx, args, auxs) # load rnet model args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) mtcnn_detector = MtcnnDetector(detectors=[PNet, RNet, ONet], ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=False) try: capture = cv2.VideoCapture(int(camera_path)) except ValueError as e: capture = cv2.VideoCapture(camera_path) first_loop = True while (capture.isOpened()): ret, img = capture.read() if img is None: continue # Initialize video writing if (first_loop): first_loop = False fourcc = cv2.VideoWriter_fourcc(*'H264') h, w = img.shape[:2] writer = cv2.VideoWriter('test.mkv', fourcc, 10, (w, h), True) t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet(img) boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print('shape: ', img.shape, '--', 'time: ', time.time() - t1) draw = img.copy() if boxes_c is not None: font = cv2.FONT_HERSHEY_SIMPLEX for b in boxes_c: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) cv2.imshow("detection result", draw) writer.write(draw) k = cv2.waitKey(1) if k == 27 or k == 113: # Esc or q key to stop writer.release() cv2.destroyAllWindows() break
def test_net(prefix, epoch, batch_size, ctx, thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, slide_window=False, camera_path='0'): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) try: capture = cv2.VideoCapture(int(camera_path)) except ValueError as e: capture = cv2.VideoCapture(camera_path) while (capture.isOpened()): ret, img = capture.read() if img is None: continue # img = cv2.imread('test01.jpg') t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet(img) boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print('shape: ', img.shape, '--', 'time: ', time.time() - t1) if boxes_c is not None: draw = img.copy() font = cv2.FONT_HERSHEY_SIMPLEX for b in boxes_c: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) cv2.imshow("detection result", draw) else: cv2.imshow("detection result", img) k = cv2.waitKey(1) if k == 27 or k == 113: # Esc or q key to stop break
def test_net(prefix, epoch, batch_size, ctx, thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, slide_window=False): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=False, ctx=ctx) if slide_window: PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model args, auxs = load_param(prefix[1], epoch[0], convert=False, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=False, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) img = cv2.imread('test01.jpg') t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet(img) boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print 'time: ', time.time() - t1 if boxes_c is not None: draw = img.copy() font = cv2.FONT_HERSHEY_SIMPLEX for b in boxes_c: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) cv2.imshow("detection result", draw) cv2.waitKey(0)
def test_net(prefix, epoch, batch_size, ctx, thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, slide_window=False, filename='test01.jpg'): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) img = cv2.imread(filename) t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet(img) boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print('time: ', time.time() - t1) if boxes_c is not None: draw = img.copy() font = cv2.FONT_HERSHEY_SIMPLEX for b in boxes_c: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) cv2.imshow("detection result", draw) f = filename.split('.') cv2.imwrite(''.join([*f[:-1], "_annotated.", f[-1]]), draw) cv2.waitKey(0)
def test_net(prefix, epoch, batch_size, ctx, thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, slide_window=False): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: # 使用滑动窗口(MTCNN的P_NET不使用了滑动窗口,而是全卷积网络) PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model args, auxs = load_param(prefix[1], epoch[1], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) # img = cv2.imread('test01.jpg') # 读取图片 # img = cv2.imread('000007.jpg') # 读取图片 # img = cv2.imread('crow.jpg') # 读取图片 img = cv2.imread('physics.jpg') # 读取图片 t1 = time.time() # 开始计时 boxes, boxes_c = mtcnn_detector.detect_pnet(img) boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print(boxes_c) print('time: ', time.time() - t1) if boxes_c is not None: draw = img.copy() font = cv2.FONT_HERSHEY_SIMPLEX # Python 一种字体 for b in boxes_c: # nms和bbr之后的结果 # for b in boxes: # nms和bbr之前的结果 # 在draw上绘制矩形框(左上角坐标+右下角坐标) cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) # 在draw上添加文字 cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) cv2.imshow("detection result", draw) cv2.waitKey(0)
class managerWindow(Ui_MainWindow): def __init__(self): super(managerWindow, self).__init__() self.setupUi(self) self.setlcd() ## self.pushButton_selectid.clicked.connect(lambda: self.folder_msg(1)) self.pushButton_selectmodel.clicked.connect(lambda: self.folder_msg(2)) self.pushButton_renamefloder.clicked.connect( lambda: self.progressBar_msg(1)) self.pushButton_resizeimage.clicked.connect( lambda: self.progressBar_msg(2)) ## self.pushButton_loadmodel.clicked.connect( lambda: self.progressBar_msg(4)) self.pushButton_opencam.clicked.connect( lambda: self.progressBar_msg(5)) self.pushButton_closecam.clicked.connect( lambda: self.progressBar_msg(6)) ## self.radioButton.toggled.connect(lambda: self.camnum_meg(0)) self.radioButton_2.toggled.connect(lambda: self.camnum_meg(1)) self.radioButton_3.toggled.connect(lambda: self.camnum_meg(2)) self.radioButton_4.toggled.connect(lambda: self.camnum_meg(3)) ## self.checkBox_fps.stateChanged.connect(lambda: self.checkbox_meg(1)) self.checkBox_box.stateChanged.connect(lambda: self.checkbox_meg(2)) self.checkBox_id.stateChanged.connect(lambda: self.checkbox_meg(3)) self.checkBox_landmarls.stateChanged.connect( lambda: self.checkbox_meg(4)) self.checkBox_distance.stateChanged.connect( lambda: self.checkbox_meg(5)) ## self.photo_match_id = [] self.img_paths = [] self.photo_bbox = [] self.photo_currentindex = -1 self.photo_dis = [] self.ret_dis = [] self.camnum = 0 self.modelcreat = False self.boxid = [] self.boxidnum = 0 self.show_landmarks = False self.show_bb = False self.show_id = False self.show_fps = False self.show_distence = False self.font = cv2.FONT_HERSHEY_SIMPLEX self.pushButton_4.clicked.connect(self.show_manager_user) self.pushButton_7.clicked.connect(self.show_searchlog) def mat_inter(self, box1, box2): # 判断两个矩形是否相交 # box=(xA,yA,xB,yB) x01, y01, x02, y02 = box1 x11, y11, x12, y12 = box2 lx = abs((x01 + x02) / 2 - (x11 + x12) / 2) ly = abs((y01 + y02) / 2 - (y11 + y12) / 2) sax = abs(x01 - x02) sbx = abs(x11 - x12) say = abs(y01 - y02) sby = abs(y11 - y12) if lx <= (sax + sbx) / 2 and ly <= (say + sby) / 2: return True else: return False def solve_coincide(self, box1, box2): # box=(xA,yA,xB,yB) # 计算两个矩形框的重合度 if self.mat_inter(box1, box2) == True: x01, y01, x02, y02 = box1 x11, y11, x12, y12 = box2 col = min(x02, x12) - max(x01, x11) row = min(y02, y12) - max(y01, y11) intersection = col * row area1 = (x02 - x01) * (y02 - y01) area2 = (x12 - x11) * (y12 - y11) coincide = intersection / ((area1 + area2 - intersection) * 1.0) return coincide else: return False def detect_boxid(self, box, label): result = False for item in self.boxid: tempbox = item.bbox[0] bb = [ int(tempbox[0]), int(tempbox[1]), int(tempbox[2]), int(tempbox[3]) ] bb2 = [int(box[0]), int(box[1]), int(box[2]), int(box[3])] coincide = self.solve_coincide(bb, bb2) #print(coincide) if coincide > 0.618: result = True item.add_box(box) item.add_label(label) result_id = item.id_num item.releasenum += 2 if len(item.labels) >= 10: c = Counter(item.labels) result_label = c.most_common()[0][0] #print(result_label) return result, result_id, result_label, self.boxid.index( item) else: return result, result_id, "Null", self.boxid.index(item) return result, 0, 0, -1 def setlcd(self): db = pymysql.connect("localhost", "root", "root", "sys", charset='utf8') cursor = db.cursor() sql = "select * from Users where levUsers not like \'teacher\'" if cursor.execute(sql): data = cursor.fetchall() self.lcdNumber.setProperty("intValue", int(len(data))) def show_manager_user(self): self.manager_userWindow = manager_userWindow() QMessageBox.warning( self, ("操作说明"), ("\n根据提示添加用户信息\n 删除用户可根据: \n 用户ID \n 用户姓名\n 二选一 \n")) self.manager_userWindow.exec_() def show_searchlog(self): self.searchlog_Window = searchlog_Window() QMessageBox.warning(self, ("操作说明"), ("\n根据提示查询信息\n 查询考勤可根据: \n 用户ID \n 用户姓名\n 二选一 \n")) self.searchlog_Window.exec_() def folder_msg(self, n): #文件夹按钮响应函数 if n == 1: directory = QFileDialog.getExistingDirectory( None, "选取ID文件夹", "/home") self.label_idfolder.setText(directory) if n == 2: directory = QFileDialog.getExistingDirectory( None, "选取模型文件夹", "/home") self.label_modelfolder.setText(directory) def checkbox_meg(self, n): if n == 1: if self.checkBox_fps.isChecked(): self.show_fps = True else: self.show_fps = False if n == 2: if self.checkBox_box.isChecked(): self.show_bb = True else: self.show_bb = False if n == 3: if self.checkBox_id.isChecked(): self.show_id = True else: self.show_id = False if n == 4: if self.checkBox_landmarls.isChecked(): self.show_landmarks = True else: self.show_landmarks = False if n == 5: if self.checkBox_distance.isChecked(): self.show_distence = True else: self.show_distence = False def progressBar_msg(self, n): if n == 1: self.progressBar.setValue(0) if self.label_idfolder.text() != "": self.rename(self.label_idfolder.text()) else: button = QMessageBox.warning(self, '警告', "目录尚未选择", QMessageBox.Yes) if n == 2: #resize images self.progressBar.setValue(0) if n == 4: self.progressBar.setValue(0) if self.label_idfolder.text( ) != "" and self.label_modelfolder.text() != "": self.args = Arg_Data( self.label_idfolder.text(), self.label_modelfolder.text() + '/model,0') self.loadModel() self.modelcreat = True else: button = QMessageBox.warning(self, '警告', "目录尚未选择", QMessageBox.Yes) if n == 5: self.timer = QTimer() self.timer.setInterval(100) self.start(self.camnum) if n == 6: self.closecap() def camnum_meg(self, n): if n == 0: if self.radioButton.isChecked(): self.camnum = 0 else: self.camnum = -1 if n == 1: if self.radioButton_2.isChecked(): self.camnum = 1 else: self.camnum = -1 if n == 2: if self.radioButton_3.isChecked(): self.camnum = 2 else: self.camnum = -1 if n == 3: if self.radioButton_4.isChecked(): self.camnum = 3 else: self.camnum = -1 def closecap(self): self.cap.release() self.boxid = [] self.boxidnum = 0 self.label_video.setText("视频画面") def start(self, camnum): if self.modelcreat: #self.image_verify() if self.camnum != -1 and self.lineEdit.text() == "": devices = '/dev/video' + str(camnum) self.cap = cv2.VideoCapture(devices) else: devices = self.lineEdit.text() self.cap = cv2.VideoCapture(devices) self.timer.start() self.timer.timeout.connect(self.video_verify) else: button = QMessageBox.warning(self, '警告', "模型没有建立", QMessageBox.Yes) def loadModel(self): self.model = face_embedding.FaceModel(self.args) detectors = [None, None, None] ctx = mx.gpu(0) prefix = ['mtcnnmodel/pnet', 'mtcnnmodel/rnet', 'mtcnnmodel/onet'] epoch = [16, 16, 16] batch_size = [2048, 256, 16] thresh = [0.6, 0.6, 0.7] min_face_size = 24 stride = 2 slide_window = False # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet self.mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) #print (self.model) self.id_dataset, self.idnums = self.get_id_data(self.args.id_dir) def find_matching_id(self, embedding): threshold = 1.12 #1.1 if dist less than this ,believe person id min_dist = 10.0 #10.0 matching_id = 'Unkown' for id_data in self.id_dataset: if id_data.embedding is not None: dist = self.get_embedding_distance(id_data.embedding, embedding) if dist < threshold and dist < min_dist: #if dist < threshold : min_dist = dist matching_id = id_data.name #return matching_id, min_dist return matching_id, min_dist def get_embedding_distance(self, emb1, emb2): dist = np.sqrt(np.sum(np.square(np.subtract( emb1, emb2)))) #Euclodean distence return dist def get_id_data(self, id_folder): id_dataset = [] ids = os.listdir(os.path.expanduser(id_folder)) ids.sort() for i in range(len(ids)): id_dir = os.path.join(id_folder, ids[i]) image_names = os.listdir(id_dir) image_paths = [os.path.join(id_dir, img) for img in image_names] for image_path in image_paths: #print image_path self.progressBar.setValue(i / len(ids)) img = cv2.imread(image_path.encode('utf-8')) boxes, boxes_c = self.mtcnn_detector.detect_pnet(img) if boxes_c is not None: boxes, boxes_c = self.mtcnn_detector.detect_rnet( img, boxes_c) if boxes_c is not None: boxes, boxes_c = self.mtcnn_detector.detect_onet( img, boxes_c) if boxes_c is not None: for b in boxes_c: bb = [int(b[0]), int(b[1]), int(b[2]), int(b[3])] #cv2.rectangle(img, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) temp_img = img[int(b[1]):int(b[3]), int(b[0]):int(b[2])] f1, _, _ = self.model.get_feature(temp_img) #print f1 id_dataset.append(ID_Data(ids[i], image_path, f1)) self.progressBar.setValue(100) return id_dataset, len(ids) ## def video_verify(self): #视频检测 if (self.cap.isOpened()): fla, img = self.cap.read() frame_height = self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT) start = time.time() acc_box_id = -1 fonts = PIL.ImageFont.truetype("./fonts/simhei.ttf", 20, encoding="utf-8") if fla: img = cv2.resize(img, (840, 560)) frame = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) ##第一次预检测 boxes, boxes_c = self.mtcnn_detector.detect_pnet(img) if boxes_c is not None: boxes, boxes_c = self.mtcnn_detector.detect_rnet( img, boxes_c) if boxes_c is not None: boxes, boxes_c = self.mtcnn_detector.detect_onet( img, boxes_c) if boxes_c is not None: for b in boxes_c: bb = [int(b[0]), int(b[1]), int(b[2]), int(b[3])] #cv2.rectangle(img, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) temp_img = frame[int(b[1]):int(b[3]), int(b[0]):int(b[2])] f, bbox, points = self.model.get_feature(temp_img) self.pil_im = PIL.Image.fromarray(frame) draw = PIL.ImageDraw.Draw( self.pil_im) # 括号中为需要打印的canvas,这里就是在图片上直接打印 matching_id = 'Unkown' dist = self.args.threshold if f is not None: matching_id, dist = self.find_matching_id(f) ret = self.detect_boxid(b, matching_id) if ret[0] == False: self.boxidnum += 1 tempitem = Box_id(self.boxidnum, b, matching_id) self.boxid.append(tempitem) else: matching_id = ret[2] acc_box_id = ret[1] index = ret[3] if self.boxid[index].islog: self.label_18.setText("已录入成功") else: if matching_id == "Null": self.label_18.setText("识别中") else: if self.addlog2dataset(matching_id): self.label_18.setText("录入成功") self.boxid[index].islog = True else: self.label_18.setText("录入失败") self.label_16.setText(matching_id.encode('utf-8')) if self.show_id: #cv2.putText(img, matching_id.encode('utf-8'), (bb[0], bb[3]), self.font, 1, (255, 255, 255), 1, cv2.LINE_AA) #white draw.text((bb[0], bb[3]), matching_id, (255, 255, 255), font=fonts) #self.label_16.setText(matching_id.encode('utf-8')) img = cv2.cvtColor(np.array(self.pil_im), cv2.COLOR_RGB2BGR) #cv2.putText(img, acc_matching_id, (bb[0], bb[3]), self.font, 1, (255, 255, 255), 1, cv2.LINE_AA) #white if self.show_bb: cv2.rectangle(img, (bb[0], bb[1]), (bb[2], bb[3]), (255, 0, 0), 2) cv2.putText(img, 'BOX_ID: %d' % (acc_box_id), (int(bb[0] + 5), int(bb[1])), self.font, 0.5, (255, 255, 255), 1) if self.show_distence: cv2.putText(img, '%.3f' % dist, (int(bb[0]), int(bb[1]) + 20), self.font, 0.4, (255, 255, 255), 1) if self.show_landmarks: for j in range( 5 ): # 0: righteye 1:lefteye 2:nose 3:rightmouth 4:leftmouth cv2.circle(img, (bb[0] + points[j][0], bb[1] + points[j][1]), 1, (0, 0, 255), 2) end = time.time() seconds = end - start fps = round(1 / seconds, 2) if self.show_fps: cv2.putText(img, str(fps), (0, int(frame_height) - 5), self.font, 1, (255, 255, 255), 1, cv2.LINE_AA) #out.write(img) height, width, bytesPerComponent = img.shape bytesPerLine = 3 * width cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) QImg = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888) pixmap = QPixmap.fromImage(QImg) self.label_video.setPixmap(pixmap) else: for item in self.boxid: item.releasenum -= 1 if item.releasenum <= 0: self.boxid.remove(item) if len(self.boxid) == 0: self.boxidnum = 0 height, width, bytesPerComponent = img.shape bytesPerLine = 3 * width cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img) QImg = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888) pixmap = QPixmap.fromImage(QImg) self.label_video.setPixmap(pixmap) def addlog2dataset(self, name): db = pymysql.connect("localhost", "root", "root", "sys", charset='utf8') cursor = db.cursor() sql = "select * from Users where nameUsers = \'%s\'" % (name) if cursor.execute(sql): data = cursor.fetchone() userid = data[0] time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") neartime = ( datetime.datetime.now() - datetime.timedelta(hours=2)).strftime("%Y-%m-%d %H:%M:%S") sqlnear = "select * from Log where nameLog = \'%s\' and timeLog >= \'%s\'" % ( name, neartime) print(sqlnear) if cursor.execute(sqlnear): return True else: sql2 = "insert into Log(idLog,nameLog,timeLog) values(%d,\'%s\',\'%s\')" % ( userid, name, time) if cursor.execute(sql2): db.commit() db.close() return True else: db.close() return False else: db.close() return False def rename(self, dirs): image_rename_paths = [] image_paths = [] ids = os.listdir(os.path.expanduser(dirs)) for id_name in ids: id_dir = os.path.join(dirs, id_name) if os.path.isdir(id_dir): #print(os.path.isdir(id_dir)) image_names = os.listdir(id_dir) for img in image_names: image_paths.append( os.path.join(id_dir, img).encode('utf-8')) else: continue filecount = 0 for img in image_names: filecount = filecount + 1 filename = id_name + '_' + '%04d' % (filecount) + '.png' image_rename_paths.append( os.path.join(id_dir, filename.encode('utf-8'))) for i in range(len(image_paths)): os.rename(image_paths[i], image_rename_paths[i]) self.progressBar.setValue(i * 100 / len(image_paths)) self.progressBar.setValue(100)
def test_net(prefix=['model/pnet', 'model/rnet', 'model/onet'], epoch=[16, 16, 16], batch_size=[2048, 256, 16], ctx=mx.cpu(0), thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, camera_path='0'): # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) PNet = FcnDetector(P_Net("test"), ctx, args, auxs) # load rnet model args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) mtcnn_detector = MtcnnDetector(detectors=[PNet, RNet, ONet], ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=False) try: capture = cv2.VideoCapture(int(camera_path)) except ValueError as e: capture = cv2.VideoCapture(camera_path) try: first_loop = True while (capture.isOpened()): ret, img = capture.read() if img is None: continue # Initialize video writing if (first_loop): first_loop = False fourcc = cv2.VideoWriter_fourcc(*'H264') h, w = img.shape[:2] writer = cv2.VideoWriter('test.mkv', fourcc, 10, (w, h), True) t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet(img) boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print('shape: ', img.shape, '--', 'time: ', time.time() - t1) draw = img.copy() if boxes_c is not None: font = cv2.FONT_HERSHEY_SIMPLEX for b in boxes_c: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) cv2.putText(draw, '%.3f' % b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) writer.write(draw) except KeyboardInterrupt as e: print("KeyboardInterrupt") writer.release()
def test_net(dataset_path, prefix, faceRecognize_model, epoch, batch_size, test_mode="onet", thresh=[0.6, 0.6, 0.7], min_face_size=24, margin=44, stride=2, slide_window=False): detectors = [None, None, None] model_path = ['%s-%s' % (x, y) for x, y in zip(prefix, epoch)] if slide_window: PNet = Detector(P_Net, 12, batch_size[0], model_path[0]) else: PNet = FcnDetector(P_Net, model_path[0]) detectors[0] = PNet #load rnet model if test_mode in ["rnet", "onet"]: detectors[1] = Detector(R_Net, 24, batch_size[1], model_path[1]) # load onet model if test_mode == "onet": detectors[2] = Detector(O_Net, 48, batch_size[2], model_path[2]) # load faceRecognize model face_rec = FcnRecognize(faceRecognize_model, data_size=67, batch_size=16) mtcnn_detector = MtcnnDetector(detectors=detectors, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) ######load data input_dir0 = '/mllib/ALG/facenet-tensorflow/jz_80val' classes1 = os.listdir(input_dir0) message_path = [] for cls1 in classes1: classes2_path = os.path.join(input_dir0, cls1) try: classes2 = os.listdir(classes2_path) except Exception as e: print e continue key = cv2.waitKey(1) if key == 'q': ### when keycode is q print('====------======\n') img_list_tmp = [] num_id = 0 image_message = {} message = [] meg_name = [ 'exit_id', 'img_read', 'img_detect', 'face_num', 'face_roi', 'exit_person', 'score', 'person_name' ] ##########0 exit_id :Is there an id ; 1 ,0 ##########1 img_read :Whether to read successfully image ; 1 ,0 ##########2 img_detect :Whether to detect successfully image ; 1 ,0 ##########3 face_num :The face amount ; 0, 1, 2, 3, ... ##########4 face_roi :The face of a coordinate ; (x1,y1,w1,h1),(x2,y2,w2,h2) ... ##########5 exit_person :The person amount ; 0, 1, 2, 3, ... ##########6 score :The person score ; 0, 1, 2, 3, ... ##########7 score :The person name ; name0, name1, name2, name3, ... for cls2 in classes2: classes3_path = os.path.join(classes2_path, cls2) print(classes3_path) if 1: #classes2_path == "emb_check" : image_message[meg_name[0]] = 1 else: num_id = 0 image_message[meg_name[0]] = 0 continue try: img = cv2.imread(classes3_path) except Exception as e: print e image_message[meg_name[1]] = 0 continue #img = cv2.imread(image_name)#('test01.jpg') #img = cv2.imread(input_test+'.jpg') #cv2.imshow("img", img) #cv2.waitKey(0) image_message[meg_name[0]] = 1 t1 = time.time() try: boxes, boxes_c = mtcnn_detector.detect_pnet(img) except Exception as e: image_message[meg_name[2]] = 0 print e print(classes3_path) continue image_message[meg_name[2]] = 1 #message.append("img_available") if boxes_c is None: image_message[meg_name[3]] = 0 continue boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) if boxes_c is None: image_message[meg_name[3]] = 0 continue boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) if boxes_c is None: image_message[meg_name[3]] = 0 continue image_message[meg_name[3]] = len(boxes_c) print('box_num:%d', len(boxes_c)) print 'time: ', time.time() - t1 message.append("have_face") num_box = [] if boxes_c is not None: img0 = misc.imread(classes3_path) img_size = np.asarray(img0.shape)[0:2] nrof_samples = len(boxes_c) for det in boxes_c: bb = np.zeros(4, dtype=np.int32) margin_tmp = ((det[3] - det[1]) - (det[2] - det[0])) / 2 if margin_tmp > 0: size_tmp = (det[3] - det[1]) * 0 bb[0] = np.maximum(det[0] - margin_tmp - size_tmp, 0) bb[1] = np.maximum(det[1] - size_tmp, 0) bb[2] = np.minimum(det[2] + margin_tmp + size_tmp, img_size[1]) bb[3] = np.minimum(det[3] + size_tmp, img_size[0]) else: size_tmp = (det[2] - det[0]) * 0 bb[0] = np.maximum(det[0] - size_tmp, 0) bb[1] = np.maximum(det[1] + margin_tmp - size_tmp, 0) bb[2] = np.minimum(det[2] + size_tmp, img_size[1]) bb[3] = np.minimum(det[3] - margin_tmp + size_tmp, img_size[0]) cropped = img0[int(bb[1]):int(bb[3]), int(bb[0]):int(bb[2]), :] num_box.append( "%d,%d,%d,%d" % (int(bb[1]), int(bb[3]), int(bb[0]), int(bb[2]))) #misc.imsave('hebei/%d.png'%i, cropped) #cropped=misc.imread('/home/ssd/fb_data/casic_cluster_china67/Jin Jong-oh/Jin Jong-oh_8.png') #cv2.imshow("cropped", cropped) #cv2.waitKey(500) ### aligned = misc.imresize(cropped, (67, 67), interp='bilinear') #cv2.imshow("aligned", aligned) #cv2.waitKey(500) ### #misc.imsave('hebei/%d.png'%i, aligned) prewhitened = faceRecognize.prewhiten(aligned) img_list_tmp.append(prewhitened) key = cv2.waitKey(1) image_message[meg_name[4]] = num_box print(image_message) if len(img_list_tmp) < 2: #1 : continue img_list = [None] * len(img_list_tmp) for i in range(len(img_list_tmp)): img_list[i] = img_list_tmp[i] images = np.stack(img_list) t_emb = time.time() emb = face_rec.recognize(images) np.save("./emb_check.txt", emb) print 'emb_1_time: ', time.time() - t_emb for i in range(len(img_list_tmp)): print('%1d ' % i) score_tmp = 65 score2person = -1 for j in range(len(img_list_tmp)): dist = np.sqrt( np.sum(np.square(np.subtract(emb[i, :], emb[j, :])))) print(' %1.4f ' % dict2score(dist)) print('') if dict2score(dist) > score_tmp: score_tmp = dict2score(dist) score2person = j if score_tmp > 65: image_message[meg_name[6]] = score_tmp image_message[meg_name[7]] = 'num_0' else: image_message[meg_name[6]] = 0 image_message[meg_name[7]] = 'num_0' print('-----====end compare !!===-----') print(image_message)
def test_net(prefix, epoch, batch_size, ctx, thresh=[0.6, 0.6, 0.7], min_face_size=24, stride=2, slide_window=False): detectors = [None, None, None] # load pnet model args, auxs = load_param(prefix[0], epoch[0], convert=True, ctx=ctx) if slide_window: PNet = Detector(P_Net("test"), 12, batch_size[0], ctx, args, auxs) else: PNet = FcnDetector(P_Net("test"), ctx, args, auxs) detectors[0] = PNet # load rnet model args, auxs = load_param(prefix[1], epoch[0], convert=True, ctx=ctx) RNet = Detector(R_Net("test"), 24, batch_size[1], ctx, args, auxs) detectors[1] = RNet # load onet model args, auxs = load_param(prefix[2], epoch[2], convert=True, ctx=ctx) ONet = Detector(O_Net("test"), 48, batch_size[2], ctx, args, auxs) detectors[2] = ONet mtcnn_detector = MtcnnDetector(detectors=detectors, ctx=ctx, min_face_size=min_face_size, stride=stride, threshold=thresh, slide_window=slide_window) video_capture = cv2.VideoCapture(0) boxes = [] boxes_c = [] while True: #img = cv2.imread('/home/zzg/Opensource/mtcnn-master/data/custom/02.jpg') _, img = video_capture.read() t1 = time.time() boxes, boxes_c = mtcnn_detector.detect_pnet(img) if boxes_c is None: continue boxes, boxes_c = mtcnn_detector.detect_rnet(img, boxes_c) if boxes_c is None: continue boxes, boxes_c = mtcnn_detector.detect_onet(img, boxes_c) print 'time: ', time.time() - t1 if boxes_c is not None: draw = img.copy() font = cv2.FONT_HERSHEY_SIMPLEX for b in boxes_c: cv2.rectangle(draw, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])), (0, 255, 255), 1) #cv2.putText(draw, '%.3f'%b[4], (int(b[0]), int(b[1])), font, 0.4, (255, 255, 255), 1) cv2.imshow("detection result", draw) #cv2.imwrite("o12.jpg",draw) cv2.waitKey(10)