Pu Zhibing
2025-03-10 922de4e688e18f508070972876002fa6a3810135
guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/imageModel/ImageModelUtil.java
@@ -12,6 +12,7 @@
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;
@@ -20,7 +21,9 @@
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
@@ -32,8 +35,8 @@
   /**
    * 本地存储图片地址
    */
   @Value("${filePath}")
   private static String filePath;
   private static String filePath = "/home/file/image/";
//   private static String filePath = "d:/file/";
   
   
   /**
@@ -63,20 +66,23 @@
    * @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());
         return null;
         throw new RuntimeException("顶盖密闭模型调用失败:" + execute.body());
      }
      log.info("调用顶盖密闭模型结果:{}", 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;
   }
   
@@ -86,20 +92,14 @@
    * @param url 图片网络地址
    * @return
    */
   private static List<String> cameraFaultModel(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 cameraFaultModel(file);
      } catch (MalformedURLException e) {
         throw new RuntimeException(e);
      } catch (IOException e) {
         throw new RuntimeException(e);
      }
   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);
   }
   
   
@@ -111,18 +111,21 @@
    * @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);
      post.form("file1", file);
      HttpResponse execute = post.execute();
      int status = execute.getStatus();
      if(200 != status){
         log.error("摄像头故障模型调用失败:" + execute.body());
         return null;
         throw new RuntimeException("摄像头故障模型调用失败:" + execute.body());
      }
      JSONObject result = JSON.parseObject(execute.body());
      String predicted_labels = result.getString("predicted_label");
      log.info("调用摄像头故障模型结果:{}", execute.body());
      JSONArray result = JSON.parseArray(execute.body());
      String predicted_labels = result.getJSONObject(0).getString("predicted_label");
      List<String> list = new ArrayList<>();
      list.add(predicted_labels);
      log.info("--------调用摄像头故障模型结束--------");
      return list;
   }
   
@@ -133,20 +136,14 @@
    * @param url 图片网络地址
    * @return
    */
   private static List<String> constructionWasteLoadModel(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 constructionWasteLoadModel(file);
      } catch (MalformedURLException e) {
         throw new RuntimeException(e);
      } catch (IOException e) {
         throw new RuntimeException(e);
      }
   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);
   }
   
   
@@ -164,7 +161,7 @@
      int status = execute.getStatus();
      if(200 != status){
         log.error("建筑垃圾装载模型调用失败:" + execute.body());
         return null;
         throw new RuntimeException("建筑垃圾装载模型调用失败:" + execute.body());
      }
      JSONObject result = JSON.parseObject(execute.body());
      String predicted_labels = result.getString("predicted_label");
@@ -186,22 +183,29 @@
    * @param modelEnum
    * @return
    */
   public static boolean modelCheck(String url, ImageModelEnum modelEnum){
   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 !(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 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 list3.contains("normalcy");
            return map;
      }
      return false;
      return null;
   }