u
This commit is contained in:
1 parent
ce466130c0
commit
9c026f00ad
36 files changed
+2230
-1036
No files matched your search
@@ -0,0 +1,84 @@
|
||||
package cn.g3soft;
|
||||
|
||||
|
||||
import cn.g3soft.utils.HttpUtils;
|
||||
import cn.g3soft.utils.Result;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@SpringBootTest
|
||||
public class DEMO {
|
||||
@Test
|
||||
public void testHello() throws Exception {
|
||||
long startTime = System.nanoTime(); // 开始时间
|
||||
|
||||
String filePath = "D:\\100全关门-3\\20241005082220A664.jpg"; // 你的图片路径
|
||||
|
||||
byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
|
||||
|
||||
// String fileUrl = Base64.getEncoder().encodeToString(fileBytes);
|
||||
|
||||
String fileUrl = "http://g3hd.oss-cn-hangzhou.aliyuncs.com/g3hd/2026/03/28/1774665878210_960.jpg";
|
||||
|
||||
String requestUrl = "http://127.0.0.1:8867/ocr";
|
||||
|
||||
Map<String, Object> reqPayload = new HashMap<>();
|
||||
reqPayload.put("file", fileUrl);
|
||||
reqPayload.put("fileType", 1);
|
||||
reqPayload.put("visualize", false);
|
||||
|
||||
try {
|
||||
String response = HttpUtils.postJson(requestUrl, reqPayload);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode root = mapper.readTree(response);
|
||||
|
||||
JsonNode ocrResults = root.path("result").path("ocrResults");
|
||||
if (!ocrResults.isArray() || ocrResults.isEmpty()) {
|
||||
System.out.println("空");
|
||||
}
|
||||
|
||||
String number = "";
|
||||
double maxScore = -1.0;
|
||||
|
||||
for (JsonNode item : ocrResults) {
|
||||
JsonNode pruned = item.path("prunedResult");
|
||||
JsonNode recTexts = pruned.path("rec_texts");
|
||||
JsonNode recScores = pruned.path("rec_scores");
|
||||
|
||||
for (int i = 0; i < recTexts.size(); i++) {
|
||||
double score = recScores.get(i).asDouble();
|
||||
if (score > maxScore) {
|
||||
maxScore = score;
|
||||
number = recTexts.get(i).asText();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("boxNumber", number);
|
||||
result.put("score", maxScore);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
long endTime = System.nanoTime(); // 结束时间
|
||||
long durationMs = (endTime - startTime) / 1_000_000;
|
||||
|
||||
System.out.println("耗时: " + durationMs + " ms");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user