/detection/TestDetection.cpp

https://github.com/sun11/Face-Recognition · C++ · 80 lines · 54 code · 16 blank · 10 comment · 7 complexity · 8825f50eb9569cc3440b906870c86bb8 MD5 · raw file

  1. /*
  2. * File: main.cpp
  3. * Author: elena
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dirent.h>
  8. #include "Detection.h"
  9. int main(int argc, char** argv) {
  10. // initialize detection module
  11. Detection* detection = new Detection();
  12. if (argc == 1) {
  13. printf( "Usage: \n"
  14. " [--cascade=cascade_name]\n"
  15. " [--scale=scale_value]\n"
  16. " [--hits=hits_number]\n"
  17. " [--size=min_template_size_value]\n"
  18. " [--resultImageWidth=width_of_detected_face_image]\n"
  19. " [--resultImageHeight=height_of_detected_face_image]\n"
  20. " file\n");
  21. exit(0);
  22. }
  23. vector<char*> args;
  24. for (int i = 1; i < argc-1; i++)
  25. args.push_back(argv[i]);
  26. detection->initialize(args);
  27. //create output directory if not exist
  28. string dirName = "result";
  29. DIR* dir = opendir(dirName.c_str());
  30. if (! dir) {
  31. string command = "mkdir ";
  32. command.append(dirName);
  33. system(command.c_str());
  34. }
  35. // open list of files for detection
  36. FILE *f = fopen(argv[argc-1],"r");
  37. while (!feof(f)) {
  38. char fullImageName[100];
  39. fscanf (f,"%s",fullImageName);
  40. // get image Name ( remove directory path, if exists)
  41. string imageName;
  42. string key ("/");
  43. imageName.assign(fullImageName);
  44. size_t found;
  45. found = imageName.rfind(key);
  46. if (found!=string::npos) {
  47. imageName = imageName.substr (found+1);
  48. }
  49. // load image
  50. IplImage* image = cvLoadImage(fullImageName, CV_LOAD_IMAGE_COLOR);
  51. if ( !image ) {
  52. printf("Error loading image!%s",fullImageName);
  53. exit(0);
  54. }
  55. // detect faces from image and save output
  56. vector<CvRect> faces = detection->detectObjects(image,DETECT_ALL_FACES);
  57. char resultName[110];
  58. sprintf(resultName,"%s/%s",dirName.c_str(),imageName.c_str());
  59. detection->saveResultToFile(image,faces,resultName);
  60. }
  61. fclose(f);
  62. delete detection;
  63. return (EXIT_SUCCESS);
  64. }