人脸检测——准备非人脸

来源:互联网 发布:用homebrew安装mysql 编辑:程序博客网 时间:2024/05/16 07:53


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

import osimport randomfrom PIL import Imagefrom PIL import ImageFileImageFile.LOAD_TRUNCATED_IMAGES = Trueimport cv2import numpy as npdef 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)


如果帮到你了,请赞赏支持:


原创粉丝点击