Skip to content

Instantly share code, notes, and snippets.

@sjaiswal25
Created November 17, 2018 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjaiswal25/06ab93868542212d0c07e8a9e8e264ed to your computer and use it in GitHub Desktop.
Save sjaiswal25/06ab93868542212d0c07e8a9e8e264ed to your computer and use it in GitHub Desktop.
public class Obj_Detect {
public static void main(String[] args) throws Exception {
//org.openjdk.jmh.Main.main(args);
Model model = ModelSerializer.restoreComputationGraph("/home/project/data/tiny_yolov2.zip",false);
ParallelInference parallelInference = new ParallelInference.Builder(model).inferenceMode(InferenceMode.BATCHED).batchLimit(8).workers(4).build(); //.queueLimit(32)
new Thread(() -> {
try {
new ObjectDetectorFromVideo(parallelInference).startRealTimeVideoDetection("/home/project/videos/adisys_cam_1.mp4");
);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
}
public class ObjectDetectorFromVideo {
ParallelInference parallelInference;
public static final double[][] DEFAULT_PRIOR_BOXES = {{0.57273, 0.677385}, {1.87446, 2.06253}, {3.33843, 5.47434}, {7.88282, 3.52778}, {9.77052, 9.16828}};
public ObjectDetectorFromVideo(ParallelInference parallelInference) throws IOException {
this.parallelInference = parallelInference;
}
public void startRealTimeVideoDetection(String videoFileName) throws java.lang.Exception { //, TinyYoloModel model
String windowName = videoFileName;
FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(videoFileName);
frameGrabber.start();
Mat[] v = new Mat[1];
Frame frame;
double frameRate = frameGrabber.getFrameRate();
int num_frames = frameGrabber.getLengthInFrames();
System.out.println("The inputted video clip has " + frameGrabber.getLengthInFrames() + " frames");
System.out.println("The inputted video clip has frame rate of " + frameRate);
long start_time = System.currentTimeMillis();
NativeImageLoader nil = new NativeImageLoader();
List<DetectedObject> predictedObjects;
try {
for(int i = 1; i < frameGrabber.getLengthInFrames(); i+=1) { //frameGrabber.getLengthInFrames() (int)frameRate
frameGrabber.setFrameNumber(i);
frame = frameGrabber.grab();
v[0] = new OpenCVFrameConverter.ToMat().convert(frame);
INDArray result = parallelInference.output(prepareImage(v[0],224,224));
predictedObjects = YoloUtils.getPredictedObjects(Nd4j.create(DEFAULT_PRIOR_BOXES),result,0.5,0);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
frameGrabber.stop();
}
frameGrabber.close();
}
INDArray prepareImage(Mat file, int width, int height) throws IOException {
NativeImageLoader loader = new NativeImageLoader(height, width, 3);
ImagePreProcessingScaler imagePreProcessingScaler = new ImagePreProcessingScaler(0, 1);
INDArray indArray = loader.asMatrix(file);
imagePreProcessingScaler.transform(indArray);
return indArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment