• Kv Manohar's avatar
    Merge pull request #1253 from kvmanohar22:GSoC17_dnn_objdetect · 41a5a5ea
    Kv Manohar authored
    GSoC'17 Learning compact models for object detection (#1253)
    
    * Final solver and model for SqueezeNet model
    
    * update README
    
    * update dependencies and CMakeLists
    
    * add global pooling
    
    * Add training scripts
    
    * fix typo
    
    * fix dependency of caffe
    
    * fix whitespace
    
    * Add squeezedet architecture
    
    * Pascal pre process script
    
    * Adding pre process scripts
    
    * Generate the graph of the model
    
    * more readable
    
    * fix some bugs in the graph
    
    * Post process class implementation
    
    * Complete minimal post processing and standalone running
    
    * Complete the base class
    
    * remove c++11 features and fix bugs
    
    * Complete example
    
    * fix bugs
    
    * Adding final scripts
    
    * Classification scripts
    
    * Update README.md
    
    * Add example code and results
    
    * Update README.md
    
    * Re-order and fix some bugs
    
    * fix build failure
    
    * Document classes and functions
    
    * Add instructions on how to use samples
    
    * update instructionos
    
    * fix docs failure
    
    * fix conversion types
    
    * fix type conversion warning
    
    * Change examples to sample directoryu
    
    * restructure directories
    
    * add more references
    
    * fix whitespace
    
    * retain aspect ratio
    
    * Add more examples
    
    * fix docs warnings
    
    * update with links to trained weights
    
    * threshold update
    
    * png -> jpg
    
    * fix tutorial
    
    * model files
    
    * precomp.hpp , fix readme links, module dependencies
    
    * copyrights
    
    - no copyright in samples
    - use new style OpenCV copyright header
    - precomp.hpp
    41a5a5ea
pascal_preprocess.py 1.59 KB
from skimage import io, transform
from multiprocessing.dummy import Pool as ThreadPool

def rescale(root_new, root_old, img_path, ann_path, out_shape):
  try:
    img = io.imread(root_old+"/"+img_path)
  except Exception as E:
    print E
  h, w, _ = img.shape
  f_h, f_w = float(out_shape)/h, float(out_shape)/w
  trans_img = transform.rescale(img, (f_h, f_w))
  num_objs = 0
  with open(root_old+"/"+ann_path, 'r') as f:
    ann = f.readline()
    ann = ann.rstrip()
    ann = ann.split(' ')
    ann = [float(i) for i in ann]
    num_objs = len(ann) / 5
    for idx in xrange(num_objs):
      ann[idx * 5 + 0] = int(f_w * ann[idx * 5 + 0])
      ann[idx * 5 + 1] = int(f_h * ann[idx * 5 + 1])
      ann[idx * 5 + 2] = int(f_w * ann[idx * 5 + 2])
      ann[idx * 5 + 3] = int(f_h * ann[idx * 5 + 3])
    # Write the new annotations to file
    with open(root_new+"/"+ann_path, 'w') as f_new:
      for val in ann:
        f_new.write(str(val)+' ')
  # Save the new image
  io.imwrite(root_new+"/"+img_path, trans_img)

def preprocess():
  source = '/users2/Datasets/PASCAL_VOC/VOCdevkit/VOC2012_Resize/source.txt'
  root_old = '/users2/Datasets/PASCAL_VOC/VOCdevkit/VOC2012'
  root_new = '/users2/Datasets/PASCAL_VOC/VOCdevkit/VOC2012_Resize'
  out_shape = 416
  with open(source, 'r') as src:
    lines = src.readlines()
    print 'Processing {} images and annotations'.format(len(lines))
    for line in lines:
      line = line.rstrip()
      line = line.split(' ')
      img_path = line[0]
      ann_path = line[1]
      rescale(root_new, root_old, img_path, ann_path, out_shape)

if __name__ == '__main__':
  preprocess()