support video output

This commit is contained in:
Paul Fitzpatrick
2018-05-16 17:47:14 -04:00
parent 8aab2f2a85
commit 5aa0a66fa2
4 changed files with 129 additions and 1 deletions

View File

@@ -12,11 +12,12 @@ ADD_EXECUTABLE(reanimator reanimator.cpp Prop.h Prop.cpp Pixer.h
Mapping.h Repository.h Repository.cpp Render.h Render.cpp
Input.h Input.cpp Inputs.h Renders.h Renders.cpp
GifAnim.h GifAnim.cpp
VidAnim.h VidAnim.cpp
gd_topal.c)
TARGET_LINK_LIBRARIES(reanimator filer)
if (USE_OPENCV)
add_definitions("-DMAKESWEET_USE_OPENCV")
TARGET_LINK_LIBRARIES(reanimator opencv_core opencv_imgproc)
TARGET_LINK_LIBRARIES(reanimator opencv_core opencv_highgui opencv_imgproc)
endif()

70
src/VidAnim.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "VidAnim.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <yarp/sig/Image.h>
using namespace yarp::sig;
using namespace yarp::os;
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
#include "Dbg.h"
VidAnim::~VidAnim() {
}
void VidAnim::check() {
if (!renders) {
fprintf(stderr,"No renders for VidAnim!\n");
exit(1);
}
}
void VidAnim::apply(const char *fname) {
int loops = 0;
bool have_prev = false;
// period and hold available in seconds
check();
int frames = renders->length();
int rate = 30;
float frame_rate = 1.0/rate;
float curr = 0;
float target = -1;
int base = 0;
Render *r = renders->get_render(0);
// for some reason filename / container needs to be avi?
VideoWriter video(fname, CV_FOURCC('M','P','4','V'), rate, Size(r->get().width(),
r->get().height()));
Mat m;
while (true) {
int i = (base + ((first_frame >= 0) ? first_frame : 0)) % frames;
if (curr >= target) {
if (base >= frames) break;
float step = period;
if (i == frames - 1) {
step += hold;
}
target = curr + step;
Render *r = renders->get_render(i);
ImageOf<PixelBgr> v1;
v1.copy(r->get());
m = cvarrToMat(static_cast<const IplImage*>(v1.getIplImage()));
base++;
}
video.write(m);
curr += frame_rate;
}
video.release();
}

46
src/VidAnim.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef VIDANIM_INC
#define VIDANIM_INC
#include "Renders.h"
#include <vector>
// #include <yarp/os/Bytes.h>
class VidAnim {
private:
Renders *renders;
double period;
double hold;
int first_frame;
public:
VidAnim() {
renders = 0 /*NULL*/;
period = 0.1;
hold = 5;
first_frame = -1;
}
~VidAnim();
void attach_renders(Renders *renders) {
this->renders = renders;
}
void check();
void set_first_frame(int index) {
first_frame = index;
}
void apply(const char *fname);
void set_timing(double period, double hold) {
this->period = period;
this->hold = hold;
}
};
#endif

View File

@@ -13,6 +13,7 @@
#include "Render.h"
#include "Renders.h"
#include "GifAnim.h"
#include "VidAnim.h"
#include "Dbg.h"
using namespace std;
@@ -115,6 +116,16 @@ int main(int argc, char *argv[]) {
anim.save(options.find("gif").asString().c_str());
}
if (options.check("vid")) {
VidAnim anim;
anim.attach_renders(&renders);
anim.set_timing(repo.get_period(),repo.get_hold());
if (options.check("start")) {
anim.set_first_frame(options.check("start",Value(0)).asInt());
}
anim.apply(options.find("vid").asString().c_str());
}
if (options.check("files")) {
printf("animation=%s\n", repo.is_animation()?"true":"false");
printf("length=%d\n", repo.length());