Skip to content
Snippets Groups Projects
Commit df6b5987 authored by istkabra's avatar istkabra
Browse files

* Fps in videos can now increase by time

parent 47218120
No related branches found
No related tags found
No related merge requests found
...@@ -40,8 +40,8 @@ public class Config { ...@@ -40,8 +40,8 @@ public class Config {
static String IMAGE_SEQUENCE_PATH; static String IMAGE_SEQUENCE_PATH;
static String VIDEO_PATH_AND_FILE; static String VIDEO_PATH_AND_FILE;
static int IMAGE_FPS; static int IMAGE_FPS;
static double IMAGE_FPS_DECLINE; static int IMAGES_TO_FPS_INCREASE;
static int IMAGE_FPS_MIN; static int IMAGE_FPS_MAX;
static boolean ZOOM; static boolean ZOOM;
// For images // For images
...@@ -153,8 +153,8 @@ public class Config { ...@@ -153,8 +153,8 @@ public class Config {
IMAGE_SEQUENCE_PATH = this.properties.getProperty("imageSequencePath"); IMAGE_SEQUENCE_PATH = this.properties.getProperty("imageSequencePath");
VIDEO_PATH_AND_FILE = this.properties.getProperty("videoPathAndFile"); VIDEO_PATH_AND_FILE = this.properties.getProperty("videoPathAndFile");
IMAGE_FPS = Integer.parseInt(this.properties.getProperty("imgFps")); IMAGE_FPS = Integer.parseInt(this.properties.getProperty("imgFps"));
IMAGE_FPS_DECLINE = Double.parseDouble(this.properties.getProperty("imgFpsDecline")); IMAGES_TO_FPS_INCREASE = Integer.parseInt(this.properties.getProperty("imagesToFpsIncrease"));
IMAGE_FPS_MIN = Integer.parseInt(this.properties.getProperty("imgFpsMax")); IMAGE_FPS_MAX = Integer.parseInt(this.properties.getProperty("imgFpsMax"));
ZOOM = this.properties.getProperty("zoom").equals("true"); ZOOM = this.properties.getProperty("zoom").equals("true");
} }
......
...@@ -12,16 +12,16 @@ public class Main { ...@@ -12,16 +12,16 @@ public class Main {
String propertyPath = "./src/main/resources/genetic.properties"; String propertyPath = "./src/main/resources/genetic.properties";
Config config = new Config(propertyPath); Config config = new Config(propertyPath);
int[] protein = Examples.convertStringToIntArray(Examples.SEQ20); int[] protein = Examples.convertStringToIntArray(Examples.SEQ50);
GeneticAlgorithm ga = new GeneticAlgorithm(protein); GeneticAlgorithm ga = new GeneticAlgorithm(protein);
ga.simulateGenerations(); ga.simulateGenerations();
// Create a new video if possible and desired // Create a new video if possible and desired
boolean imagesRefreshed = Arrays.stream(Config.VISUALIZERS).anyMatch(VisualizerMethods.Image::equals); boolean imagesRefreshed = Arrays.asList(Config.VISUALIZERS).contains(VisualizerMethods.Image);
boolean videoEnabled = Arrays.stream(Config.VISUALIZERS).anyMatch(VisualizerMethods.Video::equals); boolean videoEnabled = Arrays.asList(Config.VISUALIZERS).contains(VisualizerMethods.Video);
if (imagesRefreshed && videoEnabled){ if (imagesRefreshed && videoEnabled){
VideoCreator.createVideo(Config.IMAGE_SEQUENCE_PATH, Config.VIDEO_PATH_AND_FILE, VideoCreator.createVideo(Config.IMAGE_SEQUENCE_PATH, Config.VIDEO_PATH_AND_FILE,
Config.IMAGE_FPS, ga.getMaxH(), ga.getMaxW()); Config.IMAGE_FPS, Config.IMAGES_TO_FPS_INCREASE, Config.IMAGE_FPS_MAX, ga.getMaxH(), ga.getMaxW());
} }
} }
} }
...@@ -64,13 +64,13 @@ public class JCodecPNGtoMP4 { ...@@ -64,13 +64,13 @@ public class JCodecPNGtoMP4 {
}); });
} }
static void generateVideoBySequenceImages(File videoFile, String pathImages, String imageExt, int fps) throws Exception { static void generateVideoBySequenceImages(File videoFile, String pathImages, String imageExt,
int fps, int imgToFpsIncrease, int maxFps) throws Exception {
SeekableByteChannel out = null; SeekableByteChannel out = null;
try { try {
out = NIOUtils.writableFileChannel(videoFile.getCanonicalPath()); out = NIOUtils.writableFileChannel(videoFile.getCanonicalPath());
// for Android use: AndroidSequenceEncoder AWTSequenceEncoder encoder = new AWTSequenceEncoder(out, Rational.R(maxFps, 1));;
AWTSequenceEncoder encoder = new AWTSequenceEncoder(out, Rational.R(fps, 1));
Path directoryPath = Paths.get(new File(pathImages).toURI()); Path directoryPath = Paths.get(new File(pathImages).toURI());
...@@ -86,16 +86,32 @@ public class JCodecPNGtoMP4 { ...@@ -86,16 +86,32 @@ public class JCodecPNGtoMP4 {
sortByNumber(files); sortByNumber(files);
int numberImagesWithSameFps = imgToFpsIncrease;
System.out.println();
for (File img : files) { for (File img : files) {
System.err.println("Encoding image " + img.getName()); if (numberImagesWithSameFps <= 0) {
// Generate the image, for Android use Bitmap if (fps < maxFps) {
fps++; // Increase fps
numberImagesWithSameFps = imgToFpsIncrease - 1;
}
} else {
numberImagesWithSameFps--; // Countdown to increase
}
System.err.println("Encoding image " + img.getName() + " [" + fps + " fps]");
// Generate the image
BufferedImage image = ImageIO.read(img); BufferedImage image = ImageIO.read(img);
// Encode the image // Encode the image often enough to fit current fps
encoder.encodeImage(image); for (int i = 0; i < Math.round((float) maxFps / fps); i++) {
encoder.encodeImage(image);
}
// Finalize the encoding, i.e. clear the buffers, write the header, etc.
} }
} }
// Finalize the encoding, i.e. clear the buffers, write the header, etc. // Finalize the encoding, i.e. clear the buffers, write the header, etc.
encoder.finish(); encoder.finish();
System.err.println("Finished creating video: " + videoFile.getCanonicalPath());
} finally { } finally {
NIOUtils.closeQuietly(out); NIOUtils.closeQuietly(out);
} }
......
...@@ -8,8 +8,6 @@ import java.awt.image.BufferedImage; ...@@ -8,8 +8,6 @@ import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Vector;
public class VideoCreator{ public class VideoCreator{
...@@ -28,9 +26,10 @@ public class VideoCreator{ ...@@ -28,9 +26,10 @@ public class VideoCreator{
} }
}; };
private static int[] resizeImages(String imagesPath, int maxH, int maxW) throws IOException { private static void resizeImages(String imagesPath, int maxH, int maxW) throws IOException {
System.err.println("\nStarting image resizing");
dir = new File(imagesPath); dir = new File(imagesPath);
int[] widthHeight = new int[2];
if (dir.isDirectory()) { if (dir.isDirectory()) {
// reads input images and determines maximum required size // reads input images and determines maximum required size
...@@ -58,9 +57,6 @@ public class VideoCreator{ ...@@ -58,9 +57,6 @@ public class VideoCreator{
maxHeight = MAXIMUM_SIZE; maxHeight = MAXIMUM_SIZE;
} }
widthHeight[0] = maxWidth;
widthHeight[1] = maxHeight;
// Resizes all images // Resizes all images
for (final File f : dir.listFiles(imageFilter)) { for (final File f : dir.listFiles(imageFilter)) {
BufferedImage inputImage = ImageIO.read(f); BufferedImage inputImage = ImageIO.read(f);
...@@ -80,33 +76,26 @@ public class VideoCreator{ ...@@ -80,33 +76,26 @@ public class VideoCreator{
ImageIO.write(outputImage, "png", f); ImageIO.write(outputImage, "png", f);
} }
} }
return widthHeight;
} }
// MainClasses.Main function // MainClasses.Main function
public static void createVideo(String imagesPath, String videoPathAndFile, public static void createVideo(String imagesPath, String videoPathAndFile,
int imgFps, int maxH, int maxW) { int imgFps, int imgToFpsIncrease, int maxFps, int maxH, int maxW) {
try { try {
int[] widthHeight = VideoCreator.resizeImages(imagesPath, maxH, maxW); VideoCreator.resizeImages(imagesPath, maxH, maxW);
File videoFile = new File(videoPathAndFile); File videoFile = new File(videoPathAndFile);
if (!videoFile.exists()) { if (!videoFile.exists()) {
videoFile.createNewFile(); videoFile.createNewFile();
} }
Vector<String> imgLst = new Vector<>();
makeVideo(videoFile, imagesPath, imgFps); try {
JCodecPNGtoMP4.generateVideoBySequenceImages(videoFile, imagesPath, "png", imgFps, imgToFpsIncrease, maxFps);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void makeVideo(File videoFile, String imagesDir, int fps) throws MalformedURLException {
try {
JCodecPNGtoMP4.generateVideoBySequenceImages(videoFile, imagesDir, "png", fps);
} catch (Exception e) {
e.printStackTrace();
}
}
} }
\ No newline at end of file
...@@ -7,9 +7,9 @@ ...@@ -7,9 +7,9 @@
### Algorithm settings ### Algorithm settings
# Size of the population in one generation # Size of the population in one generation
populationSize = 10 populationSize = 200
# Number of generations to run the algorithm for # Number of generations to run the algorithm for
noGenerations = 10 noGenerations = 1000
# Type of population initialization [curl | straight | random] # Type of population initialization [curl | straight | random]
initializationMethod = curl initializationMethod = curl
# Type of selection that should be used [proportional | tournament | onlybest] # Type of selection that should be used [proportional | tournament | onlybest]
...@@ -47,10 +47,10 @@ ...@@ -47,10 +47,10 @@
# Path and filename for the resulting video file # Path and filename for the resulting video file
videoPathAndFile = ./visualization/video.mp4 videoPathAndFile = ./visualization/video.mp4
# Frames per second in the beginning of the video # Frames per second in the beginning of the video
imgFps = 100 imgFps = 10
# Decline of interval between images to speed up later generations TODO # Number of images until fps increases by one
imgFpsDecline = 0.01 imagesToFpsIncrease = 6
# Maximum fps TODO # Maximum fps, a larger gap between the starting and maxFps will mean a longer encoding and file size
imgFpsMax = 10 imgFpsMax = 120
# Zoom to the appropriate size, once no bigger proteins are going to follow TODO # Zoom to the appropriate size, once no bigger proteins are going to follow TODO
zoom = false zoom = false
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment