示例#1
0
def GenerateData(ftxt,data_path,net,argument=False):
    '''

    :param ftxt: name/path of the text file that contains image path,
                bounding box, and landmarks

    :param output: path of the output dir
    :param net: one of the net in the cascaded networks
    :param argument: apply augmentation or not
    :return:  images and related landmarks
    '''
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print('Net type error')
        return
    image_id = 0
    #
    f = open(join(OUTPUT,"landmark_%s_aug.txt" %(size)),'w')
    #dstdir = "train_landmark_few"
    # get image path , bounding box, and landmarks from file 'ftxt'
    data = getDataFromTxt(ftxt,data_path=data_path)
    idx = 0
    #image_path bbox landmark(5*2)
    for (imgPath, bbox, landmarkGt) in data:
        #print imgPath
        F_imgs = []
        F_landmarks = []
        print(imgPath)
        img = cv2.imread(imgPath)

        assert(img is not None)
        img_h,img_w,img_c = img.shape
        gt_box = np.array([bbox.left,bbox.top,bbox.right,bbox.bottom])
        #get sub-image from bbox
        f_face = img[bbox.top:bbox.bottom+1,bbox.left:bbox.right+1]
        # resize the gt image to specified size
        f_face = cv2.resize(f_face,(size,size))
        #initialize the landmark
        landmark = np.zeros((5, 2))

        #normalize land mark by dividing the width and height of the ground truth bounding box
        # landmakrGt is a list of tuples
        for index, one in enumerate(landmarkGt):
            # (( x - bbox.left)/ width of bounding box, (y - bbox.top)/ height of bounding box
            rv = ((one[0]-gt_box[0])/(gt_box[2]-gt_box[0]), (one[1]-gt_box[1])/(gt_box[3]-gt_box[1]))
            # put the normalized value into the new list landmark
            landmark[index] = rv
        
        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        landmark = np.zeros((5, 2))        
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print(idx, "images done")
            x1, y1, x2, y2 = gt_box
            #gt's width
            gt_w = x2 - x1 + 1
            #gt's height
            gt_h = y2 - y1 + 1        
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue
            #random shift
            for i in range(10):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8), np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = int(max(x1+gt_w/2-bbox_size/2+delta_x,0))
                ny1 = int(max(y1+gt_h/2-bbox_size/2+delta_y,0))

                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1,ny1,nx2,ny2])


                cropped_im = img[ny1:ny2+1,nx1:nx2+1,:]
                resized_im = cv2.resize(cropped_im, (size, size))
                #cal iou
                iou = IoU(crop_box, np.expand_dims(gt_box,0))
                if iou > 0.65:
                    F_imgs.append(resized_im)
                    #normalize
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0]-nx1)/bbox_size, (one[1]-ny1)/bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1,2)
                    bbox = BBox([nx1,ny1,nx2,ny2])                    

                    #mirror                    
                    if random.choice([0,1]) > 0:
                        face_flipped, landmark_flipped = flip(resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        #c*h*w
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    #rotate
                    if random.choice([0,1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), 5)#逆时针旋转
                        #landmark_offset
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                
                        #flip
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))                
                    
                    #anti-clockwise rotation
                    if random.choice([0,1]) > 0: 
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), -5)#顺时针旋转
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10)) 
                    
            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
            #print F_imgs.shape
            #print F_landmarks.shape
            for i in range(len(F_imgs)):
                #if image_id % 100 == 0:

                    #print('image id : ', image_id)

                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:
                    continue

                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue

                cv2.imwrite(join(dstdir,"%d.jpg" %(image_id)), F_imgs[i])
                landmarks = map(str,list(F_landmarks[i]))
                f.write(join(dstdir,"%d.jpg" %(image_id))+" -2 "+" ".join(landmarks)+"\n")
                image_id = image_id + 1
            
    #print F_imgs.shape
    #print F_landmarks.shape
    #F_imgs = processImage(F_imgs)
    #shuffle_in_unison_scary(F_imgs, F_landmarks)
    
    f.close()
    return F_imgs,F_landmarks
示例#2
0
def GetLandmark(target):
    assert target in ['pnet', 'rnet', 'onet']
    resize = cfg.resize[target]
    landmark_save_dir = join(cfg.path_output_files, target + '_landmark')
    if not exists(landmark_save_dir): os.mkdir(landmark_save_dir)

    
    image_id = 0
    file = open(join(cfg.path_output_txt, target + '_landmark.txt'), 'w')
    data = ReadLandmarkData(cfg.path_landmark_labels, cfg.path_landmark_imgs)

    
    if cfg.debug:
        if len(data)>200: data = data[:200]
    
    #image_path bbox landmark(5*2)
    for (imgPath, bbox, landmarkGt) in tqdm(data):
        #print imgPath
        F_imgs = []
        F_landmarks = []  
        img = cv2.imread(imgPath)
        assert(img is not None)
        img_h,img_w,img_c = img.shape
        gt_box = np.array([bbox.left,bbox.top,bbox.right,bbox.bottom])
        f_face = img[bbox.top:bbox.bottom+1,bbox.left:bbox.right+1]
        f_face = cv2.resize(f_face,(resize,resize))
        landmark = np.zeros((5, 2))
        #normalize
        for index, one in enumerate(landmarkGt):
            rv = ((one[0]-gt_box[0])/(gt_box[2]-gt_box[0]), (one[1]-gt_box[1])/(gt_box[3]-gt_box[1]))
            landmark[index] = rv
        
        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        landmark = np.zeros((5, 2))


        x1, y1, x2, y2 = gt_box
        #gt's width
        gt_w = x2 - x1 + 1
        #gt's height
        gt_h = y2 - y1 + 1        
        if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0: continue
        #random shift
        for i in range(10):
            bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8), np.ceil(1.25 * max(gt_w, gt_h)))
            delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
            delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
            nx1 = int(max(x1+gt_w/2-bbox_size/2+delta_x,0))
            ny1 = int(max(y1+gt_h/2-bbox_size/2+delta_y,0))
            
            nx2 = nx1 + bbox_size
            ny2 = ny1 + bbox_size
            if nx2 > img_w or ny2 > img_h: continue
            crop_box = np.array([nx1,ny1,nx2,ny2])
            cropped_im = img[ny1:ny2+1,nx1:nx2+1,:]
            resized_im = cv2.resize(cropped_im, (resize, resize))
            #cal iou
            iou = IoU(crop_box, np.expand_dims(gt_box,0))
            if iou < 0.65: continue

            F_imgs.append(resized_im)
            #normalize
            for index, one in enumerate(landmarkGt):
                rv = ((one[0]-nx1)/bbox_size, (one[1]-ny1)/bbox_size)
                landmark[index] = rv

            F_landmarks.append(landmark.reshape(10))
            landmark = np.zeros((5, 2))
            landmark_ = F_landmarks[-1].reshape(-1,2)
            bbox = BBox([nx1,ny1,nx2,ny2])                    

            #mirror                    
            if random.choice([0,1]) > 0:
                face_flipped, landmark_flipped = flip(resized_im, landmark_)
                face_flipped = cv2.resize(face_flipped, (resize, resize))
                #c*h*w
                F_imgs.append(face_flipped)
                F_landmarks.append(landmark_flipped.reshape(10))
            #rotate
            if random.choice([0,1]) > 0:
                face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                 bbox.reprojectLandmark(landmark_), 5)#逆时针旋转
                #landmark_offset
                landmark_rotated = bbox.projectLandmark(landmark_rotated)
                face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (resize, resize))
                F_imgs.append(face_rotated_by_alpha)
                F_landmarks.append(landmark_rotated.reshape(10))
        
                #flip
                face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                face_flipped = cv2.resize(face_flipped, (resize, resize))
                F_imgs.append(face_flipped)
                F_landmarks.append(landmark_flipped.reshape(10))                
            
            #inverse clockwise rotation
            if random.choice([0,1]) > 0: 
                face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                 bbox.reprojectLandmark(landmark_), -5)#顺时针旋转
                landmark_rotated = bbox.projectLandmark(landmark_rotated)
                face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (resize, resize))
                F_imgs.append(face_rotated_by_alpha)
                F_landmarks.append(landmark_rotated.reshape(10))
        
                face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                face_flipped = cv2.resize(face_flipped, (resize, resize))
                F_imgs.append(face_flipped)
                F_landmarks.append(landmark_flipped.reshape(10)) 
                
        F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)

        for i in range(len(F_imgs)):

            if np.sum(F_landmarks[i] <= 0) > 0: continue

            if np.sum(F_landmarks[i] >= 1) > 0: continue

            cv2.imwrite(join(landmark_save_dir,'%d.jpg' %(image_id)), F_imgs[i])
            landmarks = map(str,list(F_landmarks[i]))
            name = join('%s_landmark'%target, '%d.jpg'%image_id)
            file.write(name +" -2 "+" ".join(landmarks)+"\n")
            image_id = image_id + 1
        
    file.close()
示例#3
0
def GenerateData(ftxt, output, net, argument=False):
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print 'Net type error'
        return
    image_id = 0
    f = open(join(OUTPUT, "landmark_%s_aug.txt" % (size)), 'w')
    data = getDataFromTxt(ftxt)
    idx = 0
    #image_path bbox landmark(5*2)
    for (imgPath, bbox, landmarkGt) in data:
        #print imgPath
        F_imgs = []
        F_landmarks = []
        path = os.path.join(
            data_path,
            imgPath.split('\\')[0] + '/' + imgPath.split('\\')[1])
        img = cv2.imread(path)
        assert (img is not None)
        img_h, img_w, img_c = img.shape
        gt_box = np.array([bbox.left, bbox.top, bbox.right, bbox.bottom])
        f_face = img[bbox.top:bbox.bottom + 1, bbox.left:bbox.right + 1]
        f_face = cv2.resize(f_face, (size, size))
        landmark = np.zeros((5, 2))
        #normalize
        for index, one in enumerate(landmarkGt):
            rv = ((one[0] - gt_box[0]) / (gt_box[2] - gt_box[0]),
                  (one[1] - gt_box[1]) / (gt_box[3] - gt_box[1]))
            landmark[index] = rv

        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        landmark = np.zeros((5, 2))
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print idx, "images done"
            x1, y1, x2, y2 = gt_box
            #gt's width
            gt_w = x2 - x1 + 1
            #gt's height
            gt_h = y2 - y1 + 1
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue
            #random shift
            for i in range(10):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8),
                                        np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = max(x1 + gt_w / 2 - bbox_size / 2 + delta_x, 0)
                ny1 = max(y1 + gt_h / 2 - bbox_size / 2 + delta_y, 0)

                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])
                cropped_im = img[ny1:ny2 + 1, nx1:nx2 + 1, :]
                resized_im = cv2.resize(cropped_im, (size, size))
                #cal iou
                iou = IoU(crop_box, np.expand_dims(gt_box, 0))
                if iou > 0.65:
                    F_imgs.append(resized_im)
                    #normalize
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0] - nx1) / bbox_size,
                              (one[1] - ny1) / bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1, 2)
                    bbox = BBox([nx1, ny1, nx2, ny2])

                    # mirror
                    if random.choice([0, 1]) > 0:
                        face_flipped, landmark_flipped = flip(
                            resized_im, landmark_)
                        face_flipped = cv2.resize(
                            face_flipped, (size, size),
                            interpolation=cv2.INTER_LINEAR)
                        # c*h*w
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    # rotate
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(
                            img, bbox, bbox.reprojectLandmark(landmark_),
                            5)  # 逆时针旋转
                        # landmark_offset
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size),
                            interpolation=cv2.INTER_LINEAR)
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        # flip
                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(
                            face_flipped, (size, size),
                            interpolation=cv2.INTER_LINEAR)
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

                    # if random.choice([0, 1]) > 0:
                    #     face_rotated_by_alpha, landmark_rotated = rotate(img, bbox,
                    #                                                      bbox.reprojectLandmark(landmark_), 15)  # 逆时针旋转
                    #     # landmark_offset
                    #     landmark_rotated = bbox.projectLandmark(landmark_rotated)
                    #     face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_rotated_by_alpha)
                    #     F_landmarks.append(landmark_rotated.reshape(10))
                    #
                    #     # flip
                    #     face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                    #     face_flipped = cv2.resize(face_flipped, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_flipped)
                    #     F_landmarks.append(landmark_flipped.reshape(10))
                    #
                    # if random.choice([0, 1]) > 0:
                    #     face_rotated_by_alpha, landmark_rotated = rotate(img, bbox,
                    #                                                      bbox.reprojectLandmark(landmark_), 45)  # 逆时针旋转
                    #     # landmark_offset
                    #     landmark_rotated = bbox.projectLandmark(landmark_rotated)
                    #     face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_rotated_by_alpha)
                    #     F_landmarks.append(landmark_rotated.reshape(10))
                    #
                    #     # flip
                    #     face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                    #     face_flipped = cv2.resize(face_flipped, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_flipped)
                    #     F_landmarks.append(landmark_flipped.reshape(10))
                    #
                    # if random.choice([0, 1]) > 0:
                    #     face_rotated_by_alpha, landmark_rotated = rotate(img, bbox,
                    #                                                      bbox.reprojectLandmark(landmark_), 90)  # 逆时针旋转
                    #     # landmark_offset
                    #     landmark_rotated = bbox.projectLandmark(landmark_rotated)
                    #     face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_rotated_by_alpha)
                    #     F_landmarks.append(landmark_rotated.reshape(10))
                    #
                    #     # flip
                    #     face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                    #     face_flipped = cv2.resize(face_flipped, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_flipped)
                    #     F_landmarks.append(landmark_flipped.reshape(10))

                    # inverse clockwise rotation
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(
                            img, bbox, bbox.reprojectLandmark(landmark_),
                            -5)  # 顺时针旋转
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size),
                            interpolation=cv2.INTER_LINEAR)
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(
                            face_flipped, (size, size),
                            interpolation=cv2.INTER_LINEAR)
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

                    # if random.choice([0, 1]) > 0:
                    #     face_rotated_by_alpha, landmark_rotated = rotate(img, bbox,
                    #                                                      bbox.reprojectLandmark(landmark_),
                    #                                                      -15)  # 逆时针旋转
                    #     # landmark_offset
                    #     landmark_rotated = bbox.projectLandmark(landmark_rotated)
                    #     face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_rotated_by_alpha)
                    #     F_landmarks.append(landmark_rotated.reshape(10))
                    #
                    #     # flip
                    #     face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                    #     face_flipped = cv2.resize(face_flipped, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_flipped)
                    #     F_landmarks.append(landmark_flipped.reshape(10))
                    #
                    # if random.choice([0, 1]) > 0:
                    #     face_rotated_by_alpha, landmark_rotated = rotate(img, bbox,
                    #                                                      bbox.reprojectLandmark(landmark_),
                    #                                                      -45)  # 逆时针旋转
                    #     # landmark_offset
                    #     landmark_rotated = bbox.projectLandmark(landmark_rotated)
                    #     face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_rotated_by_alpha)
                    #     F_landmarks.append(landmark_rotated.reshape(10))
                    #
                    #     # flip
                    #     face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                    #     face_flipped = cv2.resize(face_flipped, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_flipped)
                    #     F_landmarks.append(landmark_flipped.reshape(10))
                    #
                    # if random.choice([0, 1]) > 0:
                    #     face_rotated_by_alpha, landmark_rotated = rotate(img, bbox,
                    #                                                      bbox.reprojectLandmark(landmark_),
                    #                                                      -90)  # 逆时针旋转
                    #     # landmark_offset
                    #     landmark_rotated = bbox.projectLandmark(landmark_rotated)
                    #     face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_rotated_by_alpha)
                    #     F_landmarks.append(landmark_rotated.reshape(10))
                    #
                    #     # flip
                    #     face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                    #     face_flipped = cv2.resize(face_flipped, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_flipped)
                    #     F_landmarks.append(landmark_flipped.reshape(10))
                    #
                    # if random.choice([0, 1]) > 0:
                    #     face_rotated_by_alpha, landmark_rotated = rotate(img, bbox,
                    #                                                      bbox.reprojectLandmark(landmark_),
                    #                                                      180)  # 逆时针旋转
                    #     # landmark_offset
                    #     landmark_rotated = bbox.projectLandmark(landmark_rotated)
                    #     face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_rotated_by_alpha)
                    #     F_landmarks.append(landmark_rotated.reshape(10))
                    #
                    #     # flip
                    #     face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                    #     face_flipped = cv2.resize(face_flipped, (size, size), interpolation=cv2.INTER_LINEAR)
                    #     F_imgs.append(face_flipped)
                    #     F_landmarks.append(landmark_flipped.reshape(10))

            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
            #print F_imgs.shape
            #print F_landmarks.shape
            for i in range(len(F_imgs)):
                print image_id

                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:
                    continue

                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue

                cv2.imwrite(join(dstdir, "%d.jpg" % (image_id)), F_imgs[i])
                landmarks = map(str, list(F_landmarks[i]))
                f.write(
                    join(dstdir, "%d.jpg" % (image_id)) + " -2 " +
                    " ".join(landmarks) + "\n")
                image_id = image_id + 1

    #print F_imgs.shape
    #print F_landmarks.shape
    #F_imgs = processImage(F_imgs)
    #shuffle_in_unison_scary(F_imgs, F_landmarks)

    f.close()
    return F_imgs, F_landmarks
