前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >人脸检测——生成矫正人脸——cascade cnn的思想, 但是mtcnn的效果貌似更赞

人脸检测——生成矫正人脸——cascade cnn的思想, 但是mtcnn的效果貌似更赞

作者头像
MachineLP
发布2018-01-09 14:57:12
8510
发布2018-01-09 14:57:12
举报
文章被收录于专栏:小鹏的专栏

简单粗暴,不多说,直接代码吧:(这个代码实在上篇博客的基础上:人脸检测——AFLW准备人脸

先要生成AFLW_ann.txt。

代码语言:javascript
复制
import os
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import cv2
import numpy as np


def crop_for_cal(sn, xn, yn, n):

    with open('AFLW_ann.txt','r') as f:
        lines = f.readlines()

    save_dir1 = 'data_prepare/cal_positive_'+str(n)+'_12'
    save_dir2 = 'data_prepare/cal_positive_'+str(n)+'_24'
    save_dir3 = 'data_prepare/cal_positive_'+str(n)+'_48'

    if os.path.exists(save_dir1)==False:
        os.makedirs(save_dir1)
    if os.path.exists(save_dir2)==False:
        os.makedirs(save_dir2)
    if os.path.exists(save_dir3)==False:
        os.makedirs(save_dir3)

    for idx, line in enumerate(lines):
        spl1 = line.strip().split(' ')
        image_path = spl1[0]
        x = int(spl1[1])
        y = int(spl1[2])
        w = int(spl1[3])
        h = int(spl1[4])

        x = int(x-xn*w/sn)
        y = int(y-yn*h/sn)
        w = int(w/sn)
        h = int(h/sn)

        # image = Image.open(image_path)

        
        image = cv2.imread(image_path)
        if image is None:
            continue
        if x<=0 and y<=0 and w<=0 and h<=0:
            continue
        box = (x, y, x+w, y+h)
    
        # patch = image.crop(box)
        patch = image[box[1]:box[3], box[0]:box[2], :]
        if patch is None:
            continue
        if patch.shape[0]<10 and patch.shape[1]<10:
            continue

        #patch1 = patch.resize((12, 12))
        #patch2 = patch.resize((24, 24))
        #patch3 = patch.resize((48, 48))
        patch1 = cv2.resize(patch, (12, 12))  
        patch2 = cv2.resize(patch, (24, 24))  
        patch3 = cv2.resize(patch, (48, 48))  
        

        spl2 = image_path.split('/')
        image_name = spl2[-1]

        save_path1 = save_dir1+'/'+str(idx)+image_name + '.jpg'
        save_path2 = save_dir2+'/'+str(idx)+image_name + '.jpg'
        save_path3 = save_dir3+'/'+str(idx)+image_name + '.jpg'

        #patch1.save(save_path1, 'jpeg')
        #patch2.save(save_path2, 'jpeg')
        #patch3.save(save_path3, 'jpeg')
        cv2.imwrite(save_path1, np.array(patch1))
        cv2.imwrite(save_path2, np.array(patch2))
        cv2.imwrite(save_path3, np.array(patch3))


if __name__ == '__main__':
    
    s_set = (0.83, 0.91, 1.0, 1.10, 1.21)
    x_set = (-0.17, 0, 0.17)
    y_set = (-0.17, 0, 0.17)
    n = 0
    for x in x_set:
        for y in y_set:
            for s in s_set:
                n = n + 1
                crop_for_cal(s, x, y, n)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年07月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档