• 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;
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行结果,显示"status": "Model “person_test” registered."模型已经注册
    在这里插入图片描述
    查看模型状态和访问地址
    在这里插入图片描述

    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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    推理结果
    在这里插入图片描述
    通过结果绘制原图片

    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:admin1234@192.168.66.150: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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124

    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));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

  • 相关阅读:
    gulimall基础篇回顾Day-13
    深度神经网络——什么是小样本学习?
    (八)MyBatis中参数的处理
    LiveGBS流媒体平台国标GB/T28181作为下级支持国标级联海康大华宇视华为等第三方国标平台支持对接政务公安内网国标视频平台
    leetcode40.组合总和II(去重思路精讲,经典题也可以有困难的思考!)
    如何优雅的消除系统重复代码
    内置第三方apk总结
    python自动化办公之文件整理脚本详解
    三维模型3DTile格式轻量化压缩文件大小的技术方法研究
    springcloud3 分布式事务-seata的四种模式总结以及异地容灾
  • 原文地址:https://blog.csdn.net/qq_39879126/article/details/132733138