人臉檢測(cè)——生成矯正人臉——cascade cnn的思想, 但是mtcnn的效果貌似更贊

簡(jiǎn)單粗暴,不多說(shuō),直接代碼吧:(這個(gè)代碼實(shí)在上篇博客的基礎(chǔ)上:人臉檢測(cè)——AFLW準(zhǔn)備人臉
先要生成AFLW_ann.txt。

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)  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,171評(píng)論 25 708
  • 今天開始新的騎行旅程,休整了兩天半,身體和體力都恢復(fù)的很好。 早起吃早飯,除了米粥和煮雞蛋實(shí)在沒有想吃的,無(wú)奈,買...
    史大猩閱讀 200評(píng)論 0 0
  • 跟同學(xué)聊天,她忽然問(wèn)我要不要二胎。她非常想非常想,很急切地要為孩子添個(gè)伴。我淡然地說(shuō)到要二胎的理由還沒有說(shuō)服我的。...
    江南玉笛閱讀 342評(píng)論 5 4
  • MLeaksFinder是一個(gè)很好的IOS內(nèi)存泄漏檢測(cè)工具,能夠自動(dòng)地檢測(cè)UIViewController和UIV...
    tom510230閱讀 9,991評(píng)論 14 16

友情鏈接更多精彩內(nèi)容