-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
CustomImageClassificationPredictor.java
98 lines (88 loc) · 4.46 KB
/
CustomImageClassificationPredictor.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.djl.model;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import ai.djl.Model;
import ai.djl.inference.Predictor;
import ai.djl.modality.Classifications;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CustomImageClassificationPredictor extends AbstractPredictor {
private static final Logger LOG = LoggerFactory.getLogger(CustomImageClassificationPredictor.class);
private final String modelName;
private final String translatorName;
public CustomImageClassificationPredictor(String modelName, String translatorName) {
this.modelName = modelName;
this.translatorName = translatorName;
}
@Override
public void process(Exchange exchange) throws Exception {
Model model = exchange.getContext().getRegistry().lookupByNameAndType(modelName, Model.class);
Translator translator = exchange.getContext().getRegistry().lookupByNameAndType(translatorName, Translator.class);
if (exchange.getIn().getBody() instanceof byte[]) {
byte[] bytes = exchange.getIn().getBody(byte[].class);
Map<String, Float> result = classify(model, translator, new ByteArrayInputStream(bytes));
exchange.getIn().setBody(result);
} else if (exchange.getIn().getBody() instanceof File) {
Map<String, Float> result = classify(model, translator, exchange.getIn().getBody(File.class));
exchange.getIn().setBody(result);
} else if (exchange.getIn().getBody() instanceof InputStream) {
Map<String, Float> result = classify(model, translator, exchange.getIn().getBody(InputStream.class));
exchange.getIn().setBody(result);
} else {
throw new RuntimeCamelException("Data type is not supported. Body should be byte[], InputStream or File");
}
}
private Map<String, Float> classify(Model model, Translator translator, File input) {
try (InputStream fileInputStream = new FileInputStream(input)) {
Image image = ImageFactory.getInstance().fromInputStream(fileInputStream);
return classify(model, translator, image);
} catch (IOException e) {
LOG.error(FAILED_TO_TRANSFORM_MESSAGE);
throw new RuntimeCamelException(FAILED_TO_TRANSFORM_MESSAGE, e);
}
}
private Map<String, Float> classify(Model model, Translator translator, InputStream input) {
try {
Image image = ImageFactory.getInstance().fromInputStream(input);
return classify(model, translator, image);
} catch (IOException e) {
LOG.error(FAILED_TO_TRANSFORM_MESSAGE);
throw new RuntimeCamelException(FAILED_TO_TRANSFORM_MESSAGE, e);
}
}
private Map<String, Float> classify(Model model, Translator translator, Image image) {
try (Predictor<Image, Classifications> predictor = model.newPredictor(translator)) {
Classifications classifications = predictor.predict(image);
List<Classifications.Classification> list = classifications.items();
return list.stream()
.collect(Collectors.toMap(Classifications.Classification::getClassName, x -> (float) x.getProbability()));
} catch (TranslateException e) {
LOG.error("Could not process input or output", e);
throw new RuntimeCamelException("Could not process input or output", e);
}
}
}