ImageClassificationWorkItemHandler extends AbstractLogOrThrowWorkItemHandler implements Cacheable { public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { try { InputStream imageIS = null; Object inputImage = workItem.getParameter(PARAM_IMAGE); if (inputImage instanceof InputStream) { imageIS = (InputStream) inputImage; } else if (inputImage instanceof File) { imageIS = new FileInputStream((File) inputImage); } else if (inputImage instanceof URL) { imageIS = ((URL) inputImage).openStream(); } else { throw new IllegalArgumentException("Input Parameter " + PARAM_IMAGE + " has an invalid type."); } Map<String, Object> results = new HashMap<String, Object>(); Map<String, Double> predictions = classifier.classifyImage(imageIS); results.put(RESULT_PREDICTIONS, predictions); results.put(RESULT_PREDICTION, getPrediction(predictions)); manager.completeWorkItem(workItem.getId(), results); } catch (Throwable cause) { System.out.println("Error running WIH: " + cause.getMessage()); handleException(cause); } } ImageClassificationWorkItemHandler(ClassLoader cl, String model, String labels, int width, int height, int channels);  ImageClassificationWorkItemHandler(File model, String labels, int width, int height, int channels);  ImageClassificationWorkItemHandler(URL model, String labels, int width, int height, int channels);  ImageClassificationWorkItemHandler(InputStream model, String labels, int width, int height, int channels);  ImageClassificationWorkItemHandler(InputStream model, String labels, int width, int height, int channels, DataNormalization normalization);  ImageClassificationWorkItemHandler(); private  ImageClassificationWorkItemHandler(InputStream model, List<String> labels, int width, int height,
			int channels, DataNormalization normalization); void executeWorkItem(WorkItem workItem, WorkItemManager manager); @Override void abortWorkItem(WorkItem workItem, WorkItemManager manager); @Override void close(); final static String PARAM_IMAGE; final static String RESULT_PREDICTIONS; static final String RESULT_PREDICTION; }
@Test public void testHandler() throws Exception { WorkItemImpl workItem = new WorkItemImpl(); TestWorkItemManager manager = new TestWorkItemManager(); ImageClassificationWorkItemHandler handler = new ImageClassificationWorkItemHandler(); InputStream testImageIS = ImageClassificationWorkItemHandlerTest.class.getResourceAsStream("/my_bike.jpg"); workItem.setParameter(ImageClassificationWorkItemHandler.PARAM_IMAGE, testImageIS); handler.executeWorkItem(workItem, manager); assertNotNull(manager.getResults()); assertEquals(1, manager.getResults().size()); assertEquals("mountain_bike", manager.getResults().get(workItem.getId()).get("prediction")); assertTrue(manager.getResults().containsKey(workItem.getId())); }