Commit 013a4b93 authored by Rohan  Kumar's avatar Rohan Kumar
Browse files

Merge branch 'BoundingBoxes' into 'master'

Bounding boxes

See merge request ec504/ec504_projects/group8!11
parents 070c6533 9247e686
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -5,11 +5,8 @@ import object_detection.types.Point;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.List;

public class ObjectDetector {

@@ -37,8 +34,9 @@ public class ObjectDetector {
        int NUMKF = feat_CSVs.length;

        for(int i = 0; i < NUMKF; i++){
            // single KeyFrame
            //ArrayList<ArrayList<Point>> currFrame = objSet.processFrame(feat_CSVs[i], bbox_CSVs[i]);
            // process the frame
            List<ArrayList<Point>> currFramePoints = objSet.processFrame(feat_CSVs[i], bbox_CSVs[i]);
            objSet.updateObjectSet(currFramePoints);
        }
    }

+3 −3
Original line number Diff line number Diff line
@@ -2,11 +2,12 @@ package object_detection.types;

public class BoundingBox {

    public BoundingBox(Point2D tr, Point2D tl, Point2D br, Point2D bl){
    public BoundingBox(Point2D tr, Point2D tl, Point2D br, Point2D bl, int idx){
        this.topRight = tr;
        this.topLeft = tl;
        this.botRight = br;
        this.botLeft = bl;
        this.idx = idx;
    }

    public boolean within(Point2D p){
@@ -22,6 +23,5 @@ public class BoundingBox {
    Point2D topLeft;
    Point2D botRight;
    Point2D botLeft;


    int idx;
}
+75 −12
Original line number Diff line number Diff line
@@ -37,8 +37,8 @@ public class ObjectSet {
     * @return true if objects are the same
     */
    public static boolean compareObjects(int i, int j){
        Point[] r1 = objects.get(i).getSetReps();
        Point[] r2 = objects.get(j).getSetReps();
        Point[] r1 = objects.get(i).getPoints();
        Point[] r2 = objects.get(j).getPoints();

        // compare r1 and r2
        Set<Point> s = new HashSet<>();
@@ -75,8 +75,10 @@ public class ObjectSet {
        // remove j
        objects.remove(j);

        // eventually, push i to database
        // lower the count
        count--;

        // eventually, push i to database
    }

    /**
@@ -85,20 +87,81 @@ public class ObjectSet {
     * @param featCsv : file containing each feature in space (x,y,z and index)
     * @param bboxCsv : file containing Bounding Boxes for each object in a keyframe
     */
    public void processFrame(File featCsv, File bboxCsv) throws FileNotFoundException {
    
        // first we need to extract the features and blackbox from the corresponding CSVs
        ArrayList<Point> feature;
        ArrayList<BoundingBox> bbox;
    public List<ArrayList<Point>> processFrame(File featCsv, File bboxCsv) throws FileNotFoundException {
        // Print the name of the file being processed
        System.out.println("Processing " + featCsv.getName() + " and " + bboxCsv.getName());

        Scanner sc = new Scanner(featCsv);
        sc.useDelimiter("\n");
        while(sc.hasNext()){
            String[] cl = sc.next().split(",");
        // Read bounding boxes
        List<BoundingBox> boundingBoxes = readBoundingBoxes(bboxCsv);

            //Point p = new Point(cl[4], cl[5], cl[6]);
        // Initialize results structure
        List<ArrayList<Point>> results = new ArrayList<>();
        for (int i = 0; i < boundingBoxes.size(); i++) {
            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++);

            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]));

            // Check if the point is within any bounding box
            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
                }
            }
        }
        scanner.close();

        return results;
    }

    /**
     * This method updates the existing ObjectSet to incorporate new information from the features and bounding boxes of a new keyframe.
     * It also updates the ObjectSet to merge objects that are similar.
     * 
     * @param points : the features of the new keyframe of the form List<ArrayList<Point>>
     * 
     */
    public void updateObjectSet(List<ArrayList<Point>> points) {
        for (int i = 0; i < points.size(); i++) {
            Point[] pointArray = points.get(i).toArray(new Point[0]);
            int objIdx = makeObject(pointArray);

            // Compare the new object with all existing objects
            for (int j = 0; j < objects.size(); j++) {
                if (compareObjects(objIdx, j)) {
                    combineObjects(j, objIdx);
                    break;
                }
            }
        }
    }

    private List<BoundingBox> readBoundingBoxes(File bboxCsv) throws FileNotFoundException {
        List<BoundingBox> boxes = new ArrayList<>();
        Scanner scanner = new Scanner(bboxCsv);
        scanner.nextLine(); // Skip header
        while (scanner.hasNextLine()) {
            String[] line = scanner.nextLine().split(",");
            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);
            boxes.add(new BoundingBox(tr, tl, br, bl, Integer.parseInt(line[0])));
        }
        scanner.close();
        return boxes;
    }
}
 No newline at end of file
+7 −3
Original line number Diff line number Diff line
@@ -9,18 +9,18 @@ public class Point {
    private float x;
    private float y;
    private float z;
    final int idx;
    private int idx;

    // METHODS

    /*
    Object Constructor of a feature in 3D
     */
    public Point(float xx, float yy, float zz){
    public Point(float xx, float yy, float zz, int idx){
        this.x = xx;
        this.y = yy;
        this.z = zz;
        this.idx = count++;
        this.idx = idx;
    }

    /**
@@ -64,4 +64,8 @@ public class Point {
    public float getZ() {
        return z;
    }

    public int getIdx() {
        return idx;
    }
}
+7 −1
Original line number Diff line number Diff line
@@ -4,10 +4,12 @@ public class Point2D {

    private float x;
    private float y;
    private int idx;

    public Point2D(float x, float y){
    public Point2D(float x, float y, int idx){
        this.x = x;
        this.y = y;
        this.idx = idx;
    }

    public float getY() {
@@ -17,4 +19,8 @@ public class Point2D {
    public float getX() {
        return x;
    }

    public int getIdx() {
        return idx;
    }
}
Loading