From 5aa0a66fa20ff9d43d64a8e2c012d2c9d216d3e5 Mon Sep 17 00:00:00 2001 From: Paul Fitzpatrick Date: Wed, 16 May 2018 17:47:14 -0400 Subject: [PATCH] support video output --- src/CMakeLists.txt | 3 +- src/VidAnim.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++ src/VidAnim.h | 46 ++++++++++++++++++++++++++++++ src/reanimator.cpp | 11 ++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/VidAnim.cpp create mode 100644 src/VidAnim.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1f988bc..a355396 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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() diff --git a/src/VidAnim.cpp b/src/VidAnim.cpp new file mode 100644 index 0000000..fe19863 --- /dev/null +++ b/src/VidAnim.cpp @@ -0,0 +1,70 @@ +#include "VidAnim.h" + +#include +#include +#include + +#include +using namespace yarp::sig; +using namespace yarp::os; + +#include + +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 v1; + v1.copy(r->get()); + m = cvarrToMat(static_cast(v1.getIplImage())); + base++; + } + video.write(m); + curr += frame_rate; + } + video.release(); +} diff --git a/src/VidAnim.h b/src/VidAnim.h new file mode 100644 index 0000000..75d8268 --- /dev/null +++ b/src/VidAnim.h @@ -0,0 +1,46 @@ +#ifndef VIDANIM_INC +#define VIDANIM_INC + +#include "Renders.h" + +#include + +// #include + +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 diff --git a/src/reanimator.cpp b/src/reanimator.cpp index dc3525b..9ef3115 100644 --- a/src/reanimator.cpp +++ b/src/reanimator.cpp @@ -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());