diff options
Diffstat (limited to 'sample')
-rwxr-xr-x | sample/CMakeLists.txt | 71 | ||||
-rwxr-xr-x | sample/src/main.c | 557 | ||||
-rwxr-xr-x | sample/src/tutorial_1.c | 94 | ||||
-rwxr-xr-x | sample/src/tutorial_2.c | 135 | ||||
-rwxr-xr-x | sample/src/tutorial_3.c | 135 | ||||
-rwxr-xr-x | sample/src/tutorial_4.c | 128 | ||||
-rwxr-xr-x | sample/src/tutorial_5.c | 143 |
7 files changed, 1263 insertions, 0 deletions
diff --git a/sample/CMakeLists.txt b/sample/CMakeLists.txt new file mode 100755 index 0000000..ecc5893 --- /dev/null +++ b/sample/CMakeLists.txt @@ -0,0 +1,71 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) +SET(fw_name "tutorial") + +PROJECT(${fw_name}) + +SET(CMAKE_INSTALL_PREFIX /usr) +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +INCLUDE_DIRECTORIES( + ${CMAKE_SOURCE_DIR}/include +) + +SET(dependents dlog capi-media-image-util capi-media-camera ecore) + +INCLUDE(FindPkgConfig) +pkg_check_modules(${fw_name} REQUIRED ${dependents}) +FOREACH(flag ${${fw_name}_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +IF("${ARCH}" STREQUAL "arm") + ADD_DEFINITIONS("-DTARGET") +ENDIF("${ARCH}" STREQUAL "arm") + + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +SET(name "test_face") + +ADD_EXECUTABLE(${name} src/main.c) +TARGET_LINK_LIBRARIES(${name} -L${CMAKE_SOURCE_LIB_DIR} capi-uix-face ${${fw_name}_LDFLAGS} /usr/lib/libjpeg.a) +SET_TARGET_PROPERTIES(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) +INSTALL(TARGETS ${name} DESTINATION /opt/home/root) + +SET(name "tutorial_1") + +ADD_EXECUTABLE(${name} src/tutorial_1.c) +TARGET_LINK_LIBRARIES(${name} -L${CMAKE_SOURCE_LIB_DIR} capi-uix-face ${${fw_name}_LDFLAGS} /usr/lib/libjpeg.a) +SET_TARGET_PROPERTIES(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) +INSTALL(TARGETS ${name} DESTINATION /opt/home/root) + +SET(name "tutorial_2") + +ADD_EXECUTABLE(${name} src/tutorial_2.c) +TARGET_LINK_LIBRARIES(${name} -L${CMAKE_SOURCE_LIB_DIR} capi-uix-face ${${fw_name}_LDFLAGS} /usr/lib/libjpeg.a) +SET_TARGET_PROPERTIES(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) +INSTALL(TARGETS ${name} DESTINATION /opt/home/root) + +SET(name "tutorial_3") + +ADD_EXECUTABLE(${name} src/tutorial_3.c) +TARGET_LINK_LIBRARIES(${name} -L${CMAKE_SOURCE_LIB_DIR} capi-uix-face ${${fw_name}_LDFLAGS} /usr/lib/libjpeg.a) +SET_TARGET_PROPERTIES(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) +INSTALL(TARGETS ${name} DESTINATION /opt/home/root) + +SET(name "tutorial_4") + +ADD_EXECUTABLE(${name} src/tutorial_4.c) +TARGET_LINK_LIBRARIES(${name} -L${CMAKE_SOURCE_LIB_DIR} capi-uix-face ${${fw_name}_LDFLAGS} /usr/lib/libjpeg.a) +SET_TARGET_PROPERTIES(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) +INSTALL(TARGETS ${name} DESTINATION /opt/home/root) + +SET(name "tutorial_5") + +ADD_EXECUTABLE(${name} src/tutorial_5.c) +TARGET_LINK_LIBRARIES(${name} -L${CMAKE_SOURCE_LIB_DIR} capi-uix-face ${${fw_name}_LDFLAGS} /usr/lib/libjpeg.a) +SET_TARGET_PROPERTIES(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) +INSTALL(TARGETS ${name} DESTINATION /opt/home/root) + +# INSTALL(DIRECTORY ${CMAKE_SOURCE_DIR}/TC/images/ DESTINATION /opt/home/root) diff --git a/sample/src/main.c b/sample/src/main.c new file mode 100755 index 0000000..8e55f69 --- /dev/null +++ b/sample/src/main.c @@ -0,0 +1,557 @@ +#include <stdio.h> +#include <getopt.h> +#include <jpeglib.h> +#include <stdlib.h> +#include <string.h> + +#include "face.h" + + +#define CIF_W (352) +#define CIF_H (288) + +#define QCIF_W (176) +#define QCIF_H (144) + + +static char *filename = NULL; +static bool bSaveRaw = false; +static bool bVideo = false; + +static void print_usage(char *name, FILE *stream, int exit_code) +{ + fprintf(stream, "Usage : %s [ ... ]\n", name); + fprintf(stream, + " -f --file File name\n" + " -t --type [image,video]\n" + " -v --verbose Print verbose message.\n" + " -s --saveraw Save Y data into file\n" + ); + + exit(exit_code); +} + + +int parse_param(int argc, char *argv[]) +{ + int c; + + if ( argc == 1 ) + { + print_usage(argv[0], stdout, 0); + return 0; + } + + + while (1) + { + static struct option long_options[] = + { + /* These options set a flag. */ +// {"verbose", no_argument, &verbose_flag, 1}, +// {"brief", no_argument, &verbose_flag, 0}, + + /* These options don't set a flag. We distinguish them by their indices. */ + {"file", required_argument, NULL, 0}, + {"type", required_argument, NULL, 0}, + {"saveraw", no_argument, NULL, 0}, + {0, 0, 0, 0} + }; + + /* getopt_long stores the option index here. */ + int option_index = 0; +/* + : ºÙÀ¸¸é ²À ÇÊ¿äÇÑ ¿É¼ÇÀÓÀ» ³ªÅ¸³¿. +*/ + c = getopt_long(argc, argv, "f:t:s", long_options, &option_index); + + /* Detect the end of the options. */ + if (c == -1) + break; + + switch (c) + { + case 0: + /* If this option set a flag, do nothing else now. */ + if (long_options[option_index].flag != 0) break; + + printf ("option %s", long_options[option_index].name); + + if (optarg) + printf (" with arg %s", optarg); + printf ("\n"); + break; + + case 'f': + printf ("option -f with value `%s'\n", optarg); + filename = strdup(optarg); + break; + + case 's': + printf ("option -s\n"); + bSaveRaw = true; + break; + + case 't': + printf ("option -t with value `%s'\n", optarg); + + if ( strcmp("video", optarg ) == 0 ) + { + bVideo = true; + } + else + { + bVideo = false; + } + + break; + + case '?': + print_usage(argv[0], stdout, 0); + break; + + default: + abort (); + } + } + + return 0; +} + + + +#if 0 +static void parse_error(int err) +{ + switch(err) + { + case IMAGE_UTIL_ERROR_NONE: + printf("Error=%s\n", "IMAGE_UTIL_ERROR_NONE"); + break; + case IMAGE_UTIL_ERROR_INVALID_PARAMETER: + printf("Error=%s\n", "IMAGE_UTIL_ERROR_INVALID_PARAMETER"); + break; + case IMAGE_UTIL_ERROR_OUT_OF_MEMORY: + printf("Error=%s\n", "IMAGE_UTIL_ERROR_OUT_OF_MEMORY"); + break; + case IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT: + printf("Error=%s\n", "IMAGE_UTIL_ERROR_NOT_SUPPORTED_FORMAT"); + break; + case IMAGE_UTIL_ERROR_INVALID_OPERATION: + printf("Error=%s\n", "IMAGE_UTIL_ERROR_INVALID_OPERATION"); + break; + default: + printf("Unknown Error=%d\n", err); + break; + } + +} + + +static bool _supported_jpeg_colorspace_cb(image_util_colorspace_e colorspace, void *user_data) +{ + struct colorspace { + int color; + char *name; + }; +#define stringify( name ) # name + + struct colorspace list[] = { + { IMAGE_UTIL_COLORSPACE_YUV422, stringify(IMAGE_UTIL_COLORSPACE_YUV422), }, + { IMAGE_UTIL_COLORSPACE_I420, stringify(IMAGE_UTIL_COLORSPACE_I420), }, + { IMAGE_UTIL_COLORSPACE_NV12, stringify(IMAGE_UTIL_COLORSPACE_NV12), }, + { IMAGE_UTIL_COLORSPACE_UYVY, stringify(IMAGE_UTIL_COLORSPACE_UYVY), }, + { IMAGE_UTIL_COLORSPACE_YUYV, stringify(IMAGE_UTIL_COLORSPACE_YUYV), }, + { IMAGE_UTIL_COLORSPACE_RGB565, stringify(IMAGE_UTIL_COLORSPACE_RGB565), }, + { IMAGE_UTIL_COLORSPACE_RGB888, stringify(IMAGE_UTIL_COLORSPACE_RGB888), }, + { IMAGE_UTIL_COLORSPACE_ARGB8888, stringify(IMAGE_UTIL_COLORSPACE_ARGB8888), }, + { IMAGE_UTIL_COLORSPACE_BGRA8888, stringify(IMAGE_UTIL_COLORSPACE_BGRA8888), }, + { IMAGE_UTIL_COLORSPACE_RGBA8888, stringify(IMAGE_UTIL_COLORSPACE_RGBA8888), }, + { IMAGE_UTIL_COLORSPACE_BGRX8888, stringify(IMAGE_UTIL_COLORSPACE_BGRX8888), }, + }; + + int i = 0; + + for ( i = 0 ; i < sizeof(list)/sizeof(list[0]); i++) + { + if ( list[i].color == colorspace ) + { + printf("Supported : %s\n", list[i].name); + break; + } + } + + return true; +} +#endif + + +int read_jpeg_file( char *filename, char **buf, int *w, int *h, int *bufsize ) +{ + /* these are standard libjpeg structures for reading(decompression) */ + struct jpeg_decompress_struct cinfo; + struct jpeg_error_mgr jerr; + /* libjpeg data structure for storing one row, that is, scanline of an image */ + JSAMPROW row_pointer[1]; + + FILE *infile = fopen( filename, "rb" ); + unsigned long location = 0; + int i = 0; + + if ( !infile ) + { + printf("Error opening jpeg file %s\n!", filename ); + return -1; + } + /* here we set up the standard libjpeg error handler */ + cinfo.err = jpeg_std_error( &jerr ); + /* setup decompression process and source, then read JPEG header */ + jpeg_create_decompress( &cinfo ); + /* this makes the library read from infile */ + jpeg_stdio_src( &cinfo, infile ); + /* reading the image header which contains image information */ + jpeg_read_header( &cinfo, TRUE ); + /* Uncomment the following to output image information, if needed. */ + + /* Start decompression jpeg here */ + jpeg_start_decompress( &cinfo ); + + printf( "JPEG File Information: \n" ); + printf( "Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height ); + printf( "Output width and height: %d pixels and %d pixels.\n", cinfo.output_width, cinfo.output_height ); + printf( "Color components per pixel: %d.\n", cinfo.num_components ); + printf( "Color space: %d.\n", cinfo.jpeg_color_space ); + + *w = cinfo.output_width; + *h = cinfo.output_height; + *bufsize = cinfo.output_width*cinfo.output_height*cinfo.num_components; + + unsigned char *outbuf = NULL; + + /* allocate memory to hold the uncompressed image */ + outbuf = (unsigned char*)calloc(1, cinfo.output_width*cinfo.output_height*cinfo.num_components ); + + *buf = outbuf; + + /* now actually read the jpeg into the raw buffer */ + row_pointer[0] = (unsigned char *)calloc(1, cinfo.output_width*cinfo.num_components ); + /* read one scan line at a time */ + while( cinfo.output_scanline < cinfo.image_height ) + { + jpeg_read_scanlines( &cinfo, row_pointer, 1 ); + for( i=0; i<cinfo.image_width*cinfo.num_components;i++) + outbuf[location++] = row_pointer[0][i]; + } + /* wrap up decompression, destroy objects, free pointers and close open files */ + jpeg_finish_decompress( &cinfo ); + jpeg_destroy_decompress( &cinfo ); + free( row_pointer[0] ); + fclose( infile ); + /* yup, we succeeded! */ + return 1; +} + + +void +ConvertRGB565toY(unsigned char* pBuf, unsigned char* pGrayBuf, int width, int height) +{ + unsigned char* pDst, * pEnd; + unsigned short* pSrc; + + unsigned short R, G, B; + long lt; + + pSrc = (unsigned short*) pBuf; + pDst = (unsigned char*) pGrayBuf; + pEnd = (unsigned char*) ((unsigned char*) pGrayBuf + height * width); + + while (pDst < pEnd) + { + R = (unsigned char) ((*pSrc >> 11) << 3); // R + G = (unsigned char) ((*pSrc & 0x07e0) >> 3); // +G + B = (unsigned char) ((*pSrc++ & 0x001f) << 3); // +B + + // Y = 0.299 R + 0.587 G + 0.114 B + lt = (306L * (long) R + 601L * (long) G + 117L * (long) B); + *pDst++ = (unsigned char ) (lt >> 10); + //(BYTE)(((int)R+(int)G+(int)B) /3); + } +} + +void +ConvertARGB8888toY(unsigned char* pBuf, unsigned char* pGrayBuf, int width, int height) +{ + unsigned long* pSrc = (unsigned long*) pBuf; + unsigned char* pDSt = (unsigned char*) pGrayBuf; + unsigned char* pEnd = pDSt + height * width; + + while (pDSt < pEnd) + { + unsigned long r = (*pSrc >> 16) & 0xFF; + unsigned long g = (*pSrc >> 8) & 0xFF; + unsigned long b = (*pSrc++) & 0xFF; + + *pDSt++ = (308 * r + 600 * g + 116 * b) >> 10; + } +} + +#define CLIP(a) ((a) > 255 ? 255 : (a) < 0 ? 0 : (a)) + +void +ConvertRGB888toY(unsigned char* pBuf, unsigned char* pGrayBuf, int width, int height) +{ + unsigned char* pSrc = (unsigned char*) pBuf; + unsigned char* pDSt = (unsigned char*) pGrayBuf; + + unsigned char* pEnd = pDSt + height * width; + + while (pDSt < pEnd) + { + unsigned long r = *pSrc++; + unsigned long g = *pSrc++; + unsigned long b = *pSrc++; + + *pDSt++ = (308 * r + 600 * g + 116 * b) >> 10; + } +} + +void read_raw_file(char *filename, char **buf, int *bufsize ) +{ + FILE *fp; + + fp = fopen(filename, "rb"); + + fseek(fp, 0L, SEEK_END); + long size = ftell(fp); + + fseek(fp, 0L, SEEK_SET); + + + *buf = calloc(1, size); + *bufsize = size; + + fread(buf, 1, size, fp); + + fclose(fp); + +} + +void _save_image(char *filename, void *buf, int len) +{ + FILE *fp; + + fp = fopen(filename, "wb"); + + + fwrite(buf, 1, len, fp); + + fclose(fp); + +} + +void _test_image(char *filename) +{ + unsigned char *buf = NULL; + int nW, nH; + int nBufferSize; + +#if 0 + image_util_foreach_supported_jpeg_colorspace ( _supported_jpeg_colorspace_cb, NULL); + + + int err = 0; + + err = image_util_decode_jpeg(filename, IMAGE_UTIL_COLORSPACE_RGB888, &buf, &nW, &nH, &nBufferSize ); + + if ( err != IMAGE_UTIL_ERROR_NONE ) + { + parse_error(err); + return -1; + } + else + { + printf("Open Success : %s\n", filename); + } +#endif + unsigned char *YBuf; + +#if 1 + read_jpeg_file(filename, &buf, &nW, &nH, &nBufferSize ); + + if ( bSaveRaw == true ) + { + char fname[1024]; + + sprintf(fname, "%s_%dx%d.888", filename, nW, nH); + + _save_image(fname, buf, nW*nH*3); + + printf("Write %s to disk\n", fname); + } + + YBuf = (unsigned char *)calloc(1, nW * nH); + + ConvertRGB888toY(buf, YBuf, nW, nH); + + if ( bSaveRaw == true ) + { + char fname[1024]; + + sprintf(fname, "%s_%dx%d.raw", filename, nW, nH); + + _save_image(fname, YBuf, nW*nH); + + printf("Write %s to disk\n", fname); + } +#else + read_raw_file(filename, &YBuf, &nBufferSize ); +#endif + face_h handle; + + face_create(&handle); + + face_image_h image; + + face_image_create(FACE_IMAGE_COLORSPACE_LUMINANCE_ONLY, YBuf, nW, nH, nW*nH, &image); + + int nCount = 0; + + face_rect_s *rects = NULL; + printf("rects=0x%08x &rects=0x%08x\n", rects, &rects); + + int j = 0; + + for ( j = 0 ; j < 2; j++) + { + + face_detect_faces(handle, FACE_IMAGE_TYPE_SINGLE, image, &rects, &nCount); + + if ( nCount == 0 ) + { + printf("No face is founded\n"); + break; + } + +// rects[0] rects[1] .. rects[nCount -1 ] + + printf("Detected : %d\n", nCount); + + face_component_h component; + + face_expression_e expression; + face_eye_state_e l, r; + face_feature_h feature; + + int i = 0; + for ( i = 0; i < nCount ; i++) + { + face_extract_component(handle, image, &rects[i], &component); + +// face_recognize_expression(handle, image, component, &expression); + +// face_recognize_blink(handle, image, component, &l, &r); + + face_extract_feature(handle, image, component, &feature); + + face_component_destroy(component); + face_feature_destroy(feature); + } + + free(rects); + + } + + face_image_destroy(image); + + free(YBuf); + face_destroy(handle); + +} + +void _test_video(char *filename, int nW, int nH) +{ + unsigned char *buf = NULL; + + FILE *fp; + fp = fopen(filename, "rb"); + + buf = calloc(1, nW * nH * 12 / 8); + + face_h handle; + face_create(&handle); + + int nCount = 0; + + face_rect_s *rects = NULL; + + int j = 0; + + face_rect_s prev_rect, cur_rect; + + face_image_h image; + face_image_h prev_image = NULL; + + int nRead; + + for ( j = 0 ; j < 100; j++) + { + nRead = fread (buf, 1, nW * nH * 12 /8, fp ); + + if ( nRead != nW * nH * 12 /8 ) + { + printf("Try to read %d bytes, but only %d is read\n", nW * nH * 12 /8, nRead); + } + + face_image_create(FACE_IMAGE_COLORSPACE_LUMINANCE_ONLY, buf, nW, nH, nW * nH, &image); + + face_detect_faces(handle, FACE_IMAGE_TYPE_CONTINIOUS, image, &rects, &nCount ); + + if ( nCount != 0 ) + { + face_get_movement(handle, prev_image, image, &prev_rect, &cur_rect); + + prev_rect.x = rects[0].x; + prev_rect.y = rects[0].y; + prev_rect.w = rects[0].w; + prev_rect.h = rects[0].h; + } + + face_image_destroy(prev_image); + prev_image = image; + + + } + + free(buf); + face_destroy(handle); + +} + +int main(int argc, char *argv[]) +{ + printf("Sample FacialEngine is started\n"); + + parse_param(argc, argv); + + if ( filename == NULL ) + { + printf("Cannot found image file\n"); + return -1; + } + + int nW = QCIF_W, nH = QCIF_H; + + if ( bVideo ) + { + _test_video(filename, nW, nH); + } + else + { + _test_image(filename); + } + + printf("Sample FacialEngine is ended\n"); + return -1; +} + + diff --git a/sample/src/tutorial_1.c b/sample/src/tutorial_1.c new file mode 100755 index 0000000..8333efc --- /dev/null +++ b/sample/src/tutorial_1.c @@ -0,0 +1,94 @@ +/* + * 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. + */ + +#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 -1; + } + + face_h face; + + err = face_create(&face); + + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot create face handle\n"); + return -1; + } + + 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 -1; + } + + 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 -1; + } + + if ( nCount == 0 ) + { + printf("Cannot find face\n"); + return -1; + } + + int i =0; + for ( i = 0; i < nCount; i++) + { + printf("%dth face rectangle XYWH(%d,%d,%d,%d)\n", i + 1, rects[i].x, rects[i].y, rects[i].w, rects[i].h ); + } + + free(rects); + + face_image_destroy(image); + face_destroy(face); + + free(buffer); + + return 0; +} + + + diff --git a/sample/src/tutorial_2.c b/sample/src/tutorial_2.c new file mode 100755 index 0000000..9ed215a --- /dev/null +++ b/sample/src/tutorial_2.c @@ -0,0 +1,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 component 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_point_s leye, reye; + face_rect_s mouse; + + err = face_component_get_left_eye_point(component, &leye); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot extract face component\n"); + return 0; + } + + printf("Left eye is on XY(%d,%d)\n", leye.x, leye.y); + + err = face_component_get_right_eye_point(component, &reye); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot extract face component\n"); + return 0; + } + + printf("Right eye is on XY(%d,%d)\n", reye.x, reye.y); + + err = face_component_get_mouse_rect(component, &mouse); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot extract face component\n"); + return 0; + } + + printf("Mouse area is XYWH(%d,%d,%d,%d)\n", mouse.x, mouse.y, mouse.w, mouse.h); + + free(rects); + + face_component_destroy(component); + + face_image_destroy(image); + face_destroy(face); + + free(buffer); + + return 0; +} + + diff --git a/sample/src/tutorial_3.c b/sample/src/tutorial_3.c new file mode 100755 index 0000000..e81f4d0 --- /dev/null +++ b/sample/src/tutorial_3.c @@ -0,0 +1,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; +} + diff --git a/sample/src/tutorial_4.c b/sample/src/tutorial_4.c new file mode 100755 index 0000000..ff1aee7 --- /dev/null +++ b/sample/src/tutorial_4.c @@ -0,0 +1,128 @@ +/* + * 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. + */
+ + +/* + Face tracking with camera preview. +*/ +#include <stdio.h> +#include <stdlib.h> + +#include <Ecore.h> + +#include <face.h> +#include <camera.h> + +static void on_camera_preview(void *stream_buffer, int buffer_size, int width, int height, camera_pixel_format_e format, void *user_data) +{ + face_h face = (face_h)user_data; + + int err; + + face_image_h image; + + err = face_image_create(FACE_IMAGE_COLORSPACE_YUV420, stream_buffer, width, height, buffer_size, &image); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot create image\n"); + return ; + } + + int nCount; + face_rect_s *face_rects = NULL; + + err = face_detect_faces(face, FACE_IMAGE_TYPE_CONTINIOUS, image, &face_rects, &nCount ); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot detect faces\n"); + goto end; + } + + if ( nCount != 0 ) + { + printf("Face Rect XYWH(%d,%d,%d,%d)\n", face_rects[0].x, face_rects[0].y, face_rects[0].w, face_rects[0].h); + } +end: + if ( face_rects ) + free(face_rects); + + face_image_destroy(image); + +} + +int main(int argc, char *argv[]) +{ + int err; + + ecore_init(); + + face_h face; + + err = face_create(&face); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot create face\n"); + return -1; + } + + camera_h camera; + + err = camera_create(CAMERA_DEVICE_CAMERA1, &camera); + if ( err != CAMERA_ERROR_NONE ) + { + printf("Cannot create camera\n"); + return -1; + } +
+ err = camera_set_preview_resolution (camera, 320, 240); + if ( err != CAMERA_ERROR_NONE ) + { + printf("Cannot set preview resolution\n"); + return -1; + } + + err = camera_set_preview_format (camera, CAMERA_PIXEL_FORMAT_I420); + if ( err != CAMERA_ERROR_NONE ) + { + printf("Cannot set preview format : %d\n", CAMERA_PIXEL_FORMAT_I420); + return -1; + } + + err = camera_set_preview_cb (camera, on_camera_preview, face); + if ( err != CAMERA_ERROR_NONE ) + { + printf("Cannot set callback for frame decoded\n"); + return -1; + } + + err = camera_start_preview(camera); + if ( err != CAMERA_ERROR_NONE ) + { + printf("Cannot start camera err=%d\n", err); + return -1; + } + + + ecore_main_loop_begin(); + + face_destroy(face); + camera_destroy(camera); + + return 0; +} + + + diff --git a/sample/src/tutorial_5.c b/sample/src/tutorial_5.c new file mode 100755 index 0000000..b4ca0c0 --- /dev/null +++ b/sample/src/tutorial_5.c @@ -0,0 +1,143 @@ +/* + * 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. + */ + + +/* + Calculate face simility within 2 photos +*/ + +#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[2]; + + err = face_image_create(FACE_IMAGE_COLORSPACE_YUV420, buffer, width, height, size, &image[0]); + + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot create first face_image\n"); + return 0; + } + + err = face_image_create(FACE_IMAGE_COLORSPACE_YUV420, buffer, width, height, size, &image[1]); + + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot create second face_image\n"); + return 0; + } + + face_rect_s *rects = NULL; + int nCount; + + int i; + + face_feature_h feature[2]; + + for ( i = 0; i < 2 ; i++) + { + err = face_detect_faces(face, FACE_IMAGE_TYPE_SINGLE, image[i], &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[i], interest_face, &component); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot extract face component\n"); + return 0; + } + + err = face_extract_feature(face, image[i], component, &feature[i]); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot extract face feature\n"); + return 0; + } + + free(rects); + face_component_destroy(component); + + } + + int similarity; + + err = face_compare_feature(feature[0], feature[1], &similarity); + if ( err != FACE_ERROR_NONE ) + { + printf("Cannot compare feature\n"); + return 0; + } + + printf("Face similarity is %d\n", similarity); + + + face_feature_destroy(feature[0]); + face_feature_destroy(feature[1]); + + face_image_destroy(image[0]); + face_image_destroy(image[1]); + + face_destroy(face); + + free(buffer); + + return 0; +} + |