Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import csv
  2. from os import listdir, getcwd
  3. import pickle
  4. import os
  5. from os.path import join
  6.  
  7.  
  8. classes = ["car", "truck"]
  9. def convert_annotation(image_id):
  10.     cl=[]
  11.     coord=[]
  12.     b=()
  13.     out_file = open('labels/%s.txt'%(image_id), 'w')
  14.     with open('udacity.csv', 'rb') as csvfile:
  15.         spamreader = csv.reader(csvfile, delimiter=' ', quotechar='"')
  16.         for row in spamreader:
  17.             if row[0]==image_id:
  18.                 for i in range(0,len(row)/5):
  19.                     c= int(row[(i*5)+5])
  20.                     if c not in [1, 3, 4]:
  21.                         cl.append(c)
  22.                         coord.append((row[i*5+1],row[i*5+3],row[i*5+2],row[i*5+4]))
  23.                     else:
  24.                         continue
  25.                                
  26.         for i in range(0,len(cl)):
  27.             b=(coord[i][0], coord[i][1], coord[i][2], coord[i][3])
  28.             w=int(coord[i][1])-int(coord[i][0])
  29.             h=int(coord[i][3])-int(coord[i][2])
  30.             if cl[i] == 0:
  31.                 out_file.write("car (" + " ".join([str(a) for a in b]) +") "+str(w)+" "+str(h)+ '\n')
  32.             else:
  33.                 out_file.write("truck (" + " ".join([str(a) for a in b]) +") "+str(w)+" "+str(h)+ '\n')
  34.  
  35. wd = getcwd()
  36. if not os.path.exists('labels/'):
  37.     os.makedirs('labels/')
  38. list_file = open('imageSet.txt', 'w')
  39. with open('udacity.csv', 'rb') as csvfile:
  40.     reader = csv.reader(csvfile, delimiter=' ',quotechar='"')
  41.     for row in reader:
  42.         list_file.write('%s/JPEGImages/%s\n'%(wd, row[0]))
  43.         convert_annotation(row[0])
  44.     list_file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement