├── README.md ├── Project1c++ ├── VideoCap.h ├── FD.cpp ├── main.cpp ├── Project1c++.vcxproj.filters ├── FaceRec.h └── Project1c++.vcxproj ├── Project1c++.sln ├── .gitattributes └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # Face-Detection-Recognition 2 | Console Application that detect human face and recognizes it. 3 | The source code is self descriptive and you can also go through the video tutorial: 4 | Link: https://youtu.be/7FozGIJ4LPQ 5 | 6 | -------------------------------------------------------------------------------- /Project1c++/VideoCap.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /*@author gihan tharanga*/ 3 | 4 | #include 5 | #include 6 | 7 | //video capturing methods 8 | 9 | int videoCapturing(); 10 | int videoCapOriginal(); 11 | 12 | /*detect the faces display the frames and number of face*/ 13 | int FaceDetector(std::string&); -------------------------------------------------------------------------------- /Project1c++/FD.cpp: -------------------------------------------------------------------------------- 1 | // Project1c++.cpp : This file contains the 'main' function. Program execution begins and ends there. 2 | // 3 | 4 | #include 5 | 6 | int main() 7 | { 8 | std::cout << "Hello World!\n"; 9 | } 10 | 11 | // Run program: Ctrl + F5 or Debug > Start Without Debugging menu 12 | // Debug program: F5 or Debug > Start Debugging menu 13 | 14 | // Tips for Getting Started: 15 | // 1. Use the Solution Explorer window to add/manage files 16 | // 2. Use the Team Explorer window to connect to source control 17 | // 3. Use the Output window to see build output and other messages 18 | // 4. Use the Error List window to view errors 19 | // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project 20 | // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file 21 | -------------------------------------------------------------------------------- /Project1c++/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //opencv 6 | #include 7 | #include 8 | #include 9 | #include "opencv2\core.hpp" 10 | #include 11 | #include 12 | #include "opencv2/face.hpp" 13 | 14 | //file handling 15 | #include 16 | #include 17 | 18 | //header files that contain functions 19 | #include "FaceRec.h" 20 | 21 | 22 | using namespace std; 23 | using namespace cv; 24 | using namespace cv::face; 25 | 26 | int main() 27 | { 28 | int choice; 29 | cout << "1. Recognise Face\n"; 30 | cout << "2. Add Face\n"; 31 | cout << "Choose One: "; 32 | cin >> choice; 33 | switch (choice) 34 | { 35 | case 1: 36 | FaceRecognition(); 37 | break; 38 | case 2: 39 | addFace(); 40 | eigenFaceTrainer(); 41 | break; 42 | default: 43 | return 0; 44 | } 45 | //system("pause"); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /Project1c++/Project1c++.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /Project1c++.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28803.352 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Project1c++", "Project1c++\Project1c++.vcxproj", "{FABF3CAB-629D-4AE0-A589-1E50890DD89A}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Debug|x64.ActiveCfg = Debug|x64 17 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Debug|x64.Build.0 = Debug|x64 18 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Debug|x86.ActiveCfg = Debug|Win32 19 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Debug|x86.Build.0 = Debug|Win32 20 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Release|x64.ActiveCfg = Release|x64 21 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Release|x64.Build.0 = Release|x64 22 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Release|x86.ActiveCfg = Release|Win32 23 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {73632E06-563E-4700-A8B3-B074ED1955B8} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb -------------------------------------------------------------------------------- /Project1c++/FaceRec.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //include opencv core 5 | #include "opencv2\core\core.hpp" 6 | #include "opencv2\core.hpp" 7 | #include "opencv2\face.hpp" 8 | #include "opencv2\highgui\highgui.hpp" 9 | #include "opencv2\objdetect\objdetect.hpp" 10 | #include "opencv2\opencv.hpp" 11 | #include 12 | 13 | //file handling 14 | #include 15 | #include 16 | 17 | using namespace std; 18 | using namespace cv; 19 | using namespace cv::face; 20 | 21 | CascadeClassifier face_cascade; 22 | string filename; 23 | string name; 24 | int filenumber = 0; 25 | 26 | void detectAndDisplay(Mat frame) 27 | { 28 | vector faces; 29 | Mat frame_gray; 30 | Mat crop; 31 | Mat res; 32 | Mat gray; 33 | string text; 34 | stringstream sstm; 35 | 36 | cvtColor(frame, frame_gray, COLOR_BGR2GRAY); 37 | equalizeHist(frame_gray, frame_gray); 38 | 39 | face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); 40 | 41 | Rect roi_b; 42 | Rect roi_c; 43 | 44 | size_t ic = 0; 45 | int ac = 0; 46 | 47 | size_t ib = 0; 48 | int ab = 0; 49 | 50 | for (ic = 0; ic < faces.size(); ic++) 51 | 52 | { 53 | roi_c.x = faces[ic].x; 54 | roi_c.y = faces[ic].y; 55 | roi_c.width = (faces[ic].width); 56 | roi_c.height = (faces[ic].height); 57 | 58 | ac = roi_c.width * roi_c.height; 59 | 60 | roi_b.x = faces[ib].x; 61 | roi_b.y = faces[ib].y; 62 | roi_b.width = (faces[ib].width); 63 | roi_b.height = (faces[ib].height); 64 | 65 | 66 | crop = frame(roi_b); 67 | resize(crop, res, Size(128, 128), 0, 0, INTER_LINEAR); 68 | cvtColor(crop, gray, COLOR_BGR2GRAY); 69 | stringstream ssfn; 70 | filename = "C:\\Users\\Asus\\Desktop\\Faces\\"; 71 | ssfn << filename.c_str() << name << filenumber << ".jpg"; 72 | filename = ssfn.str(); 73 | imwrite(filename, res); 74 | filenumber++; 75 | 76 | 77 | Point pt1(faces[ic].x, faces[ic].y); 78 | Point pt2((faces[ic].x + faces[ic].height), (faces[ic].y + faces[ic].width)); 79 | rectangle(frame, pt1, pt2, Scalar(0, 255, 0), 2, 8, 0); 80 | } 81 | 82 | 83 | sstm << "Crop area size: " << roi_b.width << "x" << roi_b.height << " Filename: " << filename; 84 | text = sstm.str(); 85 | 86 | if (!crop.empty()) 87 | { 88 | imshow("detected", crop); 89 | } 90 | else 91 | destroyWindow("detected"); 92 | 93 | } 94 | 95 | void addFace() 96 | { 97 | cout << "\nEnter Your Name: "; 98 | cin >> name; 99 | 100 | VideoCapture capture(0); 101 | 102 | if (!capture.isOpened()) 103 | return; 104 | 105 | if (!face_cascade.load("C:\\Users\\Asus\\Downloads\\opencv4.1\\build\\install\\etc\\haarcascades\\haarcascade_frontalface_alt.xml")) 106 | { 107 | cout << "error" << endl; 108 | return ; 109 | }; 110 | 111 | Mat frame; 112 | cout << "\nCapturing your face 10 times, Press 'C' 10 times keeping your face front of the camera"; 113 | char key; 114 | int i = 0; 115 | 116 | for (;;) 117 | { 118 | capture >> frame; 119 | 120 | //imshow("Camera", frame); 121 | detectAndDisplay(frame); 122 | i++; 123 | if (i == 10) 124 | { 125 | cout << "Face Added"; 126 | break; 127 | } 128 | //break; 129 | int c = waitKey(10); 130 | 131 | if (27 == char(c)) 132 | { 133 | break; 134 | } 135 | //imshow("Output Capture", frame); 136 | } 137 | 138 | return; 139 | } 140 | 141 | 142 | static void dbread(vector& images, vector& labels) { 143 | vector fn; 144 | filename = "C:\\Users\\Asus\\Desktop\\Faces\\"; 145 | glob(filename, fn, false); 146 | 147 | //glob("C:\\Users\\Asus\\Desktop\\Faces\\Ali\\*.jpg", fn, false); 148 | 149 | size_t count = fn.size(); 150 | 151 | for (size_t i = 0; i < count; i++) 152 | { 153 | string itsname=""; 154 | char sep = '\\'; 155 | size_t j = fn[i].rfind(sep, fn[i].length()); 156 | if (j != string::npos) 157 | { 158 | itsname=(fn[i].substr(j + 1, fn[i].length() - j-6)); 159 | } 160 | images.push_back(imread(fn[i], 0)); 161 | labels.push_back(atoi(itsname.c_str())); 162 | } 163 | } 164 | 165 | void eigenFaceTrainer() { 166 | vector images; 167 | vector labels; 168 | dbread(images, labels); 169 | cout << "size of the images is " << images.size() << endl; 170 | cout << "size of the labels is " << labels.size() << endl; 171 | cout << "Training begins...." << endl; 172 | 173 | //create algorithm eigenface recognizer 174 | Ptr model = EigenFaceRecognizer::create(); 175 | 176 | //train data 177 | model->train(images, labels); 178 | 179 | model->save("C:\\Users\\Asus\\Desktop\\eigenface.yml"); 180 | 181 | cout << "Training finished...." << endl; 182 | waitKey(10000); 183 | } 184 | 185 | void FaceRecognition() { 186 | 187 | cout << "start recognizing..." << endl; 188 | 189 | //load pre-trained data sets 190 | Ptr model = FisherFaceRecognizer::create(); 191 | model->read("C:\\Users\\Asus\\Desktop\\eigenface.yml"); 192 | 193 | Mat testSample = imread("C:\\Users\\Asus\\Desktop\\0.jpg", 0); 194 | 195 | int img_width = testSample.cols; 196 | int img_height = testSample.rows; 197 | 198 | 199 | //lbpcascades/lbpcascade_frontalface.xml 200 | 201 | string window = "Capture - face detection"; 202 | 203 | if (!face_cascade.load("C:\\Users\\Asus\\Downloads\\opencv4.1\\build\\install\\etc\\haarcascades\\haarcascade_frontalface_alt.xml")) { 204 | cout << " Error loading file" << endl; 205 | return; 206 | } 207 | 208 | VideoCapture cap(0); 209 | //VideoCapture cap("C:/Users/lsf-admin/Pictures/Camera Roll/video000.mp4"); 210 | 211 | if (!cap.isOpened()) 212 | { 213 | cout << "exit" << endl; 214 | return; 215 | } 216 | 217 | //double fps = cap.get(CV_CAP_PROP_FPS); 218 | //cout << " Frames per seconds " << fps << endl; 219 | namedWindow(window, 1); 220 | long count = 0; 221 | string Pname= ""; 222 | 223 | while (true) 224 | { 225 | vector faces; 226 | Mat frame; 227 | Mat graySacleFrame; 228 | Mat original; 229 | 230 | cap >> frame; 231 | //cap.read(frame); 232 | count = count + 1;//count frames; 233 | 234 | if (!frame.empty()) { 235 | 236 | //clone from original frame 237 | original = frame.clone(); 238 | 239 | //convert image to gray scale and equalize 240 | cvtColor(original, graySacleFrame, COLOR_BGR2GRAY); 241 | //equalizeHist(graySacleFrame, graySacleFrame); 242 | 243 | //detect face in gray image 244 | face_cascade.detectMultiScale(graySacleFrame, faces, 1.1, 3, 0, cv::Size(90, 90)); 245 | 246 | //number of faces detected 247 | //cout << faces.size() << " faces detected" << endl; 248 | std::string frameset = std::to_string(count); 249 | std::string faceset = std::to_string(faces.size()); 250 | 251 | int width = 0, height = 0; 252 | 253 | //region of interest 254 | //cv::Rect roi; 255 | 256 | for (int i = 0; i < faces.size(); i++) 257 | { 258 | //region of interest 259 | Rect face_i = faces[i]; 260 | 261 | //crop the roi from grya image 262 | Mat face = graySacleFrame(face_i); 263 | 264 | //resizing the cropped image to suit to database image sizes 265 | Mat face_resized; 266 | cv::resize(face, face_resized, Size(img_width, img_height), 1.0, 1.0, INTER_CUBIC); 267 | 268 | //recognizing what faces detected 269 | int label = -1; double confidence = 0; 270 | model->predict(face_resized, label, confidence); 271 | 272 | cout << " confidence " << confidence << " Label: " << label << endl; 273 | 274 | Pname = to_string(label); 275 | 276 | //drawing green rectagle in recognize face 277 | rectangle(original, face_i, CV_RGB(0, 255, 0), 1); 278 | string text = Pname; 279 | 280 | int pos_x = std::max(face_i.tl().x - 10, 0); 281 | int pos_y = std::max(face_i.tl().y - 10, 0); 282 | 283 | //name the person who is in the image 284 | putText(original, text, Point(pos_x, pos_y), FONT_HERSHEY_COMPLEX_SMALL, 1.0, CV_RGB(0, 255, 0), 1.0); 285 | //cv::imwrite("E:/FDB/"+frameset+".jpg", cropImg); 286 | 287 | } 288 | 289 | 290 | putText(original, "Frames: " + frameset, Point(30, 60), FONT_HERSHEY_COMPLEX_SMALL, 1.0, CV_RGB(0, 255, 0), 1.0); 291 | putText(original, "No. of Persons detected: " + to_string(faces.size()), Point(30, 90), FONT_HERSHEY_COMPLEX_SMALL, 1.0, CV_RGB(0, 255, 0), 1.0); 292 | //display to the winodw 293 | cv::imshow(window, original); 294 | 295 | //cout << "model infor " << model->getDouble("threshold") << endl; 296 | 297 | } 298 | if (waitKey(30) >= 0) break; 299 | } 300 | } 301 | -------------------------------------------------------------------------------- /Project1c++/Project1c++.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 16.0 29 | {FABF3CAB-629D-4AE0-A589-1E50890DD89A} 30 | Win32Proj 31 | Project1c 32 | 10.0 33 | 34 | 35 | 36 | Application 37 | true 38 | v142 39 | Unicode 40 | 41 | 42 | Application 43 | false 44 | v142 45 | true 46 | Unicode 47 | 48 | 49 | Application 50 | true 51 | v142 52 | Unicode 53 | 54 | 55 | Application 56 | false 57 | v142 58 | true 59 | Unicode 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | true 81 | 82 | 83 | true 84 | 85 | 86 | false 87 | 88 | 89 | false 90 | 91 | 92 | 93 | 94 | 95 | Level3 96 | Disabled 97 | true 98 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 99 | true 100 | 101 | 102 | Console 103 | true 104 | 105 | 106 | 107 | 108 | 109 | 110 | Level3 111 | Disabled 112 | true 113 | _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 114 | true 115 | C:\Users\Asus\Downloads\opencv4.1\build\install\include;%(AdditionalIncludeDirectories) 116 | 117 | 118 | Console 119 | true 120 | C:\Users\Asus\Downloads\opencv4.1\build\install\x64\vc16\lib;%(AdditionalLibraryDirectories) 121 | opencv_aruco410d.lib;opencv_bgsegm410d.lib;opencv_bioinspired410d.lib;opencv_calib3d410d.lib;opencv_ccalib410d.lib;opencv_core410d.lib;opencv_datasets410d.lib;opencv_dnn_objdetect410d.lib;opencv_dnn410d.lib;opencv_dpm410d.lib;opencv_face410d.lib;opencv_features2d410d.lib;opencv_flann410d.lib;opencv_fuzzy410d.lib;opencv_gapi410d.lib;opencv_hfs410d.lib;opencv_highgui410d.lib;opencv_img_hash410d.lib;opencv_imgproc410d.lib;opencv_line_descriptor410d.lib;opencv_ml410d.lib;opencv_objdetect410d.lib;opencv_optflow410d.lib;opencv_phase_unwrapping410d.lib;opencv_photo410d.lib;opencv_plot410d.lib;opencv_quality410d.lib;opencv_reg410d.lib;opencv_rgbd410d.lib;opencv_saliency410d.lib;opencv_shape410d.lib;opencv_stereo410d.lib;opencv_stitching410d.lib;opencv_structured_light410d.lib;opencv_superres410d.lib;opencv_surface_matching410d.lib;opencv_text410d.lib;opencv_tracking410d.lib;opencv_video410d.lib;opencv_videoio410d.lib;opencv_xfeatures2d410d.lib;opencv_ximgproc410d.lib;opencv_xobjdetect410d.lib;opencv_xphoto410d.lib;opencv_imgcodecs410d.lib;%(AdditionalDependencies) 122 | 123 | 124 | 125 | 126 | 127 | 128 | Level3 129 | MaxSpeed 130 | true 131 | true 132 | true 133 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 134 | true 135 | 136 | 137 | Console 138 | true 139 | true 140 | true 141 | 142 | 143 | 144 | 145 | 146 | 147 | Level3 148 | MaxSpeed 149 | true 150 | true 151 | true 152 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 153 | true 154 | 155 | 156 | Console 157 | true 158 | true 159 | true 160 | 161 | 162 | 163 | 164 | 165 | --------------------------------------------------------------------------------