示例#4
0
def GenerateData(ftxt, output,net,argument=False):
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print('Net type error')
        return
    image_id = 0
	#记录label的txt
    f = open(join(OUTPUT,"landmark_%s_aug.txt" %(size)),'w')
    data = getDataFromTxt(ftxt)
    idx = 0
    #image_path bbox landmark(5*2)
    for (imgPath, bbox, landmarkGt) in data:
        #存储人脸图片和关键点
        F_imgs = []
        F_landmarks = []
        #print(imgPath)
        img = cv2.imread(imgPath)
        assert(img is not None)
        img_h,img_w,img_c = img.shape
        gt_box = np.array([bbox.left,bbox.top,bbox.right,bbox.bottom])
        #人脸图片
        f_face = img[bbox.top:bbox.bottom+1,bbox.left:bbox.right+1]
        #resize成网络输入大小
        f_face = cv2.resize(f_face,(size,size))
        landmark = np.zeros((5, 2))
        #normalize
        for index, one in enumerate(landmarkGt):
            #关键点相对于左上坐标偏移量并归一化
            rv = ((one[0]-gt_box[0])/(gt_box[2]-gt_box[0]), (one[1]-gt_box[1])/(gt_box[3]-gt_box[1]))
            landmark[index] = rv
        
        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        landmark = np.zeros((5, 2))        
        if argument:
            #对图像变换
            idx = idx + 1
			#只取100张照片 节省时间 测试用
            if idx % 100 == 0:
                print(idx, "images done")
                break
            x1, y1, x2, y2 = gt_box
            #gt's width
            gt_w = x2 - x1 + 1
            #gt's height
            gt_h = y2 - y1 + 1        
            #除去过小图像
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue
            #random shift,每张照片处理10次
            for i in range(10):
                #随机裁剪图像大小
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8), np.ceil(1.25 * max(gt_w, gt_h)))
                #随机左上坐标偏移量
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                #计算左上坐标
                nx1 = max(x1+gt_w/2-bbox_size/2+delta_x,0)
                ny1 = max(y1+gt_h/2-bbox_size/2+delta_y,0)
                
                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                #除去超过边界的
                if nx2 > img_w or ny2 > img_h:
                    continue
                #裁剪边框,图片
                crop_box = np.array([nx1,ny1,nx2,ny2])
                nx1, nx2, ny1, ny2 = int(nx1), int(nx2), int(ny1), int(ny2)
                cropped_im = img[ny1:ny2+1,nx1:nx2+1,:]
                resized_im = cv2.resize(cropped_im, (size, size))
                #计算 iou
                iou = IoU(crop_box, np.expand_dims(gt_box,0))
                #只保留pos图像
                if iou > 0.65:
                    F_imgs.append(resized_im)
                    #关键点相对偏移 normalize
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0]-nx1)/bbox_size, (one[1]-ny1)/bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1,2)
                    bbox = BBox([nx1,ny1,nx2,ny2])                    
                    #镜像
                    #mirror                    
                    if random.choice([0,1]) > 0:
                        face_flipped, landmark_flipped = flip(resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        #c*h*w
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    #逆时针翻转
                    if random.choice([0,1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), 5)#逆时针旋转
                        #关键点偏移
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                
                        #左右翻转
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))                
                    #顺时针翻转
                    #inverse clockwise rotation
                    if random.choice([0,1]) > 0: 
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), -5)#顺时针旋转
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10)) 
                    
            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
            ##print F_imgs.shape
            ##print F_landmarks.shape
            for i in range(len(F_imgs)):
                #print image_id
                cv2.imwrite(join(dstdir,"%d.jpg" %(image_id)), F_imgs[i])
                landmarks = list(map(str,list(F_landmarks[i])))
                f.write(join(dstdir,"%d.jpg" %(image_id))+" -2 "+" ".join(landmarks)+"\n")
                image_id = image_id + 1
            
    ##print F_imgs.shape
    ##print F_landmarks.shape
    #F_imgs = processImage(F_imgs)
    #shuffle_in_unison_scary(F_imgs, F_landmarks)
    
    f.close()
    return F_imgs,F_landmarks
示例#5
0
def generate_data(data, save_folder, landmark_aug_save_folder, imsize,
                  augment):

    savefile = os.path.join(save_folder, "landmark_{}_aug.txt".format(imsize))
    if os.path.exists(savefile):
        raise Exception("Save file already exists: {}".format(savefile))

    fid = open(savefile, 'w')
    index = 0

    print("Generating landmark data for {} images".format(len(data)))

    for img_idx, (imgPath, bbox, landmarkGt) in enumerate(data):

        if img_idx % 10000 == 0:
            print("Image {} / {}".format(img_idx, len(data)))

        F_imgs = []
        F_landmarks = []
        img = cv2.imread(imgPath)
        img_h, img_w, img_c = img.shape
        gt_box = np.array([bbox.left, bbox.top, bbox.right, bbox.bottom])
        f_face = img[bbox.top:bbox.bottom + 1, bbox.left:bbox.right + 1]
        f_face = cv2.resize(f_face, (imsize, imsize))

        # landmark: shifted and normalized by width and height
        landmark = (landmarkGt - gt_box[0:2]).astype(float) / np.array(
            [[bbox.w, bbox.h]])

        F_imgs.append(f_face)
        F_landmarks.append(landmark.ravel())

        if augment:
            x1, y1, x2, y2 = gt_box
            gt_w = x2 - x1 + 1
            gt_h = y2 - y1 + 1

            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue

            # random shift
            for i in range(10):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8),
                                        np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = max(x1 + gt_w / 2 - bbox_size / 2 + delta_x, 0)
                ny1 = max(y1 + gt_h / 2 - bbox_size / 2 + delta_y, 0)

                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])
                cropped_im = img[ny1:ny2 + 1, nx1:nx2 + 1, :]
                resized_im = cv2.resize(cropped_im, (imsize, imsize))

                iou = IoU(crop_box, np.expand_dims(gt_box, 0))
                if iou > 0.65:
                    F_imgs.append(resized_im)
                    # normalize
                    landmark = (landmarkGt -
                                np.array([[nx1, ny1]])) / bbox_size
                    F_landmarks.append(landmark.reshape(10))
                    landmark_ = F_landmarks[-1].reshape(-1, 2)
                    bbox = BBox([nx1, ny1, nx2, ny2])

                    # mirror
                    if random.choice([0, 1]) > 0:
                        face_flipped, landmark_flipped = flip(
                            resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped,
                                                  (imsize, imsize))
                        # c*h*w
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    # rotate
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(
                            img, bbox, bbox.reprojectLandmark(landmark_),
                            5)  # 逆时针旋转
                        # landmark_offset
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (imsize, imsize))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        # flip
                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped,
                                                  (imsize, imsize))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

                    # inverse clockwise rotation
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(
                            img, bbox, bbox.reprojectLandmark(landmark_),
                            -5)  # 顺时针旋转
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (imsize, imsize))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped,
                                                  (imsize, imsize))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

        for i in range(len(F_imgs)):

            if np.any(F_landmarks[i] <= 0) or np.any(F_landmarks[i] >= 1):
                continue

            # save image
            img_filepath = os.path.join(landmark_aug_save_folder,
                                        "{}.jpg".format(index))
            cv2.imwrite(img_filepath, F_imgs[i])

            # save meta information
            landmark_str = F_landmarks[i].tolist()
            landmark_str = map(str, landmark_str)
            landmark_str = " ".join(landmark_str)
            line = img_filepath + " -2 " + landmark_str + "\n"
            fid.write(line)

            index = index + 1

    fid.close()
    return F_imgs, F_landmarks
示例#6
0
def GenerateData(ftxt, data_path, net, argument = False):
"""

"""
    """根据网络名来判断size大小"""
    if net == 'PNet':
        size = 12
    elif net == 'RNet':
        size = 24
    elif net == 'ONet':
        size = 48
    else:
        print('不是PNet,RNet,ONet,net类型错误,请检查')
        return
    
    image_id = 0
    f = open(os.path.join(OUTPUT, 'landmark_%s_aug.txt'%(size)), 'w')
    data = getDataFromTxt(ftxt, data_path = data_path)
    #将txt的1234对应BBOx的0123,左上右下
    #列表 带有标记位置的路径,label框,标记点坐标
    idx = 0#计数图片标记点,label狂的信息提取出了多少
    for (imgPath, bbox, landmarkGt) in data:
    """遍历txt文件中的所有数据"""
        F_imgs = []#每张照片的所有人脸图像
        F_landmarks = []#每张照片的所有标记点
        img = cv2.imread(imgPath)
        #读取图像路径的图片
        assert(img is not None)
        img_h, img_w, img_c = img.shape
        gt_box = np.array([bbox.left, bbox.top,
                           bbox.right, bbox.bottom])
        #gt_box:左下右上,0123 txt,左右下上
        """作者把上下名称颠倒了"""
        f_face = img[bbox.top : bbox.bootom+1, bbox.left : bbox.right+1]
        #像素点
        f_face = cv2.resize(f_face, (size, size))
        #读取人脸图像
        landmark = np.zeros((5, 2))
        for index, one in enumerate(landmarkGt):
        """遍历每一个图像的5个标记点,
        index:0-4
        one: 标记点的位置
        """
            rv = ((one[0] - gt_box[0]) / (gt_box[2] - gt_box[0]), 
                  (one[1] - gt_box[1]) / (gt_box[3] - gt_box[1]))
            #关键点的位置信息转换成比例,距离左下角的距离和边长的比例
            landmark[index] = rv
        F_imgs.append(f_face)
        F_landmarks.append(lanmark.reshap(10))
        landmark = np.zeros((5, 2))
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print(idx, 'images done')
            x1, y1, x2, y2 = gt_box
            #label框的坐上
            gt_w = x2 - x1 + 1
            gt_h = y2 - y1 + 1
            if max(gt_w, gt_h) < 40 or x1< 0 or y1 < 0:
            #如果出错就舍弃这张图片
                continue
            for i in range(10):
               bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8),
                                       np.ceil(1.25 * max(gt_w, gt_h)))
               #偏移量是少20%的最小值,多20%的最大值
               delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
               delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
               nx1 = int(max(x1 + gt_w/2 - bbox_size/2 + delta_x, 0))
               ny1 = int(max(y1 + gt_h/2 - bbox_size/2 + delta_y, 0))
               #label框随机移动后中心点的位置在,构造label框和移动后
               #label框中心重合,但是x1, y1大于0
               nx2 = nx1 + bbox_size
               ny2 = ny1 + bbox_size
               if nx2 > img_w or ny2 > img_h:
               #x2,y2还必须在图片内
                   continue
               
               crop_box = np.array([nx1, ny1, nx2, ny2])
               cropped_im = img[ny1 : ny2+1, nx1 : nx2+1,:]
               resized_im = cv2.resize(cropped_im, (size, size))
               iou = IoU(crop_box, np.expand_dims(gt_box, 0))
               if iou > 0.65:
                   F_imgs.append(resized_im)
                   #这张图片前期添加了一个label框的像素图像了
                   #现在添加进去的是构造label框的图像
                   for index, one in enumerate(landmarkGt):
                       #将关键点相对于构造label框的信息添加进去
                       rv = ((one[0] - nx1) / bbox_size,
                             (one[1] - ny1) / bbox_size)
                       landmark[index] = rv
                      
                    F_landmarks.append(landmark.reshape(10))
                    #这张图片前期添加了一个label框的关键点的信息了
                   #现在添加进去的是构造label框的关键点的信息了
                    landmark = np.zeros((5, 2))
                    landmark_ = Flandmarks[-1].reshape(-1, 2)
                    bbox = BBox([nx1, ny1, nx2, ny2])
                    """随机镜像"""
                    if random.choice([0, 1]) > 0:
                        face_flipped, landmark_flipped = \
                        flip(resized_im, landmark)
                        face_flipped = cv2.resize(face_flipped,
                                                  (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    """随机反转"""
                    if random.choice([0, 1]) > 0:
                        face_roteted_by_alpha, landmark_rotated = \
                        rotate(img, bbox, 
                               bbox.reprojectLandmark(landmark_),5)
示例#7
0
def GenerateData(ftxt, output, net, argument=False):
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print('Net type error')
        return
    image_id = 0
    f = open(join(OUTPUT, "landmark_%s_aug.txt" % (size)), 'w')
    data = getDataFromTxt(
        ftxt
    )  # ftxt == prepare_data/trainImageList.txt, 存储有lfw_5590内保存的landmark信息
    idx = 0

    for (
            imgPath, bbox, landmarkGt
    ) in data:  # 一条data记录:lfw_5590\Aaron_Eckhart_0001.jpg 84 161 92 169 106.25 107.75 146.75 112.25 125.25 142.75 105.25 157.75 139.75 161.75
        F_imgs = []  #print imgPath
        F_landmarks = []
        img = cv2.imread(imgPath)
        assert (img is not None)
        img_h, img_w, img_c = img.shape

        gt_box = np.array([bbox.left, bbox.top, bbox.right, bbox.bottom])
        f_face_raw = img[bbox.top:bbox.bottom + 1, bbox.left:bbox.right + 1]
        f_face = cv2.resize(f_face_raw, (size, size))
        landmark = np.zeros((5, 2))

        for index, one in enumerate(landmarkGt):  # 归1化处理
            rv = ((one[0] - gt_box[0]) / (gt_box[2] - gt_box[0]),
                  (one[1] - gt_box[1]) / (gt_box[3] - gt_box[1]))
            landmark[index] = rv

        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        landmark = np.zeros((5, 2))
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print(idx, "images done")
            x1, y1, x2, y2 = gt_box
            gt_w = x2 - x1 + 1  # gt's width
            gt_h = y2 - y1 + 1  # gt's height

            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue

            for i in range(10):  # random shift
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8),
                                        np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = max(x1 + gt_w / 2 - bbox_size / 2 + delta_x, 0)
                ny1 = max(y1 + gt_h / 2 - bbox_size / 2 + delta_y, 0)
                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])
                cropped_im = img[int(ny1):int(ny2) + 1,
                                 int(nx1):int(nx2) + 1, :]
                resized_im = cv2.resize(cropped_im, (size, size))

                iou = IoU(crop_box, np.expand_dims(gt_box, 0))  #cal iou
                if iou > 0.65:
                    F_imgs.append(resized_im)
                    #normalize
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0] - nx1) / bbox_size,
                              (one[1] - ny1) / bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1, 2)
                    bbox = BBox([nx1, ny1, nx2, ny2])

                    #mirror
                    if random.choice([0, 1]) > 0:
                        face_flipped, landmark_flipped = flip(
                            resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        #c*h*w
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    #rotate
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), 5)#逆时针旋转
                        #landmark_offset
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        #flip
                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

                    #inverse clockwise rotation
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), -5)#顺时针旋转
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)

            for i in range(len(F_imgs)):
                print(image_id)
                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:
                    continue
                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue

                cv2.imwrite(join(dstdir, "%d.jpg" % (image_id)), F_imgs[i])
                landmarks = map(str, list(F_landmarks[i]))
                f.write(
                    join(dstdir, "%d.jpg" % (image_id)) + " -2 " +
                    " ".join(landmarks) + "\n")
                image_id = image_id + 1
    f.close()
    return F_imgs, F_landmarks
def GenerateData(ftxt, output, net, argument=False):
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print('Net type error')
        return
    image_id = 0
    f = open(
        join(OUTPUT, "landmark_%s_aug.txt" % (size)),
        'w')  # OUTPUT = '12',  size = 12 or 24 or 48 根据PNET,RNET,ONET 不同而不同
    #dstdir = "train_landmark_few"

    # ftxt就是trainImageList.txt 文件
    # sample line in the file: lfw_5590/Aaron_Eckhart_0001.jpg 84 161 92 169 106.250000 107.750000 146.750000 112.250000 125.250000 142.750000 105.250000 157.750000 139.750000 161.750000
    data = getDataFromTxt(ftxt)

    idx = 0
    #image_path bbox landmark(5*2)
    for (imgPath, bbox, landmarkGt) in data:  # 对应上面的sample line 就可以看出数据的结构
        #print imgPath
        F_imgs = []
        F_landmarks = []
        img = cv2.imread(imgPath)
        if img is None:
            continue
        img_h, img_w, img_c = img.shape
        gt_box = np.array([bbox.left, bbox.top, bbox.right, bbox.bottom])
        f_face = img[bbox.top:bbox.bottom + 1, bbox.left:bbox.right + 1]
        f_face = cv2.resize(f_face,
                            (size, size))  # 根据PNET,ONET,RNET 不同这里的size也不同
        landmark = np.zeros((5, 2))  # 初始化

        #normalize
        # (one[0] one[1])一共5个,每组表示每个landmark的坐标。 (gt_box[0], gt_box[1]) box 左上角坐标, (gt_box[2], gt_box[3]) 表示box  右下脚坐标
        for index, one in enumerate(
                landmarkGt
        ):  # landmark在train的时候是根据ground truth左上角来的,在没抠出图片之前是根据原图的左上角坐标来的,抠出来以后,又根据抠出来小图的左上角来的
            rv = ((one[0] - gt_box[0]) / (gt_box[2] - gt_box[0]),
                  (one[1] - gt_box[1]) / (gt_box[3] - gt_box[1]))
            landmark[index] = rv

        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        landmark = np.zeros((5, 2))

        # 对landmark数据增强,因为 WIDER FACE 里人脸检测的trainig data 一共32,032图片并包含393,703人脸,而我们这里landmark traing data 太少 只有10000多张图片
        # 其实就是random的平移一下
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print("%d images done" % idx)
            x1, y1, x2, y2 = gt_box
            #gt's width
            gt_w = x2 - x1 + 1
            #gt's height
            gt_h = y2 - y1 + 1
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue
            #random shift
            for i in range(10):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8),
                                        np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = int(max(x1 + gt_w / 2 - bbox_size / 2 + delta_x, 0))
                ny1 = int(max(y1 + gt_h / 2 - bbox_size / 2 + delta_y, 0))

                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])
                cropped_im = img[ny1:ny2 + 1, nx1:nx2 + 1, :]
                resized_im = cv2.resize(cropped_im, (size, size))
                #cal iou
                iou = IoU(crop_box, np.expand_dims(gt_box, 0))
                if iou > 0.65:  # 如果平移完了,与ground truth iou还是大于 0.65,我们认为还是landmark的样本
                    F_imgs.append(resized_im)
                    #normalize
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0] - nx1) / bbox_size,
                              (one[1] - ny1) / bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1, 2)
                    bbox = BBox([nx1, ny1, nx2, ny2])

                    #mirror i.e. 左右翻转一下图片
                    if random.choice([0, 1]) > 0:
                        face_flipped, landmark_flipped = flip(
                            resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        #c*h*w
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    #rotate i.e.  随机旋转
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), 5)#逆时针旋转
                        #landmark_offset
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        #flip
                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

                    #inverse clockwise rotation
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), -5)#顺时针旋转
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
            #print F_imgs.shape
            #print F_landmarks.shape
            for i in range(len(F_imgs)):
                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:
                    continue

                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue

                cv2.imwrite(join(dstdir, "%d.jpg" % (image_id)), F_imgs[i])
                landmarks = map(str, list(F_landmarks[i]))
                f.write(
                    join(dstdir, "%d.jpg" % (image_id)) + " -2 " +
                    " ".join(landmarks) + "\n")
                image_id = image_id + 1

    #print F_imgs.shape
    #print F_landmarks.shape
    #F_imgs = processImage(F_imgs)
    #shuffle_in_unison_scary(F_imgs, F_landmarks)

    f.close()
    return F_imgs, F_landmarks
def GenerateData(ftxt, output,net,argument=False):
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print 'Net type error'
        return
    image_id = 0
    f = open(join(OUTPUT,"landmark_%s_aug.txt" %(size)),'w')
    #dstdir = "train_landmark_few"
   
    data = getDataFromTxt(ftxt)
    idx = 0
    #image_path bbox landmark(5*2)
    for (imgPath, bbox, landmarkGt) in data:
        #print imgPath
        F_imgs = []
        F_landmarks = []        
        img = cv2.imread(imgPath)
        assert(img is not None)
        img_h,img_w,img_c = img.shape
        gt_box = np.array([bbox.left,bbox.top,bbox.right,bbox.bottom])
        f_face = img[bbox.top:bbox.bottom+1,bbox.left:bbox.right+1]
        f_face = cv2.resize(f_face,(size,size))
        landmark = np.zeros((5, 2))
        #normalize
        for index, one in enumerate(landmarkGt):
            rv = ((one[0]-gt_box[0])/(gt_box[2]-gt_box[0]), (one[1]-gt_box[1])/(gt_box[3]-gt_box[1]))
            landmark[index] = rv
        
        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        landmark = np.zeros((5, 2))        
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print idx, "images done"
            x1, y1, x2, y2 = gt_box
            #gt's width
            gt_w = x2 - x1 + 1
            #gt's height
            gt_h = y2 - y1 + 1        
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue
            #random shift
            for i in range(10):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8), np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = max(x1+gt_w/2-bbox_size/2+delta_x,0)
                ny1 = max(y1+gt_h/2-bbox_size/2+delta_y,0)
                
                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1,ny1,nx2,ny2])
                cropped_im = img[ny1:ny2+1,nx1:nx2+1,:]
                resized_im = cv2.resize(cropped_im, (size, size))
                #cal iou
                iou = IoU(crop_box, np.expand_dims(gt_box,0))
                if iou > 0.65:
                    F_imgs.append(resized_im)
                    #normalize
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0]-nx1)/bbox_size, (one[1]-ny1)/bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1,2)
                    bbox = BBox([nx1,ny1,nx2,ny2])                    

                    #mirror                    
                    if random.choice([0,1]) > 0:
                        face_flipped, landmark_flipped = flip(resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        #c*h*w
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                    #rotate
                    if random.choice([0,1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), 5)#逆时针旋转
                        #landmark_offset
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                
                        #flip
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))                
                    
                    #inverse clockwise rotation
                    if random.choice([0,1]) > 0: 
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), -5)#顺时针旋转
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10)) 
                    
            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
            #print F_imgs.shape
            #print F_landmarks.shape
            for i in range(len(F_imgs)):
                print image_id

                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:
                    continue

                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue

                cv2.imwrite(join(dstdir,"%d.jpg" %(image_id)), F_imgs[i])
                landmarks = map(str,list(F_landmarks[i]))
                f.write(join(dstdir,"%d.jpg" %(image_id))+" -2 "+" ".join(landmarks)+"\n")
                image_id = image_id + 1
            
    #print F_imgs.shape
    #print F_landmarks.shape
    #F_imgs = processImage(F_imgs)
    #shuffle_in_unison_scary(F_imgs, F_landmarks)
    
    f.close()
    return F_imgs,F_landmarks
def GenerateData(ftxt, output, net, argument=False):
    if net == 'PNet':
        size = 12
    elif net == 'RNet':
        size = 24
    elif net == 'ONet':
        size = 48
    else:
        return
    image_id = 0
    f = open(join(OUTPUT, "landmark_%s_aug.txt" % (size)), 'w')
    data = getDataFromTxt(ftxt)  #获取注解文件信息

    idx = 0
    for (imgPath, bbox, landmarkGt) in data:
        F_imgs = []  #存储图片信息
        F_landmarks = []  #存储坐标
        img = cv2.imread(imgPath)
        img_h, img_w, img_c = img.shape
        gt_box = np.array([bbox.left, bbox.top, bbox.right,
                           bbox.bottom])  #获取真实的包围框
        f_face = img[bbox.top:bbox.bottom + 1, bbox.left:bbox.right + 1]
        f_face = cv2.resize(f_face, (size, size))  #截取人脸图片并转换尺寸
        landmark = np.zeros((5, 2))
        #标准化
        for index, one in enumerate(landmarkGt):
            rv = ((one[0] - gt_box[0]) / (gt_box[2] - gt_box[0]),
                  (one[1] - gt_box[1]) / (gt_box[3] - gt_box[1]))  #规划到0-1之间
            landmark[index] = rv

        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        if argument:
            idx = idx + 1
            x1, y1, x2, y2 = gt_box
            gt_w = x2 - x1 + 1
            gt_h = y2 - y1 + 1
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue
            # random shift
            for i in range(10):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8),
                                        np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = int(max(x1 + gt_w / 2 - bbox_size / 2 + delta_x, 0))
                ny1 = int(max(y1 + gt_h / 2 - bbox_size / 2 + delta_y, 0))

                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])
                cropped_im = img[ny1:ny2 + 1, nx1:nx2 + 1, :]
                resized_im = cv2.resize(cropped_im, (size, size))
                iou = IoU(crop_box, np.expand_dims(gt_box, 0))
                if iou > 0.65:
                    F_imgs.append(resized_im)
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0] - nx1) / bbox_size,
                              (one[1] - ny1) / bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1, 2)
                    bbox = BBox([nx1, ny1, nx2, ny2])

                    if random.choice([0, 1]) > 0:
                        face_flipped, landmark_flipped = flip(
                            resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped, (size, size))

                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), 5)  # 逆时针旋转
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), -5)  # 顺时针旋转
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))

            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)

            for i in range(len(F_imgs)):
                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:
                    continue
                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue
                cv2.imwrite(join(dstdir, "%d.jpg" % (image_id)), F_imgs[i])
                landmarks = map(str, list(F_landmarks[i]))
                f.write(
                    join(dstdir, "%d.jpg" % (image_id)) + " -2 " +
                    " ".join(landmarks) + "\n")
                image_id = image_id + 1
    f.close()
    return F_imgs, F_landmarks
示例#11
0
def GenerateData(ftxt, output,net,argument=False):
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print('Net type error')
        return
    image_id = 0
    f = open(join(OUTPUT,"landmark_%s_aug.txt" %(size)), 'w')
    #dstdir = "train_landmark_few"
   
    data = getDataFromTxt(ftxt)  # ftxt == trainImageList.txt,从文本里面拿到每张图片人脸的3份数据(文件名,人脸框,特征点坐标 )
    idx = 0
    for (imgPath, bbox, landmarkGt) in data:      #遍历每张原始照片,image_path bbox landmark(5*2)
        F_imgs = []
        F_landmarks = []        
        img = cv2.imread(imgPath)
        assert(img is not None)
        img_h, img_w, img_c = img.shape
        gt_box = np.array([bbox.left, bbox.top, bbox.right, bbox.bottom])  # 标签中人脸的区域
        f_face = img[bbox.top: bbox.bottom+1, bbox.left: bbox.right+1]  # 拿到标签中人脸的矩形框
        f_face = cv2.resize(f_face, (size, size))   # 将人脸resize成12 * 12大小
        landmark = np.zeros((5, 2))
        #normalize
        for index, one in enumerate(landmarkGt):  # 遍历人脸5个特征点的label,index是数组下标。
            rv = ((one[0]-gt_box[0])/(gt_box[2]-gt_box[0]), (one[1]-gt_box[1])/(gt_box[3]-gt_box[1]))  # rv是一个(x, y),标识1个特征点坐标,做归一化处理,相对于label框框的比例
            landmark[index] = rv
        
        F_imgs.append(f_face)  # 将1个人脸像素数据加入F_img的list中
        F_landmarks.append(landmark.reshape(10))  # 将1个人脸特征点数据加入F_landmarks的list中
        landmark = np.zeros((5, 2))        
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print(idx, "images done")
            x1, y1, x2, y2 = gt_box  # 点1(x1,y1)位于左上角,点2(x2,y2)位于右下角
            gt_w = x2 - x1 + 1    # gt's width
            gt_h = y2 - y1 + 1    # gt's height
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:  # 过小的图片 && 非法图片忽略
                continue
            #random shift
            for i in range(10):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8), np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = max(x1+gt_w/2-bbox_size/2+delta_x,0)  # gt_w/2-bbox_size/2 == 因为box尺寸变化产生的偏移
                ny1 = max(y1+gt_h/2-bbox_size/2+delta_y,0)
                
                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])
                cropped_im = img[int(ny1):int(ny2)+1, int(nx1):int(nx2)+1, :]
                resized_im = cv2.resize(cropped_im, (size, size))
                #cal iou
                iou = IoU(crop_box, np.expand_dims(gt_box, 0))

                if iou > 0.65:
                    F_imgs.append(resized_im)  # 将12*12的crop框框也加入F_imgs 的list中
                    for index, one in enumerate(landmarkGt):   # normalize归一化处理
                        rv = ((one[0]-nx1)/bbox_size, (one[1]-ny1)/bbox_size)
                        landmark[index] = rv
                    F_landmarks.append(landmark.reshape(10))
                    landmark = np.zeros((5, 2))
                    landmark_ = F_landmarks[-1].reshape(-1,2)  # 将最新加入的list[-1]的len为10的一维数组,reshape成(-1,2)
                    bbox = BBox([nx1, ny1, nx2, ny2])

                    # 1 mirror   # 翻转,以轴为参照
                    if random.choice([0,1]) > 0:  #
                        face_flipped, landmark_flipped = flip(resized_im, landmark_)  # 截取的像素数据 && 特征点数据做翻转
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        #c*h*w
                        F_imgs.append(face_flipped)  # 12 * 12的图片加入F_imgs队列。
                        F_landmarks.append(landmark_flipped.reshape(10))
                     # 逆时针旋转 以中心点为参照, 旋转α角度
                    if random.choice([0, 1]) > 0:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), 5)#逆时针旋转
                    # 2  顺时针旋转
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)  # 根据landmark的坐标做重新生成landmark_rotated?
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))

                     # 3  顺时针旋转后再翻转
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))                
                    

                    if random.choice([0,1]) > 0:
                    # 4  逆时针旋转
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         bbox.reprojectLandmark(landmark_), -5)#顺时针旋转
                        landmark_rotated = bbox.projectLandmark(landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                    # 5  逆时针旋转
                        face_flipped, landmark_flipped = flip(face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10)) 
                    
            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks) # 将list转换为数组,维度保持不变。
            #print F_imgs.shape            # print F_landmarks.shape
            for i in range(len(F_imgs)):   # 一张pic里面,数据增强后形成所有 12 * 12的图片
                print(image_id)                                      # 剔除非法特征点?哪里来的非法特征点?
                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:  # 将F_landmarks中所有 <= 0的数字变为1,>0 的数变为0
                    continue
                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue

                cv2.imwrite(join(dstdir,"%d.jpg" %(image_id)), F_imgs[i])  # dstdir=12/train_PNet_landmark_aug"
                landmarks = map(str, list(F_landmarks[i]))
                f.write(join(dstdir, "%d.jpg" %(image_id))+" -2 "+" ".join(landmarks)+"\n")  # 将特征点坐标,写入landmark_12_aug的txt文件。 12/train_PNet_landmark_aug
                image_id = image_id + 1

    f.close()
    return F_imgs,F_landmarks
示例#12
0
def GenerateData(ftxt, fdir, output, net, dstdir, argument=False):
    if net == "PNet":
        size = 12
    elif net == "RNet":
        size = 24
    elif net == "ONet":
        size = 48
    else:
        print 'Net type error'
        return
    image_id = 0
    f = open(join(output, "landmark.txt"), 'w')

    data = getDataFromTxt(ftxt, fdir)
    idx = 0
    #image_path bbox landmark(5*2)
    for (imgPath, bbox, landmarkGt) in data:
        #print imgPath
        F_imgs = []
        F_bbox = []
        F_landmarks = []
        img = cv2.imread(imgPath)
        print imgPath
        assert (img is not None)
        img_h, img_w, img_c = img.shape
        gt_box = np.array([bbox.left, bbox.top, bbox.right, bbox.bottom])
        '''
        ##########need commented
        f_face = img[bbox.top:bbox.bottom+1,bbox.left:bbox.right+1]
        f_face = cv2.resize(f_face,(size,size))
        landmark = np.zeros((5, 2))
        #normalize
        for index, one in enumerate(landmarkGt):
            rv = ((one[0]-gt_box[0])/(gt_box[2]-gt_box[0]), (one[1]-gt_box[1])/(gt_box[3]-gt_box[1]))
            landmark[index] = rv
        F_imgs.append(f_face)
        F_landmarks.append(landmark.reshape(10))
        #show_landmark(f_face,landmark)
        ############
        '''

        landmark = np.zeros((5, 2))
        if argument:
            idx = idx + 1
            if idx % 100 == 0:
                print idx, "images done"
            x1, y1, x2, y2 = gt_box
            #gt's width
            gt_w = x2 - x1 + 1
            #gt's height
            gt_h = y2 - y1 + 1
            if max(gt_w, gt_h) < 40 or x1 < 0 or y1 < 0:
                continue
            #random shift
            for i in range(15):
                bbox_size = npr.randint(int(min(gt_w, gt_h) * 0.8),
                                        np.ceil(1.25 * max(gt_w, gt_h)))
                delta_x = npr.randint(-gt_w * 0.2, gt_w * 0.2)
                delta_y = npr.randint(-gt_h * 0.2, gt_h * 0.2)
                nx1 = max(x1 + gt_w / 2 - bbox_size / 2 + delta_x, 0)
                ny1 = max(y1 + gt_h / 2 - bbox_size / 2 + delta_y, 0)

                nx2 = nx1 + bbox_size
                ny2 = ny1 + bbox_size
                if nx2 > img_w or ny2 > img_h:
                    continue
                crop_box = np.array([nx1, ny1, nx2, ny2])
                cropped_im = img[ny1:ny2 + 1, nx1:nx2 + 1, :]
                resized_im = cv2.resize(cropped_im, (size, size))
                #cal iou
                iou = IoU(crop_box, np.expand_dims(gt_box, 0))
                if iou > 0.65:
                    F_imgs.append(resized_im)

                    offset_x1 = (x1 - nx1) / float(gt_w)
                    offset_y1 = (y1 - ny1) / float(gt_h)
                    offset_x2 = (x2 - nx2) / float(gt_w)
                    offset_y2 = (y2 - ny2) / float(gt_h)

                    #normalize
                    for index, one in enumerate(landmarkGt):
                        rv = ((one[0] - nx1) / bbox_size,
                              (one[1] - ny1) / bbox_size)
                        landmark[index] = rv

                    #F_bbox.append([offset_x1, offset_y1, offset_x2, offset_y2])
                    F_landmarks.append(landmark.reshape(10))
                    #print landmark
                    #print 'landmark\n',landmark.reshape(10)
                    landmark_ = landmark.copy()
                    #show_landmark(resized_im,landmark)
                    landmark = np.zeros((5, 2))

                    #landmark_ = F_landmarks[-1].reshape(-1,2)
                    bbox = BBox([nx1, ny1, nx2, ny2])

                    #mirror
                    if random.choice([0,1]) > 0 or (landmark_[0][0]+landmark_[3][0])>=2*landmark_[2][0] \
                    or (landmark_[1][0]+landmark_[4][0])>=2*landmark_[2][0]:
                        face_flipped, landmark_flipped = flip(
                            resized_im, landmark_)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        #c*h*w
                        F_imgs.append(face_flipped)
                        #F_bbox.append([-offset_x2, offset_y1, -offset_x1, offset_y2])
                        F_landmarks.append(landmark_flipped.reshape(10))
                        #show_landmark(face_flipped,landmark_flipped)
                    #rotate
                    if random.choice([0,1]) > 0 or (landmark_[0][0]+landmark_[3][0])>=2*landmark_[2][0] \
                    or (landmark_[1][0]+landmark_[4][0])>=2*landmark_[2][0]:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         landmarkGt, 20)#逆时针旋转
                        #landmark_offset
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                        #show_landmark(face_rotated_by_alpha,landmark_rotated)
                        #flip
                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                        #show_landmark(face_flipped,landmark_flipped)
                    #inverse clockwise rotation
                    if random.choice([0,1]) > 0 or (landmark_[0][0]+landmark_[3][0])>=2*landmark_[2][0] \
                    or (landmark_[1][0]+landmark_[4][0])>=2*landmark_[2][0]:
                        face_rotated_by_alpha, landmark_rotated = rotate(img, bbox, \
                                                                         landmarkGt, -20)#顺时针旋转
                        landmark_rotated = bbox.projectLandmark(
                            landmark_rotated)
                        face_rotated_by_alpha = cv2.resize(
                            face_rotated_by_alpha, (size, size))
                        F_imgs.append(face_rotated_by_alpha)
                        F_landmarks.append(landmark_rotated.reshape(10))
                        #show_landmark(face_rotated_by_alpha,landmark_rotated)
                        face_flipped, landmark_flipped = flip(
                            face_rotated_by_alpha, landmark_rotated)
                        face_flipped = cv2.resize(face_flipped, (size, size))
                        F_imgs.append(face_flipped)
                        F_landmarks.append(landmark_flipped.reshape(10))
                        #show_landmark(face_flipped,landmark_flipped)

            F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
            #print F_imgs.shape
            #print F_landmarks.shape
            for i in range(len(F_imgs)):
                if np.sum(np.where(F_landmarks[i] <= 0, 1, 0)) > 0:
                    continue

                if np.sum(np.where(F_landmarks[i] >= 1, 1, 0)) > 0:
                    continue

                cv2.imwrite(join(dstdir, "%d.jpg" % (image_id)), F_imgs[i])

                str_landmark = F_landmarks[i].copy()

                str_landmark[0] = F_landmarks[i, 0]
                str_landmark[1] = F_landmarks[i, 2]
                str_landmark[2] = F_landmarks[i, 4]
                str_landmark[3] = F_landmarks[i, 6]
                str_landmark[4] = F_landmarks[i, 8]
                str_landmark[5] = F_landmarks[i, 1]
                str_landmark[6] = F_landmarks[i, 3]
                str_landmark[7] = F_landmarks[i, 5]
                str_landmark[8] = F_landmarks[i, 7]
                str_landmark[9] = F_landmarks[i, 9]

                landmarks = map(str, list(str_landmark))
                f.write(
                    join(dstdir, "%d.jpg" % (image_id)) + " -1 -1 -1 -1 -1 " +
                    " ".join(landmarks) + "\n")
                image_id = image_id + 1

    f.close()
    return F_imgs, F_landmarks