Advertisement
Guest User

Untitled

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