Loading src/main/java/database/MongoDBInteraction.java +28 −108 Original line number Diff line number Diff line package database; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import com.mongodb.client.*; import com.mongodb.client.model.Filters; import com.mongodb.client.model.ReplaceOptions; import object_detection.types.ObjectSet; Loading @@ -17,134 +14,57 @@ import java.util.Map; public class MongoDBInteraction { private static final String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"; private MongoClient mongoClient; private MongoDatabase database; private MongoCollection<Document> pointCollection; private MongoCollection<Document> objectCollection; private Map<String, PointSet> cache; // In-memory cache for PointSets public MongoDBInteraction() { String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true"; this.mongoClient = MongoClients.create(uri); this.database = mongoClient.getDatabase("Objects"); this.pointCollection = database.getCollection("pointSets"); this.objectCollection = database.getCollection("objectSets"); this.cache = new HashMap<>(); } // 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())); public Map<String, ObjectSet> retrieveAllObjectSets() { Map<String, ObjectSet> allObjectSets = new HashMap<>(); MongoCursor<Document> cursor = objectCollection.find().iterator(); try { while (cursor.hasNext()) { Document doc = cursor.next(); Integer index = doc.getInteger("index"); if (index != null) { allObjectSets.put(index.toString(), convertDocumentToObjectSet(doc)); } return new Document("setId", setId) .append("points", pointsList); } public void update(String setId, PointSet pointSet) { Document doc = pointSetToDocument(setId, pointSet); ReplaceOptions options = new ReplaceOptions().upsert(true); pointCollection.replaceOne(Filters.eq("setId", setId), doc, options); cache.put(setId, pointSet); } finally { cursor.close(); } public PointSet retrieve(String setId) { PointSet cachedPointSet = cache.get(setId); if (cachedPointSet != null) { return cachedPointSet; return allObjectSets; } Document query = new Document("setId", setId); Document doc = pointCollection.find(query).first(); if (doc != null) { PointSet pointSet = convertDocumentToPointSet(doc); cache.put(setId, pointSet); return pointSet; } return null; public ObjectSet retrieveLatestObjectSet() { Document doc = objectCollection.find().sort(new Document("index", -1)).first(); return doc != null ? convertDocumentToObjectSet(doc) : null; } public void delete(String setId) { pointCollection.deleteOne(Filters.eq("setId", setId)); cache.remove(setId); private ObjectSet convertDocumentToObjectSet(Document doc) { List<Document> pointSetsDocs = doc.getList("objectSets", Document.class); ObjectSet objectSet = new ObjectSet(); for (Document pointSetDoc : pointSetsDocs) { objectSet.objects.add(convertDocumentToPointSet(pointSetDoc)); } return objectSet; } private PointSet convertDocumentToPointSet(Document doc) { List<Document> pointsList = (List<Document>) doc.get("points"); List<Document> pointsDocs = doc.getList("points", Document.class); PointSet pointSet = new PointSet(); for (Document pointDoc : pointsList) { for (Document pointDoc : pointsDocs) { float x = pointDoc.getDouble("x").floatValue(); float y = pointDoc.getDouble("y").floatValue(); float z = pointDoc.getDouble("z").floatValue(); int additionalData = pointDoc.getInteger("additionalData", 0); // Assuming 'additionalData' is a field in your document pointSet.addPoint(new Point(x, y, z, additionalData)); pointSet.addPoint(new Point(x, y, z)); } return pointSet; } // ObjectSet management methods private Document objectSetToDocument(int index, ObjectSet objectSet) { List<Document> pointSetsList = new ArrayList<>(); int setId = 1; for (object_detection.types.PointSet ps : objectSet.objects) { Document pointSetDoc = pointSetToDocument(String.valueOf(setId++), 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; } public void deleteObjectSet(int index) { objectCollection.deleteOne(Filters.eq("index", index)); } public ObjectSet retrieveLatestObjectSet() { // Assuming 'index' is an integer that increments with each new ObjectSet Document highestIndexDoc = objectCollection.find().sort(new Document("index", -1)).first(); if (highestIndexDoc != null) { return convertDocumentToObjectSet(highestIndexDoc); } return null; } private ObjectSet convertDocumentToObjectSet(Document doc) { List<Document> pointSetsDocs = (List<Document>) doc.get("objectSets"); ObjectSet objectSet = new ObjectSet(); for (Document pointSetDoc : pointSetsDocs) { PointSet pointSet = convertDocumentToPointSet(pointSetDoc); objectSet.objects.add(pointSet); } return objectSet; } } //public static void main(String[] args) { // MongoDBInteraction dbInteraction = new MongoDBInteraction(); // PointSet pointSet = new PointSet(new Point(1, 2, 3), new Point(4, 5, 6), new Point(7, 3, 1)); // dbInteraction.update("set13", pointSet); // PointSet retrieved = dbInteraction.retrieve("set13"); // System.out.println("Retrieved Point Set: " + retrieved.getPoints()); // dbInteraction.delete("set13"); // } //} src/main/java/object_detection/types/ObjectSet.java +1 −1 Original line number Diff line number Diff line Loading @@ -112,7 +112,7 @@ public class ObjectSet { 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])); Point point = new Point(Float.parseFloat(line[3]), Float.parseFloat(line[4]), Float.parseFloat(line[5])); // Check if the point is within any bounding box for (int i = 0; i < boundingBoxes.size(); i++) { Loading src/main/java/object_detection/types/Point.java +1 −1 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ public class Point { /* Object Constructor of a feature in 3D */ public Point(float xx, float yy, float zz, int i){ public Point(float xx, float yy, float zz){ this.x = xx; this.y = yy; this.z = zz; Loading src/main/java/top/BackendJava.java +18 −2 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder; import database.MongoDBInteraction; import object_detection.ObjectDetector; import object_detection.types.ObjectSet; import object_detection.types.Point; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; Loading @@ -18,6 +19,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @SpringBootApplication Loading @@ -28,7 +30,7 @@ public class BackendJava { } @RestController @RequestMapping("/objectset") @RequestMapping("/api") public static class ObjectSetController { private final MongoDBInteraction dbInteraction = new MongoDBInteraction(); Loading @@ -37,7 +39,7 @@ public class BackendJava { @GetMapping("/{index}") public ResponseEntity<String> getObjectSet(@PathVariable int index) { try { ObjectSet objectSet = dbInteraction.retrieveObjectSet(index); ObjectSet objectSet = dbInteraction.retrieveLatestObjectSet(); if (objectSet != null) { return ResponseEntity.ok(gson.toJson(objectSet)); } else { Loading @@ -49,6 +51,20 @@ public class BackendJava { return ResponseEntity.internalServerError().body("{\"error\":\"Internal Server Error: " + e.getMessage() + "\"}"); } } @GetMapping("/getImageAndObjects") public ResponseEntity<String> getImageAndObjects() { try { Map<String, ObjectSet> allObjectSets = dbInteraction.retrieveAllObjectSets(); if (!allObjectSets.isEmpty()) { return ResponseEntity.ok(gson.toJson(allObjectSets)); } else { return ResponseEntity.notFound().build(); } } catch (Exception e) { return ResponseEntity.internalServerError().body("{\"error\":\"Failed to retrieve all objects: " + e.getMessage() + "\"}"); } } } @Controller Loading Loading
src/main/java/database/MongoDBInteraction.java +28 −108 Original line number Diff line number Diff line package database; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoCollection; import com.mongodb.client.*; import com.mongodb.client.model.Filters; import com.mongodb.client.model.ReplaceOptions; import object_detection.types.ObjectSet; Loading @@ -17,134 +14,57 @@ import java.util.Map; public class MongoDBInteraction { private static final String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"; private MongoClient mongoClient; private MongoDatabase database; private MongoCollection<Document> pointCollection; private MongoCollection<Document> objectCollection; private Map<String, PointSet> cache; // In-memory cache for PointSets public MongoDBInteraction() { String uri = "mongodb+srv://Cluster03790:dlVzT2Z2bEh9@cluster03790.tk4cwyy.mongodb.net/myFirstDatabase?retryWrites=true"; this.mongoClient = MongoClients.create(uri); this.database = mongoClient.getDatabase("Objects"); this.pointCollection = database.getCollection("pointSets"); this.objectCollection = database.getCollection("objectSets"); this.cache = new HashMap<>(); } // 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())); public Map<String, ObjectSet> retrieveAllObjectSets() { Map<String, ObjectSet> allObjectSets = new HashMap<>(); MongoCursor<Document> cursor = objectCollection.find().iterator(); try { while (cursor.hasNext()) { Document doc = cursor.next(); Integer index = doc.getInteger("index"); if (index != null) { allObjectSets.put(index.toString(), convertDocumentToObjectSet(doc)); } return new Document("setId", setId) .append("points", pointsList); } public void update(String setId, PointSet pointSet) { Document doc = pointSetToDocument(setId, pointSet); ReplaceOptions options = new ReplaceOptions().upsert(true); pointCollection.replaceOne(Filters.eq("setId", setId), doc, options); cache.put(setId, pointSet); } finally { cursor.close(); } public PointSet retrieve(String setId) { PointSet cachedPointSet = cache.get(setId); if (cachedPointSet != null) { return cachedPointSet; return allObjectSets; } Document query = new Document("setId", setId); Document doc = pointCollection.find(query).first(); if (doc != null) { PointSet pointSet = convertDocumentToPointSet(doc); cache.put(setId, pointSet); return pointSet; } return null; public ObjectSet retrieveLatestObjectSet() { Document doc = objectCollection.find().sort(new Document("index", -1)).first(); return doc != null ? convertDocumentToObjectSet(doc) : null; } public void delete(String setId) { pointCollection.deleteOne(Filters.eq("setId", setId)); cache.remove(setId); private ObjectSet convertDocumentToObjectSet(Document doc) { List<Document> pointSetsDocs = doc.getList("objectSets", Document.class); ObjectSet objectSet = new ObjectSet(); for (Document pointSetDoc : pointSetsDocs) { objectSet.objects.add(convertDocumentToPointSet(pointSetDoc)); } return objectSet; } private PointSet convertDocumentToPointSet(Document doc) { List<Document> pointsList = (List<Document>) doc.get("points"); List<Document> pointsDocs = doc.getList("points", Document.class); PointSet pointSet = new PointSet(); for (Document pointDoc : pointsList) { for (Document pointDoc : pointsDocs) { float x = pointDoc.getDouble("x").floatValue(); float y = pointDoc.getDouble("y").floatValue(); float z = pointDoc.getDouble("z").floatValue(); int additionalData = pointDoc.getInteger("additionalData", 0); // Assuming 'additionalData' is a field in your document pointSet.addPoint(new Point(x, y, z, additionalData)); pointSet.addPoint(new Point(x, y, z)); } return pointSet; } // ObjectSet management methods private Document objectSetToDocument(int index, ObjectSet objectSet) { List<Document> pointSetsList = new ArrayList<>(); int setId = 1; for (object_detection.types.PointSet ps : objectSet.objects) { Document pointSetDoc = pointSetToDocument(String.valueOf(setId++), 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; } public void deleteObjectSet(int index) { objectCollection.deleteOne(Filters.eq("index", index)); } public ObjectSet retrieveLatestObjectSet() { // Assuming 'index' is an integer that increments with each new ObjectSet Document highestIndexDoc = objectCollection.find().sort(new Document("index", -1)).first(); if (highestIndexDoc != null) { return convertDocumentToObjectSet(highestIndexDoc); } return null; } private ObjectSet convertDocumentToObjectSet(Document doc) { List<Document> pointSetsDocs = (List<Document>) doc.get("objectSets"); ObjectSet objectSet = new ObjectSet(); for (Document pointSetDoc : pointSetsDocs) { PointSet pointSet = convertDocumentToPointSet(pointSetDoc); objectSet.objects.add(pointSet); } return objectSet; } } //public static void main(String[] args) { // MongoDBInteraction dbInteraction = new MongoDBInteraction(); // PointSet pointSet = new PointSet(new Point(1, 2, 3), new Point(4, 5, 6), new Point(7, 3, 1)); // dbInteraction.update("set13", pointSet); // PointSet retrieved = dbInteraction.retrieve("set13"); // System.out.println("Retrieved Point Set: " + retrieved.getPoints()); // dbInteraction.delete("set13"); // } //}
src/main/java/object_detection/types/ObjectSet.java +1 −1 Original line number Diff line number Diff line Loading @@ -112,7 +112,7 @@ public class ObjectSet { 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])); Point point = new Point(Float.parseFloat(line[3]), Float.parseFloat(line[4]), Float.parseFloat(line[5])); // Check if the point is within any bounding box for (int i = 0; i < boundingBoxes.size(); i++) { Loading
src/main/java/object_detection/types/Point.java +1 −1 Original line number Diff line number Diff line Loading @@ -16,7 +16,7 @@ public class Point { /* Object Constructor of a feature in 3D */ public Point(float xx, float yy, float zz, int i){ public Point(float xx, float yy, float zz){ this.x = xx; this.y = yy; this.z = zz; Loading
src/main/java/top/BackendJava.java +18 −2 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ import com.google.gson.GsonBuilder; import database.MongoDBInteraction; import object_detection.ObjectDetector; import object_detection.types.ObjectSet; import object_detection.types.Point; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; Loading @@ -18,6 +19,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @SpringBootApplication Loading @@ -28,7 +30,7 @@ public class BackendJava { } @RestController @RequestMapping("/objectset") @RequestMapping("/api") public static class ObjectSetController { private final MongoDBInteraction dbInteraction = new MongoDBInteraction(); Loading @@ -37,7 +39,7 @@ public class BackendJava { @GetMapping("/{index}") public ResponseEntity<String> getObjectSet(@PathVariable int index) { try { ObjectSet objectSet = dbInteraction.retrieveObjectSet(index); ObjectSet objectSet = dbInteraction.retrieveLatestObjectSet(); if (objectSet != null) { return ResponseEntity.ok(gson.toJson(objectSet)); } else { Loading @@ -49,6 +51,20 @@ public class BackendJava { return ResponseEntity.internalServerError().body("{\"error\":\"Internal Server Error: " + e.getMessage() + "\"}"); } } @GetMapping("/getImageAndObjects") public ResponseEntity<String> getImageAndObjects() { try { Map<String, ObjectSet> allObjectSets = dbInteraction.retrieveAllObjectSets(); if (!allObjectSets.isEmpty()) { return ResponseEntity.ok(gson.toJson(allObjectSets)); } else { return ResponseEntity.notFound().build(); } } catch (Exception e) { return ResponseEntity.internalServerError().body("{\"error\":\"Failed to retrieve all objects: " + e.getMessage() + "\"}"); } } } @Controller Loading