Advertisement
lentil

OpenDrone

Jul 20th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 34.97 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. ##
  4. ##  created by Daniel Schwarz/daniel.schwarz@topoi.org
  5. ##  released under Creative Commons/CC-BY
  6. ##  Attribution
  7. ##
  8.  
  9. use File::Basename;
  10. use File::Copy;
  11. use File::Spec;
  12. use Data::Dumper;
  13. use Time::localtime;
  14. use Switch;
  15. use POSIX qw(strftime);
  16. use JSON;
  17.  
  18. ## the defs
  19.  
  20. chomp($CURRENT_DIR  = `pwd`);
  21. chomp($BIN_PATH_REL = `dirname $0`);
  22. chomp($OS           = `uname -o`);
  23. chomp($CORES        = `ls -d /sys/devices/system/cpu/cpu[[:digit:]]* | wc -w`);
  24.  
  25. if(!File::Spec->file_name_is_absolute($BIN_PATH_REL)){
  26.     $BIN_PATH_ABS = File::Spec->rel2abs($BIN_PATH_REL);
  27. } else {
  28.     $BIN_PATH_ABS = File::Spec->rel2abs($BIN_PATH_REL);
  29. }
  30.  
  31. sub getCcdWidths{
  32.     local $/;
  33.     my $ccdPath = "$BIN_PATH_ABS/ccd_defs.json";
  34.     open( my $fh, '<', $ccdPath);
  35.  
  36.     my $ccdWidths = decode_json(<$fh>);
  37.     close($fh);
  38.     undef $/;
  39.     return $ccdWidths;
  40. }
  41.  
  42. $ccdWidths = getCcdWidths();
  43.  
  44. $BIN_PATH = $BIN_PATH_ABS."/bin";
  45.  
  46. my %objectStats    = {
  47.     count           => 0,
  48.     good            => 0,
  49.     bad             => 0,
  50.     minWidth        => 0,
  51.     minHeight       => 0,
  52.     maxWidth        => 0,
  53.     maxHeight       => 0
  54.  
  55. };
  56.  
  57. my %jobOptions    = {
  58.     resizeTo        => 0,
  59.     srcDir          => $CURRENT_DIR
  60. };
  61.  
  62. my %args = {};
  63.  
  64. $jobOptions{srcDir} = "$CURRENT_DIR";
  65.  
  66. sub run {
  67.     system($_[0]);
  68.    
  69.     if($? != 0){
  70.         die "\n\nquitting cause: \n\t$_[0]\nreturned with code ".$?."\n";
  71.     }
  72. }
  73. sub now {
  74.     system("echo `date`");
  75. }
  76.  
  77. sub parseArgs {
  78.  
  79.  ## defaults
  80.     $args{"--match-size"}            = "200";
  81.  
  82.     $args{"--resize-to"}             = "2400";
  83.    
  84.     $args{"--start-with"}            = "resize";
  85.     $args{"--end-with"}              = "odm_orthophoto";
  86.    
  87.     $args{"--cmvs-maxImages"}        = 500;
  88.    
  89.     $args{"--matcher-ratio"}         = 0.6;
  90.     $args{"--matcher-threshold"}     = 2.0;
  91.    
  92.     $args{"--pmvs-level"}            = 1;
  93.     $args{"--pmvs-csize"}            = 2;
  94.     $args{"--pmvs-threshold"}        = 0.7;
  95.     $args{"--pmvs-wsize"}            = 7;
  96.     $args{"--pmvs-minImageNum"}      = 3;
  97.  
  98.     $args{"--odm_meshing-maxVertexCount"}   = 100000;
  99.     $args{"--odm_meshing-octreeDepth"}      = 9;
  100.     $args{"--odm_meshing-samplesPerNode"}   = 1;
  101.     $args{"--odm_meshing-solverDivide"}     = 9;
  102.  
  103.     $args{"--odm_texturing-textureResolution"}   = 4096;
  104.     $args{"--odm_texturing-textureWithSize"}     = 3600;
  105.  
  106.     $args{"--odm_georeferencing-gcpFile"}   = "gcp_list.txt";
  107.     $args{"--odm_georeferencing-useGcp"}    = "true";
  108.  
  109.     $args{"--zip-results"}           = "true";
  110.    
  111.     for($i = 0; $i <= $#ARGV; $i++) {
  112.         if($ARGV[$i] =~ /^--[^a-z\-]*/){
  113.             $args{"$ARGV[$i]"} = true;
  114.            
  115.             if(!($ARGV[$i+1] =~ /^--[^a-z\-]*/)) {
  116.                 if($ARGV[$i] eq "--resize-to"){
  117.                     if($ARGV[$i+1] eq "orig" || $ARGV[$i+1] =~ /^[0-9]*$/){
  118.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  119.                     } else {
  120.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  121.                     }
  122.                 }
  123.                 if($ARGV[$i] eq "--start-with"){
  124.                     if($ARGV[$i+1] eq "resize" || $ARGV[$i+1] eq "getKeypoints" || $ARGV[$i+1] eq "match" || $ARGV[$i+1] eq "bundler" || $ARGV[$i+1] eq "cmvs" || $ARGV[$i+1] eq "pmvs"
  125.             || $ARGV[$i+1] eq "odm_meshing" || $ARGV[$i+1] eq "odm_texturing" || $ARGV[$i+1] eq "odm_georeferencing" || $ARGV[$i+1] eq "odm_orthophoto"){
  126.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  127.                     } else {    
  128.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1]."\n\t valid values are \"resize\", \"getKeypoints\", \"match\", \"bundler\", \"cmvs\", \"pmvs\",
  129.             \"odm_meshing\", \"odm_texturing\", \"odm_georeferencing\", \"odm_orthophoto\"";    
  130.                     }
  131.                 }
  132.                 if($ARGV[$i] eq "--end-with"){
  133.                     if($ARGV[$i+1] eq "resize" || $ARGV[$i+1] eq "getKeypoints" || $ARGV[$i+1] eq "match" || $ARGV[$i+1] eq "bundler" || $ARGV[$i+1] eq "cmvs" || $ARGV[$i+1] eq "pmvs"
  134.             || $ARGV[$i+1] eq "odm_meshing" || $ARGV[$i+1] eq "odm_texturing" || $ARGV[$i+1] eq "odm_georeferencing" || $ARGV[$i+1] eq "odm_orthophoto"){
  135.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  136.                     } else {    
  137.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1]."\n\t valid values are \"resize\", \"getKeypoints\", \"match\", \"bundler\", \"cmvs\", \"pmvs\",
  138.             \"odm_meshing\", \"odm_texturing\", \"odm_georeferencing\", \"odm_orthophoto\"";
  139.                     }
  140.                 }
  141.                 if($ARGV[$i] eq "--run-only"){
  142.                     if($ARGV[$i+1] eq "resize" || $ARGV[$i+1] eq "getKeypoints" || $ARGV[$i+1] eq "match" || $ARGV[$i+1] eq "bundler" || $ARGV[$i+1] eq "cmvs" || $ARGV[$i+1] eq "pmvs"
  143.             || $ARGV[$i+1] eq "odm_meshing" || $ARGV[$i+1] eq "odm_texturing" || $ARGV[$i+1] eq "odm_georeferencing" || $ARGV[$i+1] eq "odm_orthophoto"){
  144.                         $args{"--start-with"} = $ARGV[$i+1];
  145.                         $args{"--end-with"} = $ARGV[$i+1];
  146.                     } else {    
  147.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1]."\n\t valid values are \"resize\", \"getKeypoints\", \"match\", \"bundler\", \"cmvs\", \"pmvs\",
  148.             \"odm_meshing\", \"odm_texturing\", \"odm_georeferencing\", \"odm_orthophoto\"";
  149.                     }
  150.                 }
  151.                 if($ARGV[$i] eq "--matcher-threshold"){
  152.                     if($ARGV[$i+1] =~ /^-?[0-9]*\.?[0-9]*$/){
  153.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  154.                     } else {
  155.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  156.                     }
  157.                 }
  158.                 if($ARGV[$i] eq "--matcher-ratio"){
  159.                     if($ARGV[$i+1] =~ /^-?[0-9]*\.?[0-9]*$/){
  160.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  161.                     } else {
  162.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  163.                     }
  164.                 }
  165.                 if($ARGV[$i] eq "--cmvs-maxImages"){
  166.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  167.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  168.                     } else {
  169.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  170.                     }
  171.                 }
  172.                 if($ARGV[$i] eq "--pmvs-level"){
  173.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  174.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  175.                     } else {
  176.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  177.                     }
  178.                 }
  179.                 if($ARGV[$i] eq "--pmvs-csize"){
  180.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  181.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  182.                     } else {
  183.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  184.                     }
  185.                 }
  186.                 if($ARGV[$i] eq "--pmvs-threshold"){
  187.                     if($ARGV[$i+1] =~ /^-?[0-9]*\.?[0-9]*$/){
  188.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  189.                     } else {
  190.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  191.                     }
  192.                 }
  193.                 if($ARGV[$i] eq "--pmvs-wsize"){
  194.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  195.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  196.                     } else {
  197.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  198.                     }
  199.                 }
  200.                 if($ARGV[$i] eq "--pmvs-minImageNum"){
  201.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  202.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  203.                     } else {
  204.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  205.                     }
  206.                 }
  207.                
  208.                 if($ARGV[$i] eq "--force-focal"){
  209.                     if($ARGV[$i+1] =~ /^[0-9]*\.?[0-9]*$/){
  210.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  211.                     } else {
  212.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  213.                     }
  214.                 }
  215.                 if($ARGV[$i] eq "--force-ccd"){
  216.                     if($ARGV[$i+1] =~ /^[0-9]*\.?[0-9]*$/){
  217.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  218.                     } else {
  219.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  220.                     }
  221.                 }
  222.                 if($ARGV[$i] eq "--odm_meshing-maxVertexCount"){
  223.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  224.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  225.                     } else {
  226.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  227.                     }
  228.                 }
  229.                 if($ARGV[$i] eq "--odm_meshing-octreeDepth"){
  230.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  231.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  232.                     } else {
  233.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  234.                     }
  235.                 }
  236.                 if($ARGV[$i] eq "--odm_meshing-samplesPerNode"){
  237.                     if($ARGV[$i+1] =~ /^[0-9]*\.?[0-9]*$/){
  238.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  239.                     } else {
  240.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  241.                     }
  242.                 }
  243.                 if($ARGV[$i] eq "--odm_meshing-solverDivide"){
  244.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  245.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  246.                     } else {
  247.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  248.                     }
  249.                 }
  250.                 if($ARGV[$i] eq "--odm_texturing-textureResolution"){
  251.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  252.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  253.                     } else {
  254.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  255.                     }
  256.                 }
  257.         if($ARGV[$i] eq "--odm_texturing-textureWithSize"){
  258.                     if($ARGV[$i+1] =~ /^[0-9]*$/){
  259.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  260.                     } else {
  261.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  262.                     }
  263.                 }
  264.         if($ARGV[$i] eq "--odm_georeferencing-gcpFile"){
  265.                     $args{$ARGV[$i]} = $ARGV[$i+1];
  266.                 }
  267.         if($ARGV[$i] eq "--odm_georeferencing-useGcp"){
  268.                     if($ARGV[$i+1] eq "true" || $ARGV[$i+1] eq "false"){
  269.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  270.                     } else {
  271.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  272.                     }
  273.                 }
  274.         if($ARGV[$i] eq "--zip-results"){
  275.                     if($ARGV[$i+1] eq "true" || $ARGV[$i+1] eq "false"){
  276.                         $args{$ARGV[$i]} = $ARGV[$i+1];
  277.                     } else {
  278.                         die "\n invalid parameter for \"".$ARGV[$i]."\": ".$ARGV[$i+1];
  279.                     }
  280.                 }
  281.             }
  282.         }
  283.     }
  284.    
  285.  
  286.     if($args{"--help"}){        
  287.         print "\nusgae: run.pl [options]";
  288.         print "\nit should be run from the folder contining the images to which should reconstructed";
  289.         print "\n";
  290.         print "\noptions:";
  291.         print "\n              --help: ";
  292.         print "\n                      prints this screen";
  293.         print "\n  ";
  294.                    
  295.         print "\n         --resize-to: <positive integer|\"orig\">";
  296.         print "\n             default: 2400";
  297.         print "\n                      will resize the images so that the maximum width/height of the images are smaller or equal to the specified number";
  298.         print "\n                      if \"--resize-to orig\" is used it will use the images without resizing";
  299.         print "\n  ";
  300.                    
  301.         print "\n        --start-with: <\"resize\"|\"getKeypoints\"|\"match\"|\"bundler\"|\"cmvs\"|\"pmvs\">";
  302.         print "\n             default: resize";
  303.         print "\n                      will start the sript at the specified step";
  304.         print "\n  ";
  305.                    
  306.         print "\n          --end-with: <\"resize\"|\"getKeypoints\"|\"match\"|\"bundler\"|\"cmvs\"|\"pmvs\">";
  307.         print "\n             default: pmvs";
  308.         print "\n                      will stop the sript after the specified step";
  309.         print "\n  ";
  310.                    
  311.         print "\n          --run-only: <\"resize\"|\"getKeypoints\"|\"match\"|\"bundler\"|\"cmvs\"|\"pmvs\">";
  312.         print "\n                      will only execute the specified step";
  313.         print "\n                      equal to --start-with <step> --end-with <step>";
  314.         print "\n  ";
  315.                    
  316.         print "\n       --force-focal: <positive float>";
  317.         print "\n                      override the focal length information for the images";
  318.         print "\n  ";
  319.                    
  320.         print "\n         --force-ccd: <positive float>";
  321.         print "\n                      override the ccd width information for the images";
  322.         print "\n  ";
  323.                    
  324.         print "\n --matcher-threshold: <float> (percent)";
  325.         print "\n             default: 2.0";
  326.         print "\n                      ignore matched keypoints if the two images share less then <float> percent of keypoints";
  327.         print "\n  ";
  328.                    
  329.         print "\n     --matcher-ratio: <float";
  330.         print "\n             default: 0.6";
  331.         print "\n                      ratio of the distance to the next best matched keypoint";
  332.         print "\n  ";
  333.        
  334.        
  335.                    
  336.         print "\n    --cmvs-maxImages: <positive integer>";
  337.         print "\n             default: 100";
  338.         print "\n                      the maximum number of images per cluster";
  339.         print "\n  ";
  340.                    
  341.         print "\n        --pmvs-level: <positive integer>";
  342.         print "\n             default: 1";
  343.                    
  344.         print "\n        --pmvs-csize: <positive integer>";
  345.         print "\n             default: 2";
  346.                    
  347.         print "\n    --pmvs-threshold: <float: -1.0 <= x <= 1.0>";
  348.         print "\n             default: 0.7";
  349.                    
  350.         print "\n        --pmvs-wsize: <positive integer>";
  351.         print "\n             default: 7";
  352.                    
  353.         print "\n  --pmvs-minImageNum: <positive integer>";
  354.         print "\n             default: 3";
  355.         print "\n                      see http://grail.cs.washington.edu/software/pmvs/documentation.html for an explanation of these parameters";
  356.         print "\n";
  357.        
  358.         print "\n     --odm_meshing-maxVertexCount: <positive integer>";
  359.         print "\n                       default: 100000";
  360.         print "\n                                The maximum vertex count of the output mesh.";
  361.                    
  362.         print "\n     --odm_meshing-octreeDepth: <positive integer>";
  363.         print "\n                       default: 9";
  364.         print "\n                                Octree depth used in the mesh reconstruction, increase to get more vertices, recommended values are 8-12";
  365.                    
  366.         print "\n  --odm_meshing-samplesPerNode: <float: 1.0 <= x>";
  367.         print "\n                       default: 1";
  368.         print "\n                                Number of points per octree node, recommended value: 1.0";
  369.  
  370.         print "\n  --zip-results: <true|false>";
  371.         print "\n        default: true";
  372.         print "\n                 Set to false if you do not want to have gunzipped tarball of the results.";
  373.  
  374.         exit;
  375.     }
  376.    
  377.     print "\n  - configuration:";
  378.    
  379.     foreach $args_key (sort keys %args) {
  380.         if($args{$args_key} ne ""){
  381.             print "\n    $args_key: $args{$args_key}";
  382.         }
  383.     }
  384.    
  385.     print "\n";
  386.     print "\n";
  387. }
  388.  
  389. sub prepareObjects {
  390.  ## get the source list    
  391.     @source_files = `ls -1 | egrep "\.[jJ]{1}[pP]{1}[eE]{0,1}[gG]{1}"`;
  392.    
  393.     print "\n  - source files - "; now(); print "\n";
  394.  
  395.     foreach $file (@source_files) {
  396.         chomp($file);
  397.        
  398.         chomp($file_make        = `jhead \"$file\" | grep "Camera make"`);
  399.         chomp($file_model       = `jhead \"$file\" | grep "Camera model"`);
  400.         chomp($file_focal       = `jhead \"$file\" | grep "Focal length"`);
  401.         chomp($file_ccd         = `jhead \"$file\" | grep "CCD width"`);
  402.         chomp($file_resolution  = `jhead \"$file\" | grep "Resolution"`);
  403.    
  404.         my %fileObject = {};
  405.    
  406.         chomp(($fileObject{src})        = $file);
  407.         chomp(($fileObject{base})       = $file);
  408.         $fileObject{base}               =~ s/\.[^\.]*$//;
  409.    
  410.         chomp(($fileObject{make})       = $file_make =~ /: ([^\n\r]*)/);
  411.         chomp(($fileObject{model})      = $file_model =~ /: ([^\n\r]*)/);
  412.        
  413.         $fileObject{make} =~ s/^\s+//;      $fileObject{make} =~  s/\s+$//;
  414.         $fileObject{model} =~ s/^\s+//;     $fileObject{model} =~  s/\s+$//;
  415.    
  416.         $fileObject{id}                 = $fileObject{make}." ".$fileObject{model};
  417.    
  418.         ($fileObject{width}, $fileObject{height})    = $file_resolution =~ /: ([0-9]*) x ([0-9]*)/;
  419.            
  420.         if(!$args{"--force-focal"}){
  421.             ($fileObject{focal})      = $file_focal =~ /:[\ ]*([0-9\.]*)mm/;
  422.         } else {
  423.             $fileObject{focal}        = $args{"--force-focal"};
  424.         }
  425.        
  426.         if(!$args{"--force-ccd"}){
  427.             ($fileObject{ccd})        = $file_ccd =~ /:[\ ]*([0-9\.]*)mm/;
  428.            
  429.             if(!$fileObject{ccd}){;
  430.                 $fileObject{ccd}      = $ccdWidths->{$fileObject{id}};
  431.             }
  432.         } else {
  433.             $fileObject{ccd}          = $args{"--force-ccd"};
  434.         }
  435.    
  436.         if($fileObject{ccd} && $fileObject{focal} && $fileObject{width} && $fileObject{height}){
  437.             if($fileObject{width} > $fileObject{height}){
  438.                 $fileObject{focalpx} = $fileObject{width} * ($fileObject{focal} / $fileObject{ccd});
  439.             } else {
  440.                 $fileObject{focalpx} = $fileObject{height} * ($fileObject{focal} / $fileObject{ccd});
  441.             }
  442.            
  443.             $fileObject{isOk} = true;
  444.             $objectStats{good}++;
  445.            
  446.             print "\n     using $fileObject{src}     dimensions: $fileObject{width}x$fileObject{height} / focal: $fileObject{focal}mm / ccd: $fileObject{ccd}mm";
  447.         } else {
  448.             $fileObject{isOk} = false;
  449.             $objectStats{bad}++;
  450.            
  451.             print "\n    no CCD width or focal length found for $fileObject{src} - camera: \"$fileObject{id}\"";
  452.         }
  453.    
  454.         $objectStats{count}++;
  455.    
  456.         if($objectStats{minWidth} == 0)  { $objectStats{minWidth}  = $fileObject{width}; }
  457.         if($objectStats{minHeight} == 0) { $objectStats{minHeight} = $fileObject{height}; }
  458.    
  459.         $objectStats{minWidth}  = $objectStats{minWidth}  < $fileObject{width}  ? $objectStats{minWidth}  : $fileObject{width};
  460.         $objectStats{minHeight} = $objectStats{minHeight} < $fileObject{height} ? $objectStats{minHeight} : $fileObject{height};
  461.         $objectStats{maxWidth}  = $objectStats{maxWidth}  > $fileObject{width}  ? $objectStats{maxWidth}  : $fileObject{width};
  462.         $objectStats{maxHeight} = $objectStats{maxHeight} > $fileObject{height} ? $objectStats{maxHeight} : $fileObject{height};
  463.            
  464.         push(@objects, \%fileObject);
  465.     }
  466.    
  467.    
  468.    
  469.     if(!$objectStats{good}){
  470.         print "\n\n    found no usable images - quitting\n";
  471.         die;
  472.     } else {
  473.         print "\n\n    found $objectStats{good} usable images";
  474.     }
  475.    
  476.     print "\n";
  477.    
  478.     $jobOptions{resizeTo} = $args{"--resize-to"};
  479.  
  480.     print "\n  using max image size of $jobOptions{resizeTo} x $jobOptions{resizeTo}";    
  481.    
  482.     $jobOptions{jobDir} = "$jobOptions{srcDir}/reconstruction-with-image-size-$jobOptions{resizeTo}";
  483.    
  484.     $jobOptions{step_1_convert}         = "$jobOptions{jobDir}/_convert.templist.txt";
  485.     $jobOptions{step_1_vlsift}          = "$jobOptions{jobDir}/_vlsift.templist.txt";
  486.     $jobOptions{step_1_gzip}            = "$jobOptions{jobDir}/_gzip.templist.txt";
  487.    
  488.     $jobOptions{step_2_filelist}        = "$jobOptions{jobDir}/_filelist.templist.txt";
  489.     $jobOptions{step_2_macthes_jobs}    = "$jobOptions{jobDir}/_matches_jobs.templist.txt";
  490.     $jobOptions{step_2_matches_dir}     = "$jobOptions{jobDir}/matches";
  491.     $jobOptions{step_2_matches}         = "$jobOptions{jobDir}/matches.init.txt";
  492.    
  493.     $jobOptions{step_3_filelist}        = "$jobOptions{jobDir}/list.txt";
  494.     $jobOptions{step_3_bundlerOptions}  = "$jobOptions{jobDir}/options.txt";
  495.    
  496.     mkdir($jobOptions{jobDir});
  497.        
  498.     foreach $fileObject (@objects) {
  499.         if($fileObject->{isOk}){
  500.             $fileObject->{step_0_resizedImage}  = "$jobOptions{jobDir}/$fileObject->{base}.jpg";
  501.            
  502.             $fileObject->{step_1_pgmFile}       = "$jobOptions{jobDir}/$fileObject->{base}.pgm";
  503.             $fileObject->{step_1_keyFile}       = "$jobOptions{jobDir}/$fileObject->{base}.key";
  504.             $fileObject->{step_1_gzFile}        = "$jobOptions{jobDir}/$fileObject->{base}.key.gz";
  505.         }
  506.     }
  507.    
  508. #    exit
  509. }
  510.  
  511. sub resize {
  512.     print "\n";
  513.     print "\n  - preparing images - "; now(); print "\n";
  514.     print "\n";
  515.  
  516.     chdir($jobOptions{jobDir});
  517.    
  518.     foreach $fileObject (@objects) {
  519.         if($fileObject->{isOk}){
  520.             unless (-e "$fileObject->{step_0_resizedImage}"){
  521.               if($jobOptions{resizeTo} != "orig" && (($fileObject->{width} > $jobOptions{resizeTo}) || ($fileObject->{height} > $jobOptions{resizeTo}))){
  522.                     print "\n    resizing $fileObject->{src} \tto $fileObject->{step_0_resizedImage}";
  523.                
  524.                     run("convert -resize $jobOptions{resizeTo}x$jobOptions{resizeTo} -quality 100 \"$jobOptions{srcDir}/$fileObject->{src}\" \"$fileObject->{step_0_resizedImage}\"");
  525.  
  526.                 } else {
  527.                     print "\n     copying $fileObject->{src} \tto $fileObject->{step_0_resizedImage}";
  528.                
  529.                     copy("$CURRENT_DIR/$fileObject->{src}", "$fileObject->{step_0_resizedImage}");
  530.                 }
  531.             } else {   
  532.                 print "\n     using existing $fileObject->{src} \tto $fileObject->{step_0_resizedImage}";
  533.             }
  534.            
  535.             chomp($file_resolution    = `jhead \"$fileObject->{step_0_resizedImage}\" | grep "Resolution"`);
  536.             ($fileObject->{width}, $fileObject->{height})    = $file_resolution =~ /: ([0-9]*) x ([0-9]*)/;
  537.             print "\t ($fileObject->{width} x $fileObject->{height})";
  538.         }
  539.     }
  540.    
  541.     if($args{"--end-with"} ne "resize"){
  542.         getKeypoints();
  543.     }
  544. }
  545.  
  546. sub getKeypoints {
  547.     print "\n";
  548.     print "\n  - finding keypoints - "; now(); print "\n";
  549.     print "\n\n";
  550.    
  551.     chdir($jobOptions{jobDir});
  552.    
  553.     $vlsiftJobs    = "";
  554.    
  555.     $c = 0;
  556.  
  557.     foreach $fileObject (@objects) {
  558.         $c = $c+1;
  559.    
  560.         if($fileObject->{isOk}){
  561.             if($args{"--lowe-sift"}){
  562.                 $vlsiftJobs    .= "echo -n \"$c/$objectStats{good} - \" && convert -format pgm \"$fileObject->{step_0_resizedImage}\" \"$fileObject->{step_1_pgmFile}\"";
  563.                 $vlsiftJobs    .= " && \"$BIN_PATH/sift\" < \"$fileObject->{step_1_pgmFile}\" > \"$fileObject->{step_1_keyFile}\"";
  564.                 $vlsiftJobs    .= " && gzip -f \"$fileObject->{step_1_keyFile}\"";
  565.                 $vlsiftJobs    .= " && rm -f \"$fileObject->{step_1_pgmFile}\"";
  566.                 $vlsiftJobs    .= " && rm -f \"$fileObject->{step_1_keyFile}.sift\"\n";
  567.             } else {
  568.                 unless (-e "$jobOptions{jobDir}/$fileObject->{base}.key.bin") {
  569.                     $vlsiftJobs    .= "echo -n \"$c/$objectStats{good} - \" && convert -format pgm \"$fileObject->{step_0_resizedImage}\" \"$fileObject->{step_1_pgmFile}\"";
  570.                     $vlsiftJobs    .= " && \"$BIN_PATH/vlsift\" \"$fileObject->{step_1_pgmFile}\" -o \"$fileObject->{step_1_keyFile}.sift\" > /dev/null && perl \"$BIN_PATH/../convert_vlsift_to_lowesift.pl\" \"$jobOptions{jobDir}/$fileObject->{base}\"";
  571.                     $vlsiftJobs    .= " && gzip -f \"$fileObject->{step_1_keyFile}\"";
  572.                     $vlsiftJobs    .= " && rm -f \"$fileObject->{step_1_pgmFile}\"";
  573.                     $vlsiftJobs    .= " && rm -f \"$fileObject->{step_1_keyFile}.sift\"\n";
  574.                 } else {
  575.                     print "using existing $jobOptions{jobDir}/$fileObject->{base}.key.bin\n";
  576.                 }
  577.             }
  578.         }
  579.     }
  580.    
  581.     open (SIFT_DEST, ">$jobOptions{step_1_vlsift}");
  582.     print SIFT_DEST $vlsiftJobs;
  583.     close(SIFT_DEST);
  584.    
  585.     run("\"$BIN_PATH/parallel\" --halt-on-error 1 -j+0 < \"$jobOptions{step_1_vlsift}\"");
  586.    
  587.     if($args{"--end-with"} ne "getKeypoints"){
  588.         match();
  589.     }
  590. }
  591.  
  592. sub match {
  593.     print "\n";
  594.     print "\n  - matching keypoints - "; now(); print "\n";
  595.     print "\n";
  596.  
  597.     chdir($jobOptions{jobDir});
  598.     mkdir($jobOptions{step_2_matches_dir});
  599.    
  600.     $matchesJobs = "";
  601.  
  602.     my $c = 0;
  603.     my $t = ($objectStats{good}-1) * ($objectStats{good}/2);
  604.    
  605.     for (my $i = 0; $i < $objectStats{good}; $i++) {
  606.         for (my $j = $i+1; $j < $objectStats{good}; $j++) {
  607.             $c++;
  608.             unless (-e "$jobOptions{step_2_matches_dir}/$i-$j.txt"){
  609.                
  610.                
  611.                 $matchesJobs        .=  "echo -n \".\" && touch \"$jobOptions{step_2_matches_dir}/$i-$j.txt\" && \"$BIN_PATH/KeyMatch\" \"@objects[$i]->{step_1_keyFile}\" \"@objects[$j]->{step_1_keyFile}\" \"$jobOptions{step_2_matches_dir}/$i-$j.txt\" $args{'--matcher-ratio'} $args{'--matcher-threshold'}\n";
  612.             }
  613.         }
  614.     }
  615.    
  616.     open (MATCH_DEST, ">$jobOptions{step_2_macthes_jobs}");
  617.     print MATCH_DEST $matchesJobs;
  618.     close(MATCH_DEST);
  619.    
  620.     run("\"$BIN_PATH/parallel\" --halt-on-error 1 -j+0 < \"$jobOptions{step_2_macthes_jobs}\"");
  621.    
  622.     run("rm -f \"$jobOptions{step_2_matches}\"");
  623.    
  624.     for (my $i = 0; $i < $objectStats{good}; $i++) {
  625.         for (my $j = $i+1; $j < $objectStats{good}; $j++) {
  626.             $c++;
  627.            
  628.             if (-e "$jobOptions{step_2_matches_dir}/$i-$j.txt" && (-s "$jobOptions{step_2_matches_dir}/$i-$j.txt") > 0) {
  629.                 run("echo \"$i $j\" >> \"$jobOptions{step_2_matches}\" && cat \"$jobOptions{step_2_matches_dir}/$i-$j.txt\" >> \"$jobOptions{step_2_matches}\"");
  630.             }
  631.         }
  632.     }
  633.    
  634.     foreach $fileObject (@objects) {
  635.         if($fileObject->{isOk}){
  636.             if($fileObject->{isOk}){
  637.                 $filesList        .= "$fileObject->{step_1_keyFile}\n";
  638.             }
  639.         }
  640.     }
  641.    
  642.     open (MATCH_DEST, ">$jobOptions{step_2_filelist}");
  643.     print MATCH_DEST $filesList;
  644.     close(MATCH_DEST);
  645.    
  646.  #   run("\"$BIN_PATH/KeyMatchFull\" \"$jobOptions{step_2_filelist}\" \"$jobOptions{step_2_matches}\"    ");
  647.    
  648.     if($args{"--end-with"} ne "match"){
  649.         bundler();
  650.     }
  651. }
  652.  
  653. sub bundler {
  654.     print "\n";
  655.     print "\n  - running bundler - "; now(); print "\n";
  656.     print "\n";
  657.    
  658.     chdir($jobOptions{jobDir});
  659.    
  660.     mkdir($jobOptions{jobDir}."/bundle");
  661.     mkdir($jobOptions{jobDir}."/pmvs");
  662.     mkdir($jobOptions{jobDir}."/pmvs/txt");
  663.     mkdir($jobOptions{jobDir}."/pmvs/visualize");
  664.     mkdir($jobOptions{jobDir}."/pmvs/models");
  665.    
  666.     $filesList = "";
  667.    
  668.     foreach $fileObject (@objects) {
  669.         if($fileObject->{isOk}){
  670.             if($fileObject->{isOk}){
  671.                 $filesList        .= sprintf("\./%s.jpg 0 %0.5f\n", $fileObject->{base}, $fileObject->{focalpx});
  672.             }
  673.         }
  674.     }
  675.    
  676.     chomp($filesList);
  677.    
  678.     $bundlerOptions  = "--match_table matches.init.txt\n";
  679.     $bundlerOptions .= "--output bundle.out\n";
  680.     $bundlerOptions .= "--output_all bundle_\n";
  681.     $bundlerOptions .= "--output_dir bundle\n";
  682.     $bundlerOptions .= "--variable_focal_length\n";
  683.     $bundlerOptions .= "--use_focal_estimate\n";
  684.     $bundlerOptions .= "--constrain_focal\n";
  685.     $bundlerOptions .= "--constrain_focal_weight 0.0\n";
  686.     $bundlerOptions .= "--estimate_distortion\n";
  687.     $bundlerOptions .= "--use_ceres\n";
  688.     $bundlerOptions .= "--run_bundle";
  689.        
  690.     system("echo \"$bundlerOptions\" > \"$jobOptions{step_3_bundlerOptions}\"");
  691.  
  692.     open (BUNDLER_DEST, ">$jobOptions{step_3_filelist}");
  693.     print BUNDLER_DEST $filesList;
  694.     close(BUNDLER_DEST);
  695.    
  696.     run("\"$BIN_PATH/bundler\" \"$jobOptions{step_3_filelist}\" --options_file \"$jobOptions{step_3_bundlerOptions}\" > bundle/out");
  697.     run("\"$BIN_PATH/Bundle2PMVS\" \"$jobOptions{step_3_filelist}\" bundle/bundle.out");
  698.     run("\"$BIN_PATH/RadialUndistort\" \"$jobOptions{step_3_filelist}\" bundle/bundle.out pmvs");
  699.    
  700.     $i = 0;
  701.    
  702.     foreach $fileObject (@objects) {
  703.         if($fileObject->{isOk}){
  704.             if($fileObject->{isOk}){
  705.                 if (-e "pmvs/$fileObject->{base}.rd.jpg"){
  706.                     $nr = sprintf("%08d", $i++);
  707.                
  708.                     system("mv pmvs/$fileObject->{base}.rd.jpg pmvs/visualize/$nr.jpg");
  709.                     system("mv pmvs/$nr.txt pmvs/txt/$nr.txt");
  710.                 }
  711.             }
  712.         }
  713.     }
  714.    
  715.     system("\"$BIN_PATH/Bundle2Vis\" pmvs/bundle.rd.out pmvs/vis.dat");
  716.    
  717.     if($args{"--end-with"} ne "bundler"){
  718.         cmvs();
  719.     }
  720. }
  721.  
  722. sub cmvs {
  723.     print "\n";
  724.     print "\n  - running cmvs - "; now(); print "\n";
  725.     print "\n";
  726.  
  727.     chdir($jobOptions{jobDir});
  728.    
  729.     run("\"$BIN_PATH/cmvs\" pmvs/ $args{'--cmvs-maxImages'} $CORES");
  730.     run("\"$BIN_PATH/genOption\" pmvs/ $args{'--pmvs-level'} $args{'--pmvs-csize'} $args{'--pmvs-threshold'} $args{'--pmvs-wsize'} $args{'--pmvs-minImageNum'} $CORES");
  731.    
  732.     if($args{"--end-with"} ne "cmvs"){
  733.         pmvs();
  734.     }
  735. }
  736.  
  737. sub pmvs {
  738.     print "\n";
  739.     print "\n  - running pmvs - "; now(); print "\n";
  740.     print "\n";
  741.  
  742.     chdir($jobOptions{jobDir});
  743.    
  744.     run("\"$BIN_PATH/pmvs2\" pmvs/ option-0000");
  745.    
  746.     system("cp -Rf \"$jobOptions{jobDir}/pmvs/models\" \"$jobOptions{jobDir}-results\"");
  747.    
  748.     if($args{"--end-with"} ne "pmvs"){
  749.         odm_meshing();
  750.     }
  751. }
  752.  
  753. sub odm_meshing {
  754.     print "\n";
  755.     print "\n  - running meshing - "; now(); print "\n";
  756.     print "\n";
  757.    
  758.     chdir($jobOptions{jobDir});
  759.     mkdir($jobOptions{jobDir}."/odm_meshing");    
  760.  
  761.  
  762.     run("\"$BIN_PATH/odm_meshing\" -inputFile $jobOptions{jobDir}-results/option-0000.ply -outputFile $jobOptions{jobDir}-results/odm_mesh-0000.ply -logFile $jobOptions{jobDir}/odm_meshing/odm_meshing_log.txt -maxVertexCount $args{'--odm_meshing-maxVertexCount'} -octreeDepth $args{'--odm_meshing-octreeDepth'} -samplesPerNode $args{'--odm_meshing-samplesPerNode'}" );
  763.    
  764.     if($args{"--end-with"} ne "odm_meshing"){
  765.         odm_texturing();
  766.     }
  767. }
  768.  
  769. sub odm_texturing {
  770.     print "\n";
  771.     print "\n  - running texturing - "; now(); print "\n";
  772.     print "\n";
  773.    
  774.     chdir($jobOptions{jobDir});
  775.     mkdir($jobOptions{jobDir}."/odm_texturing");  
  776.     mkdir("$jobOptions{jobDir}-results/odm_texturing");
  777.  
  778.    
  779.     run("\"$BIN_PATH/odm_texturing\" -bundleFile $jobOptions{jobDir}/pmvs/bundle.rd.out -imagesPath $jobOptions{srcDir}/ -imagesListPath $jobOptions{jobDir}/pmvs/list.rd.txt -inputModelPath $jobOptions{jobDir}-results/odm_mesh-0000.ply -outputFolder $jobOptions{jobDir}-results/odm_texturing/ -textureResolution $args{'--odm_texturing-textureResolution'} -bundleResizedTo $jobOptions{resizeTo} -textureWithSize $args{'--odm_texturing-textureWithSize'} -logFile $jobOptions{jobDir}/odm_texturing/odm_texturing_log.txt" );
  780.    
  781.     if($args{"--end-with"} ne "odm_texturing"){
  782.         odm_georeferencing();
  783.     }
  784.  
  785. }
  786.  
  787. sub odm_georeferencing {
  788.     print "\n";
  789.     print "\n  - running georeferencing - "; now(); print "\n";
  790.     print "\n";
  791.    
  792.     chdir($jobOptions{jobDir});
  793.     mkdir($jobOptions{jobDir}."/odm_georeferencing");
  794.  
  795.     if($args{"--odm_georeferencing-useGcp"} ne "true") {
  796.         run("\"$BIN_PATH/odm_extract_utm\" -imagesPath $jobOptions{srcDir}/ -imageListFile $jobOptions{jobDir}/pmvs/list.rd.txt -outputCoordFile $jobOptions{jobDir}/odm_georeferencing/coordFile.txt");  
  797.         run("\"$BIN_PATH/odm_georef\" -bundleFile $jobOptions{jobDir}/pmvs/bundle.rd.out -coordFile $jobOptions{jobDir}/odm_georeferencing/coordFile.txt -inputFile $jobOptions{jobDir}-results/odm_texturing/odm_textured_model.obj -outputFile $jobOptions{jobDir}-results/odm_texturing/odm_textured_model_geo.obj -logFile $jobOptions{jobDir}/odm_georeferencing/odm_georeferencing_log.txt");
  798.     } elsif (-e "$jobOptions{srcDir}/$args{'--odm_georeferencing-gcpFile'}") {
  799.         run("\"$BIN_PATH/odm_georef\" -bundleFile $jobOptions{jobDir}/pmvs/bundle.rd.out -gcpFile $jobOptions{srcDir}/$args{'--odm_georeferencing-gcpFile'} -imagesPath $jobOptions{srcDir}/ -imagesListPath $jobOptions{jobDir}/pmvs/list.rd.txt -bundleResizedTo $jobOptions{resizeTo} -inputFile $jobOptions{jobDir}-results/odm_texturing/odm_textured_model.obj -outputFile $jobOptions{jobDir}-results/odm_texturing/odm_textured_model_geo.obj -logFile $jobOptions{jobDir}/odm_georeferencing/odm_georeferencing_log.txt");
  800.     } else {
  801.         print "Warning: No GCP file. Consider rerunning with argument --odm_georeferencing-useGcp false --start-with odm_georeferencing";
  802.         print "Skipping orthophoto";
  803.         $args{"--end-with"} = "odm_georeferencing";
  804.     }
  805.    
  806.     if($args{"--end-with"} ne "odm_georeferencing"){
  807.         odm_orthophoto();
  808.     }
  809.  
  810. }
  811.  
  812.  
  813. sub odm_orthophoto {
  814.     print "\n";
  815.     print "\n  - running orthophoto generation - "; now(); print "\n";
  816.     print "\n";
  817.    
  818.     chdir($jobOptions{jobDir});
  819.     mkdir($jobOptions{jobDir}."/odm_orthophoto");
  820.  
  821.  
  822.     run("\"$BIN_PATH/odm_orthophoto\" -inputFile $jobOptions{jobDir}-results/odm_texturing/odm_textured_model_geo.obj -logFile $jobOptions{jobDir}/odm_orthophoto/odm_orthophoto_log.txt -outputFile $jobOptions{jobDir}-results/odm_orthphoto.png -resolution 20.0");
  823.  
  824. }
  825.  
  826. parseArgs();
  827. prepareObjects();
  828.    
  829. chdir($jobOptions{jobDir});
  830.    
  831. switch ($args{"--start-with"}) {
  832.     case "resize"              { resize();              }
  833.     case "getKeypoints"        { getKeypoints();        }    
  834.     case "match"               { match();               }
  835.     case "bundler"             { bundler();             }
  836.     case "cmvs"                { cmvs();                }
  837.     case "pmvs"                { pmvs();                }
  838.     case "odm_meshing"         { odm_meshing();         }
  839.     case "odm_texturing"       { odm_texturing();       }
  840.     case "odm_georeferencing"  { odm_georeferencing();  }
  841.     case "odm_orthophoto"      { odm_orthophoto();      }
  842. }
  843.  
  844. if($args{"--zip-results"} eq "true") {
  845.     print "\nCompressing results - "; now(); print "\n";
  846.     print "\n";
  847.     run("cd $jobOptions{jobDir}-results/ && tar -czf $jobOptions{jobDir}-results.tar.gz *");
  848. }
  849.  
  850. print "\n";
  851. print "\n  - done - "; now(); print "\n";
  852. print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement