zouyu
2025-09-26 3fbbfcc8f509c352c58dc8a126220b49b72ed5a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package com.xindao.ocr.smartjavaai.model.common.direction;
 
import ai.djl.MalformedModelException;
import ai.djl.engine.Engine;
import ai.djl.inference.Predictor;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.ndarray.NDManager;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
import ai.djl.repository.zoo.ModelZoo;
import ai.djl.repository.zoo.ZooModel;
import cn.smartjavaai.common.pool.PredictorFactory;
import cn.smartjavaai.common.utils.FileUtils;
import cn.smartjavaai.common.utils.ImageUtils;
import cn.smartjavaai.common.utils.OpenCVUtils;
import com.xindao.ocr.smartjavaai.config.DirectionModelConfig;
import com.xindao.ocr.smartjavaai.entity.DirectionInfo;
import com.xindao.ocr.smartjavaai.entity.OcrBox;
import com.xindao.ocr.smartjavaai.entity.OcrItem;
import com.xindao.ocr.smartjavaai.enums.AngleEnum;
import com.xindao.ocr.smartjavaai.exception.OcrException;
import com.xindao.ocr.smartjavaai.model.common.detect.OcrCommonDetModel;
import com.xindao.ocr.smartjavaai.model.common.direction.criteria.DirectionCriteriaFactory;
import com.xindao.ocr.smartjavaai.utils.OcrUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.opencv.core.Mat;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * PPOCRMobileV2Model 方向分类模型
 * @author dwj
 * @date 2025/4/21
 */
@Slf4j
public class PPOCRMobileV2ClsModel implements OcrDirectionModel {
 
 
    private GenericObjectPool<Predictor<Image, DirectionInfo>> predictorPool;
 
    private DirectionModelConfig config;
 
    private ZooModel<Image, DirectionInfo> model;
 
    private OcrCommonDetModel textDetModel;
 
 
    @Override
    public void loadModel(DirectionModelConfig config){
        if(StringUtils.isBlank(config.getModelPath())){
            throw new OcrException("modelPath is null");
        }
        this.config = config;
        this.textDetModel = config.getTextDetModel();
        ConcurrentHashMap params = new ConcurrentHashMap<String, String>();
        if(StringUtils.isNotBlank(config.getBatchifier())){
            params.put("batchifier", config.getBatchifier());
        }
        Criteria<Image, DirectionInfo> criteria = DirectionCriteriaFactory.createCriteria(config);
        try{
            model = ModelZoo.loadModel(criteria);
            // 创建池子:每个线程独享 Predictor
            this.predictorPool = new GenericObjectPool<>(new PredictorFactory<>(model));
            int predictorPoolSize = config.getPredictorPoolSize();
            if(config.getPredictorPoolSize() <= 0){
                predictorPoolSize = Runtime.getRuntime().availableProcessors(); // 默认等于CPU核心数
            }
            predictorPool.setMaxTotal(predictorPoolSize);
            log.debug("当前设备: " + model.getNDManager().getDevice());
            log.debug("当前引擎: " + Engine.getInstance().getEngineName());
            log.debug("模型推理器线程池最大数量: " + predictorPoolSize);
        } catch (IOException | ModelNotFoundException | MalformedModelException e) {
            throw new OcrException("模型加载失败", e);
        }
    }
 
