summaryrefslogtreecommitdiff
path: root/doc/examples/facial_landmark_sync.c
blob: ef2d3baf53258cbadba1d82b6ba53abf1e6f7ba3 (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
/**
 * Copyright (c) 2024 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.
 */

#include <dlog.h>
#include <string.h>

#include <image_util.h>
#include <mv_facial_landmark.h>

#undef LOG_TAG
#define LOG_TAG "FACIAL_LANDMARK_SYNC_EXAMPLE"

void load_image_to_source(const char *path, mv_source_h mv_source)
{
	unsigned char *dataBuffer = NULL;
	size_t long bufferSize = 0;
	unsigned int width = 0;
	unsigned int height = 0;
	image_util_decode_h imageDecoder = NULL;
	image_util_image_h decodedImage = NULL;

	int error_code = image_util_decode_create(&imageDecoder);
	if (error_code != IMAGE_UTIL_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = image_util_decode_set_input_path(imageDecoder, path);
	if (error_code != IMAGE_UTIL_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = image_util_decode_set_colorspace(imageDecoder, IMAGE_UTIL_COLORSPACE_RGB888);
	if (error_code != IMAGE_UTIL_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = image_util_decode_run2(imageDecoder, &decodedImage);
	if (error_code != IMAGE_UTIL_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = image_util_get_image(decodedImage, &width, &height, NULL, &dataBuffer, &bufferSize);
	if (error_code != IMAGE_UTIL_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = image_util_decode_destroy(imageDecoder);
	if (error_code != IMAGE_UTIL_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	/* Fill the dataBuffer to mv_source */
	error_code = mv_source_fill_by_buffer(mv_source, dataBuffer, (unsigned int) bufferSize, width, height,
										  MEDIA_VISION_COLORSPACE_RGB888);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = image_util_destroy_image(decodedImage);
	if (error_code != IMAGE_UTIL_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);
}

//! [FLD sync]
int main()
{
	int error_code = 0;
	mv_facial_landmark_h handle;
	mv_source_h mv_source = NULL;

	error_code = mv_create_source(&mv_source);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	load_image_to_source("/path/to/image", mv_source);

	error_code = mv_facial_landmark_create(&handle);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = mv_facial_landmark_configure(handle);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = mv_facial_landmark_prepare(handle);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = mv_facial_landmark_inference(handle, mv_source);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	unsigned long frame_number;
	unsigned int cnt;

	int error_code = mv_facial_landmark_get_result_count(handle, &frame_number, &cnt);
	if (error_code == MEDIA_VISION_ERROR_INVALID_OPERATION)
		break;

	for (unsigned int idx = 0; idx < cnt; ++idx) {
		unsigned int pos_x, pos_y;

		error_code = mv_facial_landmark_get_position(handle, idx, &pos_x, &pos_y);
		if (error_code != MEDIA_VISION_ERROR_NONE)
			dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

		dlog_print(DLOG_INFO, LOG_TAG, "frame = %lu, idx = %u, x = %u, y = %u", frame_number, idx, pos_x, pos_y);
	}

	error_code = mv_destroy_source(mv_source);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);

	error_code = mv_facial_landmark_destroy(handle);
	if (error_code != MEDIA_VISION_ERROR_NONE)
		dlog_print(DLOG_ERROR, LOG_TAG, "error code = %d", error_code);
}
//! [FLD sync]