|
32 | 32 | import java.util.*; |
33 | 33 |
|
34 | 34 | /** |
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. |
36 | 40 | * |
37 | 41 | */ |
38 | 42 | public class TensorflowObjectRecogniser implements Closeable { |
39 | 43 |
|
40 | | - private static final Logger LOG = LoggerFactory.getLogger(TensorflowObjectRecogniser.class); |
| 44 | +private static final Logger LOG = LoggerFactory.getLogger(TensorflowObjectRecogniser.class); |
41 | 45 |
|
42 | | - private ManagedChannel channel; |
43 | | - private InceptionBlockingStub stub; |
| 46 | +private ManagedChannel channel; |
| 47 | +private InceptionBlockingStub stub; |
44 | 48 |
|
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 | +} |
58 | 59 |
|
59 | | - public List<Map.Entry<String, Double>> recognise(InputStream stream) throws Exception { |
| 60 | +public List<Map.Entry<String, Double>> recognise(InputStream stream) throws Exception { |
60 | 61 |
|
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 | +} |
80 | 79 |
|
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 | +} |
88 | 87 | } |
0 commit comments