    @Override
    public List<OcrItem> detect(String imagePath){
        if(!FileUtils.isFileExists(imagePath)){
            throw new OcrException("图像文件不存在");
        }
        Image img = null;
        try {
            img = ImageFactory.getInstance().fromFile(Paths.get(imagePath));
            return detect(img);
        } catch (IOException e) {
            throw new OcrException("无效的图片", e);
        }finally {
            if(img != null){
                ((Mat)img.getWrappedImage()).release();
            }
        }
    }
 
 
    @Override
    public List<OcrItem> detect(Image image){
        if(Objects.isNull(textDetModel)){
            throw new OcrException("textDetModel is null");
        }
        //检测文本
        List<OcrBox> boxeList = textDetModel.detect(image);
        if(Objects.isNull(boxeList) || boxeList.isEmpty()){
            throw new OcrException("未检测到文本");
        }
        Mat srcMat = (Mat) image.getWrappedImage();
        return detect(boxeList, srcMat);
    }
 
 
//    /**
//     * 基于文本框检测方向
//     * @param box
//     * @param srcMat
//     * @param predictor
//     * @param manager
//     * @return
//     */
//    private OcrItem detect(OcrBox box, Mat srcMat, Predictor<Image, DirectionInfo> predictor, NDManager manager){
//        if(Objects.isNull(box)){
//            throw new OcrException("box参数为空");
//        }
//        try {
//            //透视变换及裁剪
//            Image subImg = OcrUtils.transformAndCrop(srcMat, box);
//            DirectionInfo directionInfo = null;
//            String angle;
//            //高宽比 > 1.5 纵向
//            if (subImg.getHeight() * 1.0 / subImg.getWidth() > 1.5) {
//                //旋转图片90度
//                subImg = OcrUtils.rotateImg(manager, subImg);
//                //检测方向
//                directionInfo = predictor.predict(subImg);
//                if (directionInfo.getName().equalsIgnoreCase("Rotate")) {
//                    angle = "270";
//                } else {
//                    angle = "90";
//                }
//            }else{ //横向
//                directionInfo = predictor.predict(subImg);
//                if (directionInfo.getName().equalsIgnoreCase("No Rotate")) {
//                    angle = "0";
//                } else {
//                    angle = "180";
//                }
//            }
//            ((Mat)subImg.getWrappedImage()).release();
//            return new OcrItem(box, AngleEnum.fromValue(angle), directionInfo.getProb().floatValue());
//        } catch (Exception e) {
//            throw new OcrException("OCR检测错误", e);
//        }
//    }
 
    @Override
    public List<OcrItem> detect(List<OcrBox> boxList,Mat srcMat){
        if(Objects.isNull(boxList) || boxList.isEmpty()){
            throw new OcrException("boxList为空");
        }
        List<List<OcrItem>> ocrItemList = batchDetect(Collections.singletonList(boxList), Collections.singletonList(srcMat));
        if(Objects.isNull(ocrItemList) || ocrItemList.isEmpty()){
            throw new OcrException("方向检测失败");
        }
        return ocrItemList.get(0);
    }
 
    @Override
    public void detectAndDraw(String imagePath, String outputPath) {
        if(!FileUtils.isFileExists(imagePath)){
            throw new OcrException("图像文件不存在");
        }
        Image img = null;
        try {
            img = ImageFactory.getInstance().fromFile(Paths.get(imagePath));
            List<OcrItem> itemList = detect(img);
            if(Objects.isNull(itemList) || itemList.isEmpty()){
                throw new OcrException("未检测到文字");
            }
            OcrUtils.drawRectWithText((Mat) img.getWrappedImage(), itemList);
            Path output = Paths.get(outputPath);
            log.debug("Saving to {}", output.toAbsolutePath().toString());
            img.save(Files.newOutputStream(output), "png");
        } catch (IOException e) {
            throw new OcrException(e);
        } finally {
            if (img != null){
                ((Mat)img.getWrappedImage()).release();
            }
        }
    }
 
    @Override
    public List<OcrItem> detect(BufferedImage image) {
        if(!ImageUtils.isImageValid(image)){
            throw new OcrException("图像无效");
        }
        Image img = ImageFactory.getInstance().fromImage(OpenCVUtils.image2Mat(image));
        List<OcrItem> ocrItemList = detect(img);
        ((Mat)img.getWrappedImage()).release();
        return ocrItemList;
    }
 
