Ejemplo n.º 1
0
    def test_cluster(self):

        self.test_add_face()

        base_path = os.path.join('..', 'test_files', 'face_models')

        face_rec_data = os.path.abspath(os.path.join(base_path, 'face_rec_data'))

        params = {c.GLOBAL_FACE_MODELS_MIN_DIFF_KEY: -1,
                  c.GLOBAL_FACE_REC_DATA_DIR_PATH_KEY: face_rec_data}

        fm = FaceModels(params)

        base_path = os.path.join('..', 'test_files', 'face_models')

        label = 3816
        tag = 'Mameli_Giacomo-2'

        image_path = os.path.join(base_path, '0000000.png')

        aligned_image_path = os.path.join(base_path, '0000000_aligned.png')

        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)

        eye_pos = (303, 131, 352, 134)

        bbox = (260, 76, 137, 137)

        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)

        label = 3817
        tag = 'Fadda_Paolo-2'

        image_path = os.path.join(base_path, '0000001.png')

        aligned_image_path = os.path.join(base_path, '0000001_aligned.png')

        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)

        eye_pos = (323, 150, 376, 145)

        bbox = (267, 87, 166, 166)

        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)

        # Get clusters
        fm.cluster_models()
        clusters = fm.get_clusters()

        # Check correctness of clusters
        self.assertEqual(len(clusters), 4)

        self.assertIn(3816, clusters[0])
Ejemplo n.º 2
0
    def test_add_face_from_whole_image(self):

        base_path = os.path.join('..', 'test_files', 'face_models')

        face_rec_data = os.path.abspath(os.path.join(base_path, 'face_rec_data'))

        params = {c.GLOBAL_FACE_REC_DATA_DIR_PATH_KEY: face_rec_data}

        fm = FaceModels(params)
        label = 0
        tag = 'Mameli_Giacomo'
        im_path = os.path.join(base_path, '0000000.png')
        fm.add_face(label, tag, im_path)
Ejemplo n.º 3
0
    c.GLOBAL_FACE_REC_DATA_DIR_PATH_KEY: 'face_rec_data',
    c.LBP_GRID_X_KEY: 4,
    c.LBP_GRID_Y_KEY: 5,
    c.LBP_NEIGHBORS_KEY: 8,
    c.LBP_RADIUS_KEY: 1
}

fm = FaceModels(params)

# Delete possible already existing models
fm.delete_models()

# Add face from first image to face models
label = 0
tag = ''
fm.add_face(label, tag, image_path_1)

# Detect face in second image
result = detect_faces_in_image(
   image_path_2, align_path, params, show_results=False)

# Recognize aligned face detected in second image
if result and c.FACES_KEY in result:
    faces = result[c.FACES_KEY]
    if len(faces) == 1:

        # Get aligned face and calculate distance between faces
        file_name = faces[0][c.ALIGNED_FACE_FILE_NAME_KEY]
        file_name_complete = file_name + '_gray.png'
        file_path = os.path.join(align_path, file_name_complete)
        aligned_face = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
Ejemplo n.º 4
0
    def test_add_face(self):
        
        base_path = os.path.join('..', 'test_files', 'face_models')

        face_rec_data = os.path.abspath(os.path.join(base_path, 'face_rec_data'))

        params = {c.GLOBAL_FACE_REC_DATA_DIR_PATH_KEY: face_rec_data}
        
        fm = FaceModels(params)

        fm.delete_models()

        label = 3812
        tag = 'Mameli_Giacomo'

        image_path = os.path.join(base_path, '0000000.png')
        
        aligned_image_path = os.path.join(base_path, '0000000_aligned.png')
        
        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)
        
        eye_pos = (303, 131, 352, 134)
        
        bbox = (260, 76, 137, 137)
        
        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)

        label = 3813
        tag = 'Fadda_Paolo'

        image_path = os.path.join(base_path, '0000001.png')
        
        aligned_image_path = os.path.join(base_path, '0000001_aligned.png')
        
        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)
        
        eye_pos = (323, 150, 376, 145)
        
        bbox = (267, 87, 166, 166)
        
        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)

        label = 3815
        tag = 'Giannotta_Michele'
        
        image_path = os.path.join(base_path, '0000002.png')
        
        aligned_image_path = os.path.join(base_path, '0000002_aligned.png')
        
        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)
        
        eye_pos = (337, 190, 393, 183)
        
        bbox = (282, 121, 161, 161)
        
        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)
        
        label = 3814
        tag = 'Leoni_Mario'
        
        image_path = os.path.join(base_path, '0000003.png')
        
        aligned_image_path = os.path.join(base_path, '0000003_aligned.png')
        
        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)
        
        eye_pos = (355, 148, 407, 152)
        
        bbox = (300, 89, 156, 156)
        
        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)

        label = 3814
        tag = 'Leoni_Mario'

        image_path = os.path.join(base_path, '0000004.png')
        
        aligned_image_path = os.path.join(base_path, '0000004_aligned.png')
        
        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)
        
        eye_pos = (138, 150, 158, 151)
        
        bbox = (119, 128, 60, 60)
        
        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)
        
        label = 3812
        tag = 'Mameli_Giacomo'
        
        image_path = os.path.join(base_path, '0000005.png')
        
        aligned_image_path = os.path.join(base_path, '0000005_aligned.png')
        
        aligned_face = cv2.imread(aligned_image_path, cv2.IMREAD_GRAYSCALE)
        
        eye_pos = (318, 128, 361, 126)
        
        bbox = (272, 86, 126, 126)
        
        fm.add_face(label, tag, image_path, aligned_face, eye_pos, bbox)
Ejemplo n.º 5
0
def main(argv):
	# Set path of images to be analyzed
	image_path_1 = argv[1]
	print "image_path_1 ", image_path_1
	image_path_2 = argv[2]
	print "image_path_2 ", image_path_2

	# Set path of directory where aligned faces will be saved
	align_path = '/tmp'

	# Set parameters
	params = {
	    c.CHECK_EYE_POSITIONS_KEY: True,
	    c.CLASSIFIERS_DIR_PATH_KEY: '/home/active/gitactive/ACTIVE/face_extractor/examples/haarcascades',
	    c.CROPPED_FACE_HEIGHT_KEY: 250,
	    c.CROPPED_FACE_WIDTH_KEY: 200,
	    c.EYE_DETECTION_CLASSIFIER_KEY: 'haarcascade_mcs_lefteye.xml',
	    c.FACE_DETECTION_ALGORITHM_KEY: 'HaarCascadeFrontalFaceAlt2',
	    c.FLAGS_KEY: 'DoCannyPruning',
	    c.MIN_NEIGHBORS_KEY: 5,
	    c.MIN_SIZE_HEIGHT_KEY: 20,
	    c.MIN_SIZE_WIDTH_KEY: 20,
	    c.OFFSET_PCT_X_KEY: 0.30,
	    c.OFFSET_PCT_Y_KEY: 0.42,
	    c.SCALE_FACTOR_KEY: 1.1,
	    c.MAX_EYE_ANGLE_KEY: 0.125,
	    c.MIN_EYE_DISTANCE_KEY: 0.25,
	    c.USE_EYES_POSITION_KEY: True,
	    c.USE_NOSE_POS_IN_DETECTION_KEY: False,
	    c.ALIGNED_FACES_PATH_KEY: align_path,
	    c.GLOBAL_FACE_REC_DATA_DIR_PATH_KEY: '/home/active/gitactive/ACTIVE/face_extractor/examples/face_rec_data2',
	    c.LBP_GRID_X_KEY: 4,
	    c.LBP_GRID_Y_KEY: 5,
	    c.LBP_NEIGHBORS_KEY: 8,
	    c.LBP_RADIUS_KEY: 1
	}

	fm = FaceModels(params)

	# Delete possible already existing models
	fm.delete_models()

	# Add face from first image to face models
	label = 0
	tag = ''
	fm.add_face(label, tag, image_path_1)

	# Detect face in second image
	result = detect_faces_in_image(
	   image_path_2, align_path, params, show_results=False)

	# Recognize aligned face detected in second image
	if result and c.FACES_KEY in result:
	    faces = result[c.FACES_KEY]
	    if len(faces) == 1:

		# Get aligned face and calculate distance between faces
		file_name = faces[0][c.ALIGNED_FACE_FILE_NAME_KEY]
		file_name_complete = file_name + '_gray.png'
		file_path = os.path.join(align_path, file_name_complete)
		aligned_face = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
		(label, conf) = fm.recognize_face(aligned_face)

		# Draw bounding box around face
		img = cv2.imread(image_path_2)
		(x, y, w, h) = faces[0][c.BBOX_KEY]
		cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3, 8, 0)

		# Write distance from reference image
		cv2.putText(img, str(conf), (x, y + h + 30),
		            cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
		print "distance ", conf
		cv2.imshow('Image', img)
		cv2.waitKey(0)

		image_path_2 = os.path.splitext(image_path_2)[0]
		image_path_2 = image_path_2 + '_comparison.png'
		cv2.imwrite(image_path_2, img)