Pu Zhibing
2025-01-21 836df57f8158c86d0e4825b07cf745faa27d7389
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.stylefeng.guns.modular.system.util.imageModel;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.modular.system.model.enums.ImageModelEnum;
import com.stylefeng.guns.modular.system.util.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author zhibing.pu
 * @Date 2024/12/20 15:48
 */
@Slf4j
public class ImageModelUtil {
    
    /**
     * 本地存储图片地址
     */
    private static String filePath = "/home/file/image/";
//    private static String filePath = "d:/file/";
    
    
    /**
     * 顶盖密闭模型
     * @param url  图片网络地址
     */
    private static List<String> closedTopModel(String url){
        try {
            String fileName = url.substring(url.lastIndexOf("."));
            URLConnection urlConnection = new URL(url).openConnection();
            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();
            File file = FileUtil.writeFromStream(inputStream, filePath + UUIDUtil.getTimeStr() + fileName);
            inputStream.close();
            return closedTopModel(file);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    
    
    /**
     * 顶盖密闭模型
     * @param file  图片文件
     */
    private static List<String> closedTopModel(File file){
        log.info("--------调用顶盖密闭模型开始--------");
        HttpRequest post = HttpUtil.createPost("http://120.232.235.142:5000/predict");
        post.form("file", file);
        HttpResponse execute = post.execute();
        int status = execute.getStatus();
        if(200 != status){
            log.error("顶盖密闭模型调用失败:" + execute.body());
            throw new RuntimeException("顶盖密闭模型调用失败:" + execute.body());
        }
        JSONObject result = JSON.parseObject(execute.body());
        JSONArray predicted_labels = result.getJSONArray("predicted_labels");
        List<String> list = new ArrayList<>();
        for (int i = 0; i < predicted_labels.size(); i++) {
            list.add(predicted_labels.getString(i));
        }
        log.info("--------调用顶盖密闭模型结束--------");
        return list;
    }
    
    
    /**
     * 摄像头故障模型
     * @param url 图片网络地址
     * @return
     */
    private static List<String> cameraFaultModel(String url) throws Exception{
        String fileName = url.substring(url.lastIndexOf("."));
        URLConnection urlConnection = new URL(url).openConnection();
        urlConnection.connect();
        InputStream inputStream = urlConnection.getInputStream();
        File file = FileUtil.writeFromStream(inputStream, filePath + UUIDUtil.getTimeStr() + fileName);
        inputStream.close();
        return cameraFaultModel(file);
    }
    
    
    
    
    /**
     * 摄像头故障模型
     * @param file  图片文件
     * @return
     */
    private static List<String> cameraFaultModel(File file){
        log.info("--------调用摄像头故障模型开始--------");
        HttpRequest post = HttpUtil.createPost("http://120.232.235.142:4000/predict");
        post.form("file", file);
        HttpResponse execute = post.execute();
        int status = execute.getStatus();
        if(200 != status){
            log.error("摄像头故障模型调用失败:" + execute.body());
            throw new RuntimeException("摄像头故障模型调用失败:" + execute.body());
        }
        JSONObject result = JSON.parseObject(execute.body());
        String predicted_labels = result.getString("predicted_label");
        List<String> list = new ArrayList<>();
        list.add(predicted_labels);
        log.info("--------调用摄像头故障模型结束--------");
        return list;
    }
    
    
    
    /**
     * 建筑垃圾装载模型
     * @param url 图片网络地址
     * @return
     */
    private static List<String> constructionWasteLoadModel(String url) throws Exception{
        String fileName = url.substring(url.lastIndexOf("."));
        URLConnection urlConnection = new URL(url).openConnection();
        urlConnection.connect();
        InputStream inputStream = urlConnection.getInputStream();
        File file = FileUtil.writeFromStream(inputStream, filePath + UUIDUtil.getTimeStr() + fileName);
        inputStream.close();
        return constructionWasteLoadModel(file);
    }
    
    
    
    
    /**
     * 建筑垃圾装载模型
     * @param file  图片文件
     * @return
     */
    private static List<String> constructionWasteLoadModel(File file){
        HttpRequest post = HttpUtil.createPost("http://120.232.235.142:");
        post.form("file", file);
        HttpResponse execute = post.execute();
        int status = execute.getStatus();
        if(200 != status){
            log.error("建筑垃圾装载模型调用失败:" + execute.body());
            throw new RuntimeException("建筑垃圾装载模型调用失败:" + execute.body());
        }
        JSONObject result = JSON.parseObject(execute.body());
        String predicted_labels = result.getString("predicted_label");
        List<String> list = new ArrayList<>();
        list.add(predicted_labels);
        return list;
    }
    
    
    /**
     * 1号模型四类标签分别是
     * Loaded Cargo - Unsealed  装载-未关闭
     * Empty Container - Sealed 空载-关闭
     * Empty Container - Unsealed  空载-未关闭
     * Loaded Cargo - Sealed  装载-关闭
     * ttt 未识别到货箱
     * 2号模型四类分别是:'blurred'(模糊), 'no_video'(无视频),'normalcy'(正常), 'splash_screen(花屏)'
     * @param url
     * @param modelEnum
     * @return
     */
    public static Map<String, Object> modelCheck(String url, ImageModelEnum modelEnum) throws Exception{
        Map<String, Object> map = new HashMap<>();
        switch (modelEnum){
            case TOP_SEAL:
                List<String> list1 = closedTopModel(url);
                map.put("r", JSON.toJSONString(list1));
                map.put("b", !(list1.contains("Loaded Cargo - Unsealed") || list1.contains("ttt")));
                //其中包含装载未关闭,视为异常
                return map;
            case CAMERA_FAULT:
                List<String> list2 = cameraFaultModel(url);
                map.put("r", JSON.toJSONString(list2));
                map.put("b", list2.contains("normalcy"));
                //其中包含正常,视为正常
                return map;
            case CONSTRUCTION_WASTE_LOAD: // TODO 待完善
                List<String> list3 = constructionWasteLoadModel(url);
                map.put("r", JSON.toJSONString(list3));
                map.put("b", list3.contains("normalcy"));
                //其中包含正常,视为正常
                return map;
        }
        return null;
    }
    
    
    
}