Advertisement
Guest User

associate.py fix2 by senia kalma

a guest
Dec 1st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.27 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Software License Agreement (BSD License)
  3. #
  4. # Copyright (c) 2013, Juergen Sturm, TUM
  5. # All rights reserved.
  6. # Edited and improved by Senia Kalma 01/12/15
  7. # Now creates rgb_sync & depth_sync folders and puts the ordered synced pictures there(AS timestamps).
  8. #
  9. # Redistribution and use in source and binary forms, with or without
  10. # modification, are permitted provided that the following conditions
  11. # are met:
  12. #
  13. #  * Redistributions of source code must retain the above copyright
  14. #    notice, this list of conditions and the following disclaimer.
  15. #  * Redistributions in binary form must reproduce the above
  16. #    copyright notice, this list of conditions and the following
  17. #    disclaimer in the documentation and/or other materials provided
  18. #    with the distribution.
  19. #  * Neither the name of TUM nor the names of its
  20. #    contributors may be used to endorse or promote products derived
  21. #    from this software without specific prior written permission.
  22. #
  23. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. # POSSIBILITY OF SUCH DAMAGE.
  35. #
  36. # Requirements:
  37. # sudo apt-get install python-argparse
  38.  
  39. """
  40. The Kinect provides the color and depth images in an un-synchronized way. This means that the set of time stamps from the color images do not intersect with those of the depth images. Therefore, we need some way of associating color images to depth images.
  41.  
  42. For this purpose, you can use the ''associate.py'' script. It reads the time stamps from the rgb.txt file and the depth.txt file, and joins them by finding the best matches.
  43. """
  44.  
  45. import argparse
  46. import sys
  47. import os
  48. import numpy
  49.  
  50. #senia:
  51. import shutil
  52.  
  53.  
  54. def read_file_list(filename):
  55.     """
  56.    Reads a trajectory from a text file.
  57.    
  58.    File format:
  59.    The file format is "stamp d1 d2 d3 ...", where stamp denotes the time stamp (to be matched)
  60.    and "d1 d2 d3.." is arbitary data (e.g., a 3D position and 3D orientation) associated to this timestamp.
  61.    
  62.    Input:
  63.    filename -- File name
  64.    
  65.    Output:
  66.    dict -- dictionary of (stamp,data) tuples
  67.    
  68.    """
  69.     file = open(filename)
  70.     data = file.read()
  71.     lines = data.replace(","," ").replace("\t"," ").replace("rgb/","").replace("depth/","").split("\n")
  72.     list = [[v.strip() for v in line.split(" ") if v.strip()!=""] for line in lines if len(line)>0 and line[0]!="#"]
  73.     list = [(float(l[0]),l[1:]) for l in list if len(l)>1]
  74.     return dict(list)
  75.  
  76. def associate(first_list, second_list,offset,max_difference):
  77.     """
  78.    Associate two dictionaries of (stamp,data). As the time stamps never match exactly, we aim
  79.    to find the closest match for every input tuple.
  80.    
  81.    Input:
  82.    first_list -- first dictionary of (stamp,data) tuples
  83.    second_list -- second dictionary of (stamp,data) tuples
  84.    offset -- time offset between both dictionaries (e.g., to model the delay between the sensors)
  85.    max_difference -- search radius for candidate generation
  86.  
  87.    Output:
  88.    matches -- list of matched tuples ((stamp1,data1),(stamp2,data2))
  89.    
  90.    """
  91.     first_keys = first_list.keys()
  92.     second_keys = second_list.keys()
  93.     potential_matches = [(abs(a - (b + offset)), a, b)
  94.                          for a in first_keys
  95.                          for b in second_keys
  96.                          if abs(a - (b + offset)) < max_difference]
  97.     potential_matches.sort()
  98.     matches = []
  99.     for diff, a, b in potential_matches:
  100.         if a in first_keys and b in second_keys:
  101.             first_keys.remove(a)
  102.             second_keys.remove(b)
  103.             matches.append((a, b))
  104.    
  105.     matches.sort()
  106.     return matches
  107.  
  108. if __name__ == '__main__':
  109.    
  110.     # parse command line
  111.     parser = argparse.ArgumentParser(description='''
  112.    This script takes two data files with timestamps and associates them  
  113.    ''')
  114.     parser.add_argument('first_file', help='first text file (format: timestamp data)')
  115.     parser.add_argument('second_file', help='second text file (format: timestamp data)')
  116.     parser.add_argument('--first_only', help='only output associated lines from first file', action='store_true')
  117.     parser.add_argument('--offset', help='time offset added to the timestamps of the second file (default: 0.0)',default=0.0)
  118.     parser.add_argument('--max_difference', help='maximally allowed time difference for matching entries (default: 0.02)',default=0.02)
  119.     args = parser.parse_args()
  120.  
  121.     first_list = read_file_list(args.first_file)
  122.     second_list = read_file_list(args.second_file)
  123.  
  124.     matches = associate(first_list, second_list,float(args.offset),float(args.max_difference))
  125.  
  126.     if args.first_only:
  127.         for a,b in matches:
  128.             print("%f %s"%(a," ".join(first_list[a])))
  129.     else:
  130.         dirRGB=os.getcwd()+"\\rgb_sync"
  131.         dirDepth=os.getcwd()+"\\depth_sync"
  132.         print("From(RGB): " + os.getcwd() + "\\rgb\nFrom(Depth): " + os.getcwd() + "\depth")
  133.         print("To(RGB): " + dirRGB + "\nTo(Depth):" + dirDepth + "\n--Starting..")
  134.         if not os.path.exists(dirRGB):
  135.             os.makedirs(dirRGB)
  136.         if not os.path.exists(dirDepth):
  137.             os.makedirs(dirDepth)
  138.         for a,b in matches:
  139.             RGBFullLine=("%s\\rgb\%s-%s\\rgb_sync\%s"%(os.getcwd(), " ".join(first_list[a]), os.getcwd(), " ".join(first_list[a])))
  140.             DepthFullLine=("%s\\depth\%s-%s\\depth_sync\%s"%(os.getcwd(), " ".join(second_list[b]), os.getcwd(), " ".join(second_list[b])))
  141.             lineRGB = RGBFullLine.split("-")
  142.             lineDepth = DepthFullLine.split("-")
  143.             shutil.copyfile(lineRGB[0], lineRGB[1])
  144.             shutil.copyfile(lineDepth[0], lineDepth[1])
  145.         print("---Finished.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement