def sort_imgs(annotation_folder, reqcat=5, to_break=False): #reqcat = 5 for airplanes #get airplane paths global plane_name global n co = coco.COCO(annotation_folder) plane_ids = co.getImgIds(catIds=[reqcat]) n = 0 #iterate over airplane IDs while n < len(plane_ids): #for plane_id in plane_ids: plane_img = co.loadImgs(ids=plane_ids[n])[0] plane_name = plane_img['file_name'] # '000000221184.jpg' img_path = os.path.join(img_dir, plane_name) # set windows size and read plane path fig, ax = plt.subplots(1) mngr = plt.get_current_fig_manager() mngr.window.setGeometry(250, 100, 1280, 824) image = cv2.imread(img_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #get contour annIds = co.getAnnIds(imgIds=plane_ids[n]) for annId in annIds: if co.anns[annId]['category_id'] == reqcat: try: seg = co.anns[annId]['segmentation'][0] poly = np.array(seg).reshape( (int(len(seg) / 2), 1, 2)).astype(int) #arr(points,1,2) image_con = image image_con = cv2.drawContours(image_con, [poly], 0, (0, 255, 0), 3) except: print(co.imgs[plane_ids[n]]['file_name'], plane_ids[n], annId) #show images and connect event ax.imshow(image_con) plt.connect('key_press_event', onkeypress) plt.show() n += 1 if n % 50 == 0: print(n) np.save('good_pic_name', good_full_plane_ids) if to_break: if n > 5: break
def create_coco(val_json_file, use_cpp_extension=True): """Creates Microsoft COCO helper class object and return it.""" if val_json_file.startswith('gs://'): _, local_val_json = tempfile.mkstemp(suffix='.json') tf.gfile.Remove(local_val_json) tf.gfile.Copy(val_json_file, local_val_json) atexit.register(tf.gfile.Remove, local_val_json) else: local_val_json = val_json_file if use_cpp_extension: coco_gt = coco.COCO(local_val_json, False) else: coco_gt = COCO(local_val_json) return coco_gt
def __init__(self, filename, use_cpp_extension=False): """Constructs COCO evaluation class. The class provides the interface to metrics_fn in TPUEstimator. The _update_op() takes detections from each image and push them to self.detections. The _evaluate() loads a JSON file in COCO annotation format as the groundtruths and runs COCO evaluation. Args: filename: Ground truth JSON file name. If filename is None, use groundtruth data passed from the dataloader for evaluation. use_cpp_extension: use cocoeval C++ library. """ if filename: if filename.startswith('gs://'): _, local_val_json = tempfile.mkstemp(suffix='.json') tf.gfile.Remove(local_val_json) tf.gfile.Copy(filename, local_val_json) atexit.register(tf.gfile.Remove, local_val_json) else: local_val_json = filename self.use_cpp_extension = use_cpp_extension if self.use_cpp_extension: self.coco_gt = coco.COCO(local_val_json, use_mask=True) else: self.coco_gt = MaskCOCO(local_val_json) self.filename = filename self.metric_names = [ 'AP', 'AP50', 'AP75', 'APs', 'APm', 'APl', 'ARmax1', 'ARmax10', 'ARmax100', 'ARs', 'ARm', 'ARl' ] mask_metric_names = ['mask_' + x for x in self.metric_names] self.metric_names.extend(mask_metric_names) self._reset()
trainorval = 'train' reqcat = 5 savedir = 'cut_planes_{}'.format(trainorval) if not os.path.isdir(savedir): os.mkdir(savedir) file__path = os.path.dirname(__file__) ann_folder = 'instances_{}2017.json'.format(trainorval) annotation_folder = os.path.join(file__path, 'annotations', ann_folder) img_dir = os.path.join(file__path, '{}2017'.format(trainorval)) good_plane_names = np.load('good_plane_names.npy') n = 0 co = coco.COCO(annotation_folder) plane_ids = co.getImgIds(catIds=[reqcat]) adjusted_good_cuts = [] while n < len(good_plane_names): plane_name_plus = good_plane_names[n] plane_img = co.loadImgs(ids=plane_ids[n])[0] plane_name_one = plane_img['file_name'] # '000000221184.jpg' #print(n,adjusted_good_cuts) value, plane_name = plane_name_plus.split(' ') if value == 'ok' and plane_name_one == plane_name: fig, ax = plt.subplots(1) img_path = os.path.join(img_dir, plane_name)
""" import coco import os import cv2 import numpy as np from lxml import etree import xml.etree.cElementTree as ET #from generate_xml import write_xml trainorval='val' reqcat=5 annotation_file=os.path.join(os.path.join(os.path.dirname(__file__),'annotations'),'instances_{}2017.json'.format(trainorval)) co=coco.COCO(annotation_file) print(co.createIndex()) plane_ids=co.getImgIds(catIds=[reqcat]) planeann_ids=co.getAnnIds(catIds=[reqcat]) planeann_ids2=co.getAnnIds(imgIds=plane_ids) anns=co.loadAnns(ids=planeann_ids) #co.showAnns(anns[0]) #anns[0]['bbox'] #anns[0]['image_id'] #anns[0]['segmentation'] #co.loadImgs(ids=[anns[0]['image_id']])[0]['file_name'] #300659 #for i in anns:print(i['image_id'])
imagefile = "/asm/project_mvtec/mvtec_d2s/images" imageurl = imagefile + Imgs['file_name'] #print imageurl I = io.imread(imageurl) plt.imshow(I) plt.show() return Imgs, I ,Imgs['file_name'] dataDir='..' dataType='val2017' annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType) annFile = "/asm/project_mvtec/mvtec_d2s/annotations/D2S_training.json" coco=coco.COCO(annFile) #print coco.loadAnns(coco.getAnnIds()) # print (len(coco.getCatIds())) # print coco.getAnnIds()[0]) cats = coco.loadCats(coco.getCatIds()) nms=[cat['name'] for cat in cats] # for i in range(len(nms)): # print nms[i] #time.sleep(100) I_w = 1920.0 I_h = 1440.0 #print coco.loadAnns(coco.getAnnIds()[0])