Loading pom.xml +1 −1 Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <exec.mainClass>top.BackendJava</exec.mainClass> <exec.mainClass>object_detection.ObjectDetector</exec.mainClass> </properties> <!-- FOR THE YOLO USAGE: Loading src/main/java/object_detection/ObjectDetector.java +19 −1 Original line number Diff line number Diff line Loading @@ -12,6 +12,11 @@ import java.util.List; public class ObjectDetector { /** * The starting function that creates an object set, compiles informatino, and returns it * TODO: need to add functionality to update Database * @throws FileNotFoundException */ public static void startProcess() throws FileNotFoundException { // for now, we can just set paths to the directories that hold keyframes and featurepoint CSVs Loading @@ -32,21 +37,34 @@ public class ObjectDetector { System.err.println("ERROR: features and bounding box directories have differing number of Keyframes"); } /* ################################################# In the section below, we create a new ObjectSet, and iterate over each Keyframe ################################################## */ System.out.println("====> Starting Object Mapping"); // initialize ObjectSet ObjectSet objSet = new ObjectSet(); // for each keyframe, process bounding box csv and feature csv int NUMKF = feat_CSVs.length; for(int i = 0; i < NUMKF; i++){ // process the frame List<ArrayList<Point>> currFramePoints = objSet.processFrame(feat_CSVs[i], bbox_CSVs[i]); // update the current object set objSet.updateObjectSet(currFramePoints); } // printing final object set for sanity System.out.println(objSet); } /** * A helper function used to pull all files out of a directory path * @param dir_pth * @return */ public static File[] getDirFiles(String dir_pth){ File[] f_arr; // get the csv files of each frame Loading src/main/java/object_detection/types/BoundingBox.java +14 −5 Original line number Diff line number Diff line Loading @@ -12,10 +12,10 @@ public class BoundingBox { } public boolean within(Point2D p){ return p.getX() < this.topRight.getX() && p.getX() > this.topLeft.getX() && p.getY() < this.topLeft.getY() && p.getY() > this.botLeft.getY(); return p.getX() <= this.topRight.getX() && p.getX() >= this.topLeft.getX() && p.getY() >= this.topLeft.getY() && p.getY() <= this.botLeft.getY(); } Loading @@ -26,4 +26,13 @@ public class BoundingBox { Point2D botLeft; int idx; String predClass; @Override public String toString(){ return "BBOX with corners:" + "\n TR: " + topRight + "\n TL: " + topLeft + "\n BR: " + botRight + "\n BL: " + botLeft + "\n"; } } src/main/java/object_detection/types/ObjectSet.java +35 −20 Original line number Diff line number Diff line Loading @@ -7,7 +7,6 @@ import java.util.*; public class ObjectSet { public List<PointSet> objects; int count = 0; public ObjectSet(){ objects = new ArrayList<>(); } Loading @@ -18,16 +17,18 @@ public class ObjectSet { * @return : the index of this object, so we can quickly add and remove objects from list */ public int makeObject(Point...pp){ // blank pointset PointSet ps = new PointSet(); ps.addAll(pp); this.count++; // add all points ps.updateReps(); // add pointset to object list objects.add(count-1, ps); objects.add(ps); // return index of new object return count-1; // return index of new object, which will be at end of ArrayList return objects.size()-1; } /** Loading @@ -37,10 +38,11 @@ public class ObjectSet { * @return true if objects are the same */ public boolean compareObjects(int i, int j){ // O(n), moving points Point[] r1 = this.objects.get(i).getPoints(); Point[] r2 = this.objects.get(j).getPoints(); // compare r1 and r2 // O(n) Set<Point> s = new HashSet<>(); for(Point r : r1){ if(r != null){ Loading @@ -48,6 +50,7 @@ public class ObjectSet { } } // O(n) int count = 0; for(Point p : r2){ if(p!=null && s.contains(p)){ Loading @@ -74,11 +77,6 @@ public class ObjectSet { // remove j this.objects.remove(j); // lower the count this.count--; // eventually, push i to database } /** Loading @@ -101,15 +99,11 @@ public class ObjectSet { results.add(new ArrayList<>()); } int lineNum = 0; // Process feature points Scanner scanner = new Scanner(featCsv); while (scanner.hasNextLine()) { // Print the line of the file being processed //System.out.println("Processing line " + lineNum++); // get the Point and its corresponding 2D projection from the current KeyFrame String[] line = scanner.nextLine().split(","); Point2D point2D = new Point2D(Float.parseFloat(line[0]), Float.parseFloat(line[1]), Integer.parseInt(line[2])); Point point = new Point(Float.parseFloat(line[3]), Float.parseFloat(line[4]), Float.parseFloat(line[5]), Integer.parseInt(line[2])); Loading @@ -118,7 +112,7 @@ public class ObjectSet { for (int i = 0; i < boundingBoxes.size(); i++) { if (boundingBoxes.get(i).within(point2D)) { results.get(i).add(point); break; // Assuming a point belongs to only one bounding box break; // Assuming a point belongs to only one bounding box, first come first serve } } } Loading @@ -135,12 +129,18 @@ public class ObjectSet { * */ public void updateObjectSet(List<ArrayList<Point>> points) { // for each BoundingBox we have all points that fall within it for(ArrayList<Point> point : points) { Point[] pointArray = point.toArray(new Point[0]); int objIdx = makeObject(pointArray); // Compare the new object with all existing objects for (int j = 0; j < objects.size(); j++) { for (int j = 0; j < this.objects.size(); j++) { // don't compare with yourself if(objIdx == j){ continue; } if (compareObjects(objIdx, j)) { combineObjects(j, objIdx); break; Loading @@ -149,17 +149,28 @@ public class ObjectSet { } } /** * Takes a file of bounding boxes; just another utility function * @param bboxCsv : File object holding info about a single csv of bounding boxes from one frame * @return : list of BoundingBox objects from that KeyFrame * @throws FileNotFoundException */ private List<BoundingBox> readBoundingBoxes(File bboxCsv) throws FileNotFoundException { List<BoundingBox> boxes = new ArrayList<>(); Scanner scanner = new Scanner(bboxCsv); scanner.nextLine(); // Skip header int i = 0; while (scanner.hasNextLine()) { String[] line = scanner.nextLine().split(","); // get all coordinate points Point2D tr = new Point2D(Float.parseFloat(line[2]) + Float.parseFloat(line[4]), Float.parseFloat(line[3]), 0); Point2D tl = new Point2D(Float.parseFloat(line[2]), Float.parseFloat(line[3]), 0); Point2D br = new Point2D(Float.parseFloat(line[2]) + Float.parseFloat(line[4]), Float.parseFloat(line[3]) + Float.parseFloat(line[5]), 0); Point2D bl = new Point2D(Float.parseFloat(line[2]), Float.parseFloat(line[3]) + Float.parseFloat(line[5]), 0); Point2D br = new Point2D((Float.parseFloat(line[2]) + Float.parseFloat(line[4])), Float.parseFloat(line[3]) + Float.parseFloat(line[5]), 0); Point2D bl = new Point2D(Float.parseFloat(line[2]), (Float.parseFloat(line[3]) + Float.parseFloat(line[5])), 0); // create the box accordingly boxes.add(new BoundingBox(tr, tl, br, bl, i, line[0])); i++; } Loading @@ -167,6 +178,10 @@ public class ObjectSet { return boxes; } /** * Just used for debugging purposes * @return res : String representation of the object set */ @Override public String toString(){ StringBuilder res = new StringBuilder("ObjectSet:"); Loading src/main/java/object_detection/types/Point2D.java +5 −0 Original line number Diff line number Diff line Loading @@ -23,4 +23,9 @@ public class Point2D { public int getIdx() { return idx; } @Override public String toString(){ return "{"+ x + ", " + y + "}"; } } Loading
pom.xml +1 −1 Original line number Diff line number Diff line Loading @@ -12,7 +12,7 @@ <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <exec.mainClass>top.BackendJava</exec.mainClass> <exec.mainClass>object_detection.ObjectDetector</exec.mainClass> </properties> <!-- FOR THE YOLO USAGE: Loading
src/main/java/object_detection/ObjectDetector.java +19 −1 Original line number Diff line number Diff line Loading @@ -12,6 +12,11 @@ import java.util.List; public class ObjectDetector { /** * The starting function that creates an object set, compiles informatino, and returns it * TODO: need to add functionality to update Database * @throws FileNotFoundException */ public static void startProcess() throws FileNotFoundException { // for now, we can just set paths to the directories that hold keyframes and featurepoint CSVs Loading @@ -32,21 +37,34 @@ public class ObjectDetector { System.err.println("ERROR: features and bounding box directories have differing number of Keyframes"); } /* ################################################# In the section below, we create a new ObjectSet, and iterate over each Keyframe ################################################## */ System.out.println("====> Starting Object Mapping"); // initialize ObjectSet ObjectSet objSet = new ObjectSet(); // for each keyframe, process bounding box csv and feature csv int NUMKF = feat_CSVs.length; for(int i = 0; i < NUMKF; i++){ // process the frame List<ArrayList<Point>> currFramePoints = objSet.processFrame(feat_CSVs[i], bbox_CSVs[i]); // update the current object set objSet.updateObjectSet(currFramePoints); } // printing final object set for sanity System.out.println(objSet); } /** * A helper function used to pull all files out of a directory path * @param dir_pth * @return */ public static File[] getDirFiles(String dir_pth){ File[] f_arr; // get the csv files of each frame Loading
src/main/java/object_detection/types/BoundingBox.java +14 −5 Original line number Diff line number Diff line Loading @@ -12,10 +12,10 @@ public class BoundingBox { } public boolean within(Point2D p){ return p.getX() < this.topRight.getX() && p.getX() > this.topLeft.getX() && p.getY() < this.topLeft.getY() && p.getY() > this.botLeft.getY(); return p.getX() <= this.topRight.getX() && p.getX() >= this.topLeft.getX() && p.getY() >= this.topLeft.getY() && p.getY() <= this.botLeft.getY(); } Loading @@ -26,4 +26,13 @@ public class BoundingBox { Point2D botLeft; int idx; String predClass; @Override public String toString(){ return "BBOX with corners:" + "\n TR: " + topRight + "\n TL: " + topLeft + "\n BR: " + botRight + "\n BL: " + botLeft + "\n"; } }
src/main/java/object_detection/types/ObjectSet.java +35 −20 Original line number Diff line number Diff line Loading @@ -7,7 +7,6 @@ import java.util.*; public class ObjectSet { public List<PointSet> objects; int count = 0; public ObjectSet(){ objects = new ArrayList<>(); } Loading @@ -18,16 +17,18 @@ public class ObjectSet { * @return : the index of this object, so we can quickly add and remove objects from list */ public int makeObject(Point...pp){ // blank pointset PointSet ps = new PointSet(); ps.addAll(pp); this.count++; // add all points ps.updateReps(); // add pointset to object list objects.add(count-1, ps); objects.add(ps); // return index of new object return count-1; // return index of new object, which will be at end of ArrayList return objects.size()-1; } /** Loading @@ -37,10 +38,11 @@ public class ObjectSet { * @return true if objects are the same */ public boolean compareObjects(int i, int j){ // O(n), moving points Point[] r1 = this.objects.get(i).getPoints(); Point[] r2 = this.objects.get(j).getPoints(); // compare r1 and r2 // O(n) Set<Point> s = new HashSet<>(); for(Point r : r1){ if(r != null){ Loading @@ -48,6 +50,7 @@ public class ObjectSet { } } // O(n) int count = 0; for(Point p : r2){ if(p!=null && s.contains(p)){ Loading @@ -74,11 +77,6 @@ public class ObjectSet { // remove j this.objects.remove(j); // lower the count this.count--; // eventually, push i to database } /** Loading @@ -101,15 +99,11 @@ public class ObjectSet { results.add(new ArrayList<>()); } int lineNum = 0; // Process feature points Scanner scanner = new Scanner(featCsv); while (scanner.hasNextLine()) { // Print the line of the file being processed //System.out.println("Processing line " + lineNum++); // get the Point and its corresponding 2D projection from the current KeyFrame String[] line = scanner.nextLine().split(","); Point2D point2D = new Point2D(Float.parseFloat(line[0]), Float.parseFloat(line[1]), Integer.parseInt(line[2])); Point point = new Point(Float.parseFloat(line[3]), Float.parseFloat(line[4]), Float.parseFloat(line[5]), Integer.parseInt(line[2])); Loading @@ -118,7 +112,7 @@ public class ObjectSet { for (int i = 0; i < boundingBoxes.size(); i++) { if (boundingBoxes.get(i).within(point2D)) { results.get(i).add(point); break; // Assuming a point belongs to only one bounding box break; // Assuming a point belongs to only one bounding box, first come first serve } } } Loading @@ -135,12 +129,18 @@ public class ObjectSet { * */ public void updateObjectSet(List<ArrayList<Point>> points) { // for each BoundingBox we have all points that fall within it for(ArrayList<Point> point : points) { Point[] pointArray = point.toArray(new Point[0]); int objIdx = makeObject(pointArray); // Compare the new object with all existing objects for (int j = 0; j < objects.size(); j++) { for (int j = 0; j < this.objects.size(); j++) { // don't compare with yourself if(objIdx == j){ continue; } if (compareObjects(objIdx, j)) { combineObjects(j, objIdx); break; Loading @@ -149,17 +149,28 @@ public class ObjectSet { } } /** * Takes a file of bounding boxes; just another utility function * @param bboxCsv : File object holding info about a single csv of bounding boxes from one frame * @return : list of BoundingBox objects from that KeyFrame * @throws FileNotFoundException */ private List<BoundingBox> readBoundingBoxes(File bboxCsv) throws FileNotFoundException { List<BoundingBox> boxes = new ArrayList<>(); Scanner scanner = new Scanner(bboxCsv); scanner.nextLine(); // Skip header int i = 0; while (scanner.hasNextLine()) { String[] line = scanner.nextLine().split(","); // get all coordinate points Point2D tr = new Point2D(Float.parseFloat(line[2]) + Float.parseFloat(line[4]), Float.parseFloat(line[3]), 0); Point2D tl = new Point2D(Float.parseFloat(line[2]), Float.parseFloat(line[3]), 0); Point2D br = new Point2D(Float.parseFloat(line[2]) + Float.parseFloat(line[4]), Float.parseFloat(line[3]) + Float.parseFloat(line[5]), 0); Point2D bl = new Point2D(Float.parseFloat(line[2]), Float.parseFloat(line[3]) + Float.parseFloat(line[5]), 0); Point2D br = new Point2D((Float.parseFloat(line[2]) + Float.parseFloat(line[4])), Float.parseFloat(line[3]) + Float.parseFloat(line[5]), 0); Point2D bl = new Point2D(Float.parseFloat(line[2]), (Float.parseFloat(line[3]) + Float.parseFloat(line[5])), 0); // create the box accordingly boxes.add(new BoundingBox(tr, tl, br, bl, i, line[0])); i++; } Loading @@ -167,6 +178,10 @@ public class ObjectSet { return boxes; } /** * Just used for debugging purposes * @return res : String representation of the object set */ @Override public String toString(){ StringBuilder res = new StringBuilder("ObjectSet:"); Loading
src/main/java/object_detection/types/Point2D.java +5 −0 Original line number Diff line number Diff line Loading @@ -23,4 +23,9 @@ public class Point2D { public int getIdx() { return idx; } @Override public String toString(){ return "{"+ x + ", " + y + "}"; } }