Commit 098873e4 authored by Rohan  Kumar's avatar Rohan Kumar
Browse files

new

parent 78a6f36e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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>object_detection.ObjectDetector</exec.mainClass>
        <exec.mainClass>top.BackendJava</exec.mainClass>
    </properties>

    <build>
+61 −2
Original line number Diff line number Diff line
package database;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerApi;
import com.mongodb.ServerApiVersion;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOptions;
@@ -20,7 +24,18 @@ public class MongoDBInteraction {

    public MongoDBInteraction() {
        String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true";
        this.mongoClient = MongoClients.create(uri);

        // Construct a ServerApi instance using the ServerApi.builder() method
        ServerApi serverApi = ServerApi.builder()
                .version(ServerApiVersion.V1)
                .build();

        MongoClientSettings settings = MongoClientSettings.builder()
                .applyConnectionString(new ConnectionString(uri))
                .serverApi(serverApi)
                .build();

        this.mongoClient = MongoClients.create(settings);
        this.database = mongoClient.getDatabase("Objects");
        this.objectCollection = database.getCollection("objectSets");
    }
@@ -57,8 +72,10 @@ public class MongoDBInteraction {
    }

    private PointSet convertDocumentToPointSet(Document doc) {
        int idx = Integer.parseInt(doc.getString("setId"));
        List<Document> pointsDocs = doc.getList("points", Document.class);
        PointSet pointSet = new PointSet();

        PointSet pointSet = new PointSet(idx);
        for (Document pointDoc : pointsDocs) {
            float x = pointDoc.getDouble("x").floatValue();
            float y = pointDoc.getDouble("y").floatValue();
@@ -67,4 +84,46 @@ public class MongoDBInteraction {
        }
        return pointSet;
    }

    // PointSet management methods
    private Document pointSetToDocument(String setId, PointSet pointSet) {
        List<Document> pointsList = new ArrayList<>();
        for (Point p : pointSet.getPoints()) {
            pointsList.add(new Document("x", p.getX())
                    .append("y", p.getY())
                    .append("z", p.getZ()));
        }
        return new Document("setId", setId)
                .append("points", pointsList);
    }


    // ObjectSet management methods
    private Document objectSetToDocument(int index, ObjectSet objectSet) {
        List<Document> pointSetsList = new ArrayList<>();

        for (object_detection.types.PointSet ps : objectSet.objects) {
            Document pointSetDoc = pointSetToDocument(Integer.toString(ps.getIDX()), ps);
            pointSetsList.add(pointSetDoc);
        }
        return new Document("index", index)
                .append("objectSets", pointSetsList);
    }


    public void updateObjectSet(int index, ObjectSet objectSet) {
        Document doc = objectSetToDocument(index, objectSet);
        ReplaceOptions options = new ReplaceOptions().upsert(true);
        objectCollection.replaceOne(Filters.eq("index", index), doc, options);
    }

    public ObjectSet retrieveObjectSet(int index) {
        Document query = new Document("index", index);
        Document doc = objectCollection.find(query).first();
        if (doc != null) {
            return convertDocumentToObjectSet(doc);
        }
        return null;
    }

}
+4 −0
Original line number Diff line number Diff line
package object_detection;

import database.MongoDBInteraction;
import object_detection.types.ObjectSet;
import object_detection.types.Point;

@@ -59,6 +60,9 @@ public class ObjectDetector {
        // printing final object set for sanity
        System.out.println(objSet);
        System.out.println("====> Finished Object Mapping");

        MongoDBInteraction mdbi = new MongoDBInteraction();
        mdbi.updateObjectSet(5, objSet);
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ public class ObjectSet {
     */
    public int makeObject(Point...pp){
        // blank pointset
        PointSet ps = new PointSet();
        PointSet ps = new PointSet(objects.size());
        ps.addAll(pp);

        // add all points
@@ -103,7 +103,7 @@ public class ObjectSet {
            // 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]));
            Point point = new Point(Float.parseFloat(line[3]), Float.parseFloat(line[4]), Float.parseFloat(line[5]), 0);

            // Check if the point is within any bounding box
            for (int i = 0; i < boundingBoxes.size(); i++) {
+7 −1
Original line number Diff line number Diff line
@@ -10,16 +10,19 @@ public class PointSet {
    Set<Point> pset;
    Point[] reps;

    final int IDX;

    /**
     *
     * @param pp : the points that this PointSet will contain
     */
    public PointSet(Point ...pp){
    public PointSet(int id, Point ...pp){
        pset = new HashSet<>();

        // add every point blankly to pointset
        pset.addAll(Arrays.asList(pp));
        reps = new Point[NUM_REPS];
        IDX = id;
    }

    public void addPoint(Point p){
@@ -70,4 +73,7 @@ public class PointSet {
        }
    }

    public int getIDX() {
        return this.IDX;
    }
}
Loading