/src/conv-net-0.1/jni_fexample/ConvNet.cpp

https://github.com/eldog/fface · C++ · 63 lines · 48 code · 13 blank · 2 comment · 3 complexity · 4194983ec0faed53ff3e716252cda5d7 MD5 · raw file

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <opencv/highgui.h>
  5. #include "cvconvnet.h"
  6. #include "ConvNet.h"
  7. using namespace std;
  8. JNIEXPORT jdouble JNICALL
  9. Java_ConvNet_runConv(JNIEnv *env,
  10. jobject obj,
  11. jstring xmlFilePath,
  12. jstring imageFilePath)
  13. {
  14. // Parse the file path strings
  15. const char *xmlFile;
  16. xmlFile = env->GetStringUTFChars(xmlFilePath, 0);
  17. const char *imageFile;
  18. imageFile = env->GetStringUTFChars(imageFilePath, 0);
  19. CvConvNet net;
  20. CvSize input_size = cvSize(128, 128);
  21. // Load the xml file
  22. ifstream ifs(xmlFile);
  23. string xml((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>());
  24. if (!net.fromString(xml))
  25. {
  26. return -1000.0;
  27. } // if
  28. cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);
  29. cvMoveWindow("Image", input_size.height, input_size.width);
  30. CvFont font;
  31. cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0);
  32. IplImage *img;
  33. if ((img = cvLoadImage(imageFile, CV_LOAD_IMAGE_GRAYSCALE)) == 0)
  34. {
  35. return -1000.0;
  36. } // if
  37. jdouble value;
  38. value = (jdouble) net.fprop(img);
  39. ostringstream displayValue;
  40. displayValue << (double) value;
  41. cvPutText(img,
  42. displayValue.str().c_str(),
  43. cvPoint(0, input_size.height / 2),
  44. &font,
  45. CV_RGB(0, 255, 0));
  46. cvShowImage("Image", img);
  47. cvWaitKey(1000);
  48. cvReleaseImage(&img);
  49. return value;
  50. } // Java_ConvNet_runConv