Skip to content

Commit

Permalink
fixed all the examples and added an MNIST detector example
Browse files Browse the repository at this point in the history
  • Loading branch information
cansik committed Mar 18, 2020
1 parent 642764e commit 3c1a53e
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'ch.bildspur'
version '0.3.1'
version '0.3.2'

apply plugin: 'java'

Expand Down
51 changes: 51 additions & 0 deletions examples/FaceDetectorSelfie/FaceDetectorSelfie.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import ch.bildspur.vision.*;
import ch.bildspur.vision.result.*;
import java.util.List;

PImage testImage;

DeepVision vision;
ULFGFaceDetectionNetwork network;
List<ObjectDetectionResult> detections;

public void setup() {
size(640, 480);
colorMode(HSB, 360, 100, 100);

vision = new DeepVision(this);
testImage = loadImage(sketchPath("data/selfie.jpg"));

println("creating network...");
network = vision.createULFGFaceDetectorRFB640();
network.setConfidenceThreshold(0.2);

println("loading model...");
network.setup();

//network.setConfidenceThreshold(0.2f);

println("inferencing...");
detections = network.run(testImage);
println("done!");

for (ObjectDetectionResult detection : detections) {
System.out.println(detection.getClassName() + "\t[" + detection.getConfidence() + "]");
}

println("found " + detections.size() + " faces!");
}

public void draw() {
background(55);
image(testImage, 0, 0);

noFill();
strokeWeight(2f);

stroke(200, 80, 100);
for (ObjectDetectionResult detection : detections) {
rect(detection.getX(), detection.getY(), detection.getWidth(), detection.getHeight());
}

surface.setTitle("Face Recognition Test - Faces: " + detections.size());
}
Binary file added examples/FaceDetectorSelfie/data/selfie.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/HumanPoseWebcam/HumanPoseWebcam.pde
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ HumanPoseResult result;

Capture cam;

float threshold = 0.5;

