2018-02-24 10:49:49 -05:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
import java.awt.AWTException;
|
2018-02-24 14:42:05 -05:00
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Graphics;
|
2018-02-24 10:49:49 -05:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.opencv.core.Core;
|
|
|
|
import org.opencv.core.Mat;
|
|
|
|
import org.opencv.core.MatOfByte;
|
2018-02-28 00:16:57 -05:00
|
|
|
import org.opencv.core.Point;
|
2018-02-24 10:49:49 -05:00
|
|
|
import org.opencv.core.Rect2d;
|
2018-02-28 00:16:57 -05:00
|
|
|
import org.opencv.core.Scalar;
|
2018-02-24 10:49:49 -05:00
|
|
|
import org.opencv.imgcodecs.Imgcodecs;
|
2018-02-28 00:16:57 -05:00
|
|
|
import org.opencv.imgproc.Imgproc;
|
2018-02-24 10:49:49 -05:00
|
|
|
import org.opencv.tracking.Tracker;
|
|
|
|
import org.opencv.tracking.TrackerKCF;
|
2018-02-24 14:42:05 -05:00
|
|
|
import org.opencv.tracking.TrackerMIL;
|
|
|
|
import org.opencv.tracking.TrackerBoosting;
|
2018-02-28 00:16:57 -05:00
|
|
|
import org.opencv.tracking.TrackerGOTURN;
|
2018-02-24 14:42:05 -05:00
|
|
|
import org.opencv.tracking.TrackerMedianFlow;
|
2018-02-24 10:49:49 -05:00
|
|
|
import org.opencv.videoio.VideoCapture;
|
|
|
|
|
|
|
|
class ObjectTrackerTest {
|
|
|
|
|
2018-02-28 00:16:57 -05:00
|
|
|
|
2018-02-24 10:49:49 -05:00
|
|
|
@Test
|
2018-03-07 00:51:47 -05:00
|
|
|
void testObjectTrackingWithAllVideosInDirectory() throws Exception {
|
2018-03-21 04:56:48 -04:00
|
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
|
|
|
2018-02-28 00:16:57 -05:00
|
|
|
String videoDirectory = "/home/dpapp/Videos/";
|
2018-03-21 04:56:48 -04:00
|
|
|
//createPresentationGraphics(videoDirectory, "gameplay-2018-03-19_05.50.27.mp4");
|
|
|
|
|
|
|
|
createPresentationGraphics(videoDirectory, "mining_guild_inv.mp4");
|
|
|
|
/*for (File video : getListOfFilesFromDirectory(videoDirectory)) {
|
2018-02-28 00:16:57 -05:00
|
|
|
if (video.isFile()) {
|
|
|
|
testObjectTrackingInVideo(videoDirectory, video.getName());
|
|
|
|
}
|
2018-03-21 04:56:48 -04:00
|
|
|
}*/
|
2018-02-28 00:16:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public File[] getListOfFilesFromDirectory(String videoDirectoryPath) {
|
|
|
|
File videoDirectory = new File(videoDirectoryPath);
|
|
|
|
return videoDirectory.listFiles();
|
|
|
|
}
|
|
|
|
|
2018-03-21 04:56:48 -04:00
|
|
|
void createPresentationGraphics(String videoDirectory, String videoFileName) throws Exception {
|
|
|
|
VideoCapture video = new VideoCapture(videoDirectory + videoFileName);
|
|
|
|
ObjectDetector objectDetector = new ObjectDetector();
|
|
|
|
|
|
|
|
Mat frame = new Mat();
|
|
|
|
boolean frameReadSuccess = video.read(frame);
|
|
|
|
int counter = 0;
|
|
|
|
while (video.read(frame)) {
|
|
|
|
counter++;
|
|
|
|
if (counter % 2 == 0) {
|
|
|
|
//counter++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BufferedImage screencapture = Mat2BufferedImage(frame);
|
2018-04-14 08:41:07 -04:00
|
|
|
Graphics g = screencapture.getGraphics();
|
2018-03-21 04:56:48 -04:00
|
|
|
|
|
|
|
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(Mat2BufferedImage(frame), 0.30);
|
|
|
|
for (DetectedObject detectedObject : detectedObjects) {
|
|
|
|
Rect2d boundingBox = detectedObject.getBoundingRect2d();
|
|
|
|
if (objectDetector.isObjectPresentInBoundingBoxInImage(detectedObjects, boundingBox, "ironOre")) {
|
|
|
|
g.setColor(new Color(0, 255, 0));
|
|
|
|
}
|
|
|
|
else if (objectDetector.isObjectPresentInBoundingBoxInImage(detectedObjects, boundingBox, "ore")) {
|
|
|
|
g.setColor(new Color(255, 0, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.setColor(new Color(255, 255, 255));
|
|
|
|
}
|
|
|
|
g.drawRect((int) (boundingBox.x * 735 / 510), (int) boundingBox.y, (int) (boundingBox.width * 685 / 510), (int) boundingBox.height);
|
|
|
|
}
|
|
|
|
System.out.println(counter);
|
|
|
|
ImageIO.write(screencapture, "jpg", new File(videoDirectory + "/mining_guild_inv/frame_" + videoFileName + counter + ".jpg"));
|
|
|
|
//counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 00:16:57 -05:00
|
|
|
void testObjectTrackingInVideo(String videoDirectory, String videoFileName) throws Exception {
|
|
|
|
VideoCapture video = new VideoCapture(videoDirectory + videoFileName);
|
2018-02-24 10:49:49 -05:00
|
|
|
|
|
|
|
ObjectDetector objectDetector = new ObjectDetector();
|
|
|
|
|
|
|
|
Mat frame = new Mat();
|
|
|
|
boolean frameReadSuccess = video.read(frame);
|
|
|
|
assertTrue(frameReadSuccess);
|
|
|
|
|
2018-02-28 00:16:57 -05:00
|
|
|
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(Mat2BufferedImage(frame), 0.80);
|
2018-02-24 10:49:49 -05:00
|
|
|
System.out.println("Tracking " + detectedObjects.size() + " objects.");
|
2018-02-24 14:42:05 -05:00
|
|
|
|
2018-02-28 00:16:57 -05:00
|
|
|
ArrayList<ObjectTracker> objectTrackers = new ArrayList<ObjectTracker>();
|
2018-02-24 10:49:49 -05:00
|
|
|
ArrayList<Rect2d> boundingBoxes = new ArrayList<Rect2d>();
|
2018-02-28 00:16:57 -05:00
|
|
|
|
2018-02-24 14:42:05 -05:00
|
|
|
for (int i = 0; i < detectedObjects.size(); i++) {
|
2018-02-24 10:49:49 -05:00
|
|
|
boundingBoxes.add(detectedObjects.get(i).getBoundingRect2d());
|
2018-02-28 00:16:57 -05:00
|
|
|
objectTrackers.add(new ObjectTracker(frame, detectedObjects.get(i).getBoundingRect2d()));
|
2018-02-24 10:49:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
while (video.read(frame)) {
|
2018-02-24 14:42:05 -05:00
|
|
|
BufferedImage screencapture = Mat2BufferedImage(frame);
|
|
|
|
|
2018-03-21 04:56:48 -04:00
|
|
|
Graphics g = screencapture.getGraphics();
|
2018-02-24 14:42:05 -05:00
|
|
|
for (int i = 0; i < objectTrackers.size(); i++) {
|
2018-02-24 10:49:49 -05:00
|
|
|
objectTrackers.get(i).update(frame, boundingBoxes.get(i));
|
|
|
|
boolean trackingSuccess = objectTrackers.get(i).update(frame, boundingBoxes.get(i));
|
2018-03-21 04:56:48 -04:00
|
|
|
//assertTrue(trackingSuccess);
|
|
|
|
|
|
|
|
if (objectDetector.isObjectPresentInBoundingBoxInImage(detectedObjects, boundingBoxes.get(i), "ironOre")) {
|
|
|
|
g.setColor(new Color(0, 255, 0));
|
|
|
|
}
|
|
|
|
else if (objectDetector.isObjectPresentInBoundingBoxInImage(detectedObjects, boundingBoxes.get(i), "ironOre")) {
|
|
|
|
g.setColor(new Color(255, 0, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g.setColor(new Color(255, 255, 255));
|
|
|
|
}
|
|
|
|
g.drawRect((int) boundingBoxes.get(i).x, (int) boundingBoxes.get(i).y, (int) boundingBoxes.get(i).width, (int) boundingBoxes.get(i).height);
|
2018-02-24 10:49:49 -05:00
|
|
|
}
|
2018-04-14 08:41:07 -04:00
|
|
|
ImageIO.write(screencapture, "png", new File("/home/dpapp/Videos/BlurredRandom/NonBlurred/" + videoFileName + counter + ".png"));
|
2018-02-24 10:49:49 -05:00
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 00:16:57 -05:00
|
|
|
|
2018-02-24 10:49:49 -05:00
|
|
|
private BufferedImage Mat2BufferedImage(Mat matrix)throws Exception {
|
|
|
|
MatOfByte mob=new MatOfByte();
|
|
|
|
Imgcodecs.imencode(".jpg", matrix, mob);
|
|
|
|
byte ba[]=mob.toArray();
|
|
|
|
|
|
|
|
BufferedImage bi=ImageIO.read(new ByteArrayInputStream(ba));
|
|
|
|
return bi;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|