summaryrefslogtreecommitdiff
path: root/sample/src/tutorial_3.c
blob: e81f4d00009e440fd2743f16e3d3fde62a9b1fcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/*
	Extracting face feature from image file
*/

#include <stdio.h>
#include <stdlib.h>

#include <face.h>
#include <image_util.h>

#define FILE_JPG "/opt/media/Images/face_1.jpg"

int main(int argc, char *argv[])
{
	int err;

	unsigned char *buffer = NULL;
	int width, height;
	unsigned int size;

	err = image_util_decode_jpeg (FILE_JPG, IMAGE_UTIL_COLORSPACE_YUV420 , &buffer, &width, &height, &size);

	if ( err != IMAGE_UTIL_ERROR_NONE )
	{
		printf("Image loading failed\n");
		return 0;
	}

	face_h face;

	err = face_create(&face);

	if ( err != FACE_ERROR_NONE )
	{
		printf("Cannot create face handle\n");
		return 0;
	}

	face_image_h image;

	err = face_image_create(FACE_IMAGE_COLORSPACE_YUV420, buffer, width, height, size, &image);

	if ( err != FACE_ERROR_NONE )
	{
		printf("Cannot create face handle\n");
		return 0;
	}

	face_rect_s *rects = NULL;
	int nCount;

	err = face_detect_faces(face, FACE_IMAGE_TYPE_SINGLE, image, &rects, &nCount);
	if ( err != FACE_ERROR_NONE )
	{
		printf("Error occured during detecting\n");
		return 0;
	}

	if ( nCount == 0 )
	{
		printf("Cannot find face\n");
		return 0;
	}

	face_rect_s *interest_face = &rects[0];		// Choose 1st faces

	face_component_h component;

	err = face_extract_component(face, image, interest_face, &component);
	if ( err != FACE_ERROR_NONE )
	{
		printf("Cannot extract face component\n");
		return 0;
	}

	face_feature_h feature;

	err = face_extract_feature(face, image, component, &feature);
	if ( err != FACE_ERROR_NONE )
	{
		printf("Cannot extract face feature\n");
		return 0;
	}

	unsigned char *feature_buffer;
	int feature_length;

	err = face_feature_get_data(feature, &feature_buffer, &feature_length);
	if ( err != FACE_ERROR_NONE )
	{
		printf("Cannot extract face component\n");
		return 0;
	}

// Now you have serialized fecture data
	FILE *fp;

	fp = fopen("feature_vector.dat", "wb");

	fwrite(feature_buffer, 1, feature_length, fp);

	fclose(fp);

	printf("Write feature vector to disk\n");

	free(rects);

	face_component_destroy(component);
	face_feature_destroy(feature);

	face_image_destroy(image);
	face_destroy(face);

	free(buffer);

	return 0;
}