Ejemplo n.º 1
0
def gen_rnet_data(data_dir,
                  anno_file,
                  pnet_model_file,
                  prefix_path='',
                  use_cuda=True,
                  vis=False):
    # load the pnet and pnet_detector

    mtcnn_detector = MtcnnDetector(p_model_path=pnet_model_file,
                                   r_model_path=None,
                                   o_model_path=None,
                                   min_face_size=12,
                                   use_cuda=True)
    device = mtcnn_detector.device

    imagedb = ImageDB(anno_file, mode="test", prefix_path=prefix_path)
    imdb = imagedb.load_imdb()
    image_reader = TestImageLoader(imdb, 1, False)

    all_boxes = []
    batch_idx = 0

    for databatch in image_reader:
        if batch_idx % 100 == 0:
            print("%d images done" % batch_idx)
        im = databatch
        t = time.time()
        boxes, boxes_align = mtcnn_detector.detect_pnet(im)
        if boxes_align is None:
            all_boxes.append(np.array([]))
            continue
        if vis:
            vision.vis_face(im, boxes_align)

        t1 = time.time() - t
        print('time cost for image {} / {} : {:.4f}'.format(
            batch_idx, image_reader.size, t1))
        all_boxes.append(boxes_align)
        batch_idx += 1

    save_path = config.TRAIN_DATA_DIR
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    save_file = os.path.join(save_path,
                             "pnet_detections_%d.pkl" % int(time.time()))

    with open(save_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    save_file = '/home/liujing/Codes/MTCNN/data/pnet_detections_1532530263.pkl'
    get_rnet_sample_data(data_dir, anno_file, save_file, prefix_path)
Ejemplo n.º 2
0
def gen_onet_data(data_dir, anno_file, pnet_model_file, rnet_model_file, prefix_path='', use_cuda=True, vis=False):
    
    pnet, rnet, _ = create_mtcnn_net(p_model_path=pnet_model_file, r_model_path=rnet_model_file, use_cuda=use_cuda)
    mtcnn_detector = MtcnnDetector(pnet=pnet, rnet=rnet, min_face_size=12)

    imagedb = ImageDB(anno_file, mode="test", prefix_path=prefix_path)
    imdb = imagedb.load_imdb()
    image_reader = TestImageLoader(imdb,1,False)

    all_boxes = list()
    batch_idx = 0
    
    for databatch in image_reader:
        if batch_idx % 100 == 0:
            print("%d images done" % batch_idx)
        im = databatch
        t = time.time()
        #detect an image by pnet and rnet
        p_boxes, p_boxes_align = mtcnn_detector.detect_pnet(im=im)
        boxes, boxes_align = mtcnn_detector.detect_rnet(im=im, dets=p_boxes_align)
        if boxes_align is None:
            all_boxes.append(np.array([]))
            batch_idx += 1
            continue
        if vis:
            rgb_im = cv2.cvtColor(np.asarray(im), cv2.COLOR_BGR2RGB)
            vision.vis_two(rgb_im, boxes, boxes_align)

        t1 = time.time() - t
        print 'time cost for image ', batch_idx, '/', image_reader.size, ': ', t1
        all_boxes.append(boxes_align)
        batch_idx += 1
        
    save_path = config.MODLE_STORE_DIR
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    
    save_file = os.path.join(save_path, "rnet_detections_%d.pkl" % int(time.time()))
    
    with open(save_file, 'wb') as f:
        cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL)
    
    
    #save_file = '/home/wujiyang/FaceProjects/MTCNN_TRAIN/model_store/rnet_detections_1527304558.pkl'
    get_onet_sample_data(data_dir, anno_file, save_file, prefix_path)
Ejemplo n.º 3
0
Created on Sat May 26 13:57:59 2018

