Log images on the jevois.

This could fill up the sd card if we don't clean it periodically,
(~10000 images).

We should come up with a method/philosophy on cleaning it before merging
this in.

Change-Id: I5bd78d7a984f6e50352b8804a0c5e4f0a1d11e68
diff --git a/y2019/vision/BUILD b/y2019/vision/BUILD
index 5cfc52b..e19c8cd 100644
--- a/y2019/vision/BUILD
+++ b/y2019/vision/BUILD
@@ -34,6 +34,13 @@
     tools = [":constants_formatting"],
 )
 
+cc_library(
+    name = "image_writer",
+    srcs = ["image_writer.cc"],
+    hdrs = ["image_writer.h"],
+    deps = ["//aos/vision/image:image_types"],
+)
+
 sh_test(
     name = "constants_formatting_test",
     srcs = ["constants_formatting_test.sh"],
@@ -91,6 +98,7 @@
     srcs = ["target_sender.cc"],
     restricted_to = VISION_TARGETS,
     deps = [
+        ":image_writer",
         ":target_finder",
         "//aos/logging",
         "//aos/logging:implementations",
diff --git a/y2019/vision/image_writer.cc b/y2019/vision/image_writer.cc
new file mode 100644
index 0000000..a2c5f0e
--- /dev/null
+++ b/y2019/vision/image_writer.cc
@@ -0,0 +1,50 @@
+#include <fstream>
+#include <sys/stat.h>
+
+#include "y2019/vision/image_writer.h"
+
+namespace y2019 {
+namespace vision {
+
+void ImageWriter::WriteImage(::aos::vision::DataRef data) {
+  LOG(INFO, "Writing image %d", image_count_);
+  std::ofstream ofs(
+      dir_path_ + file_prefix_ + std::to_string(image_count_) + ".yuyv",
+      std::ofstream::out);
+  ofs << data;
+  ofs.close();
+  ++image_count_;
+}
+
+void ImageWriter::ProcessImage(::aos::vision::DataRef data,
+                                size_t num_targets) {
+  ++debounce_count_;
+  if (debounce_count_ < 10) {
+    return;
+  }
+  // Write the image if there are fewer targets in this frame than the last.
+  if (num_targets < previous_num_targets_) {
+    WriteImage(previous_image_);
+    WriteImage(data);
+    debounce_count_ = 0;
+  }
+  //data.swap(previous_image_);
+  data.copy(previous_image_, sizeof(previous_image_));
+}
+
+void ImageWriter::SetDirPath() {
+  std::string base_path = "/jevois/data/run_";
+  for (int i = 0;; ++i) {
+    struct stat st;
+    std::string option = base_path + std::to_string(i);
+    if (stat(option.c_str(), &st) != 0) {
+      file_prefix_ = option + "/";
+      LOG(INFO, "Writing to %s\n", file_prefix_.c_str());
+      mkdir(file_prefix_.c_str(), 0777);
+      break;
+    }
+  }
+}
+
+}  // namespace vision
+}  // namespace y2019
diff --git a/y2019/vision/image_writer.h b/y2019/vision/image_writer.h
new file mode 100644
index 0000000..3d0d934
--- /dev/null
+++ b/y2019/vision/image_writer.h
@@ -0,0 +1,39 @@
+#ifndef _Y2019_VISION_IMAGE_WRITER_H_
+#define _Y2019_VISION_IMAGE_WRITER_H_
+
+#include <string>
+
+#include "aos/logging/logging.h"
+#include "aos/vision/image/image_types.h"
+
+namespace y2019 {
+namespace vision {
+
+class ImageWriter {
+  public:
+   ImageWriter() {
+     LOG(INFO, "Initializing image writer\n");
+     SetDirPath();
+   }
+
+   // This is destructive to data.
+   void ProcessImage(::aos::vision::DataRef data, size_t num_targets);
+  private:
+   void SetDirPath();
+
+   void WriteImage(::aos::vision::DataRef data);
+
+   std::string file_prefix_ = std::string("debug_viewer_jpeg_");
+   std::string dir_path_;
+
+   size_t previous_num_targets_ = 0;
+   char previous_image_[640 * 480 * 2];
+
+   unsigned int image_count_ = 0;
+   unsigned int debounce_count_ = 0;
+};
+
+}  // namespace vision
+}  // namespace y2017
+
+#endif  // _Y2019_VISION_IMAGE_WRITER_H_
diff --git a/y2019/vision/target_sender.cc b/y2019/vision/target_sender.cc
index 6dcf6c0..6515e5b 100644
--- a/y2019/vision/target_sender.cc
+++ b/y2019/vision/target_sender.cc
@@ -12,6 +12,7 @@
 #include "y2019/jevois/serial.h"
 #include "y2019/jevois/structures.h"
 #include "y2019/jevois/uart.h"
+#include "y2019/vision/image_writer.h"
 #include "y2019/vision/target_finder.h"
 
 using ::aos::events::DataSocket;
@@ -288,6 +289,7 @@
   // dup2(itsDev, 2);
 
   TargetFinder finder_;
+  ImageWriter writer_;
 
   aos::vision::CameraParams params0;
   params0.set_exposure(50);
@@ -371,6 +373,8 @@
         LOG(INFO, "Some problem happened");
       }
     }
+
+    writer_.ProcessImage(data, results.size());
   });
 
   aos::events::EpollLoop loop;
diff --git a/y2019/vision/tools/deploy.sh b/y2019/vision/tools/deploy.sh
index 3c16b03..28da309 100755
--- a/y2019/vision/tools/deploy.sh
+++ b/y2019/vision/tools/deploy.sh
@@ -24,10 +24,10 @@
 echo "OK"
 
 echo "Copying files ..."
-cp ./austin_cam.sh "${TARGET_DIR}"/
-cp ./launch.sh "${TARGET_DIR}"/deploy/
+sudo cp ./austin_cam.sh "${TARGET_DIR}"/
+sudo cp ./launch.sh "${TARGET_DIR}"/deploy/
 
-cp "${BAZEL_BIN}/y2019/vision/target_sender" \
+sudo cp "${BAZEL_BIN}/y2019/vision/target_sender" \
   "${BAZEL_BIN}/y2019/vision/serial_waiter" \
   "${TARGET_DIR}"/deploy/