From 31e0d0c171b4d6a7dc9b9697e69e165651d3fe93 Mon Sep 17 00:00:00 2001 From: houxiao <houxiao@454eff88-639b-444f-9e54-f578c98de674> Date: 星期三, 28 十二月 2016 18:32:20 +0800 Subject: [PATCH] face detect ok --- RtspFace/PL_SensetimeFaceDetect.cpp | 155 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 138 insertions(+), 17 deletions(-) diff --git a/RtspFace/PL_SensetimeFaceDetect.cpp b/RtspFace/PL_SensetimeFaceDetect.cpp index 5df118c..f6b22c1 100644 --- a/RtspFace/PL_SensetimeFaceDetect.cpp +++ b/RtspFace/PL_SensetimeFaceDetect.cpp @@ -1,20 +1,25 @@ #include "PL_SensetimeFaceDetect.h" +#include "MaterialBuffer.h" #include <opencv2/opencv.hpp> #include <cv_face.h> struct PL_SensetimeFaceDetect_Internal { - uint8_t buffer[1920*1080*4]; - size_t buffSize; - size_t buffSizeMax; + //uint8_t buffer[1920*1080*4]; + //size_t buffSize; + //size_t buffSizeMax; MB_Frame lastFrame; + SensetimeFaceDetectConfig config; bool payError; + cv_handle_t handle_track; + PL_SensetimeFaceDetect_Internal() : - buffSize(0), buffSizeMax(sizeof(buffer)), lastFrame(), - payError(true) + //buffSize(0), buffSizeMax(sizeof(buffer)), + lastFrame(), config(), payError(true), + handle_track(nullptr) { } @@ -24,11 +29,15 @@ void reset() { - buffSize = 0; + //buffSize = 0; payError = true; MB_Frame _lastFrame; lastFrame = _lastFrame; + SensetimeFaceDetectConfig _config; + config = _config; + + handle_track = nullptr; } }; @@ -52,6 +61,37 @@ PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal; in->reset(); + SensetimeFaceDetectConfig* config = (SensetimeFaceDetectConfig*)args; + in->config = *config; + if (in->config.point_size == 21) + in->config.point_size_config = CV_DETECT_ENABLE_ALIGN_21; + else if (in->config.point_size == 106) + in->config.point_size_config = CV_DETECT_ENABLE_ALIGN_106; + else + { + printf("alignment point size must be 21 or 106\n"); + return false; + } + + // init handle + cv_result_t cv_result = cv_face_create_tracker(&(in->handle_track), nullptr, + in->config.point_size_config | CV_FACE_TRACKING_TWO_THREAD); + if (cv_result != CV_OK) + { + printf("cv_face_create_tracker failed, error code %d\n", cv_result); + return false; + } + + int val = 0; + cv_result = cv_face_track_set_detect_face_cnt_limit(in->handle_track, in->config.detect_face_cnt_limit, &val); + if (cv_result != CV_OK) + { + printf("cv_face_track_set_detect_face_cnt_limit failed, error : %d\n", cv_result); + return false; + } + else + printf("detect face count limit : %d\n", val); + return true; } @@ -59,6 +99,78 @@ { PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal; + // destroy track handle + cv_face_destroy_tracker(in->handle_track); + in->handle_track = nullptr; +} + +int doFaceDetect(PL_SensetimeFaceDetect_Internal* in, + uint8_t* buffer, size_t width, size_t height, size_t stride, cv_pixel_format cvPixFmt) +{ + //resize(bgr_frame, bgr_frame, Size(frame_width, frame_height), 0, 0, INTER_LINEAR); + + int face_count = 0; + cv_result_t cv_result = CV_OK; + cv_face_t* p_face = nullptr; + + // realtime track + cv_result = cv_face_track(in->handle_track, buffer, cvPixFmt, + width, height, stride, + CV_FACE_UP, &p_face, &face_count); + if (cv_result != CV_OK) + { + printf("cv_face_track failed, error : %d\n", cv_result); + cv_face_release_tracker_result(p_face, face_count); + return -1; + } + + // draw the video + cv::Mat yuvMat(cv::Size(1920,1080), CV_8UC3, buffer); + cv::Mat yMat(cv::Size(1920,1080), CV_8UC1, buffer); + for (int i = 0; i < face_count; i++) + { + printf("face: %d-----[%d, %d, %d, %d]-----id: %d\n", i, + p_face[i].rect.left, p_face[i].rect.top, + p_face[i].rect.right, p_face[i].rect.bottom, p_face[i].ID); + + printf("face pose: [yaw: %.2f, pitch: %.2f, roll: %.2f, eye distance: %.2f]\n", + p_face[i].yaw, + p_face[i].pitch, p_face[i].roll, p_face[i].eye_dist); + + cv::Scalar scalar_color = CV_RGB(p_face[i].ID * 53 % 256, + p_face[i].ID * 93 % 256, + p_face[i].ID * 143 % 256); + + //cv::rectangle(yMat, cv::Point2f(0, 0), cv::Point2f(50, 50), scalar_color, 2); + //cv::rectangle(yMat, cv::Point2f(500, 500), cv::Point2f(550, 550), scalar_color, 2); + + cv::rectangle(yMat, cv::Point2f(static_cast<float>(p_face[i].rect.left), + static_cast<float>(p_face[i].rect.top)), + cv::Point2f(static_cast<float>(p_face[i].rect.right), + static_cast<float>(p_face[i].rect.bottom)), scalar_color, 2); + + for (int j = 0; j < p_face[i].points_count; j++) + { + cv::circle(yMat, cv::Point2f(p_face[i].points_array[j].x, + p_face[i].points_array[j].y), 1, cv::Scalar(255, 255, 255)); + } + } + + //if (face_count > 0) + //{ + // static size_t f=0; + // char fname[50]; + // sprintf(fname, "face-%u.yuv420", ++f); + // FILE * pFile = fopen (fname,"wb"); + // fwrite (yuvMat.data , sizeof(char), 1920*1080*1.5, pFile); + // printf("write face file %s\n", fname); + // fclose(pFile); + //} + + // release the memory of face + cv_face_release_tracker_result(p_face, face_count); + + return face_count; } bool PL_SensetimeFaceDetect::pay(const PipeMaterial& pm) @@ -81,16 +193,21 @@ return false; } - + int face_count = doFaceDetect(in, frame->buffer, 1920, 1080, 1920, CV_PIX_FMT_YUV420P); + if (face_count < 0) + { + in->payError = true; + return false; + } + else + in->payError = false; //in->buffer readly - //static size_t f=0; - //char fname[50]; - //sprintf(fname, "%u.bgra", ++f); - //FILE * pFile = fopen (fname,"wb"); - //fwrite (in->buffer , sizeof(char), in->buffSize, pFile); - //fclose(pFile); + in->lastFrame.type = MB_Frame::MBFT_YUV420; + in->lastFrame.buffer = frame->buffer;//#todo should copy + in->lastFrame.buffSize = frame->buffSize; + in->lastFrame.pts = frame->pts; return true; } @@ -99,9 +216,13 @@ { PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal; - pm.type = PipeMaterial::PMT_FRAME; - pm.buffer = (uint8_t*)(&(in->lastFrame)); - pm.buffSize = sizeof(in->lastFrame); + if (!in->payError) + { + pm.type = PipeMaterial::PMT_FRAME; + pm.buffer = (uint8_t*)(&(in->lastFrame)); + pm.buffSize = sizeof(in->lastFrame); + pm.former = this; + } pm.former = this; - return true; + return !in->payError; } -- Gitblit v1.8.0