Explorar el Código

新增 吸虫塔重新识别功能

zhaiyifei hace 10 meses
padre
commit
5b2554bb6b

+ 2 - 0
src/main/java/com/yunfeiyun/agmp/iot/common/domain/reqvo/IotCbdrecogAgainReqVo.java

@@ -29,4 +29,6 @@ public class IotCbdrecogAgainReqVo
     /** 设备标识 */
     private String devBid;
 
+    private String devtypeBid;
+
 }

+ 32 - 4
src/main/java/com/yunfeiyun/agmp/iot/common/service/YfCbdRecogeService.java

@@ -4,6 +4,7 @@ import cn.hutool.http.Header;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.iot.common.util.dev.XctToCbdUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
@@ -16,9 +17,12 @@ public class YfCbdRecogeService {
     private static String username = "软件事业部专用";
     private static String password = "2024@yfkj!@#$";
 
-    private static String recogeUrlByA = "http://8.136.98.49:8002/identify/9143";
+    private static String recogeUrlByCbdA = "http://8.136.98.49:8002/identify/9143";
 
-    private static String recogeUrlByB = "http://8.136.98.49:8002/identify/9153";
+    private static String recogeUrlByCbdB = "http://8.136.98.49:8002/identify/9153";
+
+//    private static String recogeUrlByXct = "https://web.hnyfwlw.com:58002/identify/20222";
+    private static String recogeUrlByXct = "http://8.136.98.49:8002/identify/20222";
 
     public byte[] getImgBytes(String imgUrl){
         return HttpUtil.downloadBytes(imgUrl);
@@ -34,11 +38,22 @@ public class YfCbdRecogeService {
         return jsonObject.getJSONObject("data");
     }
 
+    private JSONObject getXctRecogeResult(String content) {
+        JSONObject jsonObject = JSONObject.parseObject(content);
+        log.info("[云飞吸虫塔手动识别服务]: 识别结果:{}", jsonObject);
+        if (jsonObject.getIntValue("code") != 0){
+            log.error("[云飞吸虫塔手动识别服务]: 识别失败,错误信息:{}", jsonObject);
+            throw new RuntimeException("识别失败,请检查图片是否正确");
+        }
+        JSONObject resultData = jsonObject.getJSONObject("data");
+        return XctToCbdUtil.xctToCbdData(resultData);
+    }
+
     public JSONObject getRecogeResultByA(String imgUrl){
         String[] pathTemp = imgUrl.split("/");
         String fileName = pathTemp[pathTemp.length - 1];
         byte[] fileBytes = getImgBytes(imgUrl);
-        HttpRequest request = HttpUtil.createPost(recogeUrlByA);
+        HttpRequest request = HttpUtil.createPost(recogeUrlByCbdA);
         request.basicAuth(username, password);
         request.form("imageFile", fileBytes, fileName);
         String response = request.execute().body();
@@ -49,7 +64,7 @@ public class YfCbdRecogeService {
         String[] pathTemp = imgUrl.split("/");
         String fileName = pathTemp[pathTemp.length - 1];
         byte[] fileBytes = getImgBytes(imgUrl);
-        HttpRequest request = HttpUtil.createPost(recogeUrlByB);
+        HttpRequest request = HttpUtil.createPost(recogeUrlByCbdB);
         request.header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36");
         request.basicAuth(username, password);
         request.contentType("multipart/form-data");
@@ -58,6 +73,19 @@ public class YfCbdRecogeService {
         return getRecogeResult(response);
     }
 
+    public JSONObject getRecogeResultByXct(String imgUrl){
+        String[] pathTemp = imgUrl.split("/");
+        String fileName = pathTemp[pathTemp.length - 1];
+        byte[] fileBytes = getImgBytes(imgUrl);
+        HttpRequest request = HttpUtil.createPost(recogeUrlByXct);
+        request.header(Header.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36");
+        request.basicAuth(username, password);
+        request.contentType("multipart/form-data");
+        request.form("imageFile", fileBytes, fileName);
+        String response = request.execute().body();
+        return getXctRecogeResult(response);
+    }
+
 //    public static void main(String[] args) {
 //        YfCbdRecogeService service = new YfCbdRecogeService();
 //        JSONObject result = service.getRecogeResultByA("https://web.hnyfwlw.com:58003/Basics/cbd/864865062128484/2024/9/29/864865062128484-20240929-004951.jpg");

+ 101 - 0
src/main/java/com/yunfeiyun/agmp/iot/common/util/dev/XctToCbdUtil.java

@@ -0,0 +1,101 @@
+package com.yunfeiyun.agmp.iot.common.util.dev;
+
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.common.utils.StringUtils;
+
+import java.util.Map;
+
+public class XctToCbdUtil {
+    public static JSONObject xctToCbdData(JSONObject dataJson) {
+        JSONObject newJson = transXctPestPoint(dataJson);
+        return transXctDataToCbdData(newJson);
+    }
+
+    /**
+     * 由于吸虫塔和测报灯的害虫编号重复将吸虫塔中的虫子编号进行转化
+     * @param dataJson
+     * @return
+     */
+    private static JSONObject transXctPestPoint(JSONObject dataJson){
+        String cbdrecogMachinemark = dataJson.getString("label");
+        String cbdrecogResult = dataJson.getString("return_string");
+
+        JSONArray jsonArray = new JSONArray();
+        try{
+            if(StringUtils.isNotEmpty(cbdrecogMachinemark)){
+                jsonArray = JSONArray.parseArray(cbdrecogMachinemark);
+            }
+        }catch (Exception e){}
+
+        JSONArray transAfterData = new JSONArray();
+
+        int totalPestNum = 0;
+        for(int i = jsonArray.size()-1;i>=0;i--){
+            JSONObject returnStrItem = jsonArray.getJSONObject(i);
+            for(Map.Entry<String,Object> entry : returnStrItem.entrySet()){
+                String key = entry.getKey();
+                Object value = entry.getValue();
+                totalPestNum++;
+                if(!"4".equals(key)) {
+                    returnStrItem.remove(key);
+                    returnStrItem.put("xct"+key,value);
+                    transAfterData.add(returnStrItem);
+                }
+            }
+        }
+        String label = "";
+        if(!transAfterData.isEmpty()){
+            label = transAfterData.toJSONString();
+        }
+
+        for(int i = 1;i<=3;i++){
+            cbdrecogResult = cbdrecogResult.replaceFirst(i+",","xct"+i+",");
+        }
+        cbdrecogResult = cbdrecogResult.replaceAll("4,\\d+#|#4,\\d+","");
+
+        if(StringUtils.isEmpty(label) || StringUtils.isEmpty(cbdrecogResult)){
+            label = "";
+            cbdrecogResult = "";
+            totalPestNum = 0;
+        }
+
+        dataJson.put("label", label);
+        dataJson.put("return_string",cbdrecogResult);
+        dataJson.put("Result_c", totalPestNum);
+        return dataJson;
+    }
+
+    /**
+     * 将吸虫塔的数据格式转化成测报灯的数据格式
+     * @param jsonObject
+     * @return
+     */
+    private static JSONObject transXctDataToCbdData(JSONObject jsonObject) {
+        String return_string = jsonObject.getString("return_string");
+        if(StringUtils.isEmpty(return_string)){
+            return_string = "0";
+        }
+
+        String label = jsonObject.getString("label");
+        if(StringUtils.isEmpty(label)){
+            label = "0";
+        }
+
+        String Result_c = "0," + jsonObject.getString("Result_c");
+
+        JSONObject result = new JSONObject();
+        result.put("imei",jsonObject.get("device_id"));
+        result.put("Image",jsonObject.get("img"));
+        result.put("Result", return_string);
+        result.put("Result_image",jsonObject.get("img"));
+        result.put("Result_code", label);
+        result.put("Image_b","0");
+        result.put("Result_b","0");
+        result.put("Result_image_b","0");
+        result.put("Result_code_b","0");
+        result.put("Result_c", Result_c);
+        result.put("disable","0");
+        return result;
+    }
+}