Commit 650a61b0 authored by Andrey Filippov's avatar Andrey Filippov

Added webm output

parent 8cfd4834
...@@ -748,6 +748,7 @@ public class CuasMotion { ...@@ -748,6 +748,7 @@ public class CuasMotion {
targets60hz, // final double [][][] targets60hz, targets60hz, // final double [][][] targets60hz,
false, // final boolean batch_mode, false, // final boolean batch_mode,
debugLevel); // final int debugLevel) debugLevel); // final int debugLevel)
// if (show_full_images) {
ShowDoubleFloatArrays.showArrays( ShowDoubleFloatArrays.showArrays(
replaced_targets, replaced_targets,
cuasMotion.gpu_max_width, cuasMotion.gpu_max_width,
...@@ -773,7 +774,19 @@ public class CuasMotion { ...@@ -773,7 +774,19 @@ public class CuasMotion {
scene_titles, // String [] titles, scene_titles, // String [] titles,
diamond_path, // //String diamond_path, diamond_path, // //String diamond_path,
debugLevel); // final int debugLevel) { debugLevel); // final int debugLevel) {
if (imp_color != null) {
// temporary, will use model directory
String save_directory = "/media/elphel/NVME/lwir16-proc/eagle_mountain/linked/movement/selected/video/";
String video_path_tmp = save_directory + imp_name+"-TARGETS";
String video_path = saveAsVideo(
clt_parameters, // CLTParameters clt_parameters,
video_path_tmp, // String file_path,
imp_color, // ImagePlus imp_color,
debugLevel); // int debugLevel) {
imp_color.show(); imp_color.show();
}
continue; continue;
} }
...@@ -2932,9 +2945,11 @@ public class CuasMotion { ...@@ -2932,9 +2945,11 @@ public class CuasMotion {
String diamond_path, String diamond_path,
final int debugLevel) { final int debugLevel) {
final int scale = scale2x? 2 : 1; final int scale = scale2x? 2 : 1;
final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
final AtomicInteger ai = new AtomicInteger(0);
// String diamond_path = "/media/elphel/NVME/lwir16-proc/eagle_mountain/graphics/diamond21x17.png"; // String diamond_path = "/media/elphel/NVME/lwir16-proc/eagle_mountain/graphics/diamond21x17.png";
boolean annotate_mono = clt_parameters.imp.annotate_mono; final boolean annotate_mono = clt_parameters.imp.annotate_mono;
boolean annotate_transparent_mono = clt_parameters.imp.annotate_transparent_mono; final boolean annotate_transparent_mono = clt_parameters.imp.annotate_transparent_mono;
final Color annotate_color_mono = clt_parameters.imp.annotate_color_mono; final Color annotate_color_mono = clt_parameters.imp.annotate_color_mono;
ColorProcessor diamond_cp = null; ColorProcessor diamond_cp = null;
try { try {
...@@ -2968,38 +2983,79 @@ public class CuasMotion { ...@@ -2968,38 +2983,79 @@ public class CuasMotion {
titles); // String [] titles) titles); // String [] titles)
imp.getProcessor().setMinAndMax(-input_range/2, input_range/2); imp.getProcessor().setMinAndMax(-input_range/2, input_range/2);
String imp_title = imp.getTitle(); String imp_title = imp.getTitle();
FloatProcessor fp = (FloatProcessor) imp.getProcessor();
if (scale2x) {
ImageStack stack = imp.getStack();
int width_new = scale * imp.getWidth(); int width_new = scale * imp.getWidth();
int height_new = scale * imp.getHeight(); int height_new = scale * imp.getHeight();
if (scale2x) {
ImageStack stack = imp.getStack();
String [] labels = new String [stack.getSize()]; String [] labels = new String [stack.getSize()];
for (int i = 0; i < labels.length; i++) { for (int i = 0; i < labels.length; i++) {
labels[i] = stack.getSliceLabel(i+1); labels[i] = stack.getSliceLabel(i+1);
} }
FloatProcessor [] stackfp = new FloatProcessor[stack.getSize()]; final FloatProcessor [] stackfp = new FloatProcessor[stack.getSize()];
for (int i = 0; i < stackfp.length; i++) { for (int i = 0; i < stackfp.length; i++) {
stackfp[i] = (FloatProcessor) stack.getProcessor(i+1); stackfp[i] = (FloatProcessor) stack.getProcessor(i+1);
} }
System.out.println("stackfp.length="+stackfp.length); System.out.println("stackfp.length="+stackfp.length);
// TODO: multithreaded // TODO: multithreaded
/*
for (int i = 0; i < stackfp.length; i++) { for (int i = 0; i < stackfp.length; i++) {
stackfp[i] = (FloatProcessor) stackfp[i].resize(stackfp[i].getWidth() * 2, stackfp[i].getHeight()*2); stackfp[i] = (FloatProcessor) stackfp[i].resize(stackfp[i].getWidth() * 2, stackfp[i].getHeight()*2);
} }
*/
stack = new ImageStack(width_new,height_new); // ,num_scenes); ai.set(0);
for (int ithread = 0; ithread < threads.length; ithread++) {
threads[ithread] = new Thread() {
public void run() {
for (int nSlice = ai.getAndIncrement(); nSlice < stackfp.length; nSlice = ai.getAndIncrement()) {
stackfp[nSlice] = (FloatProcessor) stackfp[nSlice].resize(stackfp[nSlice].getWidth() * 2, stackfp[nSlice].getHeight()*2);
}
}
};
}
ImageDtt.startAndJoin(threads);
stack = new ImageStack(width_new,height_new);
for (int i = 0; i < stackfp.length; i++) { for (int i = 0; i < stackfp.length; i++) {
stack.addSlice(labels[i],stackfp[i]); stack.addSlice(labels[i],stackfp[i]);
} }
// FloatProcessor fp2x = (FloatProcessor) fp.resize(fp.getWidth() * 2, fp.getHeight()*2);
imp = new ImagePlus(imp_title, stack); // imp.getTitle(),stack); imp = new ImagePlus(imp_title, stack); // imp.getTitle(),stack);
imp.show(); // imp.show();
// imp.setProcessor(fp2x);
// fp.scale(2.0, 2.0);
} }
// imp.show(); // imp.show();
/*
ImageConverter imageConverter = new ImageConverter(imp); ImageConverter imageConverter = new ImageConverter(imp);
imageConverter.convertToRGB(); // very slow - need multitherading imageConverter.convertToRGB(); // very slow - need multitherading
*/
// ImageStack conv_stack = imp.getStack();
final FloatProcessor [] float_processors = new FloatProcessor[imp.getStack().getSize()];
final ColorProcessor [] color_processors = new ColorProcessor[float_processors.length];
final String [] cp_labels = new String [imp.getStack().getSize()];
for (int i = 0; i < float_processors.length; i++) {
float_processors[i] = (FloatProcessor) imp.getStack().getProcessor(i+1);
cp_labels[i] = imp.getStack().getSliceLabel(i+1);
}
ai.set(0);
for (int ithread = 0; ithread < threads.length; ithread++) {
threads[ithread] = new Thread() {
public void run() {
for (int nSlice = ai.getAndIncrement(); nSlice < float_processors.length; nSlice = ai.getAndIncrement()) {
ImagePlus imp1 = new ImagePlus(cp_labels[nSlice],float_processors[nSlice]);
ImageConverter imageConverter = new ImageConverter(imp1);
imageConverter.convertToRGB();
color_processors[nSlice] = (ColorProcessor) imp1.getProcessor();
}
}
};
}
ImageDtt.startAndJoin(threads);
ImageStack color_stack = new ImageStack(width_new,height_new);
for (int i = 0; i < color_processors.length; i++) {
color_stack.addSlice(cp_labels[i],color_processors[i]);
}
imp = new ImagePlus(imp_title, color_stack); // imp.getTitle(),stack);
final int width = imp.getProcessor().getWidth(); final int width = imp.getProcessor().getWidth();
final int height = imp.getProcessor().getHeight(); final int height = imp.getProcessor().getHeight();
// imp.show(); // imp.show();
...@@ -3007,13 +3063,12 @@ public class CuasMotion { ...@@ -3007,13 +3063,12 @@ public class CuasMotion {
final ImageStack fstack_scenes = imp.getImageStack(); final ImageStack fstack_scenes = imp.getImageStack();
// final int width = imp_scenes_pair[nstereo].getWidth(); // final int width = imp_scenes_pair[nstereo].getWidth();
// final int height = imp_scenes_pair[nstereo].getHeight(); // final int height = imp_scenes_pair[nstereo].getHeight();
final int posX= scale * ( width - 119); // 521; final int posX= width - scale * 119; // 521;
final int posY= scale * (height + 1); // 513; final int posY= height + scale * 1; // 513;
final Font font = new Font("Monospaced", Font.PLAIN, scale2x? 24: 12); final Font font = new Font("Monospaced", Font.PLAIN, scale2x? 24: 12);
final int nSlices = fstack_scenes.getSize(); final int nSlices = fstack_scenes.getSize();
final Thread[] threads = ImageDtt.newThreadArray(QuadCLT.THREADS_MAX);
final AtomicInteger ai = new AtomicInteger(0);
if (annotate_mono) { if (annotate_mono) {
ai.set(0);
for (int ithread = 0; ithread < threads.length; ithread++) { for (int ithread = 0; ithread < threads.length; ithread++) {
threads[ithread] = new Thread() { threads[ithread] = new Thread() {
public void run() { public void run() {
...@@ -3022,7 +3077,11 @@ public class CuasMotion { ...@@ -3022,7 +3077,11 @@ public class CuasMotion {
ImageProcessor ip = fstack_scenes.getProcessor(nSlice+1); ImageProcessor ip = fstack_scenes.getProcessor(nSlice+1);
ip.setColor(fcolor); // Color.BLUE); ip.setColor(fcolor); // Color.BLUE);
ip.setFont(font); ip.setFont(font);
if (annotate_transparent_mono) {
ip.drawString(scene_title, posX, posY); // transparent ip.drawString(scene_title, posX, posY); // transparent
} else {
ip.drawString(scene_title, posX, posY, Color.BLACK); // transparent
}
} }
} }
}; };
...@@ -3093,11 +3152,83 @@ public class CuasMotion { ...@@ -3093,11 +3152,83 @@ public class CuasMotion {
} }
ImageDtt.startAndJoin(threads); ImageDtt.startAndJoin(threads);
imp.show(); imp.show();
return imp; return imp;
} }
public static String saveAsVideo(
CLTParameters clt_parameters,
String file_path,
ImagePlus imp_color,
int debugLevel) {
double video_fps = clt_parameters.imp.video_fps;
int mode_avi = clt_parameters.imp.mode_avi;
int avi_JPEG_quality = clt_parameters.imp.avi_JPEG_quality; // 90;
boolean run_ffmpeg = clt_parameters.imp.run_ffmpeg;
String video_ext = clt_parameters.imp.video_ext;
String video_codec = clt_parameters.imp.video_codec.toLowerCase();
int video_crf = clt_parameters.imp.video_crf;
boolean remove_avi = clt_parameters.imp.remove_avi;
boolean dry_run = false;
String avi_path = null;
try {
avi_path=QuadCLT.saveAVI(
file_path, // String file_path, // forces avi, removes .tif, tiff, and .avi
dry_run, // !generate_mapped, // boolean dry_run,
mode_avi, // int avi_mode,
avi_JPEG_quality, // int avi_JPEG_quality,
video_fps, // double fps,
imp_color); // imp_scenes_pair[nstereo]); // ImagePlus imp)
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
if (avi_path == null) {
return null;
}
if (!run_ffmpeg) {
return avi_path; // webm not requested
}
String webm_path = avi_path.substring(0, avi_path.length()-4)+video_ext;
String stereo_meta = "";
String shellCommand = String.format("ffmpeg -y -i %s -c %s -b:v 0 -crf %d %s %s",
avi_path, video_codec, video_crf, stereo_meta, webm_path);
Process p = null;
if (!dry_run) {
int exit_code = -1;
try {
p = Runtime.getRuntime().exec(shellCommand);
} catch (IOException e) {
System.out.println("Failed shell command: \""+shellCommand+"\"");
}
if (p != null) {
try {
p.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exit_code = p.exitValue();
}
System.out.println("Ran shell command: \""+shellCommand+"\" -> "+exit_code);
// Check if webm file exists
if ((exit_code != 0) || !(new File(webm_path)).exists()) {
System.out.println("Failed to create : \""+webm_path+"\"");
return avi_path;
}
} else {
System.out.println("Simulated shell command: \""+shellCommand);
}
if (remove_avi && !dry_run) {
(new File(avi_path)).delete();
System.out.println("Deleted AVI video file: \""+avi_path+"\"");
}
System.out.println("Saved video to "+webm_path);
return webm_path;
}
......
...@@ -5896,6 +5896,8 @@ public class QuadCLTCPU { ...@@ -5896,6 +5896,8 @@ public class QuadCLTCPU {
String x3d_path = getX3dDirectory(); String x3d_path = getX3dDirectory();
String file_name = (suffix==null) ? imp.getTitle():(image_name + suffix); String file_name = (suffix==null) ? imp.getTitle():(image_name + suffix);
String file_path = x3d_path + Prefs.getFileSeparator() + file_name; // + ".tiff"; String file_path = x3d_path + Prefs.getFileSeparator() + file_name; // + ".tiff";
/*
for (String ext:remove_ext) { for (String ext:remove_ext) {
if (file_path.endsWith(ext)) { if (file_path.endsWith(ext)) {
file_path = file_path.substring(0,file_path.length()-ext.length()); file_path = file_path.substring(0,file_path.length()-ext.length());
...@@ -5914,7 +5916,49 @@ public class QuadCLTCPU { ...@@ -5914,7 +5916,49 @@ public class QuadCLTCPU {
System.out.println("saveAVIInModelDirectory(): saved "+file_path); System.out.println("saveAVIInModelDirectory(): saved "+file_path);
} }
return file_path; return file_path;
*/
return saveAVI(
file_path, // String file_path, // forces avi, removes .tif, tiff, and .avi
dry_run, // boolean dry_run,
mode_avi, // int mode_avi,
avi_JPEG_quality, // int avi_JPEG_quality,
fps, // double fps,
imp); // ImagePlus imp) throws IOException
}
public static String saveAVI(
String file_path, // forces avi, removes .tif, tiff, and .avi
boolean dry_run,
int mode_avi,
int avi_JPEG_quality,
double fps,
ImagePlus imp) throws IOException
{
String [] remove_ext = {".tiff", ".tif", ".avi"};
for (String ext:remove_ext) {
if (file_path.endsWith(ext)) {
file_path = file_path.substring(0,file_path.length()-ext.length());
}
} }
file_path += ".avi";
imp.getCalibration().fps = fps;
if (dry_run) {
System.out.println("saveAVIInModelDirectory(): simulated writing "+file_path);
} else {
(new AVI_Writer()).writeImage (
imp, // ImagePlus imp,
file_path, // String path,
mode_avi, // int compression,
avi_JPEG_quality); //int jpegQuality)
System.out.println("saveAVIInModelDirectory(): saved "+file_path);
}
return file_path;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment