示例#1
0
def generate_h5py(data, h5_path, txt_path, augment=False):
    '''
	Get images and turn them into h5py files
	Input:
		- data: a tuple of [imgpath, bbox, landmark]
		- h5_path: h5py file name
		- txt_path: h5 txt name
	'''
    F_imgs = []
    F_landmarks = []
    for (imgpath, bbox, landmark) in data:
        img = cv2.imread(imgpath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
        print("processing %s") % imgpath
        if not check_bbox(img, bbox):
            print 'BBox out of range.'
            continue
        face = img[bbox.top:bbox.bottom, bbox.left:bbox.right]

        if augment:
            face_flip, landmark_flip = flip(face, landmark)
            face_flip = cv2.resize(face_flip, (39, 39)).reshape(1, 39, 39)
            landmark_flip = landmark_flip.reshape(10)
            F_imgs.append(face_flip)
            F_landmarks.append(landmark_flip)

        face = cv2.resize(face, (39, 39)).reshape(1, 39, 39)
        landmark = landmark.reshape(10)
        F_imgs.append(face)
        F_landmarks.append(landmark)

    F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
    F_imgs = processImage(F_imgs)
    F_imgs, F_landmarks = shuffle(F_imgs, F_landmarks, random_state=42)

    with h5py.File(h5_path, 'w') as f:
        f['data'] = F_imgs.astype(np.float32)
        f['landmarks'] = F_landmarks.astype(np.float32)
    with open(txt_path, 'w') as f:
        f.write(h5_path)
示例#2
0
def generate_h5py(data, h5_path, txt_path, augment=False):
    '''
	Get images and turn them into h5py files
	Input:
		- data: a tuple of [imgpath, bbox, landmark]
		- h5_path: h5py file name
		- txt_path: h5 txt name
	'''
    F_imgs = []
    F_landmarks = []
    img_size = 40
    num_landmark = 68 * 2
    num_sample = 0
    for (imgpath, bbox, landmarkGt) in data:
        img = cv2.imread(imgpath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
        print("processing %s") % imgpath
        if not check_bbox(img, bbox):
            print 'BBox out of range.'
            continue
        face = img[bbox.top:bbox.bottom, bbox.left:bbox.right]

        if augment:
            # flip the face
            #face_flip, landmark_flip = flip(face, landmarkGt)
            #face_flip = cv2.resize(face_flip, (img_size,img_size)).reshape(1,img_size,img_size)

            #fit=0
            #for i in range(0,num_landmark/2):
            #    if landmark_flip[i,0]<0 or landmark_flip[i,0]>1 or landmark_flip[i,1]<0 or landmark_flip[i,1]>1:
            #        fit=1
            #        break
            #if fit==0:
            #print landmark_flipped_alpha
            #    F_imgs.append(face_flip)
            #    F_landmarks.append(landmark_flip.reshape(num_landmark))

            #print landmark_flip
            #angles=[5,10,15,20,25,30,-5,-10,-15,-20,-25,-30]
            #for alpha in angles:
            # rotate the face
            #face_rotated_by_alpha, landmark_rotated_alpha = rotate(img, bbox,bbox.reprojectLandmark(landmarkGt), alpha)
            #landmark_rotated_alpha = bbox.projectLandmark(landmark_rotated_alpha)
            #face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (img_size,img_size))

            #fit=0
            #for i in range(0,num_landmark/2):
            #    if landmark_rotated_alpha[i,0]<0 or landmark_rotated_alpha[i,0]>1 or landmark_rotated_alpha[i,1]<0 or landmark_rotated_alpha[i,1]>1:
            #        fit=1
            #        break
            #if fit==0:
            #print landmark_rotated_alpha
            #    F_imgs.append(face_rotated_by_alpha.reshape((1, img_size,img_size)))
            #    F_landmarks.append(landmark_rotated_alpha.reshape(num_landmark))

            #print landmark_rotated_5
            # flip with the rotation
            #face_flipped_alpha, landmark_flipped_alpha = flip(face_rotated_by_alpha, landmark_rotated_alpha)
            #face_flipped_alpha = cv2.resize(face_flipped_alpha, (img_size,img_size))
            #fit=0
            #for i in range(0,num_landmark/2):
            #    if landmark_flipped_alpha[i,0]<0 or landmark_flipped_alpha[i,0]>1 or landmark_flipped_alpha[i,1]<0 or landmark_flipped_alpha[i,1]>1:
            #        fit=1
            #        break
            #if fit==0:
            #print landmark_flipped_alpha
            #    F_imgs.append(face_flipped_alpha.reshape((1, img_size,img_size)))
            #    F_landmarks.append(landmark_flipped_alpha.reshape(num_landmark))

            # debug
            #center = ((bbox.left+bbox.right)/2, (bbox.top+bbox.bottom)/2)
            #rot_mat = cv2.getRotationMatrix2D(center, alpha, 1)
            #img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, img.shape)
            #landmark_rotated_alpha = bbox.reprojectLandmark(landmark_rotated_alpha) # will affect the flip "landmark_rotated_alpha"
            #img_rotated_by_alpha = drawLandmark(img_rotated_by_alpha, bbox, landmark_rotated_alpha)
            #fp = 'debug_results/'+ str(num_sample)+'.jpg'
            #cv2.imwrite(fp, img_rotated_by_alpha)
            #num_sample=num_sample+1

            # debug

            #use bounding box perturbation
            y = bbox.top
            x = bbox.left
            h = bbox.bottom - bbox.top
            w = bbox.right - bbox.left
            # original landmark position
            landmarkGT_scale = bbox.reprojectLandmark(landmarkGt)
            for cur_scale in [0.83, 0.91, 1.0, 1.10, 1.21]:
                for cur_x in [-0.17, 0, 0.17]:
                    for cur_y in [-0.17, 0, 0.17]:
                        s_n = 1 / cur_scale
                        x_n = -cur_x / cur_scale
                        y_n = -cur_y / cur_scale

                        x_temp = int(x - (x_n * w / s_n))
                        y_temp = int(y - (y_n * h / s_n))
                        w_temp = int(w / s_n)
                        h_temp = int(h / s_n)
                        # generate new bounding box
                        bbox_left = x_temp
                        bbox_right = x_temp + w_temp
                        bbox_top = y_temp
                        bbox_bottom = y_temp + h_temp
                        new_bbox = map(
                            int,
                            [bbox_left, bbox_right, bbox_top, bbox_bottom])
                        new_bbox = BBox(new_bbox)
                        if not check_bbox(img, new_bbox):
                            print 'BBox out of range.'
                            continue

                        # project landmark onto the new bounding box
                        new_landmarkGT = new_bbox.projectLandmark(
                            landmarkGT_scale)
                        new_landmarkGT_org = new_landmarkGT.copy()

                        angles = [
                            5, 10, 15, 20, 25, 30, -5, -10, -15, -20, -25, -30
                        ]
                        for alpha in angles:
                            # rotate the face
                            face_rotated_by_alpha, landmark_rotated_alpha = rotate(
                                img, new_bbox,
                                new_bbox.reprojectLandmark(new_landmarkGT),
                                alpha)
                            landmark_rotated_alpha = new_bbox.projectLandmark(
                                landmark_rotated_alpha)
                            face_rotated_by_alpha = cv2.resize(
                                face_rotated_by_alpha, (img_size, img_size))

                            fit = 0
                            for i in range(0, num_landmark / 2):
                                if landmark_rotated_alpha[
                                        i, 0] < 0 or landmark_rotated_alpha[
                                            i,
                                            0] > 1 or landmark_rotated_alpha[
                                                i,
                                                1] < 0 or landmark_rotated_alpha[
                                                    i, 1] > 1:
                                    fit = 1
                                    break
                            if fit == 0:
                                #print landmark_rotated_alpha
                                F_imgs.append(
                                    face_rotated_by_alpha.reshape(
                                        (1, img_size, img_size)))
                                F_landmarks.append(
                                    landmark_rotated_alpha.reshape(
                                        num_landmark))

                                # debug
                                #center = ((new_bbox.left+new_bbox.right)/2, (new_bbox.top+new_bbox.bottom)/2)
                                #rot_mat = cv2.getRotationMatrix2D(center, alpha, 1)
                                #img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, img.shape)
                                #landmark_rotated_alpha = new_bbox.reprojectLandmark(landmark_rotated_alpha) # will affect the flip "landmark_rotated_alpha"
                                #img_rotated_by_alpha = drawLandmark(img_rotated_by_alpha, new_bbox, landmark_rotated_alpha)
                                #fp = 'debug_results/'+ str(num_sample)+'.jpg'
                                #cv2.imwrite(fp, img_rotated_by_alpha)
                                #num_sample=num_sample+1
                                # debug

                        # project landmark onto the new bounding box
                        landmarkGT_project = new_landmarkGT_org.copy(
                        )  # error is fixed here
                        #print landmarkGT_project

                        fit = 0
                        for i in range(0, num_landmark / 2):
                            if landmarkGT_project[
                                    i, 0] < 0 or landmarkGT_project[
                                        i, 0] > 1 or landmarkGT_project[
                                            i, 1] < 0 or landmarkGT_project[
                                                i, 1] > 1:
                                fit = 1
                                break
                        if fit == 0:
                            #print landmarkGT_project
                            #if landmarkGT_project[i,0]<0 or landmarkGT_project[i,0]>1 or landmarkGT_project[i,1]<0 or landmarkGT_project[i,1]>1:
                            #    pdb.set_trace()
                            cropped_face = img[new_bbox.top:new_bbox.bottom,
                                               new_bbox.left:new_bbox.right]
                            cropped_face = cv2.resize(
                                cropped_face, (img_size, img_size)).reshape(
                                    1, img_size, img_size)
                            F_imgs.append(cropped_face)
                            F_landmarks.append(
                                landmarkGT_project.reshape(num_landmark))

        # debug on the bounding box perturbation
        #landmark_org = new_bbox.reprojectLandmark(landmarkGT_project) # will affect the flip "landmark_rotated_alpha"
        #img_debug = drawLandmark(img, new_bbox, landmark_org)
        #fp = 'debug_results/'+ str(num_sample)+'_bbox.jpg'
        #cv2.imwrite(fp, img_debug)
        #num_sample=num_sample+1

        face = cv2.resize(face,
                          (img_size, img_size)).reshape(1, img_size, img_size)

        fit = 0
        for i in range(0, num_landmark / 2):
            if landmarkGt[i, 0] < 0 or landmarkGt[i, 0] > 1 or landmarkGt[
                    i, 1] < 0 or landmarkGt[i, 1] > 1:
                fit = 1
                break
        if fit == 0:
            #print landmark_flipped_alpha
            F_imgs.append(face)
            F_landmarks.append(landmarkGt.reshape(num_landmark))

    F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks)
    F_imgs = processImage(F_imgs)
    F_imgs, F_landmarks = shuffle(F_imgs, F_landmarks, random_state=42)

    with h5py.File(h5_path, 'w') as f:
        f['data'] = F_imgs.astype(np.float32)
        f['landmarks'] = F_landmarks.astype(np.float32)
    with open(txt_path, 'w') as f:
        f.write(h5_path)
        f.write(str(len(F_landmarks)))
示例#3
0
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            print 'predicting %s' % img_path
            face = load_test_img(gray, bbox)
            landmark = net.forward(face, 'fc2')
            landmark = bbox.reprojectLandmark(landmark)

            img = drawLandmark(img, bbox, landmark)
            fp = os.path.join(test_dir_out, os.path.basename(img_path))
            cv2.imwrite(fp, img)

    # Test for celeba dataset
    elif len(sys.argv) == 3 and sys.argv[2] == 'celeba':
        data = load_celeba_data()
        train, val, test = get_train_val_test_list()
        test_data = [data[i - 1] for i in test]

        for (img_path, bbox, landmark_ground) in test_data:
            img = cv2.imread(img_path)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            print 'predicting %s' % img_path
            if not check_bbox(gray, bbox):
                print 'BBox out of range...'
                continue
            face = load_test_img(gray, bbox)
            landmark = net.forward(face, 'fc2')
            landmark = bbox.reprojectLandmark(landmark)

            img = drawLandmark(img, bbox, landmark)
            fp = os.path.join(test_dir_out, os.path.basename(img_path))
            cv2.imwrite(fp, img)
        #y_min=face[1]
        #x_max=face[0] + face[2]
        #y_max=face[1]+face[3]
        # used by dlib
        x_min = face.left()
        y_min = face.top()
        x_max = face.right()
        y_max = face.bottom()
        w, h = x_max - x_min, y_max - y_min
        w = h = min(w, h)
        x_new = x_min - w * ratio
        y_new = y_min - h * ratio
        w_new = w * (1 + 2 * ratio)
        h_new = h * (1 + 2 * ratio)
        new_bbox = map(int, [x_new, x_new + w_new, y_new, y_new + h_new])
        new_bbox = BBox(new_bbox)
        #print bbox_left,bbox_top,bbox_right,bbox_bottom
        if not check_bbox(gray.transpose(), new_bbox):
            print 'BBox out of range.'
            continue
        face = load_test_img(gray, new_bbox)
        #landmark = net.forward(face, 'Dense2')
        #print landmark
        #landmark = new_bbox.reprojectLandmark(landmark)
        gender = net.forward_gender(face, 'prob_gender')
        age = net.forward_age(face, 'prob_age')
        img = drawAttribute(img, new_bbox, gender, age)
        #img = drawLandmark_multiple(img, new_bbox, landmark)

        cv2.imwrite('demo_result.jpg', img)
示例#5
0
			landmark = net.forward(face, 'fc2')
			landmark = bbox.reprojectLandmark(landmark)

			img = drawLandmark(img, bbox, landmark)
			fp = os.path.join(test_dir_out, os.path.basename(img_path))
			cv2.imwrite(fp, img)

	# Test for celeba dataset
	elif len(sys.argv) == 3 and sys.argv[2] == 'celeba':
		data = load_celeba_data()
		train, val, test = get_train_val_test_list()
		test_data = [data[i-1] for i in test]

		for (img_path, bbox, landmark_ground) in test_data:
			img = cv2.imread(img_path)
			gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
			print 'predicting %s' % img_path
			if not check_bbox(gray, bbox):
				print 'BBox out of range...'
				continue
			face = load_test_img(gray, bbox)
			landmark = net.forward(face, 'fc2')
			landmark = bbox.reprojectLandmark(landmark)

			img = drawLandmark(img, bbox, landmark)
			fp = os.path.join(test_dir_out, os.path.basename(img_path))
			cv2.imwrite(fp, img)