void setup() {
size(640, 480, FX2D);

Expand Down Expand Up @@ -81,6 +83,9 @@ private void drawHuman(HumanPoseResult human) {
int i = 0;
fill(0);
for (KeyPointResult point : human.getKeyPoints()) {
if(point.getProbability() < threshold)
continue;

ellipse(point.getX(), point.getY(), 10, 10);
text(i, point.getX() + 5, point.getY());
i++;
Expand All @@ -92,6 +97,9 @@ private void connect(KeyPointResult... keyPoints) {
KeyPointResult a = keyPoints[i];
KeyPointResult b = keyPoints[i + 1];

if(a.getProbability() < threshold || b.getProbability() < threshold)
continue;

line(a.getX(), a.getY(), b.getX(), b.getY());
}
}
93 changes: 93 additions & 0 deletions examples/MNISTDetector/MNISTDetector.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import ch.bildspur.vision.*;
import ch.bildspur.vision.result.*;

DeepVision vision;
MNISTClassificationNetwork network;

ClassificationResult result;
long inferenceTime = 0;
PImage canvas = new PImage(28, 28, RGB);

boolean mouseDrawing = false;
float factor = 560 / 28;

public void setup() {
size(560, 560, FX2D);
colorMode(HSB, 360, 100, 100);

vision = new DeepVision(this);

println("creating network...");
network = vision.createMNISTClassifier();

println("loading model...");
network.setup();

clearCanvas();
println("ready!");
}

public void draw() {
background(55);

if (mouseDrawing) {
int x = round(mouseX / factor);
int y = round(mouseY / factor);

canvas.set(x, y, color(255));
canvas.updatePixels();
}

//image(canvas, 0, 0, width, height);
for (int y = 0; y < canvas.width; y++) {
for (int x = 0; x < canvas.width; x++) {
color c = canvas.get(x, y);
fill(c);
noStroke();
rect(x * factor, y * factor, factor, factor);
}
}

// display info
fill(140, 80, 100);
textSize(16);
text("space: run inference / c: clear canvas", 10, 20);

if (result != null) {
text("Detected '" + result.getClassName() + "' with " + result.getConfidence() + "%", 10, 50);
}

surface.setTitle("MNIST Detector");
}

void mousePressed() {
mouseDrawing = true;
}

void mouseReleased() {
mouseDrawing = false;
}

void keyPressed() {
if (key == ' ') {
println("inferencing...");
int start = millis();
result = network.run(canvas);
inferenceTime = millis() - start;
}

if (key == 'c' || key == 'C') {
println("clearing canvas...");
clearCanvas();
result = null;
}
}

void clearCanvas() {
for (int y = 0; y < canvas.width; y++) {
for (int x = 0; x < canvas.width; x++) {
canvas.set(x, y, color(0));
}
}
canvas.updatePixels();
}
4 changes: 2 additions & 2 deletions examples/RealSensePoseCapture/RealSensePoseCapture.pde
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ch.bildspur.vision.DeepVision;
import ch.bildspur.vision.SingleHumanPoseEstimationNetwork;
import ch.bildspur.vision.SingleHumanPoseNetwork;
import ch.bildspur.vision.result.HumanPoseResult;
import ch.bildspur.vision.result.KeyPointResult;

Expand All @@ -9,7 +9,7 @@ RealSenseCamera camera = new RealSenseCamera(this);

DeepVision vision = new DeepVision(this);

SingleHumanPoseEstimationNetwork pose;
SingleHumanPoseNetwork pose;
HumanPoseResult result;

void setup()
Expand Down
2 changes: 1 addition & 1 deletion examples/RealSenseYoloDetector/RealSenseYoloDetector.pde
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void draw()
strokeWeight(2f);

for (ObjectDetectionResult detection : result) {
stroke(round(360.0f * (float) detection.getClassId() / net.getNames().size()), 75, 100);
stroke(round(360.0f * (float) detection.getClassId() / net.getClassNames().size()), 75, 100);
rect(detection.getX(), detection.getY(), detection.getWidth(), detection.getHeight());

textSize(15);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import ch.bildspur.vision.result.KeyPointResult;

PImage testImage;

DeepVision vision = new DeepVision(this);
DeepVision vision;

SingleHumanPoseEstimationNetwork pose;
SingleHumanPoseNetwork pose;
HumanPoseResult result;

PImage image;
Expand All @@ -16,10 +16,10 @@ void setup() {
size(480, 640, FX2D);

colorMode(HSB, 360, 100, 100);

testImage = loadImage(sketchPath("data/pose.jpg"));

println("creating network...");
vision = new DeepVision(this);
pose = vision.createSingleHumanPoseEstimation();

println("loading model...");
Expand Down
11 changes: 6 additions & 5 deletions examples/YOLODetectObjects/YOLODetectObjects.pde
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ public void setup() {
image = loadImage("hk.jpg");

println("creating model...");
yolo = deepVision.createYOLOv3Tiny();
yolo = deepVision.createYOLOv3();

println("loading yolo model...");
yolo.setup();

println("inferencing...");
yolo.setConfidenceThreshold(0.3f);
detections = yolo.run(image);
}

public void draw() {
background(55);

image(image, 0, 0);

yolo.setConfidenceThreshold(0.2f);
detections = yolo.run(image);

noFill();
strokeWeight(2f);

for (ObjectDetectionResult detection : detections) {
stroke((int)(360.0 / yolo.getNames().size() * detection.getClassId()), 80, 100);
stroke((int)(360.0 / yolo.getClassNames().size() * detection.getClassId()), 80, 100);
rect(detection.getX(), detection.getY(), detection.getWidth(), detection.getHeight());
}

Expand Down
5 changes: 3 additions & 2 deletions examples/YoloWecamExample/YoloWecamExample.pde
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public void setup() {
println("loading yolo model...");
yolo.setup();

cam = new Capture(this);
String[] cams = Capture.list();
cam = new Capture(this, cams[0]);
cam.start();
}

Expand All @@ -47,7 +48,7 @@ public void draw() {
strokeWeight(2f);

for (ObjectDetectionResult detection : detections) {
stroke((int)(360.0 / yolo.getNames().size() * detection.getClassId()), 80, 100);
stroke((int)(360.0 / yolo.getClassNames().size() * detection.getClassId()), 80, 100);
rect(detection.getX(), detection.getY(), detection.getWidth(), detection.getHeight());
}

Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors=[Florian Bruggisser](https://broox.ch)
url=https://github.com/cansik/deep-vision-processing
sentence=Deep computer-vision algorithms for Processing
paragraph=Run deep neural networks on images in Processing.
version=003010
prettyVersion=0.3.1
version=003020
prettyVersion=0.3.2
minRevision=0
maxRevision=0
5 changes: 5 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ cp "build/libs/$ARCHIVE_NAME-complete.jar" "$OUTPUT/library/$ARCHIVE_NAME.jar"
# cp -r native "$OUTPUT/library/"
cp -r "build/docs/javadoc" "$OUTPUT/reference"

# clean networks from examples
cd "examples"
rm -rf **/networks
cd ..

cp -r "examples" "$OUTPUT/"
cp library.properties "$OUTPUT/"
cp -r readme "$OUTPUT/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public ClassificationResult run(Mat frame) {
for (int i = 0; i < out.cols(); i++) {
float probability = data.get(i);

System.out.println("# " + i + ": " + probability + "%");
// todo: fix probability issue
// System.out.println("# " + i + ": " + probability + "%");

if (probability > maxProbability) {
maxProbability = probability;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void setup() {
testImage = loadImage(sketchPath("data/office.jpg"));

println("creating network...");
network = vision.createULFGFaceDetectorSlim640();
network = vision.createULFGFaceDetectorRFB640();

println("loading model...");
network.setup();
Expand Down

0 comments on commit 3c1a53e

Please sign in to comment.