Skip to content

Commit 00e0b76

Browse files
committed
Added Javadoc
1 parent 5de86b9 commit 00e0b76

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

src/main/java/com/github/megachucky/kafka/streams/machinelearning/TensorflowObjectRecogniser.java

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,57 +32,56 @@
3232
import java.util.*;
3333

3434
/**
35-
* This class offers image recognition implementation.
35+
* This class offers image recognition implementation using gRCP communication
36+
* with TensorFlow Serving server where the Neural Network for image recognition
37+
* is deployed.
38+
*
39+
* Uses the gRPC stub and generated classes for RPC communication.
3640
*
3741
*/
3842
public class TensorflowObjectRecogniser implements Closeable {
3943

40-
private static final Logger LOG = LoggerFactory.getLogger(TensorflowObjectRecogniser.class);
44+
private static final Logger LOG = LoggerFactory.getLogger(TensorflowObjectRecogniser.class);
4145

42-
private ManagedChannel channel;
43-
private InceptionBlockingStub stub;
46+
private ManagedChannel channel;
47+
private InceptionBlockingStub stub;
4448

45-
public TensorflowObjectRecogniser(String host, int port) {
46-
LOG.debug("Creating channel host:{}, port={}", host, port);
47-
try {
48-
channel = NettyChannelBuilder
49-
.forAddress(host, port)
50-
.usePlaintext(true)
51-
.build();
52-
stub = new InceptionBlockingStub(channel);
53-
//TODO: test channel here with a sample image
54-
} catch (Exception e) {
55-
throw new RuntimeException(e);
56-
}
57-
}
49+
public TensorflowObjectRecogniser(String host, int port) {
50+
LOG.debug("Creating channel host:{}, port={}", host, port);
51+
try {
52+
channel = NettyChannelBuilder.forAddress(host, port).usePlaintext(true).build();
53+
stub = new InceptionBlockingStub(channel);
54+
// TODO: test channel here with a sample image
55+
} catch (Exception e) {
56+
throw new RuntimeException(e);
57+
}
58+
}
5859

59-
public List<Map.Entry<String, Double>> recognise(InputStream stream) throws Exception {
60+
public List<Map.Entry<String, Double>> recognise(InputStream stream) throws Exception {
6061

61-
List<Map.Entry<String, Double>> objects = new ArrayList<>();
62-
ByteString jpegData = ByteString.readFrom(stream);
63-
InceptionRequest request = InceptionRequest.newBuilder()
64-
.setJpegEncoded(jpegData)
65-
.build();
66-
long st = System.currentTimeMillis();
67-
InceptionResponse response = stub.classify(request);
68-
long timeTaken = System.currentTimeMillis() - st;
69-
LOG.debug("Time taken : {}ms", timeTaken);
70-
Iterator<String> classes = response.getClassesList().iterator();
71-
Iterator<Float> scores = response.getScoresList().iterator();
72-
while (classes.hasNext() && scores.hasNext()){
73-
String className = classes.next();
74-
Float score = scores.next();
75-
Map.Entry<String, Double>object = new AbstractMap.SimpleEntry<>(className, score.doubleValue());
76-
objects.add(object);
77-
}
78-
return objects;
79-
}
62+
List<Map.Entry<String, Double>> objects = new ArrayList<>();
63+
ByteString jpegData = ByteString.readFrom(stream);
64+
InceptionRequest request = InceptionRequest.newBuilder().setJpegEncoded(jpegData).build();
65+
long st = System.currentTimeMillis();
66+
InceptionResponse response = stub.classify(request);
67+
long timeTaken = System.currentTimeMillis() - st;
68+
LOG.debug("Time taken : {}ms", timeTaken);
69+
Iterator<String> classes = response.getClassesList().iterator();
70+
Iterator<Float> scores = response.getScoresList().iterator();
71+
while (classes.hasNext() && scores.hasNext()) {
72+
String className = classes.next();
73+
Float score = scores.next();
74+
Map.Entry<String, Double> object = new AbstractMap.SimpleEntry<>(className, score.doubleValue());
75+
objects.add(object);
76+
}
77+
return objects;
78+
}
8079

81-
@Override
82-
public void close() throws IOException {
83-
if (channel != null){
84-
LOG.debug("Closing the channel ");
85-
channel.shutdownNow();
86-
}
87-
}
80+
@Override
81+
public void close() throws IOException {
82+
if (channel != null) {
83+
LOG.debug("Closing the channel ");
84+
channel.shutdownNow();
85+
}
86+
}
8887
}

0 commit comments

Comments
 (0)