Skip to content

Commit

Permalink
Merge pull request #41 from CommonWealthRobotics/kh/triangulation
Browse files Browse the repository at this point in the history
Add triangualtion to the kernel and to the STL export.
  • Loading branch information
madhephaestus authored Jul 6, 2023
2 parents 1f79eca + aea1f53 commit 152106f
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 107 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,11 @@ dependencies {

// https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j
implementation group: 'net.lingala.zip4j', name: 'zip4j', version: '2.11.5'

// https://mvnrepository.com/artifact/com.alphacephei/vosk
implementation group: 'com.alphacephei', name: 'vosk', version: '0.3.38'
implementation( 'com.alphacephei:vosk:0.3.38')
{
exclude group: 'net.java.dev.jna', module: 'jna'
}
// Add OpenCV
implementation 'org.openpnp:opencv:4.7.0-0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class PredictorFactory {
static {
Engine.getEngine("PyTorch"); // Make sure PyTorch engine is loaded
Engine.getEngine("OnnxRuntime"); // Make sure PyTorch engine is loaded
}
private static HashMap<ImagePredictorType, Predictor<Image, DetectedObjects>> preloaded = new HashMap<>();
private static Predictor<Image, float[]> features = null;
Expand Down Expand Up @@ -75,7 +76,8 @@ public static Predictor<Image, DetectedObjects> imageContentsFactory(ImagePredic
String MODEL_URL = "https://mlrepo.djl.ai/model/cv/object_detection/ai/djl/onnxruntime/yolo5s/0.0.1/yolov5s.zip";

Criteria<Image, DetectedObjects> criteria2 = Criteria.builder()
.setTypes(Image.class, DetectedObjects.class).optModelUrls(MODEL_URL).optEngine("OnnxRuntime")
.setTypes(Image.class, DetectedObjects.class).optModelUrls(MODEL_URL)
.optEngine("OnnxRuntime")
.optTranslatorFactory(new YoloV5TranslatorFactory()).build();
preloaded.put(type, criteria2.loadModel().newPredictor());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,17 @@ public void addCSG(Collection<CSG> toAdd, File source) {

// Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
long heapMaxSize = Runtime.getRuntime().maxMemory();
System.out.println("Heap remaining "+(heapMaxSize-Runtime.getRuntime().totalMemory()));
System.out.println("Of Heap "+(heapMaxSize));
//System.out.println("Heap remaining "+(heapMaxSize-Runtime.getRuntime().totalMemory()));
//System.out.println("Of Heap "+(heapMaxSize));
for(int i=0;i<totalAssembly.size();i++) {
List<CSG> tmp = Arrays.asList(totalAssembly.get(i));
totalAssembly.set(i,null);
System.out.println("Before Heap remaining "+(heapMaxSize-Runtime.getRuntime().totalMemory()));
//System.out.println("Before Heap remaining "+(heapMaxSize-Runtime.getRuntime().totalMemory()));

new CadFileExporter(m.getUi()).generateManufacturingParts(tmp, dir);
tmp=null;
System.gc();
System.out.println("After Heap remaining "+(heapMaxSize-Runtime.getRuntime().totalMemory()));
//System.out.println("After Heap remaining "+(heapMaxSize-Runtime.getRuntime().totalMemory()));

}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class PrintBedManager {
HashMap<Integer, CSG> bedReps = new HashMap<Integer, CSG>();
ArrayList<PrintBedObject> objects = new ArrayList<PrintBedObject>();
private ArrayList<CSG> parts;
private HashSet<String> names = new HashSet<String>();

public PrintBedManager(String url, ArrayList<CSG> parts) {
this.url = url;
Expand All @@ -64,20 +66,25 @@ public PrintBedManager(String url, ArrayList<CSG> parts) {
database = new UserManagedPrintBedData();
database.init();
}
names.clear();
for (CSG bit : parts) {
int index = bit.getPrintBedIndex();
int colorIndex = index % 4;
double zrot = -90 * (index);
double yval = index > 4 ? database.bedX * (index - 4) : 0;
System.out.println(bit.getName() + " on " + index + " rot " + zrot + " y " + yval);

CSG bed = new Cube(database.bedX, database.bedY, 1).toCSG().toXMin().toYMin().toZMax().rotz(zrot)
.movey(yval);
bed.setColor(colors.get(colorIndex));
bedReps.put(index, bed);
String name = bit.getName();

CSG prepedBit = bit.prepForManufacturing();
if (prepedBit != null && name.length() > 0) {
if(names.contains(name))
continue;
names.add(name);
System.out.println(bit.getName() + " on " + index + " rot " + zrot + " y " + yval);
if (database.locations.get(name) == null) {
database.locations.put(name, new TransformNR());
}
Expand All @@ -96,12 +103,16 @@ public ArrayList<CSG> makePrintBeds() {
if (url == null)
return parts;
HashMap<Integer, ArrayList<CSG>> beds = new HashMap<>();
names.clear();
for (CSG bit : parts) {
ArrayList<String> formats = bit.getExportFormats();
String name = bit.getName();
int index = bit.getPrintBedIndex();
bit = bit.prepForManufacturing();
if (bit != null && name.length()>0) {
if(names.contains(name))
continue;
names.add(name);
if(bit.getMinZ()<0)
bit=bit.toZMin();
if (beds.get(index) == null) {
Expand All @@ -115,6 +126,7 @@ public ArrayList<CSG> makePrintBeds() {
if (formats != null)
for (String s : formats)
bit.addExportFormat(s);
bit.setName(name);
beds.get(index).add(bit);
}
}
Expand All @@ -124,6 +136,7 @@ public ArrayList<CSG> makePrintBeds() {
ArrayList<CSG> bedComps = beds.get(i);
CSG bed = null;
for (CSG p : bedComps) {
bedsOutputs.add(p);
ArrayList<String> formats = p.getExportFormats();
if (bed == null)
bed = p;
Expand All @@ -134,15 +147,18 @@ public ArrayList<CSG> makePrintBeds() {
for (String s : formats)
bed.addExportFormat(s);
}
if (bed != null)
if (bed != null) {
//System.out.println("Mesh fixing for "+name);
//bed=bed.union(bed);
bed.setName(name);
}
else {
bed = new Cube().toCSG();
bed = new Cube().toCSG().toZMin();
bed.setManufacturing(incoming -> null);
}
bedsOutputs.add(bed);
}
bedsOutputs.addAll(parts);

return bedsOutputs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public PrintBedObject(String name, CSG part, double xMax, double xMin, double yM
this.globalPose = startPose;

manip = new manipulation(affine, new Vector3d(1, 1, 0), part, startPose);
manip.addSaveListener(() -> System.out.println("Saving PrintBedObject "+name));
checkBounds();
}
public void addEventListener(Runnable r) {
Expand Down
197 changes: 102 additions & 95 deletions test/java/src/junit/bowler/PyTorchResNetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,100 +31,107 @@ public class PyTorchResNetTest {
String image3URL = "https://static1.squarespace.com/static/56ffcc3901dbae8fc9b21603/t/572f5e92cf80a12c4b5c66fe/1462722410298/?format=1500w";
String notme = "https://images.squarespace-cdn.com/content/v1/51f533d1e4b0de43ba620290/1472046872933-F3PCYXEK429ZJHDZSWLC/image-asset.jpeg?format=300w";

@Test
public void testResNet() throws Exception {

}

@Test
public void testretinaface() throws Exception {
BufferedImage bimg = ImageIO.read(new URL(imageUrl));

Predictor<Image, DetectedObjects> predictor = PredictorFactory
.imageContentsFactory(ImagePredictorType.retinaface);

for (int i = 0; i < 3; i++) {
Image img = ImageFactory.getInstance().fromImage(bimg);

DetectedObjects objects = predictor.predict(img);
saveBoundingBoxImage(img, objects, "retinaface");
}
}

@Test
public void testUltraNet() throws Exception {
BufferedImage bimg = ImageIO.read(new URL(imageUrl));

Predictor<Image, DetectedObjects> predictor = PredictorFactory
.imageContentsFactory(ImagePredictorType.ultranet);

for (int j = 0; j < 3; j++) {
Image img = ImageFactory.getInstance().fromImage(bimg);

DetectedObjects objects = predictor.predict(img);
List<DetectedObject> items = objects.items();
for (int i = 0; i < items.size(); i++) {
DetectedObject c = items.get(i);
BoundingBox cGetBoundingBox = c.getBoundingBox();
cGetBoundingBox = c.getBoundingBox();
Iterator<Point> path = cGetBoundingBox.getPath().iterator();
List<Point> list = new ArrayList<>();
// keypoints in the image
path.forEachRemaining(list::add);

Landmark lm;
if (Landmark.class.isInstance(cGetBoundingBox)) {
lm = (Landmark) cGetBoundingBox;

}
}
saveBoundingBoxImage(img, objects, "ultranet");
}
}

private static void saveBoundingBoxImage(Image img, DetectedObjects detection, String type) throws Exception {
Path outputDir = Paths.get("build/output");
Files.createDirectories(outputDir);

img.drawBoundingBoxes(detection);

Path imagePath = outputDir.resolve(type + ".png").toAbsolutePath();
img.save(Files.newOutputStream(imagePath), "png");
System.out.println("Face detection result image has been saved in: {} " + imagePath);
}

@Test
public void testYolo() throws Exception {

Predictor<Image, DetectedObjects> predictor = PredictorFactory.imageContentsFactory(ImagePredictorType.yolov5);
for (int i = 0; i < 3; i++) {
Image input = ImageFactory.getInstance()
.fromUrl("https://github.com/ultralytics/yolov5/raw/master/data/images/bus.jpg");

DetectedObjects objects = predictor.predict(input);
saveBoundingBoxImage(input, objects, "yolov5");
}
}

@Test
public void testFeatures() throws Exception {
BufferedImage img = ImageIO.read(new URL(imageUrl));
BufferedImage img2 = ImageIO.read(new URL(image2URL));
BufferedImage img3 = ImageIO.read(new URL(image3URL));

Predictor<Image, float[]> predictor = PredictorFactory.faceFeatureFactory();
float[] back = predictor.predict(ImageFactory.getInstance().fromImage(img));
float[] back2 = predictor.predict(ImageFactory.getInstance().fromImage(img2));
float[] back3 = predictor.predict(ImageFactory.getInstance().fromImage(img3));
float[] back4 = predictor.predict(ImageFactory.getInstance().fromImage(ImageIO.read(new URL(notme))));

System.out.println(" Comprair 1 to 2 " + PredictorFactory.calculSimilarFaceFeature(back, back2));
System.out.println(" Comprair 1 to 3 " + PredictorFactory.calculSimilarFaceFeature(back, back3));
System.out.println(" Comprair 2 to 3 " + PredictorFactory.calculSimilarFaceFeature(back2, back3));
System.out.println(" Comprair 1 to 4 " + PredictorFactory.calculSimilarFaceFeature(back, back4));

System.out.println(Arrays.toString(back));

}
// @Test
// public void testResNet() throws Exception {
// System.err.println(Thread.currentThread().getStackTrace()[1].getMethodName());
//
// }
//
// @Test
// public void testretinaface() throws Exception {
// System.err.println(Thread.currentThread().getStackTrace()[1].getMethodName());
//
// BufferedImage bimg = ImageIO.read(new URL(imageUrl));
//
// Predictor<Image, DetectedObjects> predictor = PredictorFactory
// .imageContentsFactory(ImagePredictorType.retinaface);
//
// for (int i = 0; i < 3; i++) {
// Image img = ImageFactory.getInstance().fromImage(bimg);
//
// DetectedObjects objects = predictor.predict(img);
// saveBoundingBoxImage(img, objects, "retinaface");
// }
// }
//
// @Test
// public void testUltraNet() throws Exception {
// System.err.println(Thread.currentThread().getStackTrace()[1].getMethodName());
//
// BufferedImage bimg = ImageIO.read(new URL(imageUrl));
//
// Predictor<Image, DetectedObjects> predictor = PredictorFactory
// .imageContentsFactory(ImagePredictorType.ultranet);
//
// for (int j = 0; j < 3; j++) {
// Image img = ImageFactory.getInstance().fromImage(bimg);
//
// DetectedObjects objects = predictor.predict(img);
// List<DetectedObject> items = objects.items();
// for (int i = 0; i < items.size(); i++) {
// DetectedObject c = items.get(i);
// BoundingBox cGetBoundingBox = c.getBoundingBox();
// cGetBoundingBox = c.getBoundingBox();
// Iterator<Point> path = cGetBoundingBox.getPath().iterator();
// List<Point> list = new ArrayList<>();
// // keypoints in the image
// path.forEachRemaining(list::add);
//
// Landmark lm;
// if (Landmark.class.isInstance(cGetBoundingBox)) {
// lm = (Landmark) cGetBoundingBox;
//
// }
// }
// saveBoundingBoxImage(img, objects, "ultranet");
// }
// }
//
// private static void saveBoundingBoxImage(Image img, DetectedObjects detection, String type) throws Exception {
// Path outputDir = Paths.get("build/output");
// Files.createDirectories(outputDir);
//
// img.drawBoundingBoxes(detection);
//
// Path imagePath = outputDir.resolve(type + ".png").toAbsolutePath();
// img.save(Files.newOutputStream(imagePath), "png");
// System.out.println("Face detection result image has been saved in: {} " + imagePath);
// }
//
// @Test
// public void testYolo() throws Exception {
// System.err.println(Thread.currentThread().getStackTrace()[1].getMethodName());
//
// Predictor<Image, DetectedObjects> predictor = PredictorFactory.imageContentsFactory(ImagePredictorType.yolov5);
// for (int i = 0; i < 3; i++) {
// Image input = ImageFactory.getInstance()
// .fromUrl("https://github.com/ultralytics/yolov5/raw/master/data/images/bus.jpg");
//
// DetectedObjects objects = predictor.predict(input);
// saveBoundingBoxImage(input, objects, "yolov5");
// }
// }
//
// @Test
// public void testFeatures() throws Exception {
// System.err.println(Thread.currentThread().getStackTrace()[1].getMethodName());
// BufferedImage img = ImageIO.read(new URL(imageUrl));
// BufferedImage img2 = ImageIO.read(new URL(image2URL));
// BufferedImage img3 = ImageIO.read(new URL(image3URL));
//
// Predictor<Image, float[]> predictor = PredictorFactory.faceFeatureFactory();
// float[] back = predictor.predict(ImageFactory.getInstance().fromImage(img));
// float[] back2 = predictor.predict(ImageFactory.getInstance().fromImage(img2));
// float[] back3 = predictor.predict(ImageFactory.getInstance().fromImage(img3));
// float[] back4 = predictor.predict(ImageFactory.getInstance().fromImage(ImageIO.read(new URL(notme))));
//
// System.out.println(" Comprair 1 to 2 " + PredictorFactory.calculSimilarFaceFeature(back, back2));
// System.out.println(" Comprair 1 to 3 " + PredictorFactory.calculSimilarFaceFeature(back, back3));
// System.out.println(" Comprair 2 to 3 " + PredictorFactory.calculSimilarFaceFeature(back2, back3));
// System.out.println(" Comprair 1 to 4 " + PredictorFactory.calculSimilarFaceFeature(back, back4));
//
// System.out.println(Arrays.toString(back));
//
// }

}

0 comments on commit 152106f

Please sign in to comment.