    @Override
    public List<OcrItem> detect(byte[] imageData) {
        if(Objects.isNull(imageData)){
            throw new OcrException("图像无效");
        }
        try {
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));
            return detect(image);
        } catch (IOException e) {
            throw new OcrException("错误的图像", e);
        }
    }
 
    @Override
    public BufferedImage detectAndDraw(BufferedImage sourceImage) {
        if(!ImageUtils.isImageValid(sourceImage)){
            throw new OcrException("图像无效");
        }
        Image img = ImageFactory.getInstance().fromImage(OpenCVUtils.image2Mat(sourceImage));
        List<OcrItem> ocrItemList = detect(img);
        if(Objects.isNull(ocrItemList) || ocrItemList.isEmpty()){
            throw new OcrException("未检测到文字");
        }
        OcrUtils.drawRectWithText((Mat) img.getWrappedImage(), ocrItemList);
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            // 调用 save 方法将 Image 写入字节流
            img.save(outputStream, "png");
            // 将字节流转换为 BufferedImage
            byte[] imageBytes = outputStream.toByteArray();
            return ImageIO.read(new ByteArrayInputStream(imageBytes));
        } catch (IOException e) {
            throw new OcrException("导出图片失败", e);
        } finally {
            if (img != null){
                ((Mat) img.getWrappedImage()).release();
            }
        }
    }
 
    @Override
    public List<List<OcrItem>> batchDetect(List<List<OcrBox>> boxList, List<Mat> srcMatList) {
        if(CollectionUtils.isEmpty(boxList)){
            throw new OcrException("boxList 不能为空");
        }
        if(CollectionUtils.isEmpty(srcMatList)){
            throw new OcrException("srcMatList 不能为空");
        }
        //检查参数
        for (int i = 0; i < srcMatList.size(); i++) {
            List<OcrBox> ocrBoxes = boxList.get(i);
            Mat mat = srcMatList.get(i);
            if (ocrBoxes == null) {
                throw new OcrException("第 " + i + " 个 boxList 为 null");
            }
            if (ocrBoxes.isEmpty()) {
                throw new OcrException("第 " + i + " 个 boxList 没有检测结果");
            }
            if (mat.empty()) {
                throw new OcrException("第 " + i + " 张图片为空 Mat");
            }
        }
        List<Image> imageList = new ArrayList<Image>();
        List<Boolean> isRotatedList = new ArrayList<Boolean>();
        int index = 0;
        try (NDManager manager = model.getNDManager().newSubManager()){
            for(int i = 0; i < srcMatList.size(); i++){
                for (int j = 0; j < boxList.get(i).size(); j++){
                    //透视变换及裁剪
                    Image subImg = OcrUtils.transformAndCrop(srcMatList.get(i), boxList.get(i).get(j));
                    //高宽比 > 1.5 纵向
                    if (subImg.getHeight() * 1.0 / subImg.getWidth() > 1.5) {
                        //旋转图片90度
                        subImg = OcrUtils.rotateImg(manager, subImg);
                        isRotatedList.add(true);
                        imageList.add(subImg);
                    }else{
                        isRotatedList.add(false);
                        imageList.add(subImg);
                    }
                    index++;
                }
            }
            List<List<OcrItem>> result = new ArrayList<>();
            List<DirectionInfo> directionInfos = batchDetect(imageList);
            if(CollectionUtils.isEmpty(directionInfos)){
                throw new OcrException("方向检测失败");
            }
            index = 0;
            for(int i = 0; i < srcMatList.size(); i++){
                List<OcrItem> ocrItemList = new ArrayList<>();
                for (int j = 0; j < boxList.get(i).size(); j++){
                    DirectionInfo directionInfo = directionInfos.get(index);
                    if(Objects.isNull(directionInfo)){
                        throw new OcrException("方向检测失败: 第" + i + "张图片, 第" + j + "个文本块,未检测到方向");
                    }
                    String angle;
                    if(isRotatedList.get(index)){
                        if (directionInfo.getName().equalsIgnoreCase("Rotate")) {
                            angle = "270";
                        } else {
                            angle = "90";
                        }
                    }else{
                        if (directionInfo.getName().equalsIgnoreCase("No Rotate")) {
                            angle = "0";
                        } else {
                            angle = "180";
                        }
                    }
                    OcrItem ocrItem = new OcrItem(boxList.get(i).get(j), AngleEnum.fromValue(angle), directionInfo.getProb().floatValue());
                    ocrItemList.add(ocrItem);
                    index++;
                }
                result.add(ocrItemList);
            }
            return result;
        }
    }
 
    private List<DirectionInfo> batchDetect(List<Image> imageList) {
        Predictor<Image, DirectionInfo> predictor = null;
        try {
            predictor = predictorPool.borrowObject();
            return predictor.batchPredict(imageList);
        } catch (Exception e) {
            throw new OcrException("OCR检测错误", e);
        }finally {
            if (predictor != null) {
                try {
                    predictorPool.returnObject(predictor); //归还
                } catch (Exception e) {
                    log.warn("归还Predictor失败", e);
                    try {
                        predictor.close(); // 归还失败才销毁
                    } catch (Exception ex) {
                        log.error("关闭Predictor失败", ex);
                    }
                }
            }
        }
    }
 
    @Override
    public void setTextDetModel(OcrCommonDetModel detModel) {
        this.textDetModel = detModel;
    }
 
    @Override
    public OcrCommonDetModel getTextDetModel() {
        return textDetModel;
    }
 
    @Override
    public GenericObjectPool<Predictor<Image, DirectionInfo>> getPool() {
        return predictorPool;
    }
 
    @Override
    public void close() throws Exception {
        try {
            if (predictorPool != null) {
                predictorPool.close();
            }
        } catch (Exception e) {
            log.warn("关闭 predictorPool 失败", e);
        }
        try {
            if (model != null) {
                model.close();
            }
        } catch (Exception e) {
            log.warn("关闭 model 失败", e);
        }
    }
}