Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JNA support for darknet #4560

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jna/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.directory
*.iml
target
30 changes: 30 additions & 0 deletions jna/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>darknet</groupId>
<artifactId>darknet</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>darknet</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.5.0</version>
</dependency>
</dependencies>
</project>
104 changes: 104 additions & 0 deletions jna/src/main/java/dark/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package dark;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.bytedeco.opencv.opencv_core.Mat;


import java.io.File;

import static java.lang.System.out;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;


public class App
{
network net;
metadata meta;
String[] altNames;
//DarknetLib darknet = (DarknetLib) Native.load(new File(".").getAbsolutePath()+"/libdark.so", DarknetLib.class);
DarknetLib darknet = (DarknetLib) Native.load(new File(".").getAbsolutePath()+"/dark.dll", DarknetLib.class);

public static void main( String[] args ) {
detection[] detections = new App().PerformDetect("data/dog.jpg",0.25f,"./cfg/yolov3.cfg","./cfg/yolov3.weights", "./cfg/coco.data",false);
}

App(){
}

App(String cfgFile,String weightsFile,String classesFile){
PerformDetect(null,0,"./cfg/yolov3.cfg","./cfg/yolov3.weights", "./cfg/coco.data",true);
}

public detection[] Detect(String filename, float thresh, float hier_thresh, float nms) {
image im = darknet.load_image_color(filename, 0, 0);
Mat mat = imread(filename);

detection[] ret = DetectImage(im, thresh, hier_thresh, nms);

Util.showImage(mat);
darknet.free_image(im);
return ret;
}

public detection[] DetectImage(image im, float thresh, float hier_thresh, float nms) {
IntByReference pnum = new IntByReference(0);
float pre = darknet.network_predict_image(net, im);
int letter_box = 0;
int map = 0;
IntByReference pmap = new IntByReference(map);
Pointer p = darknet.get_network_boxes(net, im.w, im.h, thresh, hier_thresh, pmap, 0, pnum, letter_box);
int num = pnum.getValue();
detection det = new detection(p);
if (nms>0) {
darknet.do_nms_sort(det, num, meta.classes, nms);
}
detection[] detections = (detection[])det.toArray(num);
for(int j=0;j<num;j++) {
detection detect = detections[j];
float[] probabilites = detect.prob.getFloatArray(0,meta.classes);
for (int i = 0; i < meta.classes; i++) {
if (probabilites[i] > 0) {
String nameTag = altNames[i];
//out.println("name:"+nameTag+"("+probabilites[i]+")");
}
}
}
darknet.free_detections(p, num);
return detections;
}

public detection[] PerformDetect(String imagePath,float thresh,String configPath,String weightPath,String metaPath,boolean initOnly){
if(0 < thresh && thresh > 1) {
out.println("Threshold should be a float between zero and one (non-inclusive)");
}
if(! new File(configPath).exists()) {
out.println("Invalid config path `"+ new File(configPath).getAbsolutePath()+"`");
}
if(!new File(weightPath).exists()) {
out.println("Invalid weight path `"+new File(weightPath).getAbsolutePath()+"`");
}
if(!new File(metaPath).exists()) {
out.println("Invalid meta path `"+new File(metaPath).getAbsolutePath()+"`");
}
if(net == null) {
net = darknet.load_network_custom(configPath,weightPath, 0, 1);// #batch size = 1
}
if(meta == null) {
meta = darknet.get_metadata(metaPath);
}
if(altNames==null) {
altNames = meta.names.getStringArray(0, meta.classes);
}
if(initOnly) {
out.println("Initialized detector");
return null;
}
float hier_thresh=.5f;
float nms=.45f;
detection[] detections = Detect(imagePath, thresh,hier_thresh,nms);
int success = darknet.dispose();
return detections;
}
}
4 changes: 4 additions & 0 deletions jna/src/main/java/dark/DarknetApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dark;

public class DarknetApp {
}
68 changes: 68 additions & 0 deletions jna/src/main/java/dark/DarknetLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dark;

import com.sun.jna.Library;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.FloatByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.LongByReference;
import com.sun.jna.ptr.PointerByReference;

public interface DarknetLib extends Library, YoloV2_Lib {
// parser.c
network load_network(String configurationFilename, String weightsFilename, int clear, int batch);// # batch size = 1
network load_network(String cfg,String weights, int clear);
network load_network_custom(String cfg,String weights, int clear, int batch);//works
// box.h
void do_nms_sort(detection dets, int total, int classes, float thresh);//works
void do_nms_obj(detection dets, int total, int classes, float thresh);
void diounms_sort(detection dets, int total, int classes, float thresh, int nms_kind, float beta1);
// network.h
float network_predict(network net, FloatByReference input);
float network_predict_ptr(network net, FloatByReference input);
Pointer get_network_boxes(network net, int w, int h, float thresh, float hier,IntByReference map, int relative, IntByReference num, int letter);//works
void free_detections(Pointer dets, int n);//works
void fuse_conv_batchnorm(network net);
void calculate_binary_weights(network net);
String detection_to_json(detection dets, int nboxes, int classes,String[] names,long frame_id, String filename);
Pointer make_network_boxes(network net, float thresh, IntByReference num);
void reset_rnn(network net);
float network_predict_image(network net, image im);//works
float network_predict_image_letterbox(network net, image im);
float validate_detector_map(String datacfg,String cfgfile,String weightfile, float thresh_calc_avg_iou, float iou_thresh,int map_points, int letter_box, network existing_net);
void train_detector(String datacfg,String cfgfile, String weightfile, IntByReference gpus, int ngpus, int clear, int dont_show, int calc_map, int mjpeg_port, int show_imgs);
void test_detector(String datacfg,String cfgfile, String weightfile, String filename, float thresh, float hier_thresh, int dont_show, int ext_output, int save_labels, String outfile, int letter_box);
int network_width(network net);
int network_height(network net);
void optimize_picture(network net, image orig, int max_layer, float scale, float rate, float thresh, int norm);
// image.h
image resize_image(image im, int w, int h);
void copy_image_from_bytes(image im, Pointer pdata);
image letterbox_image(image im, int w, int h);
void rgbgr_image(image im);
image make_image(int w, int h, int c);
image load_image_color(String filename, int w, int h);//works
void free_image(image m);//works
// layer.h
void free_layer(layer lay);
// data.c
void free_data(data d);
void load_thread(Pointer ptr);
// dark_cuda.h
void cuda_pull_array(FloatByReference x_gpu, FloatByReference x, LongByReference n);
void cuda_pull_array_async(FloatByReference x_gpu, FloatByReference x, LongByReference n);
void cuda_set_device(int n);
void cuda_get_context();
// utils.h
void free_ptrs(PointerByReference ptrs, int n);
void top_k(FloatByReference a, int n, int k, IntByReference index);
// tree.h
tree read_tree(String filename);
// option_list.h
metadata get_metadata(String file);
// http_stream.h
void delete_json_sender();
void send_json_custom(byte[] send_buf, int port, int timeout);
double get_time_point();
// gemm.h
void init_cpu();
}
Loading