Created
April 15, 2021 01:02
-
-
Save yiyuezhuo/d9028a40420350c6ae542b43dac024ad to your computer and use it in GitHub Desktop.
Java call ONNX models using ONNXRUNTIME and opencv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// opencv | |
import org.opencv.core.Core; | |
import org.opencv.core.CvType; | |
import org.opencv.core.Mat; | |
import org.opencv.core.Size; | |
import org.opencv.imgproc.Imgproc; | |
import org.opencv.imgcodecs.Imgcodecs; | |
// onnxruntime | |
import ai.onnxruntime.NodeInfo; | |
import ai.onnxruntime.OnnxTensor; | |
import ai.onnxruntime.OrtEnvironment; | |
import ai.onnxruntime.OrtException; | |
import ai.onnxruntime.OrtSession; | |
import ai.onnxruntime.OrtSession.Result; | |
import ai.onnxruntime.OrtSession.SessionOptions; | |
import ai.onnxruntime.OrtSession.SessionOptions.OptLevel; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.util.regex.Pattern; | |
public class Type6test { | |
private static final Logger logger = Logger.getLogger(Type6test.class.getName()); | |
public static void main(String[] args) throws OrtException, IOException{ | |
System.out.println("Hello"); | |
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); | |
String file ="face.jpg"; | |
// Imgcodecs imageCodecs = new Imgcodecs(); | |
// Mat img_bgr = imageCodecs.imread(file); | |
Mat img_bgr = Imgcodecs.imread(file); | |
Mat img_rgb = new Mat(); | |
Imgproc.cvtColor(img_bgr, img_rgb, Imgproc.COLOR_BGR2RGB); | |
Mat img = new Mat(); | |
Mat resizeimage = new Mat(); | |
Size sz = new Size(320, 240); // size is (x, y) | |
Imgproc.resize( img_rgb, img, sz ); | |
float[][][][] batch_arr = new float[1][3][240][320]; | |
for(int i=0; i<240; i++){ | |
for(int j=0; j<320; j++){ | |
var c = img.get(i, j); | |
for(int k=0; k<3;k++){ | |
// logger.info(""+ i + ", " + j); | |
batch_arr[0][k][i][j] = (float)(c[k] - 127) / 128; | |
} | |
} | |
} | |
logger.info("Data setup finished start to setup ONNXRUNTIME"); | |
try(OrtEnvironment env = OrtEnvironment.getEnvironment(); | |
OrtSession.SessionOptions opts = new SessionOptions()){ | |
opts.setOptimizationLevel(OptLevel.BASIC_OPT); | |
try(OrtSession session = env.createSession("updated2_ultra_light_320.onnx", opts)){ | |
logger.info("Inputs:"); | |
for (NodeInfo i : session.getInputInfo().values()) { | |
logger.info(i.toString()); | |
} | |
logger.info("Outputs:"); | |
for (NodeInfo i : session.getOutputInfo().values()) { | |
logger.info(i.toString()); | |
} | |
String inputName = session.getInputNames().iterator().next(); | |
try(OnnxTensor batch = OnnxTensor.createTensor(env, batch_arr); | |
Result output = session.run(Collections.singletonMap(inputName, batch))){ | |
System.out.println("output: " + output); | |
} | |
logger.info("Done!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment