-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ZooObjectDetectionPredictor.java
99 lines (89 loc) · 4.16 KB
/
ZooObjectDetectionPredictor.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
99
/*
* 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 ai.djl.Application;
import ai.djl.MalformedModelException;
import ai.djl.inference.Predictor;
import ai.djl.modality.cv.Image;
import ai.djl.modality.cv.ImageFactory;
import ai.djl.modality.cv.output.DetectedObjects;
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 ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ZooObjectDetectionPredictor extends AbstractPredictor {
private static final Logger LOG = LoggerFactory.getLogger(ZooObjectDetectionPredictor.class);
private final ZooModel<Image, DetectedObjects> model;
public ZooObjectDetectionPredictor(String artifactId) throws ModelNotFoundException, MalformedModelException, IOException {
Criteria<Image, DetectedObjects> criteria = Criteria.builder()
.optApplication(Application.CV.OBJECT_DETECTION)
.setTypes(Image.class, DetectedObjects.class)
.optArtifactId(artifactId)
.optProgress(new ProgressBar())
.build();
this.model = ModelZoo.loadModel(criteria);
}
@Override
public void process(Exchange exchange) {
if (exchange.getIn().getBody() instanceof byte[]) {
byte[] bytes = exchange.getIn().getBody(byte[].class);
DetectedObjects result = classify(new ByteArrayInputStream(bytes));
exchange.getIn().setBody(result);
} else if (exchange.getIn().getBody() instanceof File) {
DetectedObjects result = classify(exchange.getIn().getBody(File.class));
exchange.getIn().setBody(result);
} else if (exchange.getIn().getBody() instanceof InputStream) {
DetectedObjects result = classify(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");
}
}
public DetectedObjects classify(Image image) {
try (Predictor<Image, DetectedObjects> predictor = model.newPredictor()) {
DetectedObjects detectedObjects = predictor.predict(image);
return detectedObjects;
} catch (TranslateException e) {
throw new RuntimeCamelException("Could not process input or output", e);
}
}
public DetectedObjects classify(File input) {
try (InputStream fileInputStream = new FileInputStream(input)) {
Image image = ImageFactory.getInstance().fromInputStream(fileInputStream);
return classify(image);
} catch (IOException e) {
LOG.error(FAILED_TO_TRANSFORM_MESSAGE);
throw new RuntimeCamelException(FAILED_TO_TRANSFORM_MESSAGE, e);
}
}
public DetectedObjects classify(InputStream input) {
try {
Image image = ImageFactory.getInstance().fromInputStream(input);
return classify(image);
} catch (IOException e) {
LOG.error(FAILED_TO_TRANSFORM_MESSAGE);
throw new RuntimeCamelException(FAILED_TO_TRANSFORM_MESSAGE, e);
}
}
}