From 6e6415433f972ce98f845e775b85e1cdd897ecab Mon Sep 17 00:00:00 2001 From: Ruben Date: Thu, 16 Nov 2017 20:12:29 +0100 Subject: [PATCH] Add face rectangle to JSON & test single image scan --- opencv-webcam-demo/opencv-webcam-demo.cpp | 47 ++++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/opencv-webcam-demo/opencv-webcam-demo.cpp b/opencv-webcam-demo/opencv-webcam-demo.cpp index 59a2eb0..3780656 100644 --- a/opencv-webcam-demo/opencv-webcam-demo.cpp +++ b/opencv-webcam-demo/opencv-webcam-demo.cpp @@ -22,6 +22,30 @@ using namespace affdex; using namespace cv; +FeaturePoint minPoint(VecFeaturePoint points) +{ + VecFeaturePoint::iterator it = points.begin(); + FeaturePoint ret = *it; + for (; it != points.end(); it++) + { + if (it->x < ret.x) ret.x = it->x; + if (it->y < ret.y) ret.y = it->y; + } + return ret; +}; + +FeaturePoint maxPoint(VecFeaturePoint points) +{ + VecFeaturePoint::iterator it = points.begin(); + FeaturePoint ret = *it; + for (; it != points.end(); it++) + { + if (it->x > ret.x) ret.x = it->x; + if (it->y > ret.y) ret.y = it->y; + } + return ret; +}; + std::string getAsJson(int framenr, const std::map faces, const double timeStamp) { std::stringstream ss; @@ -30,11 +54,12 @@ std::string getAsJson(int framenr, const std::map faces, const dou ss << "'faces':["; int i(0); + for (auto & face_id_pair : faces) { Face f = face_id_pair.second; - + if(i > 0) { ss << ","; } i++; @@ -78,7 +103,13 @@ std::string getAsJson(int framenr, const std::map faces, const dou ss << "'" << expression << "':" << (*values) << ","; values++; } - + + FeaturePoint tl = minPoint(f.featurePoints); + FeaturePoint br = maxPoint(f.featurePoints); + + ss << "'rect':{'x':" << tl.x << ",'y':" << tl.y + << ",'w':" << (br.x - tl.x) << ",'h':" << (br.y - tl.y) << "},"; + ss << "'ioDistance':"<< f.measurements.interocularDistance << ","; ss << "'id':"<< f.id; ss << "}"; @@ -240,7 +271,7 @@ int main(int argsc, char ** argsv) std::cerr << "Failed to read frame from webcam! " << std::endl; break; }*/ - std::string infile = "/home/crowd/capt0000.jpg"; + std::string infile = "/home/crowd/IMG_0011.JPG"; cv::Mat img = imread(infile, 1); //~ imread(img); @@ -254,11 +285,10 @@ int main(int argsc, char ** argsv) capture_fps = 1.0f / (seconds - last_timestamp); last_timestamp = seconds; frameDetector->process(f); //Pass the frame to detector - std::cerr << "test1"; + // For each frame processed if (listenPtr->getDataSize() > 0) { - std::cerr << "test2"; framenr++; std::pair > dataPoint = listenPtr->getData(); @@ -284,16 +314,15 @@ int main(int argsc, char ** argsv) char buff[100]; snprintf(buff, sizeof(buff), "frame%06d.jpg", framenr); std::string targetFilename = buff; // convert to std::string - + vector compression_params; compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); compression_params.push_back(90); imwrite(targetFilename, img, compression_params); + + break; } - - - break; }