@author: wujiyang
"""

import sys
sys.path.append("/home/wujiyang/FaceProjects/MTCNN_TRAIN")

import cv2
from tools.detect import create_mtcnn_net, MtcnnDetector
import tools.vision as vision


if __name__ == '__main__':

    pnet, rnet, onet = create_mtcnn_net(p_model_path="./model_store/pnet_model_final.pt",
                                        r_model_path="./model_store/rnet_model_final.pt",
                                        o_model_path="./model_store/onet_model_final.pt", 
                                        use_cuda=False)
    
    mtcnn_detector = MtcnnDetector(pnet=pnet, rnet=rnet, onet=onet, min_face_size=24)

    img = cv2.imread("./test2.jpg")
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    bboxs, landmarks = mtcnn_detector.detect_face(img)
    
    #print bboxs.shape[0]
    #print landmarks.shape[0]

    vision.vis_face(img, bboxs, landmarks)
Ejemplo n.º 4
0
    score = clf.decision_function(feature)
    return pred, score


if __name__ == '__main__':
    # 下面四行做何用?
    parser = ap.ArgumentParser()
    parser.add_argument('-m',
                        "--model_name",
                        help="model name",
                        default="re_train_model_V4.m")
    args = vars(parser.parse_args())
    model = args["model_name"]

    # MTCNN人脸检测器初始化
    mtcnn_detector = MtcnnDetector(min_face_size=24,
                                   use_cuda=False)  # mtcnn_detector的数据格式需要解析
    logger.info("Init the MtcnnDetector.")
    project_root = pathlib.Path()
    model_path = project_root / "model" / str(model)

    # 从摄像头读取图片,目前已不需要
    cap = cv2.VideoCapture(0)
    cap.set(3, 640)  # 设置分辨率
    cap.set(4, 480)
    cap.set(15, -6.0)
    while True:
        ret, frame = cap.read(0)  # 帧提取,ret无用
        start = time.time()
        bboxs, landmarks = mtcnn_detector.detect_face(
            frame)  # landmarks无用,bbox人脸区域坐标矩阵
        detect_time = time.time() - start
Ejemplo n.º 5
0

def draw_images(img, bboxs, landmarks):  # 在图片上绘制人脸框及特征点
    num_face = bboxs.shape[0]
    for i in range(num_face):
        cv2.rectangle(img, (int(bboxs[i, 0]), int(bboxs[i, 1])),
                      (int(bboxs[i, 2]), int(bboxs[i, 3])), (0, 255, 0), 3)
    for p in landmarks:
        for i in range(5):
            cv2.circle(img, (int(p[2 * i]), int(p[2 * i + 1])), 6, (0, 0, 255),
                       -1)
    return img


if __name__ == '__main__':
    mtcnn_detector = MtcnnDetector(min_face_size=24,
                                   use_cuda=False)  # 加载模型参数,构造检测器
    logger.info("Init the MtcnnDetector.")
    project_root = pathlib.Path()
    inputPath = project_root / "data" / "test_images"
    outputPath = project_root / "data" / "you_result"
    outputPath.mkdir(exist_ok=True)

    start = time.time()
    for num, input_img_filename in enumerate(inputPath.iterdir()):
        logger.info("Start to process No.{} image.".format(num))
        img_name = input_img_filename.name
        logger.info("The name of the image is {}.".format(img_name))

        img = cv2.imread(str(input_img_filename))
        RGB_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        bboxs, landmarks = mtcnn_detector.detect_face(
Ejemplo n.º 6
0
        # r_y = min(bboxs[i, 3] + 0.4 * b_h, h)
        r_y = bboxs[i, 3]
        cb_0 = (0.4 * b_w) if bboxs[i, 0] > (0.4 * b_w) else bboxs[i, 0]
        cb_1 = (0.4 * b_h) if bboxs[i, 1] > (0.4 * b_h) else bboxs[i, 1]
        cb_2 = (1.4 * b_w) if bboxs[i, 0] > (0.4 * b_w) else bboxs[i, 0] + b_w
        cb_3 = cb_2 + b_h
        cropped.append(img[int(l_y):int(r_y), int(l_x):int(r_x)])
        croped_bboxs.append(int(cb_0))
        croped_bboxs.append(int(cb_1))
        croped_bboxs.append(int(cb_2))
        croped_bboxs.append(int(cb_3))
    return cropped, croped_bboxs


if __name__ == '__main__':
    mtcnn_detector = MtcnnDetector(min_face_size=24, use_cuda=True)
    for i_path in range(len(data_path_list)):
        data_path = data_path_list[i_path]
        img_name_list = os.listdir(in_path + data_path)
        num_imgs = len(img_name_list)
        cropped_img_list = list()
        cropped_bboxs_list = list()
        img_list = list()
        bboxs_list = list()
        print("Now at Part " + str(i_path) + " of " +
              str(len(data_path_list)) + " " + data_path +
              " | number of imgs: " + str(num_imgs))

        for i in range(num_imgs):
            img_name = img_name_list[i]
            print(">>> Processing: " + str(i) + " of " + str(num_imgs) + " " +
Ejemplo n.º 7
0
def demo(path):

    #trained model
    p_model_path = "./model/pnet_epoch_train.pt"
    r_model_path = "./model/rnet_epoch_train.pt"
    o_model_path = "./model/onet_epoch_train.pt"
    pnet, rnet, onet = create_mtcnn_net(p_model_path=p_model_path,
                                        r_model_path=r_model_path,
                                        o_model_path=o_model_path,
                                        use_cuda=False)
    mtcnn_detector = MtcnnDetector(pnet=pnet,
                                   rnet=rnet,
                                   onet=onet,
                                   min_face_size=24,
                                   threshold=[0.6, 0.7, 0.7])

    img = cv2.imread(path)

    bboxs = mtcnn_detector.detect_face(img)
    # print box_align

    c = 0
    st = 0
    for i in range(bboxs.shape[0]):
        b = bboxs[i, :4]
        l = b[2] - b[0]
        h = b[3] - b[1]
        s = l * h
        if st < s:
            st = s
            cv2.rectangle(img, (int(b[0]), int(b[1])), (int(b[2]), int(b[3])),
                          (0, 0, 255), 2)

            image_r = img[int(b[1]) - 10:int(b[3]) + 10,
                          int(b[0]) - 10:int(b[2]) + 10]

            # cv2.imwrite("./img/" + "wxf" + ".jpg", image_r, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

    image = cv2.cvtColor(image_r, cv2.COLOR_BGR2HSV)
    sumRed = 0
    sumYello = 0
    sumblack = 0
    sumwhite = 0
    sumcyan = 0

    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            if (image[i, j, 0] <= 180 & image[i, j, 0] >= 156) & (
                    image[i, j, 1] >= 43
                    & image[i, j, 1] <= 255) & (image[i, j, 2] >=
                                                46 & image[i, j, 2] <= 255):
                sumRed = sumRed + 1
            if (image[i, j, 0] <= 10 & image[i, j, 0] >= 0) & (
                    image[i, j, 1] >= 43
                    & image[i, j, 1] <= 255) & (image[i, j, 2] >=
                                                46 & image[i, j, 2] <= 255):
                sumRed = sumRed + 1
            if (image[i, j, 0] <= 180 & image[i, j, 0] >=
                    0) & (image[i, j, 1] >= 0
                          & image[i, j, 1] <=
                          255) & (image[i, j, 2] >= 0 & image[i, j, 2] <= 46):
                sumblack = sumblack + 1
            if (image[i, j, 0] <= 180 & image[i, j, 0] >= 0) & (
                    image[i, j, 1] >= 0
                    & image[i, j, 1] <= 30) & (image[i, j, 2] >=
                                               221 & image[i, j, 2] <= 255):
                sumwhite = sumwhite + 1
            if (image[i, j, 0] <= 255 & image[i, j, 0] >= 34) & (
                    image[i, j, 1] >= 43
                    & image[i, j, 1] <= 255) & (image[i, j, 2] >=
                                                46 & image[i, j, 2] <= 255):
                sumYello = sumYello + 1
            if (image[i, j, 0] <= 99 & image[i, j, 0] >= 78) & (
                    image[i, j, 1] >= 43
                    & image[i, j, 1] <= 255) & (image[i, j, 2] >=
                                                46 & image[i, j, 2] <= 255):
                sumcyan = sumcyan + 1
    all = image.shape[0] * image.shape[1]
    red = sumRed / all
    yello = sumYello / all
    black = sumblack / all
    cyan = sumcyan / all
    white = sumwhite / all
    print('red:', red, 'yello:', yello, 'white:', white, 'black:', black,
          'cyan:', cyan)
Ejemplo n.º 8
0
console_handler.formatter = formatter  # 也可以直接给formatter赋值


def draw_images(img, bboxs, landmarks):  # 在图片上绘制人脸框及特征点
    num_face = bboxs.shape[0]
    for i in range(num_face):
        cv2.rectangle(img, (int(bboxs[i, 0]), int(bboxs[i, 1])), (int(
            bboxs[i, 2]), int(bboxs[i, 3])), (0, 255, 0), 3)
    for p in landmarks:
        for i in range(5):
            cv2.circle(img, (int(p[2 * i]), int(p[2 * i + 1])), 6, (0, 0, 255), -1)
    return img


if __name__ == '__main__':
    mtcnn_detector = MtcnnDetector(min_face_size=24, use_cuda=False)  # 加载模型参数,构造检测器
    logger.info("Init the MtcnnDetector.")
    project_root = pathlib.Path()
    inputPath = project_root / "data" / "test_images"
    outputPath = project_root / "data" / "you_result"
    outputPath.mkdir(exist_ok=True)

    start = time.time()
    for num, input_img_filename in enumerate(inputPath.iterdir()):
        logger.info("Start to process No.{} image.".format(num))
        img_name = input_img_filename.name
        logger.info("The name of the image is {}.".format(img_name))

        img = cv2.imread(str(input_img_filename))
        RGB_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        bboxs, landmarks = mtcnn_detector.detect_face(RGB_image)  # 检测得到bboxs以及特征点