An advantage of BarcodeSink is that it makes it easy to add custom computer vision to a system using Cognex Dataman devices. This tutorial shows you how to send your images to Roboflow. If you've got an old Cognex device laying around or you want to update your computer vision software without changing out your facility's existing hardware, read on.
public class CameraServerRoboflow extends CameraServer {
private String roboflowKey;
private URI roboflowLocation;
public CameraServerRoboflow(InetSocketAddress address, URI roboflowLocation, String roboflowKey) throws IOException {
super(address);
this.roboflowLocation = roboflowLocation;
this.roboflowKey = roboflowKey;
}
@Override
protected void onMessage(CameraInfo connection, CameraScanResult result) {
sendImageToRoboflow(result);
}
private void sendImageToRoboflow(CameraScanResult result) {
try {
String writerName = "BMP";
ByteArrayOutputStream output = new ByteArrayOutputStream();
boolean isWriter = ImageIO.write(result.getImage(), writerName, output);
String imageString = Base64.getEncoder().encodeToString(output.toByteArray());
JSONObject jsonImage = new JSONObject();
jsonImage.put("type", "base64");
jsonImage.put("value", imageString);
JSONObject jsonInputs = new JSONObject();
jsonInputs.put("image", jsonImage);
JSONObject jsonRoot = new JSONObject();
jsonRoot.put("inputs", jsonInputs);
jsonRoot.put("api_key", roboflowKey);
HttpClient client = HttpClient.newBuilder()
.version(Version.HTTP_1_1)
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(roboflowLocation)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRoot.toString(), StandardCharsets.UTF_8))
.timeout(Duration.ofMinutes(1))
.build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
} catch (IOException e) {
System.out.println(e);
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
That's it. With Roboflow set up we can now succesfully send data.
{
"outputs": [
{
"model_predictions": {
"image": {
"width": 1024,
"height": 768
},
"predictions": [
{
"width": 613.0,
"height": 362.0,
"x": 716.5,
"y": 306.0,
"confidence": 0.5204567909240723,
"class_id": 73,
"class": "book",
"detection_id": "e4a035a4-4a35-40ca-aa21-5d5654aee480",
"parent_id": "image"
}
]
}
}
],
"profiler_trace": []
}
If you want to repurpose existing Cognex Dataman devices to use Roboflow, or any other computer vision system, without changing out hardware, BarcodeSink can help.