summaryrefslogtreecommitdiff
path: root/splice.pl
blob: 8985bea482f33be12113788d4378ca7e8a854ae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/perl

use Getopt::Long;
use IO::Handle;

STDERR->autoflush(1);
STDOUT->autoflush(1);

my $module = "pix2pixhd";
my $dataset = "PLACEHOLDER";
my $sequence = "PLACEHOLDER";
my $folder_id = 0;
my $start_frame = 0;
my $end_frame = 0;
my $endpoint = undef;
GetOptions (
  "module=s" => \$module,
  "dataset=s"  => \$dataset,
  "sequence=s"  => \$sequence,
  "folder_id=i" => \$folder_id,
  "start_frame=i" => \$start_frame,
  "end_frame=i" => \$end_frame,
  "endpoint=s" => \$endpoint,
)
or die("Error in command line arguments\n");

if ($dataset eq 'PLACEHOLDER' or $sequence eq 'PLACEHOLDER' || ! -e "sequences/$sequence") {
  print "No dataset specified\n";
  exit(1);
}

if ($start_frame == 0 or $end_frame == 0) {
  print "No frames specified\n";
  exit(1);
}

print "module: $module\n";
print "dataset: $dataset\n";
print "sequence: $sequence\n";
print "folder_id: $folder_id\n";
print "start_frame: $start_frame\n";
print "end_frame: $end_frame\n";
print "endpoint: $endpoint\n";

mkdir("./datasets/$dataset");
mkdir("./datasets/$dataset/train_A");
mkdir("./datasets/$dataset/train_B");
mkdir("./datasets/$dataset/test_A");
mkdir("./datasets/$dataset/test_B");
mkdir("./sequences/$dataset");

sub read_dir {
  my $path = shift;
  my $files = [];
  opendir my $dir, $path or die "Cannot open directory: $!";
  while (my $file = readdir($dir)) {
    if ($file =~ /\.png$/) {
      push @$files, $file;
    }
  }
  closedir $dir;
  return $files;
}

my $sequence_files = read_dir("sequences/$sequence");
my $dataset_files = read_dir("sequences/$dataset");

print "Sequence count: " . (scalar @$sequence_files) . "\n";
print "Dataset count: " . (scalar @$dataset_files) . "\n";

my $pwd = `pwd`;
chomp $pwd;
$pwd .= '/';
print $pwd . "\n";

my $sequence_i = 0;
my $dataset_i = scalar @$dataset_files;
my $last_fn = undef;
for my $fn (sort @$sequence_files) {
  my $filename = "sequences/$sequence/$fn";
  if ($start_frame <= $sequence_i and $sequence_i <= $end_frame) {
    chomp $filename;
    # print($pwd . $filename. "\n");
    my $a_fn = sprintf('./datasets/%s/train_A/%s_%05d.png', $dataset, $sequence, $sequence_i);
    my $b_fn = sprintf('./datasets/%s/train_B/%s_%05d.png', $dataset, $sequence, $sequence_i);
    if (! -e $a_fn) {
      system('ln', '-s', $pwd . $filename, sprintf('./sequences/%s/frame_%05d.png', $dataset, $dataset_i));
      $dataset_i += 1;
    }
    if (defined $last_fn) {
      if (! -e $a_fn && ! -e $b_fn) {
        system('ln', '-s', $pwd . $last_fn, $a_fn);
        system('ln', '-s', $pwd . $filename, $b_fn);
      }
    }
    $last_fn = $filename;
  }
  if ($end_frame < $sequence_i) {
    break;
  }
  $sequence_i += 1;
}

if (defined $endpoint) {
  print("post to $endpoint\n");

  system("curl",
    "-X", "POST",
    "-d", "folder_id=$folder_id",
    "-d", "module=$module",
    "-d", "name=$dataset.mov",
    "-d", "url=https://s3.amazonaws.com/i.asdf.us/cortex/lens/data/$folder_id/$dataset.mov",
    "-d", "dataset=$dataset",
    "-d", "activity=splice",
    "-d", "generated=0",
    "-d", "processed=1",
    "-d", "datatype=video",
    $endpoint
);
}

END {
  # chdir($parent_dir);
}