*/
#include <stdexcept>
-#include <opencv2/core.hpp>
+#include <opencv2/imgcodecs.hpp>
+#include <opencv2/imgproc.hpp>
#include "MvFaceLandmarkDetectionTask.h"
#include "SingleoTimer.h"
if (ret != MEDIA_VISION_ERROR_NONE)
throw runtime_error("Fail to create face landmark detection handle.");
+ ret = mv_facial_landmark_set_model(_handle, "FLD_U2NET", "fld_u2net_160x160.tflite", "fld_u2net_160x160.json", "");
+ if (ret != MEDIA_VISION_ERROR_NONE)
+ throw runtime_error("Fail to set face landmark detection model.");
+
ret = mv_facial_landmark_configure(_handle);
if (ret != MEDIA_VISION_ERROR_NONE)
throw runtime_error("Fail to configure face landmark detection.");
mv_destroy_source(_mv_src);
}
+void MvFaceLandmarkDetectionTask::getLandmarks(cv::Mat &data, Points &landmarks, int roi_left, int roi_right)
+{
+ int ret = mv_source_fill_by_buffer(_mv_src, data.data, data.cols * data.rows * data.channels(), data.cols,
+ data.rows, MEDIA_VISION_COLORSPACE_RGB888);
+ if (ret != MEDIA_VISION_ERROR_NONE)
+ throw runtime_error("Fail to convert to mv source.");
+
+ ret = mv_facial_landmark_inference(_handle, _mv_src);
+ if (ret != MEDIA_VISION_ERROR_NONE)
+ throw runtime_error("Fail to invoke face landmark detection.");
+
+ unsigned long frame_number;
+ unsigned int result_cnt;
+ ret = mv_facial_landmark_get_result_count(_handle, &frame_number, &result_cnt);
+ if (ret != MEDIA_VISION_ERROR_NONE)
+ throw runtime_error("Fail to get face landmark detection result count.");
+
+ Point landmark;
+ for (unsigned int idx = 0; idx < result_cnt; ++idx) {
+
+ ret = mv_facial_landmark_get_position(_handle, idx, &landmark.x, &landmark.y);
+ if (ret != MEDIA_VISION_ERROR_NONE)
+ throw runtime_error("Fail to get face landmark detection bound box.");
+
+ landmark.x += roi_left;
+ landmark.y += roi_right;
+ landmarks.push_back(landmark);
+ SINGLEO_LOGD("idx[%2zd]: (%3zd, %3zd)", idx, landmark.x, landmark.y);
+ }
+}
+
FaceResult& MvFaceLandmarkDetectionTask::handle(unsigned char* data, unsigned int width, unsigned int height, unsigned int byte_per_pixel, FaceResult &result)
{
SINGLEO_LOGD("MvFaceLandmarkDetectionTask::handle()");
Timer timer;
cv::Mat cvData(cv::Size(width, height), CV_MAKE_TYPE(CV_8U, byte_per_pixel), data);
- cv::Mat roiCvData;
+ Points landmarks;
- if (result._rects.empty())
- roiCvData = cvData;
- else{
+ if (result._rects.empty()) {
+ getLandmarks(cvData, landmarks);
+
+ result._landmarks.push_back(landmarks);
+ result = MvFaceTaskManager::handle(data, width, height, byte_per_pixel, result);
+ } else {
for (auto &rect : result._rects) {
- roiCvData = cvData(cv::Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top));
-
- int ret = mv_source_fill_by_buffer(_mv_src, roiCvData.data, roiCvData.cols * roiCvData.rows * byte_per_pixel, roiCvData.cols,
- roiCvData.rows, MEDIA_VISION_COLORSPACE_RGB888);
- if (ret != MEDIA_VISION_ERROR_NONE)
- throw runtime_error("Fail to convert to mv source.");
-
- ret = mv_facial_landmark_inference(_handle, _mv_src);
- if (ret != MEDIA_VISION_ERROR_NONE)
- throw runtime_error("Fail to invoke face landmark detection.");
-
- unsigned long frame_number;
- unsigned int result_cnt;
- ret = mv_facial_landmark_get_result_count(_handle, &frame_number, &result_cnt);
- if (ret != MEDIA_VISION_ERROR_NONE)
- throw runtime_error("Fail to get face landmark detection result count.");
-
- Points landmarks;
- for (unsigned int idx = 0; idx < result_cnt; ++idx) {
- Point landmark;
-
- ret = mv_facial_landmark_get_position(_handle, idx, &landmark.x, &landmark.y);
- if (ret != MEDIA_VISION_ERROR_NONE)
- throw runtime_error("Fail to get face landmark detection bound box.");
-
- landmark.x += rect.left;
- landmark.y += rect.top;
- landmarks.push_back(landmark);
- SINGLEO_LOGD("idx[%2zd]: (%3zd, %3zd)", idx, landmark.x, landmark.y);
- }
+ cv::Mat roiCvData = cvData(cv::Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top)).clone();
+
+ getLandmarks(roiCvData, landmarks, rect.left, rect.top);
result._landmarks.push_back(landmarks);
result = MvFaceTaskManager::handle(data, width, height, byte_per_pixel, result);
#include "MvInferenceFaceService.h"
#include "MvFaceDetectionTask.h"
#include "MvFaceLandmarkDetectionTask.h"
+#include <opencv2/core.hpp>
+#include <opencv2/imgcodecs.hpp>
+#include <opencv2/imgproc.hpp>
using namespace std;
ImageDataType &data = dynamic_cast<ImageDataType &>(input);
_result = _taskManager->handle(data.ptr, data.width, data.height, data.byte_per_pixel, _result);
+
+ cv::Mat _dump(cv::Size(data.width, data.height),CV_MAKE_TYPE(CV_8U, data.byte_per_pixel), data.ptr);
+
+ int id = 0;
+ for (auto &rect : _result._rects) {
+ cv::rectangle(_dump, cv::Rect(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top), cv::Scalar(0, 255, 0), 2);
+ auto &points = _result._landmarks[id];
+ for (auto &point : points)
+ cv::circle(_dump, cv::Point(point.x, point.y), 2, cv::Scalar(255, 0, 0), 2);
+ id++;
+ }
+
+ cv::cvtColor(_dump, _dump, cv::COLOR_RGB2BGR);
+ cv::imwrite("dump_face.jpg",_dump);
}
FaceResult& MvInferenceFaceService::result()