Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. package nl.byelex.logodetectie.node.debug.recognizer;
  2.  
  3. import com.google.common.base.Throwables;
  4. import nl.byelex.logodetectie.lib.commondomain.ClusteringAlgorithm;
  5. import nl.byelex.logodetectie.lib.commondomain.LogoType;
  6. import nl.byelex.logodetectie.lib.commondomain.PointInt;
  7. import nl.byelex.logodetectie.lib.keypoints.LogoKeypointsGenerator;
  8. import nl.byelex.logodetectie.lib.keypoints.LogoTrainData;
  9. import nl.byelex.logodetectie.lib.mask.Mask;
  10. import nl.byelex.logodetectie.lib.opencvutils.ImgUtils;
  11. import nl.byelex.logodetectie.lib.xml.xmodel.ImageKeypointData;
  12. import nl.byelex.logodetectie.node.processing.ExtractKeypointsAndFeatures;
  13. import nl.byelex.logodetectie.node.processing.logodetector.ISingleLogoDetector;
  14. import nl.byelex.logodetectie.node.processing.logodetector.LogoDetectorFactory;
  15. import nl.byelex.logodetectie.node.processing.logodetector.LogoMatchCandidate;
  16. import nl.byelex.logodetectie.opencvutils.InitOpenCv;
  17. import org.opencv.core.Mat;
  18. import org.opencv.core.MatOfKeyPoint;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21.  
  22. import javax.annotation.Nonnull;
  23. import javax.imageio.ImageIO;
  24. import java.awt.image.BufferedImage;
  25. import java.io.IOException;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.nio.file.Paths;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import java.util.stream.Collectors;
  32.  
  33. public class KeypointDetectTest {
  34.  
  35. private static final Logger logger = LoggerFactory.getLogger(KeypointDetectTest.class);
  36. public final static String queryDir = "/mnt/drive/temp/main/040_task2777_nn_training_data/frames/";
  37. public final static String trainDir = "/mnt/drive/temp/kpn_logo_train";
  38. private long pointSize = 0;
  39. private long time = 0;
  40. private long timeMatch = 0;
  41. private long logoSize = 0;
  42.  
  43. private ThreadLocal<ExtractKeypointsAndFeatures.ExtractionContext> context = ThreadLocal.withInitial(ExtractKeypointsAndFeatures.ExtractionContext::new);
  44.  
  45. public void run() throws Exception {
  46. List<LogoDetectorFactory> builders = new ArrayList<>();
  47. List<Path> trainFilesList = Files.list(Paths.get(trainDir)).collect(Collectors.toList());
  48. for (int i = 0; i < trainFilesList.size(); i++) {
  49. LogoDetectorFactory factory = new LogoDetectorFactory();
  50. addTrainLogo(i, trainFilesList.get(i), factory, ClusteringAlgorithm.DBSCAN);
  51. builders.add(factory);
  52. }
  53.  
  54. List<Path> queryFilesList = Files.list(Paths.get(queryDir)).collect(Collectors.toList());
  55. for (int i = 0; i < queryFilesList.size(); i++) {
  56. processFrameImpl(queryFilesList.get(i), builders);
  57. }
  58. System.out.print("time: " + time + " keypoints: " + pointSize + " logos: " + logoSize + " files: " + queryFilesList.size() + " match time: " + timeMatch + " ");
  59. System.out.println("mean time: " + (((double) time) / queryFilesList.size()) + " keypoints: " + ((double) pointSize) / queryFilesList.size() + " logos " + ((double) logoSize) / queryFilesList.size() + " " + "mean time match: " + (((double) timeMatch) / queryFilesList.size()));
  60.  
  61. }
  62.  
  63.  
  64. private void addTrainLogo(long id, Path filePath, LogoDetectorFactory factory, @Nonnull ClusteringAlgorithm clusteringAlgorithm) {
  65.  
  66. LogoTrainData keypoints = LogoKeypointsGenerator.generateKeypoints(filePath);
  67.  
  68. Mat features = null;
  69. try {
  70. BufferedImage img = ImageIO.read(filePath.toFile());
  71.  
  72.  
  73. Mask mask = new Mask(new PointInt(0, 0), new PointInt(0, img.getHeight()), new PointInt(img.getWidth(), img.getHeight()), new PointInt(img.getWidth(), img.getHeight()));
  74.  
  75.  
  76. features = keypoints.buildFeaturesMat();
  77. factory.identifiers("oo", id + 1)
  78. .clusteringAlgorithm(clusteringAlgorithm)
  79. .keypoints(keypoints.getKpa(), features)
  80. .mask(mask.getPolygon())
  81. .trainingSizes(keypoints.getImageWidth(), keypoints.getImageHeight())
  82. .logoType(LogoType.IMAGE)
  83. .keepRejected();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. } finally {
  87. if (features != null)
  88. features.release();
  89. }
  90. }
  91.  
  92. public static void main(String[] args) {
  93. InitOpenCv.init();
  94. try {
  95. new KeypointDetectTest().run();
  96. } catch (Exception e) {
  97. logger.warn(Throwables.getRootCause(e).getMessage(), e);
  98. }
  99. }
  100.  
  101. @SuppressWarnings("Duplicates")
  102. private void processFrameImpl(Path queryFile, List<LogoDetectorFactory> builders) throws IOException {
  103. final ExtractKeypointsAndFeatures.ExtractionContext ctx = context.get();
  104. MatOfKeyPoint kpm = null;
  105. Mat features = new Mat();
  106. Mat m = ImgUtils.imread(queryFile);
  107. try {
  108.  
  109.  
  110. long startTime = System.currentTimeMillis();
  111. kpm = new MatOfKeyPoint();
  112. // Pair<MatOfKeyPoint,Mat> pair = ctx.getKeypointsDetector().detectKeypointsAndExtractFeatures(m, null, null);
  113. // kpm = pair.getKey();
  114. // features = pair.getValue();
  115.  
  116. ctx.getKeypointsDetector().detectKeypointsAndExtractFeatures(m, kpm, features);
  117.  
  118.  
  119. time += (System.currentTimeMillis() - startTime);
  120.  
  121. pointSize += kpm.toList().size();
  122.  
  123.  
  124. if (features.type() != 5) {
  125. float[] matrix = matToMatrixB(features);
  126. int rows = features.rows();
  127. features = ImageKeypointData.arrToFeatureMatF(matrix, rows);
  128. }
  129.  
  130.  
  131. startTime = System.currentTimeMillis();
  132. List<LogoMatchCandidate> candidates = new ArrayList<>();
  133. for (LogoDetectorFactory builder : builders) {
  134. ISingleLogoDetector detector = builder.createDetector(kpm.toList(), features, m.width(), m.height());
  135. for (LogoMatchCandidate c : detector.detect()) {
  136. if (c.isRejected()) {
  137. // final LogoVariant logoVariant = brandLogoVariants.getLogoVariant(c.getVariantId());
  138. // frameRejected0.addMatch(new LogoMatch(frameRejected0, logoVariant, MatchStage.RECOGNITION, c.getMaskRegion(), c.getTransformation()));
  139. } else
  140. candidates.add(c);
  141. }
  142. }
  143. timeMatch += (System.currentTimeMillis() - startTime);
  144. logoSize += candidates.size();
  145. //
  146. // new ResolveLogoDuplicates(brandLogoVariants, candidates).resolve();
  147. //
  148. // for (LogoMatchCandidate rr : candidates) {
  149. // final LogoVariant logoVariant = brandLogoVariants.getLogoVariant(rr.getVariantId());
  150. // final Frame frame = rr.isRejected() ? frameRejected1 : frameResult;
  151. // frame.addMatch(new LogoMatch(frame, logoVariant, MatchStage.RECOGNITION, rr.getMaskRegion(), rr.getTransformation()));
  152. // }
  153.  
  154. } finally {
  155. kpm.release();
  156. features.release();
  157. m.release();
  158. }
  159. }
  160.  
  161. private static float[] matToMatrixB(Mat mat) {
  162. int c = mat.cols();
  163. int r = mat.rows();
  164. byte d[] = new byte[c * r];
  165. mat.get(0, 0, d);
  166. int s = 0;
  167. float[] matrix = new float[r * c];
  168. for (int i = 0; i < r; i++) {
  169. for (int j = 0; j < c; j++) {
  170. matrix[s] = d[s];
  171. // System.out.println("f "+matrix[i][j]+ " "+d[s]+arr[2]);
  172. s++;
  173. }
  174. }
  175. return matrix;
  176. }
  177.  
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement