Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode.Network;
  2.  
  3. import com.disnodeteam.dogecv.OpenCVPipeline;
  4.  
  5. import org.firstinspires.ftc.robotcore.external.Telemetry;
  6. import org.opencv.core.CvType;
  7. import org.opencv.core.Mat;
  8. import org.opencv.core.Size;
  9. import org.opencv.imgproc.Imgproc;
  10. import org.tensorflow.lite.Interpreter;
  11.  
  12. import java.io.File;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15.  
  16.  
  17. public class VisionPipeline extends OpenCVPipeline {
  18. public Mat workingMat = new Mat();
  19. public boolean imagedUpdated = false;
  20. public Model model;
  21. @Override
  22. public Mat processFrame(Mat rgba, Mat gray) {
  23. rgba.copyTo(workingMat);
  24. Imgproc.cvtColor(workingMat,workingMat,Imgproc.COLOR_RGBA2RGB);
  25. imagedUpdated = true;
  26. return rgba;
  27. }
  28. public void initModel(String modelName, Action[] actions){
  29. model = new Model(modelName,actions);
  30. }
  31. public Action predictAction(){
  32. Mat copy = new Mat();
  33. workingMat.copyTo(copy);
  34. return model.predict(copy);
  35. }
  36. public enum Action{
  37. Left,Right,Forwards,Backwards,CW,CCW
  38. }
  39.  
  40. public class Model {
  41.  
  42. public Action[] actions;
  43.  
  44. public Interpreter model;
  45.  
  46. //add model stuff
  47. public Model(String modelName, Action[] actions){
  48.  
  49.  
  50. this.actions = actions;
  51.  
  52.  
  53. String baseDirectory = "/sdcard/FIRST/CS/";
  54. String path = baseDirectory + modelName + ".tflite";
  55. File f = new File(path);
  56. model = new Interpreter(f);
  57.  
  58. }
  59. /*
  60. where are the file on the phone? and how to upload. Credit to team 9773 for uploading examples.
  61. storage / emulated / FIRST / team9773 / json18
  62. 1) open terminal tab on android studio
  63. 2) get to the right dir on the computer, for example
  64. cd TeamCode/src/main/java/org/firstinspires/ftc/teamcode/json/
  65. 3) push a file to the phone:
  66. /sdcard/FIRST/CS/
  67. adb push cnnrandomL.tflite /sdcard/FIRST/CS/
  68. adb push myfile.json /sdcard/FIRST/team9773/json18/
  69. location of adb on mac: $HOME/Library/Android/sdk/platform-tools
  70. where you can get the $HOME value by typing "echo $HOME" in a terminal
  71. export PATH=$PATH:$HOME/Library/Android/sdk/platform-tools
  72. 4) get a file from the phone
  73. adb pull /sdcard/FIRST/team9773/json18/myfile.json
  74. */
  75. public Action predict(Mat mat){
  76. mat.convertTo(mat,CvType.CV_64F);
  77. int numActions = actions.length;
  78. double[] values = new double[numActions];
  79. for (int i = 0; i < numActions; i++){
  80. double[] actionInput = new double[numActions];
  81. actionInput[i] = 1;
  82. int width = mat.width();
  83. int height = mat.height();
  84. double[][][] imageArray = new double[height][width][3];
  85. for (int k = 0; k< height; k++){
  86. for (int j = 0; j < width; j++){
  87. double[] element = mat.get(k,j);
  88. for (int l = 0; l < element.length; l++) imageArray[k][j][l] = element[l] / 255;
  89. }
  90. }
  91. Object[] inputs = {imageArray,actionInput};
  92. double[] output = new double[1];
  93. Map<Integer,Object> outputs = new HashMap();
  94. outputs.put(0,output);
  95. model.runForMultipleInputsOutputs(inputs,outputs);
  96. values[i] = ((double) outputs.get(0));
  97. }
  98. int action = -1;
  99. double value = 0;
  100. for (int i = 0; i < numActions; i++){
  101. if(action == -1 || values[i] > value){
  102. action = i;
  103. value = values[i];
  104. }
  105. }
  106. Action actionName = actions[action];
  107. return actionName;
  108. }
  109. //k-means clustering will go here
  110. public Mat processFrame(Mat frame){
  111. return frame;
  112. }
  113.  
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement