def camera_demo(): args = build_argparser().parse_args() model_xml = "openvino/480x640/FP32/dbface.xml" #<--- CPU #model_xml = "openvino/480x640/FP16/dbface.xml" #<--- MYRIAD model_bin = os.path.splitext(model_xml)[0] + ".bin" ie = IECore() net = ie.read_network(model=model_xml, weights=model_bin) input_blob = next(iter(net.inputs)) exec_net = ie.load_network(network=net, device_name='CPU') cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) ok, frame = cap.read() while ok: img = frame[np.newaxis, :, :, :] # Batch size axis add img = img.transpose((0, 3, 1, 2)) # NHWC to NCHW objs = detect(exec_net, input_blob, img) for obj in objs: common.drawbbox(frame, obj) cv2.imshow("demo DBFace", frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break ok, frame = cap.read() cap.release() cv2.destroyAllWindows()
def camera_demo(): dbface = DBFace() dbface.eval() if HAS_CUDA: dbface.cuda() dbface.load("model/dbface.pth") cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) ok, frame = cap.read() while ok: objs = detect(dbface, frame) for obj in objs: common.drawbbox(frame, obj) cv2.imshow("demo DBFace", frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break ok, frame = cap.read() cap.release() cv2.destroyAllWindows()
def _draw_box(img: np.array, face_infos: List[Dict], output_filename): for face_info in face_infos: common.drawbbox(img, face_info) # common.imwrite("detect_result/" + common.file_name_no_suffix(filename) + ".draw.jpg", image) if output_filename != "": common.imwrite(output_filename, img)
def camera_demo(): interpreter = Interpreter(model_path='model_float32.tflite', num_threads=4) interpreter.allocate_tensors() input_blob = interpreter.get_input_details() output_blob = interpreter.get_output_details() cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) while True: ok, frame = cap.read() if not ok: continue img = cv2.resize(frame, (640, 480)) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = img.astype(np.float32) img = img[np.newaxis, :, :, :] objs = detect(interpreter, input_blob, output_blob, img) for obj in objs: common.drawbbox(frame, obj) cv2.imshow("demo DBFace", frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break cap.release() cv2.destroyAllWindows()
def detect_image(model, file): image = common.imread(file) objs = detect(model, image) for obj in objs: common.drawbbox(image, obj) common.imwrite("detect_result/" + common.file_name_no_suffix(file) + ".draw.jpg", image)
def detect_image(model, file): image = common.imread(file) objs = detect(model, image) for obj in objs: # 增加对五官的检测 frame = organ_detection(image, obj) common.drawbbox(image, obj) cv2.imshow('demo', image) cv2.waitKey()
def train_epoch(self, epoch): for indbatch, (images, heatmap_gt, heatmap_posweight, reg_tlrb, reg_mask, landmark_gt, landmark_mask, num_objs, keep_mask) in enumerate(self.train_loader): self.iter += 1 batch_objs = sum(num_objs) batch_size = self.batch_size if batch_objs == 0: batch_objs = 1 heatmap_gt = heatmap_gt.to(self.gpu_master) heatmap_posweight = heatmap_posweight.to(self.gpu_master) keep_mask = keep_mask.to(self.gpu_master) reg_tlrb = reg_tlrb.to(self.gpu_master) reg_mask = reg_mask.to(self.gpu_master) landmark_gt = landmark_gt.to(self.gpu_master) landmark_mask = landmark_mask.to(self.gpu_master) images = images.to(self.gpu_master) hm, tlrb, landmark = self.model(images) hm = hm.sigmoid() hm = torch.clamp(hm, min=1e-4, max=1 - 1e-4) tlrb = torch.exp(tlrb) hm_loss = self.focal_loss( hm, heatmap_gt, heatmap_posweight, keep_mask=keep_mask) / batch_objs reg_loss = self.giou_loss(tlrb, reg_tlrb, reg_mask) * 5 landmark_loss = self.landmark_loss(landmark, landmark_gt, landmark_mask) * 0.1 loss = hm_loss + reg_loss + landmark_loss self.optimizer.zero_grad() loss.backward() self.optimizer.step() epoch_flt = epoch + indbatch / self.per_epoch_batchs if indbatch % 10 == 0: log.info( f"iter: {self.iter}, lr: {self.lr:g}, epoch: {epoch_flt:.2f}, loss: {loss.item():.2f}, hm_loss: {hm_loss.item():.2f}, " f"box_loss: {reg_loss.item():.2f}, lmdk_loss: {landmark_loss.item():.5f}" ) if indbatch % 1000 == 0: log.info("save hm") hm_image = hm[0, 0].cpu().data.numpy() common.imwrite(f"{jobdir}/imgs/hm_image.jpg", hm_image * 255) common.imwrite(f"{jobdir}/imgs/hm_image_gt.jpg", heatmap_gt[0, 0].cpu().data.numpy() * 255) image = np.clip( (images[0].permute(1, 2, 0).cpu().data.numpy() * self.std + self.mean) * 255, 0, 255).astype(np.uint8) outobjs = eval_tool.detect_images_giou_with_netout( hm, tlrb, landmark, threshold=0.1, ibatch=0) im1 = image.copy() for obj in outobjs: common.drawbbox(im1, obj) common.imwrite(f"{jobdir}/imgs/train_result.jpg", im1)
if flags[index] != 0: continue keep.append(obj) for j in range(index + 1, len(objs)): if flags[j] == 0 and obj.iou(objs[j]) > iou: flags[j] = 1 return keep mean = [0.408, 0.447, 0.47] std = [0.289, 0.274, 0.278] trial_name = "small-H-dense-wide64-UCBA" jobdir = f"jobs/{trial_name}" image = common.imread("imgs/selfie.jpg") model = DBFace(has_landmark=True, wide=64, has_ext=True, upmode="UCBA") #model.load(f"{jobdir}/models/150.pth") model.load_from_zoo() model.eval() model.cuda() outs = eval_tool.detect_image(model, image, mean, std, 0.3) outs = nms(outs, 0.2) print("objs = %d" % len(outs)) for obj in outs: common.drawbbox(image, obj) common.imwrite("test_result/test.jpg", image) print("ok")