sdk-hwV1.3/external/fast-user-adapter/rt_media/demo/demo_face_recognition.c

336 lines
8.7 KiB
C
Executable File

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <semaphore.h>
#include <signal.h>
#define LOG_TAG "demo_face_recognition"
#include <linux/videodev2.h>
#include <media/sunxi_camera_v2.h>
#include "AW_VideoInput_API.h"
#include "aw_util.h"
#include "rt_pixtalks_face.h"
#define OUT_PUT_FILE_2 "/tmp"
#define FACE_SRC_WIDTH 480
#define FACE_SRC_HEIGHT 640
#define REGISTER_FACE_IMG_PATH_LEN 32
static int max_bitstream_count = SAVE_BITSTREAM_COUNT;
FILE *out_file_0 = NULL;
demo_video_param mparam;
static int get_yuv_frame_data(int channel0, VideoInputConfig* config0, int channel1, VideoInputConfig* config1, struct rt_pixtalks_face_config_param* face_model_config)
{
int ret = 0;
unsigned char file_name[128] = {0};
FILE *out_yuv_file0 = NULL;
FILE *out_yuv_file1 = NULL;
VideoYuvFrame mYuvFrame0;
VideoYuvFrame mYuvFrame1;
int cnt = mparam.encoder_num;
int bsave_yuv_data = 1;
sprintf(file_name, "%s/nir_%dx%d.yuv", mparam.OutputFilePath, config0->width, config0->height);
out_yuv_file0 = fopen(file_name, "wb");
if(out_yuv_file0 == NULL)
{
aw_loge("fopen failed");
}
sprintf(file_name, "%s/nir_%dx%d.yuv", mparam.OutputFilePath, config1->width, config1->height);
out_yuv_file1 = fopen(file_name, "wb");
if(out_yuv_file1 == NULL)
{
aw_loge("fopen failed");
}
while(cnt > 0)
{
if (AWVideoInput_Check_Wait_Start(channel0)) {
usleep(200*1000);
aw_logd("wait start");
continue;
}
memset(&mYuvFrame0, 0, sizeof(VideoYuvFrame));
ret = AWVideoInput_GetYuvFrame(channel0, &mYuvFrame0);
aw_logw("ret = %d, cnt = %d; frame info: w&h = %d, %d; phyAddr = %p, %p, %p, virAddr = %p, %p, %p time %lld\n",
ret, cnt, mYuvFrame0.widht, mYuvFrame0.height,
mYuvFrame0.phyAddr[0], mYuvFrame0.phyAddr[1], mYuvFrame0.phyAddr[2],
mYuvFrame0.virAddr[0], mYuvFrame0.virAddr[1], mYuvFrame0.virAddr[2], get_cur_time_us());
//usleep(100*1000);
if(ret == 0)
{
int buf_size = mYuvFrame0.widht*mYuvFrame0.height*3/2;
if(bsave_yuv_data == 1 && out_yuv_file0 != NULL)
fwrite(mYuvFrame0.virAddr[0], 1, buf_size, out_yuv_file0);
}
if (ret != 0) {
aw_logd("Getting yuv failed, break!");
break;
}
if (AWVideoInput_Check_Wait_Start(channel1)) {
usleep(200*1000);
aw_logd("wait start");
continue;
}
memset(&mYuvFrame1, 0, sizeof(VideoYuvFrame));
ret = AWVideoInput_GetYuvFrame(channel1, &mYuvFrame1);
aw_logw("ret = %d, cnt = %d; frame info: w&h = %d, %d; phyAddr = %p, %p, %p, virAddr = %p, %p, %p time %lld\n",
ret, cnt, mYuvFrame1.widht, mYuvFrame1.height,
mYuvFrame1.phyAddr[0], mYuvFrame1.phyAddr[1], mYuvFrame1.phyAddr[2],
mYuvFrame1.virAddr[0], mYuvFrame1.virAddr[1], mYuvFrame1.virAddr[2], get_cur_time_us());
//usleep(100*1000);
if(ret == 0)
{
int buf_size = mYuvFrame1.widht*mYuvFrame1.height*3/2;
if(bsave_yuv_data == 1 && out_yuv_file1 != NULL)
fwrite(mYuvFrame1.virAddr[0], 1, buf_size, out_yuv_file1);
}
if (ret != 0) {
aw_logd("Getting yuv failed, break!");
break;
}
/* cal face feature feature */
rt_pixtalks_face_cal_fea_sim(face_model_config, mYuvFrame0.virAddr[0], mYuvFrame1.virAddr[0]);
AWVideoInput_ReleaseYuvFrame(channel0, &mYuvFrame0);
AWVideoInput_ReleaseYuvFrame(channel1, &mYuvFrame1);
cnt--;
}
if (out_yuv_file0 != NULL)
fclose(out_yuv_file0);
if (out_yuv_file1 != NULL)
fclose(out_yuv_file1);
return 0;
}
static void exit_demo()
{
int i = 0;
AWVideoInput_DeInit();
aw_logd("exit: deInit end\n");
if(out_file_0)
{
fclose(out_file_0);
out_file_0 = NULL;
}
aw_logd("aw_demo, finish!\n");
return ;
}
int main(int argc, char** argv)
{
aw_logw("get yuv test start, time: %lld\n", get_cur_time_us());
pthread_t thread = 0;
int i = 0;
int ret = 0;
int channelId_0 = 0;
int channelId_1 = 1;
memset(&mparam, 0, sizeof(demo_video_param));
mparam.c0_encoder_format = -1;
mparam.pixelformat = RT_PIXEL_YUV420SP;
mparam.use_vipp_num = 0;
strcpy(mparam.OutputFilePath, OUT_PUT_FILE_2);
/******** begin parse the config paramter ********/
if(argc >= 2)
{
aw_logd("******************************\n");
for(i = 1; i < (int)argc; i += 2)
{
ParseArgument(&mparam, argv[i], argv[i + 1]);
}
aw_logd("******************************\n");
}
else
{
aw_logd(" we need more arguments \n");
PrintDemoUsage();
}
check_param(&mparam);
if(mparam.encoder_num > 0)
max_bitstream_count = mparam.encoder_num;
else
max_bitstream_count = 1;
/* channel0 */
VideoInputConfig config_0;
memset(&config_0, 0, sizeof(VideoInputConfig));
config_0.channelId = channelId_0;
config_0.gop = 30;
config_0.pixelformat = RT_PIXEL_YUV420SP;
config_0.venc_video_signal.video_format = RT_DEFAULT;
switch(mparam.color_space)
{
case V4L2_COLORSPACE_JPEG:
{
config_0.venc_video_signal.full_range_flag = 1;
config_0.venc_video_signal.src_colour_primaries = RT_VENC_YCC;
config_0.venc_video_signal.dst_colour_primaries = RT_VENC_YCC;
break;
}
case V4L2_COLORSPACE_REC709:
{
config_0.venc_video_signal.full_range_flag = 1;
config_0.venc_video_signal.src_colour_primaries = RT_VENC_BT709;
config_0.venc_video_signal.dst_colour_primaries = RT_VENC_BT709;
break;
}
case V4L2_COLORSPACE_REC709_PART_RANGE:
{
config_0.venc_video_signal.full_range_flag = 0;
config_0.venc_video_signal.src_colour_primaries = RT_VENC_BT709;
config_0.venc_video_signal.dst_colour_primaries = RT_VENC_BT709;
break;
}
default:
{
config_0.venc_video_signal.full_range_flag = 1;
config_0.venc_video_signal.src_colour_primaries = RT_VENC_BT709;
config_0.venc_video_signal.dst_colour_primaries = RT_VENC_BT709;
break;
}
}
config_0.output_mode = OUTPUT_MODE_YUV;
config_0.width = FACE_SRC_WIDTH;
config_0.height = FACE_SRC_HEIGHT;
config_0.bitrate = mparam.c0_bitrate/1024; //* kb
config_0.encodeType = mparam.c0_encoder_format;
AWVideoInput_Init();
if(AWVideoInput_Configure(channelId_0, &config_0))
{
aw_loge("config err, exit!");
goto _exit;
}
AWVideoInput_Start(channelId_0, 1);
/* channel1 */
VideoInputConfig config_1;
memset(&config_1, 0, sizeof(VideoInputConfig));
config_1.channelId = channelId_1;
config_1.gop = 30;
config_1.pixelformat = RT_PIXEL_YUV420SP;
config_1.venc_video_signal.video_format = RT_DEFAULT;
switch(mparam.color_space)
{
case V4L2_COLORSPACE_JPEG:
{
config_1.venc_video_signal.full_range_flag = 1;
config_1.venc_video_signal.src_colour_primaries = RT_VENC_YCC;
config_1.venc_video_signal.dst_colour_primaries = RT_VENC_YCC;
break;
}
case V4L2_COLORSPACE_REC709:
{
config_1.venc_video_signal.full_range_flag = 1;
config_1.venc_video_signal.src_colour_primaries = RT_VENC_BT709;
config_1.venc_video_signal.dst_colour_primaries = RT_VENC_BT709;
break;
}
case V4L2_COLORSPACE_REC709_PART_RANGE:
{
config_1.venc_video_signal.full_range_flag = 0;
config_1.venc_video_signal.src_colour_primaries = RT_VENC_BT709;
config_1.venc_video_signal.dst_colour_primaries = RT_VENC_BT709;
break;
}
default:
{
config_1.venc_video_signal.full_range_flag = 1;
config_1.venc_video_signal.src_colour_primaries = RT_VENC_BT709;
config_1.venc_video_signal.dst_colour_primaries = RT_VENC_BT709;
break;
}
}
config_1.output_mode = OUTPUT_MODE_YUV;
config_1.width = FACE_SRC_WIDTH;
config_1.height = FACE_SRC_HEIGHT;
config_1.bitrate = mparam.c0_bitrate/1024; //* kb
config_1.encodeType = mparam.c0_encoder_format;
if(AWVideoInput_Configure(channelId_1, &config_1))
{
aw_loge("config err, exit!");
goto _exit;
}
AWVideoInput_Start(channelId_1, 1);
/* face */
ret = rt_pixtalks_face_init(1<<20);
if(ret)
{
aw_loge("rt_pixtalks_face_init fail!\n");
goto _exit;
}
aw_logd("rt_pixtalks_face_init success!\n");
struct rt_pixtalks_face_config_param face_config;
memset(&face_config, 0, sizeof(rt_pixtalks_face_config_param));
ret = rt_pixtalks_face_create(&face_config);
if(SDK_CODE_OK != ret)
{
aw_loge("rt_pixtalks_face_create fail!\n");
goto rt_pixtalks_face_create_exit;
}
aw_logd("rt_pixtalks_face_create success!\n");
if (get_yuv_frame_data(channelId_0, &config_0, channelId_1, &config_1, &face_config) != 0) {
goto _exit;
}
if(thread != 0)
pthread_join(thread, (void**)&ret);
AWVideoInput_Start(channelId_0, 0);
AWVideoInput_Start(channelId_1, 0);
rt_pixtalks_face_create_exit:
rt_pixtalks_face_destroy(&face_config);
_exit:
exit_demo();
return 0;
}