Created
June 9, 2017 14:23
-
-
Save helxsz/c183ad353d0be715f9143b6ca2ac109c to your computer and use it in GitHub Desktop.
aerial imaging test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import matplotlib.patches as patches | |
| import matplotlib as mpl | |
| from PIL import Image | |
| import os | |
| def read(file_path, image_path, vis=False, annotationPath=None): | |
| src_txt_dir = file_path | |
| im = Image.open(image_path) | |
| img_ext = image_path.split('/')[-1] | |
| img_name = img_ext.split('.')[0] | |
| img_width, img_height = im.size | |
| print file_path, image_path, img_width, img_height | |
| if(vis): | |
| fig = plt.figure() | |
| ax = fig.add_subplot(111) | |
| ax.set_xlim(0,img_width);ax.set_ylim(0,img_height); | |
| plt.grid('on'); | |
| plt.imshow(im) | |
| coordinates = np.empty([0,5]) | |
| boxes = [] | |
| with open(src_txt_dir, "r") as f: | |
| i = 0 | |
| for line in f.readlines(): | |
| i+=1 | |
| if(i>6): | |
| ######################################################## | |
| print line | |
| arr = line.split(" ") | |
| center_x, center_y, width, height, angle = int(arr[2]), int(arr[3]), int(arr[4]), int(arr[5]), float(arr[6]) | |
| ######################################################## | |
| if(vis): | |
| ts = ax.transData | |
| coords = ts.transform([center_x-width,center_y-height/2]) | |
| tr = mpl.transforms.Affine2D().rotate_deg_around(coords[0], coords[1], angle) | |
| t = ts + tr | |
| rotation = mpl.transforms.Affine2D().rotate_deg(45) + ax.transData | |
| #,transform=t | |
| rec0 = patches.Rectangle((center_x-width,center_y-height),width*2.5,height*4,alpha=0.5) | |
| ax.add_patch(rec0) | |
| ######################################################## | |
| boxes.append([center_x, center_y+3*height, width*2.5, height*4]) | |
| coordinates = np.vstack( (coordinates, np.array([center_x, center_y, width, height, angle]) )) | |
| print coordinates[1:3,:], len(boxes) | |
| createAnnotation(img_name,img_width, img_height,boxes) | |
| if(vis): | |
| plt.show() | |
| def createAnnotation(img_name,width, height,bounding_boxes, dest_txt_dir = "/home/gnss/data/parking/aerial-parking/Annotations"): | |
| print "createAnnotation",img_name, len(bounding_boxes) | |
| # write in xml file | |
| fn = dest_txt_dir + '/' + img_name + '.xml' | |
| os.remove(fn) if os.path.exists(fn) else None | |
| os.mknod(fn,0777) | |
| xml_file = open((dest_txt_dir + '/' + img_name + '.xml'), 'w') | |
| xml_file.write('<annotation>\n') | |
| xml_file.write(' <folder>VOC2007</folder>\n') | |
| xml_file.write(' <filename>' + 'JPG' + '</filename>\n') | |
| xml_file.write(' <size>\n') | |
| xml_file.write(' <width>' + str(width) + '</width>\n') | |
| xml_file.write(' <height>' + str(height) + '</height>\n') | |
| xml_file.write(' <depth>3</depth>\n') | |
| xml_file.write(' </size>\n') | |
| # write the region of text on xml file | |
| for bounding_box in bounding_boxes: | |
| print bounding_box | |
| xml_file.write(' <object>\n') | |
| xml_file.write(' <name>car</name>\n') | |
| xml_file.write(' <pose>Unspecified</pose>\n') | |
| xml_file.write(' <truncated>0</truncated>\n') | |
| xml_file.write(' <difficult>0</difficult>\n') | |
| xml_file.write(' <bndbox>\n') | |
| xml_file.write(' <xmin>' + str(bounding_box[0]) + '</xmin>\n') | |
| xml_file.write(' <ymin>' + str(bounding_box[1]) + '</ymin>\n') | |
| xml_file.write(' <xmax>' + str(bounding_box[0]+bounding_box[2]) + '</xmax>\n') | |
| xml_file.write(' <ymax>' + str(bounding_box[1]+bounding_box[3]) + '</ymax>\n') | |
| xml_file.write(' </bndbox>\n') | |
| xml_file.write(' </object>\n') | |
| xml_file.write('</annotation>') | |
| import glob, os | |
| os.chdir("/home/gnss/data/aerial-image/Train") | |
| for jpg_file in glob.glob("*.JPG"): | |
| print(jpg_file) | |
| name = jpg_file.split('.')[0] | |
| samp_file = name+ "_pkw.samp" | |
| read(samp_file,jpg_file,True,"/home/gnss/data/parking/aerial-parking/Annotations") | |
| ''''' | |
| coordinates = np.empty([1,5]) | |
| #np.vstack( (coordinates, np.array([1, 2, 3, 4, 5]) )) | |
| #print coordinates | |
| np.append(coordinates, np.array([1, 2, 3, 4, 5]), axis=0) | |
| print coordinates | |
| ''''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment