Deep Java Library(五)DJLServing java client demo

1.工具类HttpUtils

package com.lihao.client;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Map;

public class HttpUtils {

    /**
     *
     * @param url API地址
     * @param params 参数
     * @param contentType 响应类型
     * @param data 二进制参数
     * @param file 文件参数
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static String postRequest(String url, Map<String,String> params, String contentType, byte[] data, Path file) throws IOException, InterruptedException {

        //初始化client
        HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();

        //初始化builder
        HttpRequest.Builder builder = HttpRequest.newBuilder();

        //处理自定义参数
        if (params != null) {
            int i = 0;
            StringBuilder sb = new StringBuilder(url);
            sb.append("?");
            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (i > 0) {
                    sb.append("&");
                }
                sb.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
                sb.append("=");
                sb.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
                i++;
            }
            url = sb.toString();
        }
        //构造请求url+参数
        builder.uri(URI.create(url));

        //设置响应类型
        if (contentType != null) {
            builder.header("Content-Type", contentType);
        }

        if (data != null) {//处理二进制参数
            builder.POST(HttpRequest.BodyPublishers.ofByteArray(data));
        } else if (file != null) {//处理文件参数
            builder.POST(HttpRequest.BodyPublishers.ofFile(file));
        } else {
            builder.POST(HttpRequest.BodyPublishers.noBody());
        }

        //初始化request
        HttpRequest request = builder.build();
        //发送请求
        HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
        //处理响应
        String result = new String(response.body(), StandardCharsets.UTF_8);
        //结果返回
        return result;

    }
}

2.注册模型RegisterDemo

package com.lihao.client;

import java.util.Map;

public class RegisterDemo {
    public static void main(String[] args) throws Exception {
        // 模型地址
        String url = "file:///D://LIHAOWORK//serving-0.23.0//person-test.zip";
        //构造参数
        Map<String, String> params = Map.of("url", url, "modelName","person-test","engine", "OnnxRuntime");
        //注册模型
        String response = HttpUtils.postRequest("http://127.0.0.1:8080/models", params, null, null, null);
        System.out.println(response);
    }
}

运行结果,显示"status": "Model “person_test” registered."模型已经注册
Deep Java Library(五)DJLServing java client demo_第1张图片
查看模型状态和访问地址
Deep Java Library(五)DJLServing java client demo_第2张图片

3.模型推理PredictDemo

package com.lihao.client;

import java.nio.file.Path;

public class PredictDemo {
    public static void main(String[] args) throws Exception {
        String url = "http://127.0.0.1:8080/predictions/person_test/";
        String response = HttpUtils.postRequest(
                                                url,
                                                null,
                                                "application/octet-stream",
                                                null,
                                                Path.of("D:\\LIHAOWORK\\serving-0.23.0\\ren.jpg"));
        System.out.println(response);
    }
}

推理结果
Deep Java Library(五)DJLServing java client demo_第3张图片
通过结果绘制原图片

4.模型推理PredictDemo

将PredictDemo更改了一下,增加了随机抽帧一张图片,进行推理,将推理结果进行绘制,最后将绘制后的图片展示出来。

package com.lihao.client;

import ai.djl.modality.cv.BufferedImageFactory;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.output.BoundingBox;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.modality.cv.output.Point;
import ai.djl.modality.cv.output.Rectangle;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class PredictDemo {
    public static void main(String[] args) throws Exception {
        String path = getImage();
        String url = "http://127.0.0.1:8080/predictions/person_test/";
        String response = HttpUtils.postRequest(
                url,
                null,
                "application/octet-stream",
                null,
                Path.of(path));
        System.out.println(response);


        DetectedObjects result = str2DetectedObjects(response);
        Image image = BufferedImageFactory.getInstance().fromFile(Path.of(path));
        image.drawBoundingBoxes(result);
        CanvasFrame canvasFrame = new CanvasFrame("摄像机");
        canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvasFrame.setAlwaysOnTop(true);
        canvasFrame.showImage(image2Frame(image));
    }

    /**
     * 获取一张图片,返回图片路径
     * @return
     */
    private static String  getImage() {
        FFmpegFrameGrabber grabber = null;
        try {
            grabber = FFmpegFrameGrabber.createDefault("rtsp://admin:[email protected]:554/cam/realmonitor?channel=12&subtype=1");
            grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式
            grabber.setOption("stimeout", "5000000");
            grabber.setPixelFormat(avutil.AV_PIX_FMT_RGB24);  // 像素格式
            grabber.setImageWidth(640);
            grabber.setImageHeight(640);
            grabber.setFrameRate(30);
            grabber.start();
            Frame frame = grabber.grabFrame();//抽取一帧
            Image image = frame2Image(frame);
            String name = UUID.randomUUID().toString();
            String path = "D:\\LIHAOWORK\\serving-0.23.0\\"+ name+".jpg";
            OutputStream os = new FileOutputStream(new File(path));
            image.save(os,"jpg");
            return path;
        } catch (FFmpegFrameGrabber.Exception e) {
            e.printStackTrace();
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * str2DetectedObjects
     *
     * @param str
     * @return
     */
    private static DetectedObjects str2DetectedObjects(String str) {
        List<JSONObject> jsons = JSONObject.parseArray(str, JSONObject.class);
        List<String> classList = new ArrayList<>();
        List<Double> probList = new ArrayList<>();
        List<BoundingBox> rectList = new ArrayList<>();
        jsons.forEach(item -> {
            classList.add(item.getString("className"));
            probList.add(item.getDouble("probability"));
            JSONObject b = item.getJSONObject("boundingBox");
            List<Point> corners = JSON.parseArray(b.getString("corners"), Point.class);
            Rectangle newBox = new Rectangle(corners.get(0), b.getDouble("width"), b.getDouble("height"));
            rectList.add(newBox);
        });
        DetectedObjects r = new DetectedObjects(classList, probList, rectList);
        return r;
    }

    /**
     * image2Frame
     *
     * @param image
     * @return
     */
    private static Frame image2Frame(Image image) {
        BufferedImage temp = (BufferedImage) image.getWrappedImage();
        Frame frame = Java2DFrameUtils.toFrame(temp);
        return frame;
    }

    /**
     * frame2Image
     *
     * @param frame
     * @return
     */
    private static Image frame2Image(Frame frame) {
        BufferedImage temp = Java2DFrameUtils.toBufferedImage(frame);
        Image image = BufferedImageFactory.getInstance().fromImage(temp);
        return image;
    }
}

5.模型注销UnregisterDemo

package com.lihao.client;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;

public class UnregisterDemo {
    public static void main(String[] args) throws Exception {
        String url = "http://127.0.0.1:8080/models/person_test/";
        HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
        HttpRequest.Builder builder = HttpRequest.newBuilder();
        builder.uri(URI.create(url));
        builder.DELETE();
        HttpRequest request = builder.build();
        HttpResponse<byte[]> response =
                client.send(request, HttpResponse.BodyHandlers.ofByteArray());
        System.out.println(new String(response.body(), StandardCharsets.UTF_8));
    }
}

Deep Java Library(五)DJLServing java client demo_第4张图片

你可能感兴趣的:(java,python,开发语言,人工智能,深度学习)