人脸检测——准备非人脸

简单粗暴,不多说,直接代码吧:

import os  
import random  
from PIL import Image  
from PIL import ImageFile  
ImageFile.LOAD_TRUNCATED_IMAGES = True  
import cv2  
import numpy as np  
  
  
def random_crop(imagepath):  
  
    # image = Image.open(imagepath)  
    # len_rand = random.randint(0, min(image.size[0], image.size[1])-1)  
    # x_rand = random.randint(0, image.size[0]-len_rand-1)  
    # y_rand = random.randint(0, image.size[1]-len_rand-1)  
      
    image = cv2.imread(imagepath)  
    len_rand = random.randint(0, min(image.shape[1], image.shape[0])-1)  
    x_rand = random.randint(0, image.shape[1]-len_rand-1)  
    y_rand = random.randint(0, image.shape[0]-len_rand-1)  
  
    box = (x_rand, y_rand, x_rand+len_rand, y_rand+len_rand)  
    # return image.crop(box)  
    return image[box[1]:box[3], box[0]:box[2], :]  
  
  
save_dir = 'data_prepare/net_negative'  
  
if os.path.exists(save_dir)==False:  
    os.makedirs(save_dir)  
  
neg_img_dir = 'imagenet_selected'  
for file in os.walk(neg_img_dir):  
    filenames = file[2]  
  
for idx, filename in enumerate(filenames):  
  
    filepath = neg_img_dir + '/' + filename  
  
    for i in range(33):  
  
        image_crop = random_crop(filepath)  
  
        savepath = save_dir + '/' + str(i) + filename  
  
        # image_crop_12 = image_crop.resize((15,15))  
        if image_crop is None:  
            continue  
        if image_crop.shape[0]<10 and image_crop.shape[1]<10:  
            continue  
        image_crop = cv2.resize(image_crop, (12, 12))    
  
        # image_crop_12.save(savepath, 'jpeg')  
        cv2.imwrite(savepath, np.array(image_crop))  
  
    print (idx)  

你可能感兴趣的:(人脸检测——准备非人脸)