commit 7dc4d6531f61265201c8ebd7e67b60c2acc5d6d0 Author: Abdelrahman Mahmoud Date: Mon Mar 9 14:22:07 2015 -0400 Add project and src file diff --git a/affdex-win-samples.sln b/affdex-win-samples.sln new file mode 100644 index 0000000..e8a0d7a --- /dev/null +++ b/affdex-win-samples.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "opencv-webcam-demo", "opencv-webcam-demo\opencv-webcam-demo.vcxproj", "{8DCDC209-C25D-4C61-B2AC-2FBA1775DD6B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8DCDC209-C25D-4C61-B2AC-2FBA1775DD6B}.Debug|Win32.ActiveCfg = Debug|Win32 + {8DCDC209-C25D-4C61-B2AC-2FBA1775DD6B}.Debug|Win32.Build.0 = Debug|Win32 + {8DCDC209-C25D-4C61-B2AC-2FBA1775DD6B}.Release|Win32.ActiveCfg = Release|Win32 + {8DCDC209-C25D-4C61-B2AC-2FBA1775DD6B}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/opencv-webcam-demo/opencv-webcam-demo.cpp b/opencv-webcam-demo/opencv-webcam-demo.cpp new file mode 100644 index 0000000..9164f82 --- /dev/null +++ b/opencv-webcam-demo/opencv-webcam-demo.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include + +#include "Frame.h" +#include "Face.h" +#include "ImageListener.h" +#include "FrameDetector.h" +#include "AffdexException.h" + +using namespace std; +using namespace AFFDEX; + +float last_timestamp = -1.0f; +float capture_fps = -1.0f; +float process_last_timestamp = -1.0f; +float process_fps = -1.0f; + + +class PlottingImageListener : public ImageListener +{ +public: + void onImageResults(vector faces, Frame image) override { + + shared_ptr imgdata = image.getBGRByteArray(); + cv::Mat img = cv::Mat(image.getHeight(), image.getWidth(), CV_8UC3, imgdata.get()); + for (int i = 0; i < faces.size(); i++) + { + Face f = faces[i]; + float smile_score = f.getSmileScore(); + int n = f.getFeaturePointCount(); + VecFeaturePoint points = f.getFeaturePoints(); + for (auto& point : points ) //Draw face feature points. + { + cv::circle(img, cv::Point(point.x, point.y), 1.0f, cv::Scalar(0, 0, 255)); + } + + //Output the results of the different classifiers. + cv::putText(img, "Smile: "+ std::to_string(f.getSmileScore()), cv::Point(30, 30), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + cv::putText(img, "BrowFurrow: " + std::to_string(f.getBrowFurrowScore()), cv::Point(30, 50), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + cv::putText(img, "BrowRaise: " + std::to_string(f.getBrowRaiseScore()), cv::Point(30, 70), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + cv::putText(img, "LipCornerDepressor: " + std::to_string(f.getLipCornerDepressorScore()), cv::Point(30, 90), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + cv::putText(img, "Engagement: " + std::to_string(f.getEngagementScore()), cv::Point(30, 110), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + cv::putText(img, "Valence: " + std::to_string(f.getValenceScore()), cv::Point(30, 130), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + + + //Calculate the processing framerate, output both the processing + capture framerate + if (process_last_timestamp >= 0.0f) + { + process_fps = 1.0f / (image.getTimestamp() - process_last_timestamp); + cv::putText(img, "capture fps: " + std::to_string(capture_fps), cv::Point(img.cols - 200, 30), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + cv::putText(img, "process fps: " + std::to_string(process_fps), cv::Point(img.cols - 200, 50), cv::FONT_HERSHEY_COMPLEX, 0.5f, cv::Scalar(0, 0, 255)); + } + process_last_timestamp = image.getTimestamp(); + } + cv::imshow("analyze-image", img); + + cv::waitKey(30); + }; + + void onImageCapture(Frame image) override + {}; +}; + +int main(int argsc, char ** argsv) +{ + + try{ + // Parse and check the data folder (with assets) + std::wstring DATA_FOLDER = L"data"; + if (argsc > 1) + { + std::string user_folder(argsv[1]); + DATA_FOLDER.assign(user_folder.begin(), user_folder.end()); + } + + int framerate = 30; + int process_frame_rate = 30; + int buffer_length = 2; + if (argsc > 2) + { + framerate = stoi(argsv[2]); + } + + if (argsc > 3) + { + process_frame_rate = stoi(argsv[3]); + } + + FrameDetector frameDetector(buffer_length, process_frame_rate); // Init the FrameDetector Class + shared_ptr listenPtr(new PlottingImageListener()); // Instanciate the ImageListener class + + cv::VideoCapture webcam(0); //Connect to the first webcam + webcam.set(CV_CAP_PROP_FPS, framerate); //Set webcam framerate. + std::cerr << "Setting the webcam frame rate to: " << framerate << std::endl; + auto start_time = std::chrono::system_clock::now(); + if (!webcam.isOpened()) + { + std::cerr << "Error opening webcam!" << std::endl; + return 1; + } + + //Initialize detectors + frameDetector.setDetectSmile(true); + frameDetector.setDetectBrowFurrow(true); + frameDetector.setDetectBrowRaise(true); + frameDetector.setDetectLipCornerDepressor(true); + frameDetector.setDetectEngagement(true); + frameDetector.setDetectValence(true); + frameDetector.setClassifierPath(DATA_FOLDER); + frameDetector.setImageListener(listenPtr.get()); + //Start the frame detector thread. + frameDetector.start(); + + do{ + cv::Mat img; + if (!webcam.read(img)) //Capture an image from the camera + { + std::cerr << "Failed to read frame from webcam! " << std::endl; + break; + } + + //Calculate the Image timestamp and the capture frame rate; + const auto milliseconds = std::chrono::duration_cast(std::chrono::system_clock::now() - start_time); + const float seconds = milliseconds.count() / 1000.f; + // Create a frame + Frame f(img.size().width, img.size().height, img.data, Frame::COLOR_FORMAT::BGR, seconds); + capture_fps = 1.0f / (seconds - last_timestamp); + last_timestamp = seconds; + std::cerr << "Capture framerate = " << capture_fps << std::endl; + frameDetector.process(f); //Pass the frame to detector + } while (!GetAsyncKeyState(VK_ESCAPE)); + + frameDetector.stop(); //Stop frame detector thread + } + catch (AffdexException ex) + { + std::cerr << "Encountered an AffdexException " << ex.what(); + return 1; + } + catch (std::exception ex) + { + std::cerr << "Encountered an exception " << ex.what(); + return 1; + } + catch (std::runtime_error err) + { + std::cerr << "Encountered a runtime error " << err.what(); + return 1; + } + + return 0; +} \ No newline at end of file diff --git a/opencv-webcam-demo/opencv-webcam-demo.vcxproj b/opencv-webcam-demo/opencv-webcam-demo.vcxproj new file mode 100644 index 0000000..0eba572 --- /dev/null +++ b/opencv-webcam-demo/opencv-webcam-demo.vcxproj @@ -0,0 +1,88 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {8DCDC209-C25D-4C61-B2AC-2FBA1775DD6B} + Win32Proj + opencvwebcamdemo + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + true + ..\affdex-sdk\include;..\opencv\build\include + + + Console + true + ..\affdex-sdk\lib\affdex-sdk.lib;..\opencv\build\x86\vc12\lib\opencv_highgui249d.lib;..\opencv\build\x86\vc12\lib\opencv_core249d.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/opencv-webcam-demo/opencv-webcam-demo.vcxproj.filters b/opencv-webcam-demo/opencv-webcam-demo.vcxproj.filters new file mode 100644 index 0000000..0c04a10 --- /dev/null +++ b/opencv-webcam-demo/opencv-webcam-demo.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file