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
package com.xindao.ocr.smartjavaai.model.common.direction.translator;
 
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.util.NDImageUtils;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.index.NDIndex;
import ai.djl.ndarray.types.Shape;
import ai.djl.translate.Batchifier;
import ai.djl.translate.Translator;
import ai.djl.translate.TranslatorContext;
import com.xindao.ocr.smartjavaai.entity.DirectionInfo;
 
import java.util.Arrays;
import java.util.List;
import java.util.Map;
 
/**
 * 方向检测
 *
 * @author Calvin
 * @mail 179209347@qq.com
 * @website www.aias.top
 */
public class PpWordRotateTranslator implements Translator<Image, DirectionInfo> {
    List<String> classes = Arrays.asList("No Rotate", "Rotate");
 
    private String batchifier;
 
    private int resizeHeight;
 
    private int resizeWidth;
 
    public PpWordRotateTranslator(Map<String, ?> arguments) {
        batchifier =  arguments.containsKey("batchifier")
                ? arguments.get("batchifier").toString()
                : "padding";
 
        resizeWidth =  arguments.containsKey("resizeWidth")
                ? (Integer) arguments.get("resizeWidth")
                : 192;
 
        resizeHeight =  arguments.containsKey("resizeHeight")
                ? (Integer) arguments.get("resizeHeight")
                : 48;
    }
 
    @Override
    public DirectionInfo processOutput(TranslatorContext ctx, NDList list) {
        NDArray prob = list.singletonOrThrow();
        float[] res = prob.toFloatArray();
        int maxIndex = 0;
        if (res[1] > res[0]) {
            maxIndex = 1;
        }
 
        return new DirectionInfo(classes.get(maxIndex), Double.valueOf(res[maxIndex]));
    }
 
//    public NDList processInput2(TranslatorContext ctx, Image input){
//        NDArray img = input.toNDArray(ctx.getNDManager());
//        img = NDImageUtils.resize(img, 192, 48);
//        img = NDImageUtils.toTensor(img).sub(0.5F).div(0.5F);
//        img = img.expandDims(0);
//        return new NDList(new NDArray[]{img});
//    }
 
    @Override
    public NDList processInput(TranslatorContext ctx, Image input) {
        NDArray img = input.toNDArray(ctx.getNDManager());
        int imgC = 3;
        int imgH = resizeHeight;
        int imgW = resizeWidth;
 
        NDArray array = ctx.getNDManager().zeros(new Shape(imgC, imgH, imgW));
 
        int h = input.getHeight();
        int w = input.getWidth();
        int resized_w = 0;
 
        float ratio = (float) w / (float) h;
        if (Math.ceil(imgH * ratio) > imgW) {
            resized_w = imgW;
        } else {
            resized_w = (int) (Math.ceil(imgH * ratio));
        }
 
        img = NDImageUtils.resize(img, resized_w, imgH);
 
        img = NDImageUtils.toTensor(img).sub(0.5F).div(0.5F);
        //    img = img.transpose(2, 0, 1);
 
        array.set(new NDIndex(":,:,0:" + resized_w), img);
 
//        array = array.expandDims(0);
 
        return new NDList(new NDArray[]{array});
    }
 
    @Override
    public Batchifier getBatchifier() {
        return Batchifier.fromString(batchifier);
    }
 
}