xiongzhu %!s(int64=3) %!d(string=hai) anos
pai
achega
810e8db791

+ 4 - 6
src/main/java/com/izouma/yags/service/RoomService.java

@@ -290,18 +290,16 @@ public class RoomService {
             try {
                 ByteArrayOutputStream os = new ByteArrayOutputStream();
                 ImageIO.write(ImageIO.read(new URL(screenShot)), format, os);
-                res = RecognizeUtil.recognize(Base64.getEncoder().encodeToString(os.toByteArray()));
+                res = RecognizeUtil.recognize1(os.toByteArray());
             } catch (Exception e) {
+                e.printStackTrace();
                 throw new BusinessException("图片识别失败");
             }
-            if (StringUtils.isNotEmpty(res.getString("error_msg"))) {
-                throw new BusinessException(res.getString("error_msg"));
-            }
             JSONArray results = res.getJSONArray("results");
             for (int i = 0; i < results.size(); i++) {
                 JSONObject json = results.getJSONObject(i);
-                String name = json.getString("name");
-                double score = json.getDouble("score");
+                String name = json.getString("label");
+                double score = json.getDouble("confidence");
                 if ("胜利".equals(name) && score > 0.8) {
                     win = true;
                 }

+ 9 - 0
src/main/java/com/izouma/yags/utils/RecognizeUtil.java

@@ -43,6 +43,15 @@ public class RecognizeUtil {
         return JSON.parseObject(res);
     }
 
+    public static JSONObject recognize1(byte[] bytes) {
+        JSONObject res = JSON.parseObject(HttpRequest.post("http://120.77.252.240:9999/?threshold=0.4")
+                .contentType("raw")
+                .send(bytes)
+                .body());
+        log.info("recognize result={}", res);
+        return res;
+    }
+
     public static void main(String[] args) throws IOException {
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         ImageIO.write(ImageIO.read(new URL("https://cdn.raex.vip/image/2022-08-08-14-51-42lexfxYDl.jpg")), "jpg", os);

+ 13 - 0
src/test/java/com/izouma/yags/CommonTest.java

@@ -3,16 +3,20 @@ package com.izouma.yags;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.github.kevinsawicki.http.HttpRequest;
 import com.izouma.yags.dto.RaexUser;
 import com.izouma.yags.exception.BusinessException;
 import com.izouma.yags.utils.RaexUtils;
 import com.izouma.yags.utils.RecognizeUtil;
+import org.apache.commons.io.FileUtils;
 import org.junit.jupiter.api.Test;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.password.PasswordEncoder;
 
 import javax.imageio.ImageIO;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
 import java.net.URL;
 import java.util.Base64;
 import java.util.HashMap;
@@ -93,4 +97,13 @@ public class CommonTest {
         List<RaexUser> users = JSON.parseArray(res.getJSONArray("users").toJSONString(), RaexUser.class);
         System.out.println(JSON.toJSONString(users, true));
     }
+
+    @Test
+    public void testPyRec() throws IOException {
+        JSONObject res = JSON.parseObject(HttpRequest.post("http://120.77.252.240:9999/?threshold=0.4")
+                .contentType("raw")
+                .send(FileUtils.readFileToByteArray(new File("/Users/drew/Desktop/WechatIMG3615.jpeg")))
+                .body());
+        System.out.println(JSON.toJSONString(res, true));
+    }
 }