예제 #1
0
def detect_face(image):
    """
    从图片二维矩阵中检测出人脸
    :param image:
    :return:
    """
    det = hdface_detector(use_cuda=False)
    # img_det = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    result = det.detect_face(image)
    boxs = []
    for one in result:
        face = one['box']
        boxs.append(face)

    return boxs
예제 #2
0
def main(args):
    det = hdface_detector(use_cuda=False)
    checkpoint = torch.load(args.model_path)
    plfd_backbone = PFLDInference().cuda()
    plfd_backbone.load_state_dict(checkpoint)
    plfd_backbone.eval()
    plfd_backbone = plfd_backbone.cuda()
    transform = transforms.Compose([transforms.ToTensor()])
    root = args.images_path

    path_list = glob.glob(os.path.join(root, "*.jpg"))
    # cap = cv2.VideoCapture("")
    for img_path in path_list:
        img = cv2.imread(img_path)

        height, width = img.shape[:2]
        img_det = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        result = det.detect_face(img_det)
        for i in range(len(result)):
            box = result[i]['box']
            cls = result[i]['cls']
            pts = result[i]['pts']
            x1, y1, x2, y2 = box
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 25))
            w = x2 - x1 + 1
            h = y2 - y1 + 1

            size_w = int(max([w, h]) * 0.9)
            size_h = int(max([w, h]) * 0.9)
            cx = x1 + w // 2
            cy = y1 + h // 2
            x1 = cx - size_w // 2
            x2 = x1 + size_w
            y1 = cy - int(size_h * 0.4)
            y2 = y1 + size_h

            left = 0
            top = 0
            bottom = 0
            right = 0
            if x1 < 0:
                left = -x1
            if y1 < 0:
                top = -y1
            if x2 >= width:
                right = x2 - width
            if y2 >= height:
                bottom = y2 - height

            x1 = max(0, x1)
            y1 = max(0, y1)

            x2 = min(width, x2)
            y2 = min(height, y2)

            cropped = img[y1:y2, x1:x2]
            print(top, bottom, left, right)
            cropped = cv2.copyMakeBorder(cropped, top, bottom, left, right,
                                         cv2.BORDER_CONSTANT, 0)

            cropped = cv2.resize(cropped, (112, 112))

            input = cv2.resize(cropped, (112, 112))
            input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB)
            input = transform(input).unsqueeze(0).cuda()
            pose, landmarks = plfd_backbone(input)
            poses = pose.cpu().detach().numpy()[0] * 180 / np.pi
            pre_landmark = landmarks[0]
            pre_landmark = pre_landmark.cpu().detach().numpy().reshape(
                -1, 2) * [size_w, size_h]
            cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0))
            for (x, y) in pre_landmark.astype(np.int32):
                cv2.circle(img, (x1 - left + x, y1 - bottom + y), 1,
                           (255, 255, 0), 1)
            plot_pose_cube(img,
                           poses[0],
                           poses[1],
                           poses[2],
                           tdx=pts['nose'][0],
                           tdy=pts['nose'][1],
                           size=(x2 - x1) // 2)
        cv2.imshow('0', img)
        cv2.waitKey(0)
예제 #3
0
    type=str,
    help='imgs dir')
parser.add_argument('--results_path',
                    default="./result",
                    type=str,
                    help='results dir')
parser.add_argument('--mode', default="cpu", type=str, help='cpu or gpu')
args = parser.parse_args()

if args.mode == "cpu":
    caffe.set_mode_cpu()
elif args.mode == "gpu":
    caffe.set_mode_gpu()
image_mean = np.array([0, 0, 0])
image_std = 255.0
det = hdface_detector(use_cuda=True)


def inference():
    net = caffe.Net(args.caffe_prototxt_path, args.caffe_model_path,
                    caffe.TEST)
    input_size = [int(v.strip()) for v in args.input_size.split(",")]
    witdh = input_size[0]
    height = input_size[1]
    # priors = define_img_size(input_size)
    net.blobs['input'].reshape(1, 3, height, witdh)
    result_path = args.results_path
    imgs_path = args.imgs_path
    if not os.path.exists(result_path):
        os.makedirs(result_path)
    listdir = os.listdir(imgs_path)