2018-02-21 19:21:59 -05:00
|
|
|
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
|
2018-02-15 12:37:38 -05:00
|
|
|
|
2018-02-21 19:21:59 -05:00
|
|
|
Licensed 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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-02-23 09:26:15 -05:00
|
|
|
import java.awt.AWTException;
|
2018-02-23 19:48:16 -05:00
|
|
|
import java.awt.Graphics2D;
|
2018-02-22 09:28:21 -05:00
|
|
|
import java.awt.Point;
|
2018-02-23 09:26:15 -05:00
|
|
|
import java.awt.Rectangle;
|
|
|
|
import java.awt.Robot;
|
2018-02-21 19:21:59 -05:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.awt.image.DataBufferByte;
|
2018-02-15 07:46:02 -05:00
|
|
|
import java.io.File;
|
2018-02-21 13:26:20 -05:00
|
|
|
import java.io.IOException;
|
2018-02-21 19:21:59 -05:00
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2018-02-21 13:26:20 -05:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
2018-02-22 09:28:21 -05:00
|
|
|
import java.util.ArrayList;
|
2018-02-23 09:26:15 -05:00
|
|
|
import java.util.HashMap;
|
2018-02-21 19:21:59 -05:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import javax.imageio.ImageIO;
|
2018-02-23 16:43:57 -05:00
|
|
|
|
2018-02-24 10:49:49 -05:00
|
|
|
import org.opencv.core.Core;
|
2018-02-23 17:04:28 -05:00
|
|
|
import org.opencv.core.Rect2d;
|
2018-02-21 13:26:20 -05:00
|
|
|
import org.tensorflow.SavedModelBundle;
|
|
|
|
import org.tensorflow.Tensor;
|
|
|
|
import org.tensorflow.types.UInt8;
|
2018-02-15 07:46:02 -05:00
|
|
|
|
2018-02-21 19:21:59 -05:00
|
|
|
/**
|
|
|
|
* Java inference for the Object Detection API at:
|
|
|
|
* https://github.com/tensorflow/models/blob/master/research/object_detection/
|
|
|
|
*/
|
2018-02-15 07:46:02 -05:00
|
|
|
public class ObjectDetector {
|
2018-02-21 19:21:59 -05:00
|
|
|
|
|
|
|
SavedModelBundle model;
|
2018-02-23 09:26:15 -05:00
|
|
|
Robot robot;
|
2018-02-15 07:46:02 -05:00
|
|
|
|
2018-02-23 09:26:15 -05:00
|
|
|
public ObjectDetector() throws AWTException {
|
2018-03-03 20:04:03 -05:00
|
|
|
this.model = SavedModelBundle.load("/home/dpapp/raccoon_dataset/results/checkpoint_56749/saved_model/", "serve");
|
2018-02-23 09:26:15 -05:00
|
|
|
this.robot = new Robot();
|
2018-02-28 00:16:57 -05:00
|
|
|
}
|
2018-02-23 09:26:15 -05:00
|
|
|
|
2018-02-28 00:16:57 -05:00
|
|
|
// Goal: reduce this to < 50 ms
|
2018-02-24 14:42:05 -05:00
|
|
|
public ArrayList<DetectedObject> getObjectsInImage(BufferedImage image, double scoreThreshold) throws Exception {
|
2018-02-21 19:21:59 -05:00
|
|
|
List<Tensor<?>> outputs = null;
|
2018-02-23 09:26:15 -05:00
|
|
|
ArrayList<DetectedObject> detectedObjectsInImage = new ArrayList<DetectedObject>();
|
|
|
|
|
2018-02-24 14:42:05 -05:00
|
|
|
makeImageTensor(image);
|
2018-02-23 16:43:57 -05:00
|
|
|
try (Tensor<UInt8> input = makeImageTensor(image)) {
|
2018-02-21 19:21:59 -05:00
|
|
|
outputs =
|
|
|
|
model
|
|
|
|
.session()
|
|
|
|
.runner()
|
|
|
|
.feed("image_tensor", input)
|
|
|
|
.fetch("detection_scores")
|
|
|
|
.fetch("detection_classes")
|
|
|
|
.fetch("detection_boxes")
|
|
|
|
.run();
|
2018-02-21 13:26:20 -05:00
|
|
|
}
|
2018-02-21 19:21:59 -05:00
|
|
|
|
|
|
|
try (Tensor<Float> scoresT = outputs.get(0).expect(Float.class);
|
|
|
|
Tensor<Float> classesT = outputs.get(1).expect(Float.class);
|
|
|
|
Tensor<Float> boxesT = outputs.get(2).expect(Float.class)) {
|
2018-02-23 09:26:15 -05:00
|
|
|
|
2018-02-21 19:21:59 -05:00
|
|
|
int maxObjects = (int) scoresT.shape()[1];
|
|
|
|
float[] scores = scoresT.copyTo(new float[1][maxObjects])[0];
|
|
|
|
float[] classes = classesT.copyTo(new float[1][maxObjects])[0];
|
|
|
|
float[][] boxes = boxesT.copyTo(new float[1][maxObjects][4])[0];
|
2018-02-23 09:26:15 -05:00
|
|
|
|
2018-02-21 19:21:59 -05:00
|
|
|
for (int i = 0; i < scores.length; ++i) {
|
2018-02-24 14:42:05 -05:00
|
|
|
if (scores[i] > scoreThreshold) {
|
2018-02-23 09:26:15 -05:00
|
|
|
detectedObjectsInImage.add(new DetectedObject(scores[i], classes[i], boxes[i]));
|
2018-02-21 19:21:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 09:26:15 -05:00
|
|
|
return detectedObjectsInImage;
|
2018-02-15 07:46:02 -05:00
|
|
|
}
|
2018-02-23 16:43:57 -05:00
|
|
|
|
2018-02-24 10:49:49 -05:00
|
|
|
|
2018-02-28 00:16:57 -05:00
|
|
|
// This is too slow -> running object detection each time reduces the framerate to 3fps, which messses up object tracking
|
2018-02-24 10:49:49 -05:00
|
|
|
public boolean isObjectPresentInBoundingBoxInImage(ArrayList<DetectedObject> detectedObjects, Rect2d boundingBox, String objectClass) throws Exception {
|
|
|
|
for (DetectedObject detectedObject : detectedObjects) {
|
|
|
|
if (detectedObject.getDetectionClass().equals(objectClass)) {
|
|
|
|
if ((Math.abs(detectedObject.getBoundingRect2d().x - boundingBox.x) < 10) &&
|
|
|
|
(Math.abs(detectedObject.getBoundingRect2d().y - boundingBox.y) < 10) &&
|
|
|
|
(Math.abs(detectedObject.getBoundingRect2d().width - boundingBox.width) < 10) &&
|
|
|
|
(Math.abs(detectedObject.getBoundingRect2d().height - boundingBox.height) < 10)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2018-02-23 16:43:57 -05:00
|
|
|
}
|
2018-02-21 19:21:59 -05:00
|
|
|
|
2018-02-23 16:43:57 -05:00
|
|
|
public ArrayList<DetectedObject> getObjectsOfClassInList(ArrayList<DetectedObject> detectedObjects, String objectClass) {
|
2018-02-23 09:26:15 -05:00
|
|
|
ArrayList<DetectedObject> detectedObjectsOfType = new ArrayList<DetectedObject>();
|
2018-02-23 16:43:57 -05:00
|
|
|
for (DetectedObject detectedObject : detectedObjects) {
|
2018-02-23 09:26:15 -05:00
|
|
|
if (detectedObject.getDetectionClass().equals(objectClass)) {
|
|
|
|
detectedObjectsOfType.add(detectedObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return detectedObjectsOfType;
|
2018-02-22 09:28:21 -05:00
|
|
|
}
|
2018-02-21 19:21:59 -05:00
|
|
|
|
2018-02-23 16:43:57 -05:00
|
|
|
private static Tensor<UInt8> makeImageTensor(BufferedImage image) throws IOException {
|
2018-02-23 19:48:16 -05:00
|
|
|
BufferedImage formattedImage = convertBufferedImage(image, BufferedImage.TYPE_3BYTE_BGR);
|
|
|
|
byte[] data = ((DataBufferByte) formattedImage.getData().getDataBuffer()).getData();
|
|
|
|
bgr2rgb(data);
|
2018-02-21 19:21:59 -05:00
|
|
|
final long BATCH_SIZE = 1;
|
|
|
|
final long CHANNELS = 3;
|
2018-02-23 19:48:16 -05:00
|
|
|
long[] shape = new long[] {BATCH_SIZE, formattedImage.getHeight(), formattedImage.getWidth(), CHANNELS};
|
2018-02-21 19:21:59 -05:00
|
|
|
return Tensor.create(UInt8.class, shape, ByteBuffer.wrap(data));
|
|
|
|
}
|
2018-02-23 09:26:15 -05:00
|
|
|
|
2018-02-23 19:48:16 -05:00
|
|
|
private static BufferedImage convertBufferedImage(BufferedImage sourceImage, int bufferedImageType) {
|
|
|
|
BufferedImage image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), bufferedImageType);
|
|
|
|
Graphics2D g2d = image.createGraphics();
|
|
|
|
g2d.drawImage(sourceImage, 0, 0, null);
|
|
|
|
g2d.dispose();
|
|
|
|
return image;
|
2018-02-23 09:26:15 -05:00
|
|
|
}
|
|
|
|
|
2018-02-23 19:48:16 -05:00
|
|
|
private static void bgr2rgb(byte[] data) {
|
|
|
|
for (int i = 0; i < data.length; i += 3) {
|
|
|
|
byte tmp = data[i];
|
|
|
|
data[i] = data[i + 2];
|
|
|
|
data[i + 2] = tmp;
|
|
|
|
}
|
|
|
|
}
|
2018-03-03 20:04:03 -05:00
|
|
|
|
|
|
|
public BufferedImage captureScreenshotGameWindow() throws IOException, AWTException {
|
|
|
|
Rectangle area = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT);
|
|
|
|
return robot.createScreenCapture(area);
|
|
|
|
}
|
2018-02-23 09:26:15 -05:00
|
|
|
}
|