diff --git a/code/crane/crane-api/crane-test/compressed.jpg b/code/crane/crane-api/crane-test/compressed.jpg new file mode 100644 index 00000000..1bce4f22 Binary files /dev/null and b/code/crane/crane-api/crane-test/compressed.jpg differ diff --git a/code/crane/crane-api/crane-test/src/main/java/com/crane/test/Test.java b/code/crane/crane-api/crane-test/src/main/java/com/crane/test/Test.java index 9b5b887b..ea08ab66 100644 --- a/code/crane/crane-api/crane-test/src/main/java/com/crane/test/Test.java +++ b/code/crane/crane-api/crane-test/src/main/java/com/crane/test/Test.java @@ -1,38 +1,88 @@ package com.crane.test; -import okhttp3.*; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; import java.util.Base64; +import javax.imageio.*; +import javax.imageio.stream.ImageOutputStream; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import okhttp3.*; + +public class Test { + + public static byte[] compressImageToTargetSize(BufferedImage image, float initialQuality, int maxSizeKB) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + float quality = initialQuality; + int maxBytes = maxSizeKB * 1024; + + while (quality > 0.05f) { + baos.reset(); + ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next(); + ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam(); + jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); + jpgWriteParam.setCompressionQuality(quality); // 0.0 ~ 1.0 + + ImageOutputStream ios = ImageIO.createImageOutputStream(baos); + jpgWriter.setOutput(ios); + jpgWriter.write(null, new IIOImage(image, null, null), jpgWriteParam); + jpgWriter.dispose(); + ios.close(); + + if (baos.size() <= maxBytes) { + break; + } + + quality -= 0.05f; + } + + return baos.toByteArray(); + } + + public static void saveCompressedImageToFile(byte[] imageBytes, String outputPath) throws IOException { + try (FileOutputStream fos = new FileOutputStream(outputPath)) { + fos.write(imageBytes); + } + System.out.println("✅ 压缩图像已保存到: " + outputPath); + } -public class Test{ public static void main(String[] args) throws IOException { String API_URL = "http://localhost:8866/ocr"; String imagePath = "D:\\workspace\\code\\crane\\crane-api\\crane-test\\src\\main\\java\\com\\crane\\test\\namorni-preprava-namorni-doprava-kamionova-doprava-kontejneru-2-1024x768.jpg"; + String compressedImagePath = "D:\\workspace\\code\\crane\\crane-api\\crane-test\\compressed.jpg"; - File file = new File(imagePath); long base64StartTime = System.nanoTime(); - byte[] fileContent = java.nio.file.Files.readAllBytes(file.toPath()); - String base64Image = Base64.getEncoder().encodeToString(fileContent); - long base64EndTime = System.nanoTime(); - long durationMs1 = (base64EndTime - base64StartTime) / 1_000_000; // 毫秒 + // 1. 加载原始图像 + BufferedImage originalImage = ImageIO.read(new File(imagePath)); - System.out.println("图片转 BASE64 耗时: " + durationMs1 + " ms"); + BufferedImage resizedImage = originalImage; // 保持原图尺寸不变 + + // 3. 压缩图片到小于 500KB + byte[] compressedBytes = compressImageToTargetSize(resizedImage, 1f, 5000); + System.out.println("压缩后图片大小: " + (compressedBytes.length / 1024) + " KB"); + + // 👉 保存压缩图像到本地 + saveCompressedImageToFile(compressedBytes, compressedImagePath); + + // 4. 转为 Base64 编码 + String base64Image = Base64.getEncoder().encodeToString(compressedBytes); + long base64EndTime = System.nanoTime(); + long durationMs1 = (base64EndTime - base64StartTime) / 1_000_000; + System.out.println("图片压缩 + BASE64 编码耗时: " + durationMs1 + " ms"); + + // 5. 构建请求 ObjectMapper objectMapper = new ObjectMapper(); ObjectNode payload = objectMapper.createObjectNode(); payload.put("file", base64Image); payload.put("fileType", 1); - OkHttpClient client = new OkHttpClient.Builder() - .connectTimeout(300, java.util.concurrent.TimeUnit.SECONDS) // 连接超时,建议20-30秒 - .writeTimeout(300, java.util.concurrent.TimeUnit.SECONDS) // 写请求体超时 - .readTimeout(300, java.util.concurrent.TimeUnit.SECONDS) // 读取响应超时,OCR服务响应较慢可设置长点 + .connectTimeout(300, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(300, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(300, java.util.concurrent.TimeUnit.SECONDS) .build(); MediaType JSON = MediaType.get("application/json; charset=utf-8"); @@ -44,26 +94,21 @@ public class Test{ .build(); long startTime = System.nanoTime(); - try (Response response = client.newCall(request).execute()) { long endTime = System.nanoTime(); - long durationMs = (endTime - startTime) / 1_000_000; // 毫秒 - + long durationMs = (endTime - startTime) / 1_000_000; System.out.println("⏱️ 请求耗时: " + durationMs + " ms"); if (response.isSuccessful()) { String responseBody = response.body().string(); - JsonNode root = objectMapper.readTree(responseBody); JsonNode result = root.get("result"); - JsonNode ocrResults = result.get("ocrResults"); for (int i = 0; i < ocrResults.size(); i++) { JsonNode item = ocrResults.get(i); JsonNode prunedResult = item.get("prunedResult"); System.out.println(prunedResult); - // 获取 rec_texts 并打印 JsonNode recTexts = prunedResult.get("rec_texts"); if (recTexts != null && recTexts.isArray()) { System.out.println("📦 rec_texts:"); diff --git a/code/crane/crane-api/ocr_result_0.jpg b/code/crane/crane-api/ocr_result_0.jpg deleted file mode 100644 index 43c931bd..00000000 Binary files a/code/crane/crane-api/ocr_result_0.jpg and /dev/null differ diff --git a/notes/后端开发/paddlepaddle/paddlenlp.md b/notes/后端开发/paddlepaddle/paddlenlp.md new file mode 100644 index 00000000..4a3020e2 --- /dev/null +++ b/notes/后端开发/paddlepaddle/paddlenlp.md @@ -0,0 +1,37 @@ +# 相关网站 + +- github:https://github.com/PaddlePaddle/PaddleNLP/tree/develop +- 官网: + + + + + + + +# 报错 + +## AttributeError: module 'h11' has no attribute 'Event'. Did you mean: '_events'? + +```bash + pip install "h11==0.14.0" --force-reinstall +``` + + + +## ImportError: cannot import name 'download' from 'aistudio_sdk.hub' + +```bash +pip install aistudio-sdk==0.2.6 +``` + + + +## ValueError: Unknown argument: show_log + +``` +pip install paddleocr==2.10 +``` + + + diff --git a/notes/后端开发/paddlepaddle/paddlex.md b/notes/后端开发/paddlepaddle/paddlex.md index 41abf896..04bb5b8c 100644 --- a/notes/后端开发/paddlepaddle/paddlex.md +++ b/notes/后端开发/paddlepaddle/paddlex.md @@ -29,7 +29,7 @@ PaddleX 3.0 是基于飞桨框架构建的低代码开发工具,它集成了 # CPU 版本 python -m pip install paddlepaddle==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/ -# GPU 版本,需显卡驱动程序版本 ≥450.80.02(Linux)或 ≥452.39(Windows) +# GPU 版本,需显卡驱动程序版本 ≥450.80.02(Linux)或 ≥452.39(Windows)(当前使用的是这个版本测试的) python -m pip install paddlepaddle-gpu==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/ # GPU 版本,需显卡驱动程序版本 ≥550.54.14(Linux)或 ≥550.54.14(Windows) diff --git a/notes/软件工具/标注工具.md b/notes/软件工具/标注工具.md index e3c07acf..d0b1f2b0 100644 --- a/notes/软件工具/标注工具.md +++ b/notes/软件工具/标注工具.md @@ -149,9 +149,41 @@ pip install pyinstaller pyrcc5 -o libs/resources.py resources.qrc # 打包可执行程序 -pyinstaller -c PPOCRLabel.py --collect-all paddleocr --collect-all pyclipper --collect-all imghdr --collect-all skimage --collect-all imgaug --collect-all scipy.io --collect-all lmdb --collect-all paddle --hidden-import=pyqt5 -p ./libs -p ./ -p ./data -p ./resources -F +pyinstaller -c PPOCRLabel.py --add-data "D:\\devtool/anaconda3\\envs\\ppocrlabel\\Lib\\site-packages\\paddlex;./paddlex" --collect-all paddleocr --collect-all pyclipper --collect-all imghdr --collect-all skimage --collect-all imgaug --collect-all scipy.io --collect-all lmdb --collect-all paddle --hidden-import=pyqt5 -p ./libs -p ./ -p ./data -p ./resources -F + +pyinstaller -F -w --clean --add-data "D:/devtool/anaconda3/envs/ppocrlabel/Lib/site-packages/paddlex;./paddlex" --collect-all paddleocr --collect-all pyclipper --collect-all imghdr --collect-all skimage --collect-all imgaug --collect-all scipy.io --collect-all lmdb --collect-all paddle --collect-submodules paddleocr --collect-submodules paddlex --collect-submodules paddlex.inference.pipelines --collect-submodules paddlex.utils --collect-submodules paddlex.visualize --collect-submodules paddlex.cv.models --hidden-import=pyqt5 --hidden-import=shapely --hidden-import=visualdl --hidden-import=skimage.metrics -p ./libs -p ./ -p ./data -p ./resources PPOCRLabel.py + + # 运行dist中的可执行程序,以windows为例 PPOCRLabel.exe --lang ch ``` +## 报错 + +### DLL load failed while importing aggregations + +**conda虚拟环境下**报这个错误,解决方案 + +```bash +conda install pandas +``` + +验证 + +```python +import pandas as pd +print(pd.__version__) +df = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) +print(df) +``` + +输出下面结果,代表成功 + +``` +2.3.0 + a b +0 1 3 +1 2 4 +``` +