|
| 1 | +#!/usr/bin/perl |
| 2 | +use File::Basename; |
| 3 | +use strict; |
| 4 | +########################################################################## |
| 5 | +# Create samples from an image applying distortions repeatedly |
| 6 | +# (create many many samples from many images applying distortions) |
| 7 | +# |
| 8 | +# perl createtrainsamples.pl <positives.dat> <negatives.dat> <vec_output_dir> |
| 9 | +# [<totalnum = 7000>] [<createsample_command_options = ./createsamples -w 20 -h 20...>] |
| 10 | +# ex) perl createtrainsamples.pl positives.dat negatives.dat samples |
| 11 | +# |
| 12 | +# Author: Naotoshi Seo |
| 13 | +# Date : 09/12/2008 Add <totalnum> and <createsample_command_options> options |
| 14 | +# Date : 06/02/2007 |
| 15 | +# Date : 03/12/2006 |
| 16 | +######################################################################### |
| 17 | +my $cmd = './createsamples -bgcolor 0 -bgthresh 0 -maxxangle 1.1 -maxyangle 1.1 maxzangle 0.5 -maxidev 40 -w 20 -h 20'; |
| 18 | +my $totalnum = 7000; |
| 19 | +my $tmpfile = 'tmp'; |
| 20 | + |
| 21 | +if ($#ARGV < 2) { |
| 22 | + print "Usage: perl createtrainsamples.pl\n"; |
| 23 | + print " <positives_collection_filename>\n"; |
| 24 | + print " <negatives_collection_filename>\n"; |
| 25 | + print " <output_dirname>\n"; |
| 26 | + print " [<totalnum = " . $totalnum . ">]\n"; |
| 27 | + print " [<createsample_command_options = '" . $cmd . "'>]\n"; |
| 28 | + exit; |
| 29 | +} |
| 30 | +my $positive = $ARGV[0]; |
| 31 | +my $negative = $ARGV[1]; |
| 32 | +my $outputdir = $ARGV[2]; |
| 33 | +$totalnum = $ARGV[3] if ($#ARGV > 2); |
| 34 | +$cmd = $ARGV[4] if ($#ARGV > 3); |
| 35 | + |
| 36 | +open(POSITIVE, "< $positive"); |
| 37 | +my @positives = <POSITIVE>; |
| 38 | +close(POSITIVE); |
| 39 | + |
| 40 | +open(NEGATIVE, "< $negative"); |
| 41 | +my @negatives = <NEGATIVE>; |
| 42 | +close(NEGATIVE); |
| 43 | + |
| 44 | +# number of generated images from one image so that total will be $totalnum |
| 45 | +my $numfloor = int($totalnum / $#positives); |
| 46 | +my $numremain = $totalnum - $numfloor * $#positives; |
| 47 | + |
| 48 | +# Get the directory name of positives |
| 49 | +my $first = $positives[0]; |
| 50 | +my $last = $positives[$#positives]; |
| 51 | +while ($first ne $last) { |
| 52 | + $first = dirname($first); |
| 53 | + $last = dirname($last); |
| 54 | + if ( $first eq "" ) { last; } |
| 55 | +} |
| 56 | +my $imgdir = $first; |
| 57 | +my $imgdirlen = length($first); |
| 58 | + |
| 59 | +for (my $k = 0; $k < $#positives; $k++ ) { |
| 60 | + my $img = $positives[$k]; |
| 61 | + my $num = ($k < $numremain) ? $numfloor + 1 : $numfloor; |
| 62 | + |
| 63 | + # Pick up negative images randomly |
| 64 | + my @localnegatives = (); |
| 65 | + for (my $i = 0; $i < $num; $i++) { |
| 66 | + my $ind = int(rand($#negatives)); |
| 67 | + push(@localnegatives, $negatives[$ind]); |
| 68 | + } |
| 69 | + open(TMP, "> $tmpfile"); |
| 70 | + print TMP @localnegatives; |
| 71 | + close(TMP); |
| 72 | + #system("cat $tmpfile"); |
| 73 | + |
| 74 | + !chomp($img); |
| 75 | + my $vec = $outputdir . substr($img, $imgdirlen) . ".vec" ; |
| 76 | + print "$cmd -img $img -bg $tmpfile -vec $vec -num $num" . "\n"; |
| 77 | + system("$cmd -img $img -bg $tmpfile -vec $vec -num $num"); |
| 78 | +} |
| 79 | +unlink($tmpfile); |
0 commit comments