def save_results(image_path, anno, fname='result'): """Saves results of the prediction. Args: image_path (string): The path to source image to predict bounding boxes. anno (Annotation, list): The predicted annotations for source image or the list of bounding boxes. Returns: Nothing. """ # draw new_img = Image.open(image_path) d = ImageDraw.Draw(new_img) is_list = type(anno) is list rects = anno if is_list else anno.rects for r in rects: if is_list: d.rectangle([r['x1'], r['y1'], r['x2'], r['y2']], outline=(255, 0, 0)) else: d.rectangle([r.left(), r.top(), r.right(), r.bottom()], outline=(255, 0, 0)) # save prediction_image_path = os.path.join(os.path.dirname(image_path), fname + '.png') new_img.save(prediction_image_path) subprocess.call(['chmod', '644', prediction_image_path]) fpath = os.path.join(os.path.dirname(image_path), fname + '.json') if is_list: json.dump({'image_path': prediction_image_path, 'rects': anno}, open(fpath, 'w')) else: al.saveJSON(fpath, anno) subprocess.call(['chmod', '644', fpath])
def save_results(image_path, anno): """Saves results of the prediction. Args: image_path (string): The path to source image to predict bounding boxes. anno (Annotation): The predicted annotations for source image. Returns: Nothing. """ # draw new_img = Image.open(image_path) d = ImageDraw.Draw(new_img) rects = anno['rects'] if type(anno) is dict else anno.rects for r in rects: d.rectangle([r.left(), r.top(), r.right(), r.bottom()], outline=(255, 0, 0)) # save fpath = os.path.join(os.path.dirname(image_path), 'result.png') new_img.save(fpath) subprocess.call(['chmod', '777', fpath]) fpath = os.path.join(os.path.dirname(image_path), 'result.json') if type(anno) is dict: with open(fpath, 'w') as f: json.dump(anno, f) else: al.saveJSON(fpath, anno) subprocess.call(['chmod', '777', fpath])
def detect(self, image): pred_annolist = al.AnnoList() #true_annolist = al.parse(args.test_boxes) #data_dir = os.path.dirname(args.test_boxes) #image_dir = get_image_dir(args) #subprocess.call('mkdir -p %s' % image_dir, shell=True) orig_img = image[:,:,:3] img = imresize(orig_img, (self.H["image_height"], self.H["image_width"])) feed = {self.x_in: img} (np_pred_boxes, np_pred_confidences) = self.sess.run([self.pred_boxes, self.pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = "test" new_img, rects = add_rectangles(self.H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=self.H['rnn_len'], min_conf=0.2, tau=0.25, show_suppressed=False) pred_anno.rects = rects pred_anno.imagePath = "none"#os.path.abspath(data_dir) pred_anno = rescale_boxes((self.H["image_height"], self.H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) predictions = [] for pred in pred_annolist: predictions.append([pred.rects[0].x1, pred.rects[0].y1, pred.rects[0].x2, pred.rects[0].y2]) misc.imsave("test.jpg", new_img) return predictions
def save_results(image_path, anno): """Saves results of the prediction. Args: image_path (string): The path to source image to predict bounding boxes. anno (Annotation): The predicted annotations for source image. Returns: Nothing. """ # draw new_img = Image.open(image_path) d = ImageDraw.Draw(new_img) for r in anno.rects: d.rectangle([r.left(), r.top(), r.right(), r.bottom()], outline=(0, 255, 0)) detections_count=len(anno.rects) if detections_count>0: print("Number of target detections:", detections_count) else: print("Target hasn't been detected.") # save output_path=os.path.dirname(image_path)+os.path.sep+'output' if not os.path.exists(output_path): os.makedirs(output_path) fpath = os.path.join(output_path, os.path.basename(image_path)+'_result.png') new_img.save(fpath) subprocess.call(['chmod', '777', fpath]) fpath = os.path.join(output_path, os.path.basename(image_path)+'_result.json') al.saveJSON(fpath, anno) subprocess.call(['chmod', '777', fpath])
def get_results(args, H, data_dir): tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] if args.frozen_graph: graph = load_frozen_graph(args.graphfile) else: new_saver = tf.train.import_meta_graph(args.graphfile) NUM_THREADS = 8 with tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=NUM_THREADS), graph=graph if args.frozen_graph else None) as sess: sess.run(tf.global_variables_initializer()) if args.frozen_graph: x_in = graph.get_tensor_by_name('x_in:0') pred_boxes = graph.get_tensor_by_name('add:0') pred_confidences = graph.get_tensor_by_name('Reshape_2:0') else: new_saver.restore(sess, args.weights) x_in = tf.get_collection('placeholders')[0] pred_boxes, pred_confidences = tf.get_collection('vars') #freeze_graph.freeze_graph("overfeat.pb", "", False, args.weights, "add,Reshape_2", "save/restore_all", #"save/Const:0", "overfeat_frozen.pb", False, '') pred_annolist = al.AnnoList() included_extenstions = ['jpg', 'bmp', 'png', 'gif'] image_names = [fn for fn in os.listdir(args.datadir) if any(fn.lower().endswith(ext) for ext in included_extenstions)] image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(image_names)): image_name = image_names[i] if H['grayscale']: orig_img = cv2.imread(image_name) cv2.cvtColor(orig_img,cv2.COLOR_BGR2RGB) if len(orig_img.shape) < 3: orig_img = cv2.cvtColor(orig_img, cv2.COLOR_GRAY2RGB) else: orig_img = cv2.imread('%s/%s' % (data_dir, image_name)) cv2.cvtColor(orig_img,cv2.COLOR_BGR2RGB) img = cv2.resize(orig_img, (H["image_width"],H["image_height"]), interpolation=cv2.INTER_CUBIC) feed = {x_in: img} start_time = time() (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) time_2 = time() pred_anno = al.Annotation() pred_anno.imageName = image_name new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) print(time() - start_time) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1], test=True) pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(image_name)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) return pred_annolist
def eval(self, weights, test_boxes, min_conf, tau, show_suppressed, expname): self.H["grid_width"] = self.H["image_width"] / self.H["region_size"] self.H["grid_height"] = self.H["image_height"] / self.H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[self.H['image_height'], self.H['image_width'], 3]) if self.H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = self.build_forward( tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = self.H['grid_height'] * self.H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * self.H['rnn_len'], 2])), [grid_area, self.H['rnn_len'], 2]) if self.H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = self.build_forward(tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, weights) pred_annolist = al.AnnoList() true_annolist = al.parse(test_boxes) data_dir = os.path.dirname(test_boxes) image_dir = self.get_image_dir(weights, expname, test_boxes) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] print(true_anno.imageName) orig_img = cv2.imread(true_anno.imageName)[:, :, :3] cv2.cvtColor(orig_img, cv2.COLOR_BGR2RGB) img = cv2.resize(orig_img, (self.H["image_width"], self.H["image_height"])) feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(self.H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=self.H['rnn_len'], min_conf=min_conf, tau=tau, show_suppressed=show_suppressed) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((self.H["image_height"], self.H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) cv2.imwrite(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name="x_in", shape=[H["image_height"], H["image_width"], 3]) googlenet = googlenet_load.init(H) if H["use_rezoom"]: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), googlenet, "test", reuse=None ) grid_area = H["grid_height"] * H["grid_width"] pred_confidences = tf.reshape( tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H["rnn_len"], 2])), [grid_area, H["rnn_len"], 2] ) if H["reregress"]: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), googlenet, "test", reuse=None ) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_idl) data_dir = os.path.dirname(args.test_idl) image_dir = get_image_dir(args) subprocess.call("mkdir -p %s" % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread("%s/%s" % (data_dir, true_anno.imageName))[:, :, :3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp="cubic") feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles( H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H["rnn_len"], min_conf=0.2, tau=args.tau, ) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes( (H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1] ) pred_annolist.append(pred_anno) imname = "%s/%s" % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist
def read_kitti_anno(label_file, detect_truck): labels = [line.rstrip().split(' ') for line in open(label_file)] rect_list = [] for label in labels: #create the label of vehicles if not (label[0] == 'Car' or label[0] == 'Van' or label[0] == 'Truck' or label[0] == 'DontCare'): continue notruck = not detect_truck if notruck and label[0] == 'Truck': continue if label[0] == 'DontCare': class_id = -1 else: class_id = 1 #create rectangle object_rect = AnnoLib.AnnoRect( x1=float(label[4]), y1=float(label[5]), x2=float(label[6]), y2=float(label[7])) #check the condition assert object_rect.x1 < object_rect.x2 assert object_rect.y1 < object_rect.y2 object_rect.classID = class_id #append to the list of the rresult rect_list.append(object_rect)
def hot_predict(image_path, init_params, options): """Makes predictions when all long running preparation operations are made. Args: image_path (string): The path to the source image. init_params (dict): The parameters produced by :func:`initialize`. options (dict): The options for more precise prediction of bounding boxes. Returns (Annotation): The annotation for the source image. """ H = init_params['hypes'] # predict orig_img = imread(image_path)[:, :, :3] img = imresize(orig_img, (H['image_height'], H['image_width']), interp='cubic') (np_pred_boxes, np_pred_confidences) = init_params['sess'].\ run([init_params['pred_boxes'], init_params['pred_confidences']], feed_dict={init_params['x_in']: img}) pred_anno = al.Annotation() pred_anno.imageName = image_path _, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=options['min_conf'], tau=options['tau'], show_suppressed=options['show_suppressed']) pred_anno.rects = [r for r in rects if r.x1 < r.x2 and r.y1 < r.y2] pred_anno.imagePath = os.path.abspath(image_path) pred_anno = rescale_boxes((H['image_height'], H['image_width']), pred_anno, orig_img.shape[0], orig_img.shape[1]) return pred_anno
def load_idl(idlfile, data_mean, net_config, jitter=True): """Take the idlfile, data mean and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = al.parse(idlfile) annos = [x for x in annolist] for anno in annos: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) while True: random.shuffle(annos) for anno in annos: if jitter: jit_image, jit_anno = annotation_jitter( anno, target_width=net_config["img_width"], target_height=net_config["img_height"]) else: jit_image = imread(anno.imageName) jit_anno = anno image = image_to_h5(jit_image, data_mean, image_scaling=1.0) boxes, box_flags = annotation_to_h5( jit_anno, net_config["grid_width"], net_config["grid_height"], net_config["region_size"], net_config["max_len"]) yield {"imname": anno.imageName, "raw": jit_image, "image": image, "boxes": boxes, "box_flags": box_flags, "anno": jit_anno}
def add_rectangles(H, orig_image, confidences, boxes, use_stitching=False, rnn_len=1, min_conf=0.1, show_removed=True, tau=0.25, show_suppressed=True): image = np.copy(orig_image[0]) num_cells = H["grid_height"] * H["grid_width"] boxes_r = np.reshape(boxes, (-1, H["grid_height"], H["grid_width"], rnn_len, 4)) confidences_r = np.reshape( confidences, (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes'])) cell_pix_size = H['region_size'] all_rects = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])] for n in range(rnn_len): for y in range(H["grid_height"]): for x in range(H["grid_width"]): bbox = boxes_r[0, y, x, n, :] abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y w = bbox[2] h = bbox[3] conf = np.max(confidences_r[0, y, x, n, 1:]) all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf)) all_rects_r = [r for row in all_rects for cell in row for r in cell] if use_stitching: from stitch_wrapper import stitch_rects acc_rects = stitch_rects(all_rects, tau) else: acc_rects = all_rects_r pairs = [(acc_rects, (0, 255, 0))] if show_suppressed: pairs.append((all_rects_r, (255, 0, 0))) for rect_set, color in pairs: for rect in rect_set: if rect.confidence > min_conf: cv2.rectangle(image, (rect.cx - int(rect.width / 2), rect.cy - int(rect.height / 2)), (rect.cx + int(rect.width / 2), rect.cy + int(rect.height / 2)), color, 2) rects = [] for rect in acc_rects: r = al.AnnoRect() r.x1 = rect.cx - rect.width / 2. r.x2 = rect.cx + rect.width / 2. r.y1 = rect.cy - rect.height / 2. r.y2 = rect.cy + rect.height / 2. r.score = rect.true_confidence rects.append(r) return image, rects
def postprocess(image_info, np_pred_boxes, np_pred_confidences, H, options): pred_anno = al.Annotation() #pred_anno.imageName = image_info['path'] #pred_anno.imagePath = os.path.abspath(image_info['path']) _, rects = add_rectangles(H, [image_info['transformed']], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=options['min_conf'], tau=options['tau'], show_suppressed=False) rects = [ r for r in rects if r.x1 < r.x2 and r.y1 < r.y2 and r.score > options['min_conf'] ] h, w = image_info['original'].shape[:2] if 'rotate90' in H['data'] and H['data']['rotate90']: # original image height is a width for roatated one rects = Rotate90.invert(h, rects) pred_anno.rects = rects pred_anno = rescale_boxes((H['image_height'], H['image_width']), pred_anno, h, w) return pred_anno
def read_kitti_anno(label_file): """ Reads a kitti annotation file. Args: label_file: Path to file Returns: Lists of rectangels: Cars and don't care area. """ labels = [line.rstrip().split(' ') for line in open(label_file)] rect_list = [] for label in labels: if not (label[0] == 'Car' or label[0] == 'Van' or label[0] == 'DontCare'): continue if label[0] == 'DontCare': class_id = -1 else: class_id = 1 object_rect = AnnoLib.AnnoRect(x1=float(label[4]), y1=float(label[5]), x2=float(label[6]), y2=float(label[7])) assert object_rect.x1 < object_rect.x2 assert object_rect.y1 < object_rect.y2 object_rect.classID = class_id rect_list.append(object_rect) return rect_list
def test_IDL(idl_filename): print("Starting Testing Dataset... May take a while") progress = progressbar.ProgressBar(widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage(), ' ', progressbar.ETA() ]) test_annos = al.parse(idl_filename) for test_anno in progress(test_annos): bb_img = Image.open(test_anno.imageName) orig_img = cv2.imread(test_anno.imageName, 0) cv2.imshow('Original Image', orig_img) cv2.waitKey(2) for test_rect in test_anno.rects: dr = ImageDraw.Draw(bb_img) cor = ( test_rect.x2, test_rect.y2, test_rect.x1, test_rect.y1 ) # DA VERIFICARE Try_2 (x1,y1, x2,y2) cor = (bb_rect.left() ,bb_rect.right(),bb_rect.bottom(),bb_rect.top()) Try_1 dr.rectangle(cor, outline="green") image_name, image_ext = os.path.splitext(test_anno.imageName) bb_img.save(image_name + '_copy' + image_ext) bb_img = cv2.imread(image_name + '_copy' + image_ext, 0) cv2.imshow('Mine Rectangle detection', bb_img) cv2.waitKey(2) os.remove(image_name + '_copy' + image_ext)
def load_idl(idlfile, data_mean, net_config, jitter=True): """Take the idlfile, data mean and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = al.parse(idlfile) annos = [x for x in annolist] for anno in annos: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) while True: random.shuffle(annos) for anno in annos: if jitter: jit_image, jit_anno = annotation_jitter( anno, target_width=net_config["img_width"], target_height=net_config["img_height"]) else: jit_image = imread(anno.imageName) jit_anno = anno image = image_to_h5(jit_image, data_mean, image_scaling=1.0) boxes, box_flags = annotation_to_h5(jit_anno, net_config["grid_width"], net_config["grid_height"], net_config["region_size"], net_config["max_len"]) yield { "imname": anno.imageName, "raw": jit_image, "image": image, "boxes": boxes, "box_flags": box_flags, "anno": jit_anno }
def load_idl_tf(idlfile, H, jitter): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = al.parse(idlfile) annos = [] for anno in annolist: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) annos.append(anno) random.seed(0) if H['data']['truncate_data']: annos = annos[:10] for epoch in itertools.count(): random.shuffle(annos) for origin_anno in annos: tiles = preprocess_image(deepcopy(origin_anno), H) for I, anno in tiles: if jitter: jitter_scale_min = 0.9 jitter_scale_max = 1.1 jitter_offset = 16 I, anno = annotation_jitter(I, anno, target_width=H["image_width"], target_height=H["image_height"], jitter_scale_min=jitter_scale_min, jitter_scale_max=jitter_scale_max, jitter_offset=jitter_offset) boxes, flags = annotation_to_h5(H, anno) yield {"image": I, "boxes": boxes, "flags": flags}
def predict_image(self, image): """ Infer buildings for a single image. Inputs: image :: n x m x 3 ndarray - Should be in RGB format """ orig_img = image.copy()[:,:,:3] img = imresize(orig_img, (self.H["image_height"], self.H["image_width"]), interp='cubic') feed = {self.x_in: img} t0 = time.time() (np_pred_boxes, np_pred_confidences) = self.session.run([self.pred_boxes, self.pred_confidences], feed_dict=feed) total_time = time.time() - t0 new_img, rects, all_rects = add_rectangles( self.H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=self.H['rnn_len'], min_conf=0.5, # only affects `rects`, not `all_rects` tau=0.25, show_suppressed=False ) pred_anno = al.Annotation() pred_anno.rects = all_rects pred_anno = rescale_boxes((self.H["image_height"], self.H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_rects = pandas.DataFrame([[r.x1, r.y1, r.x2, r.y2, r.score] for r in all_rects], columns=['x1', 'y1', 'x2', 'y2', 'score']) return pred_rects
def read_daimler_anno(label_file, detect_truck): """ Reads a daimler cyclist annotation file. Args: label_file: Path to file Returns: Lists of rectangels: . """ label_data = json.load(open(label_file)) labels = [item for item in label_data['children']] rect_list = [] for label in labels: if not (label['identity'] == 'pedestrian' or label['identity'] == 'cyclist' or label['identity'] == 'wheelchairuser' or label['identity'] == 'motorcyclist' or label['identity'] == 'tricyclist' or label['identity'] == 'mopedrider'): continue notruck = not detect_truck if notruck and label['identity'] == 'Truck': continue if label['identity'] == 'DontCare': class_id = -1 else: class_id = 1 object_rect = AnnoLib.AnnoRect(x1=float(label['mincol']), y1=float(label['minrow']), x2=float(label['maxcol']), y2=float(label['maxrow'])) assert object_rect.x1 < object_rect.x2 assert object_rect.y1 < object_rect.y2 object_rect.classID = class_id rect_list.append(object_rect) return rect_list
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: # sess.run(tf.initialize_all_variables()) sess.run(tf.global_variables_initializer()) # saver.restore(sess, './output/lstm_rezoom_lung_2017_01_17_18.24/save.ckpt-1000000') print('args.weights: %s' % (args.weights,)) saver.restore(sess, args.weights) print('run') pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=0.2, tau=args.tau) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) return pred_annolist, true_annolist
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() image_dir = args.logdir i=0 subprocess.call('mkdir -p %s' % image_dir, shell=True) for img_list in os.listdir(args.input_images): img_path=os.path.join(args.input_images,img_list) orig_img = imread(img_path) #img=orig_img[0:480,640:1280] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = img_list new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno.imagePath = args.input_images pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) pred_annolist.append(pred_anno) imname = '%s/%s' % (args.logdir, img_list) misc.imsave(imname, new_img) i +=1 if i % 25 == 0: print(i) return pred_annolist
def _load_idl_tf(idlfile, hypes, jitter=False, random_shuffel=True): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = AnnoLib.parse(idlfile) annos = [] for anno in annolist: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) annos.append(anno) random.seed(0) if hypes['data']['truncate_data']: annos = annos[:10] for epoch in itertools.count(): if random_shuffel: random.shuffle(annos) for anno in annos: im = imread(anno.imageName) if im.shape[2] == 4: im = im[:, :, :3] if im.shape[0] != hypes["image_height"] or \ im.shape[1] != hypes["image_width"]: if epoch == 0: anno = _rescale_boxes(im.shape, anno, hypes["image_height"], hypes["image_width"]) im = imresize(im, (hypes["image_height"], hypes["image_width"]), interp='cubic') if jitter: jitter_scale_min = 0.9 jitter_scale_max = 1.1 jitter_offset = 16 im, anno = annotation_jitter( im, anno, target_width=hypes["image_width"], target_height=hypes["image_height"], jitter_scale_min=jitter_scale_min, jitter_scale_max=jitter_scale_max, jitter_offset=jitter_offset) boxes, flags = annotation_to_h5(hypes, anno, hypes["grid_width"], hypes["grid_height"], hypes["rnn_len"]) boxes = boxes.reshape( [hypes["grid_height"], hypes["grid_width"], 4]) flags = flags.reshape(hypes["grid_height"], hypes["grid_width"]) yield { "image": im, "boxes": boxes, "flags": flags, "rects": anno.rects, "anno": anno }
def add_rectangles(H, orig_image, confidences, boxes, arch, use_stitching=False, rnn_len=1, min_conf=0.5, tau=0.25): from utils.rect import Rect from utils.stitch_wrapper import stitch_rects import numpy as np image = np.copy(orig_image[0]) boxes_r = np.reshape( boxes, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 4)) confidences_r = np.reshape( confidences, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 2)) cell_pix_size = H['arch']['region_size'] all_rects = [[[] for _ in range(arch["grid_width"])] for _ in range(arch["grid_height"])] for n in range(0, H['arch']['rnn_len']): for y in range(arch["grid_height"]): for x in range(arch["grid_width"]): bbox = boxes_r[0, y, x, n, :] conf = confidences_r[0, y, x, n, 1] abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y h = max(1, bbox[3]) w = max(1, bbox[2]) #w = h * 0.4 all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf)) if use_stitching: acc_rects = stitch_rects(all_rects, tau) else: acc_rects = [ r for row in all_rects for cell in row for r in cell if r.confidence > 0.1 ] for rect in acc_rects: if rect.confidence > min_conf: cv2.rectangle(image, (rect.cx - int(rect.width / 2), rect.cy - int(rect.height / 2)), (rect.cx + int(rect.width / 2), rect.cy + int(rect.height / 2)), (0, 255, 0), 2) rects = [] for rect in acc_rects: r = al.AnnoRect() r.x1 = rect.cx - rect.width / 2. r.x2 = rect.cx + rect.width / 2. r.y1 = rect.cy - rect.height / 2. r.y2 = rect.cy + rect.height / 2. r.score = rect.true_confidence rects.append(r) return image, rects
def get_results(args, H): tf.reset_default_graph() x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) #写真のInput詳細がわからない if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) pred_annolist = al.AnnoList() true_annolist = al.parse(args.test_boxes) data_dir = os.path.dirname(args.test_boxes) image_dir = get_image_dir(args) subprocess.call('mkdir -p %s' % image_dir, shell=True) for i in range(len(true_annolist)): true_anno = true_annolist[i] orig_img = imread('%s/%s' % (data_dir, true_anno.imageName))[:,:,:3] img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') #画像のResize feed = {x_in: img} #一写真一Step (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, #画像に予測した四角形を追加する use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, orig_img.shape[0], orig_img.shape[1]) #予測値(四角形のxy座標)を画像のsizeに合わせる? pred_annolist.append(pred_anno) imname = '%s/%s' % (image_dir, os.path.basename(true_anno.imageName)) misc.imsave(imname, new_img) if i % 25 == 0: print(i) #pred_annolist:予測値(確信度score:全部、予測のx1,y1,x2,y2) #true_annolist:evalファイルのx1,y1,x2,y2のまま。 return pred_annolist, true_annolist
def save_results_but_not_image(image_path, anno): """Saves results of the prediction. Args: image_path (string): The path to source image to predict bounding boxes. anno (Annotation): The predicted annotations for source image. Returns: Nothing. """ fpath = os.path.join(os.path.dirname(image_path), 'result.json') if type(anno) is dict: with open(fpath, 'w') as f: json.dump(anno, f) else: al.saveJSON(fpath, anno) subprocess.call(['chmod', '777', fpath])
def load_idl_tf(idlfile, H, jitter): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = al.parse(idlfile) annos = [] for anno in annolist: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) annos.append(anno) random.seed(0) if H['data']['truncate_data']: annos = annos[:10] for epoch in itertools.count(): random.shuffle(annos) for anno in annos: try: """ if 'grayscale' in H and 'grayscale_prob' in H: I = imread(anno.imageName, mode = 'RGB' if random.random() < H['grayscale_prob'] else 'L') if len(I.shape) < 3: I = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB) else: if len(I.shape) < 3: continue I = imread(anno.imageName, mode = 'RGB') """ I = np.expand_dims(imread(anno.imageName, mode='L'), axis=2) #I = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB) if I.shape[0] != H["image_height"] or I.shape[1] != H[ "image_width"]: if epoch == 0: anno = rescale_boxes(I.shape, anno, H["image_height"], H["image_width"]) I = imresize(I, (H["image_height"], H["image_width"]), interp='cubic') if jitter: jitter_scale_min = 0.9 jitter_scale_max = 1.1 jitter_offset = 16 I, anno = annotation_jitter( I, anno, target_width=H["image_width"], target_height=H["image_height"], jitter_scale_min=jitter_scale_min, jitter_scale_max=jitter_scale_max, jitter_offset=jitter_offset) boxes, flags = annotation_to_h5(H, anno, H["grid_width"], H["grid_height"], H["rnn_len"]) yield {"image": I, "boxes": boxes, "flags": flags} except Exception as exc: print(exc)
def calculate_medium_box(boxes): conf_sum = reduce(lambda t, b: t + b.score, boxes, 0) aggregation = {} for name in ['x1', 'y1', 'x2', 'y2']: aggregation[name] = reduce(lambda t, b: t+b.__dict__[name]*b.score, boxes, 0) / conf_sum new_box = al.AnnoRect(**aggregation) new_box.classID = boxes[0].classID new_box.score = conf_sum / len(boxes) return new_box
def read_kitti_anno(label_file, calib_file, detect_truck): """ Reads a kitti annotation file. Args: label_file: Path to file Returns: Lists of rectangels: Cars and don't care area. """ labels = [line.rstrip().split(' ') for line in open(label_file)] label_file_split = label_file.rstrip().split('/') index = label_file_split[-1].split('.')[0] #import pdb #pdb.set_trace() calibs = [line.rstrip().split(' ') for line in open(calib_file)] assert calibs[2][0] == 'P2:' calib = np.reshape(calibs[2][1:], (3, 4)).astype(np.float32) calib_pinv = np.linalg.pinv(calib) rect_list = [] for label in labels: if not (label[0] == 'Car' or label[0] == 'Van' or label[0] == 'Truck' or label[0] == 'DontCare'): continue notruck = not detect_truck if notruck and label[0] == 'Truck': continue if label[0] == 'DontCare': class_id = -1 else: class_id = 1 object_rect = AnnoLib.AnnoRect(x1=float(label[4]), y1=float(label[5]), x2=float(label[6]), y2=float(label[7]), height=float(label[8]), width=float(label[9]), length=float(label[10]), x=float(label[11]), y=float(label[12]), z=float(label[13]), alpha=float(label[14]), calib=calib, calib_pinv=calib_pinv) assert object_rect.x1 < object_rect.x2 assert object_rect.y1 < object_rect.y2 object_rect.classID = class_id view_angle = np.arctan2(object_rect.z_3d, object_rect.x_3d) object_rect.alpha += view_angle - np.pi * 0.5 rect_list.append(object_rect) return rect_list
def process_results(queue, H, args, db_args, data_dir, ts): conn = psycopg2.connect(**db_args) cur = conn.cursor() while True: item = queue.get() if item is None: return (np_pred_boxes, np_pred_confidences), meta, VERSION = item pred_anno = al.Annotation() rects = get_rectangles( H, np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, ) (roff, coff, filename, valid_geom, done, height, width, img_geom) = meta img_geom = shape(img_geom) pred_anno.rects = rects pred_anno.imagePath = os.path.abspath(data_dir) pred_anno = rescale_boxes((H["image_height"], H["image_width"]), pred_anno, height, width) bounds = img_geom.bounds ref_point = (bounds[3], bounds[0]) # top left corner for r in rects: minx, miny = raster_to_proj(r.x1 + coff, r.y1 + roff, img_geom, ref_point) maxx, maxy = raster_to_proj(r.x2 + coff, r.y2 + roff, img_geom, ref_point) building = box(minx, miny, maxx, maxy) cur.execute( """ INSERT INTO buildings.buildings (filename, minx, miny, maxx, maxy, roff, coff, score, project, ts, version, geom) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s::uuid, ST_GeomFromText(%s, 4326)) """, (filename, int(r.x1), int(r.y1), int(r.x2), int(r.y2), roff, coff, r.score, args.country, ts, VERSION, building.wkt)) if done: cur.execute( "UPDATE buildings.images SET last_tested=%s WHERE project=%s AND filename=%s", (ts, args.country, filename)) conn.commit() print('Committed image: %s' % filename)
def predict_all(self, test_boxes_file, data_dir = None): annos = json.load(open(test_boxes_file)) true_annolist = al.parse(test_boxes_file) if data_dir is None: data_dir = os.path.join(os.path.dirname(test_boxes_file)) total_time = 0.0 for anno in annos: img_data = imread(os.path.join(data_dir, anno['image_path'])) rects = self.predict_image(img_data) rects['image_id'] = anno['image_path'] yield rects
def load_idl_tf(idlfile, H, jitter): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" arch = H['arch'] annolist = al.parse(idlfile) annos = [x for x in annolist] for anno in annos: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) random.seed(0) if H['data']['truncate_data']: annos = annos[:10] while True: random.shuffle(annos) for anno in annos: if arch["image_width"] != 640 or arch["image_height"] != 480: rescale_boxes(anno, arch["image_width"], arch["image_height"]) I = imread(anno.imageName) if jitter: jit_image, jit_anno = annotation_jitter( I, anno, target_width=arch["image_width"], target_height=arch["image_height"]) else: I = imread(anno.imageName) try: jit_image, jit_anno = annotation_jitter( I, anno, target_width=arch["image_width"], target_height=arch["image_height"], jitter_scale_min=1.0, jitter_scale_max=1.0, jitter_offset=0) except: import traceback print(traceback.format_exc()) continue boxes, box_flags = annotation_to_h5(jit_anno, arch["grid_width"], arch["grid_height"], arch["rnn_len"]) yield { "imname": anno.imageName, "raw": [], "image": jit_image, "boxes": boxes, "box_flags": box_flags }
def add_rectangles(orig_image, confidences, boxes, arch, use_stitching=False, rnn_len=1, min_conf=0.5): image = np.copy(orig_image[0]) num_cells = arch["grid_height"] * arch["grid_width"] boxes_r = np.reshape( boxes, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 4)) confidences_r = np.reshape( confidences, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 2)) cell_pix_size = 32 all_rects = [[[] for _ in range(arch["grid_width"])] for _ in range(arch["grid_height"])] for n in range(rnn_len): for y in range(arch["grid_height"]): for x in range(arch["grid_width"]): bbox = boxes_r[0, y, x, n, :] conf = confidences_r[0, y, x, n, 1] abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y w = bbox[2] h = bbox[3] all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf)) if use_stitching: from stitch_wrapper import stitch_rects acc_rects = stitch_rects(all_rects) else: acc_rects = [r for row in all_rects for cell in row for r in cell] for rect in acc_rects: if rect.confidence > min_conf: cv2.rectangle(image, (rect.cx - int(rect.width / 2), rect.cy - int(rect.height / 2)), (rect.cx + int(rect.width / 2), rect.cy + int(rect.height / 2)), (0, 255, 0), 2) rects = [] for rect in acc_rects: r = al.AnnoRect() r.x1 = rect.cx - rect.width / 2. r.x2 = rect.cx + rect.width / 2. r.y1 = rect.cy - rect.height / 2. r.y2 = rect.cy + rect.height / 2. r.score = rect.true_confidence rects.append(r) return image, rects
def add_boxes(H, orig_image, confidences, boxes, use_stitching=False, rnn_len=1, min_conf=0.1, show_removed=True, tau=0.25, color_removed=(0, 0, 255), color_acc=(0, 0, 255)): image = np.copy(orig_image[0]) num_cells = H["grid_height"] * H["grid_width"] boxes_r = np.reshape(boxes, (-1, H["grid_height"], H["grid_width"], rnn_len, 6)) confidences_r = np.reshape( confidences, (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes'])) cell_pix_size = H['region_size'] all_boxes = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])] for n in range(rnn_len): for y in range(H["grid_height"]): for x in range(H["grid_width"]): bbox = boxes_r[0, y, x, n, :] abs_cx = bbox[0] abs_cy = bbox[1] abs_cz = bbox[2] w = bbox[3] h = bbox[4] d = bbox[5] conf = np.max(confidences_r[0, y, x, n, 1:]) all_boxes[y][x].append( Box(abs_cx, abs_cy, abs_cz, w, h, d, conf)) all_boxes_r = [r for row in all_boxes for cell in row for r in cell] im = Image.fromarray(image.astype('uint8')) image = np.array(im).astype('float32') boxes = [] for box in all_boxes_r: if box.true_confidence < min_conf: continue b = al.AnnoBox() b.x, b.y, b.z = box.cx, box.cy, box.cz b.w, b.h, b.d = box.width, box.height, box.depth b.score = box.true_confidence boxes.append(b) return image, boxes
def get_results(args, H): tf.reset_default_graph() H["grid_width"] = H["image_width"] / H["region_size"] H["grid_height"] = H["image_height"] / H["region_size"] x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['image_height'], H['image_width'], 3]) if H['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) grid_area = H['grid_height'] * H['grid_width'] pred_confidences = tf.reshape( tf.nn.softmax( tf.reshape(pred_confs_deltas, [grid_area * H['rnn_len'], 2])), [grid_area, H['rnn_len'], 2]) if H['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward( H, tf.expand_dims(x_in, 0), 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, args.weights) orig_img = imread('data/tshirtslayer/tss-images/wearing.jpg') img = imresize(orig_img, (H["image_height"], H["image_width"]), interp='cubic') feed = {x_in: img} now = time.time() (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) print(time.time() - now) pred_anno = al.Annotation() pred_anno.imageName = "image.jpg" new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes, use_stitching=True, rnn_len=H['rnn_len'], min_conf=args.min_conf, tau=args.tau, show_suppressed=args.show_suppressed) misc.imsave("data/xxx.jpg", new_img)
def load_idl_tf(idlfile, H, jitter): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = al.parse(idlfile) annos = [] for anno in annolist: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) annos.append(anno) random.seed(0) if H['data']['truncate_data']: annos = annos[:10] for epoch in itertools.count(): random.shuffle(annos) for anno in annos: I = imread(anno.imageName) #Skip Greyscale images if len(I.shape) < 3: continue if I.shape[2] == 4: I = I[:, :, :3] if I.shape[0] != H["image_height"] or I.shape[1] != H["image_width"]: if epoch == 0: anno = rescale_boxes(I.shape, anno, H["image_height"], H["image_width"]) I = imresize(I, (H["image_height"], H["image_width"]), interp='cubic') if jitter: jitter_scale_min=0.9 jitter_scale_max=1.1 jitter_offset=16 I, anno = annotation_jitter(I, anno, target_width=H["image_width"], target_height=H["image_height"], jitter_scale_min=jitter_scale_min, jitter_scale_max=jitter_scale_max, jitter_offset=jitter_offset) boxes, flags = annotation_to_h5(H, anno, H["grid_width"], H["grid_height"], H["rnn_len"]) yield {"image": I, "boxes": boxes, "flags": flags}
def test_IDL(idl_filename): print("Starting Testing Dataset... May take a while") progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) test_annos = al.parse(idl_filename) for test_anno in progress(test_annos): bb_img = Image.open(test_anno.imageName) orig_img = cv2.imread(test_anno.imageName, 0) cv2.imshow('Original Image',orig_img) cv2.waitKey(2) for test_rect in test_anno.rects: dr = ImageDraw.Draw(bb_img) cor = (test_rect.x2,test_rect.y2,test_rect.x1,test_rect.y1 ) # DA VERIFICARE Try_2 (x1,y1, x2,y2) cor = (bb_rect.left() ,bb_rect.right(),bb_rect.bottom(),bb_rect.top()) Try_1 dr.rectangle(cor, outline="green") image_name, image_ext = os.path.splitext(test_anno.imageName) bb_img.save(image_name+'_copy'+image_ext) bb_img = cv2.imread(image_name+'_copy'+image_ext, 0) cv2.imshow('Mine Rectangle detection',bb_img) cv2.waitKey(2) os.remove(image_name+'_copy'+image_ext)
def load_idl_tf(idlfile, H, jitter): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" arch = H['arch'] annolist = al.parse(idlfile) annos = [x for x in annolist] for anno in annos: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) random.seed(0) if H['data']['truncate_data']: annos = annos[:10] while True: random.shuffle(annos) for anno in annos: if arch["image_width"] != 640 or arch["image_height"] != 480: rescale_boxes(anno, arch["image_width"], arch["image_height"]) I = imread(anno.imageName) if jitter: jit_image, jit_anno = annotation_jitter(I, anno, target_width=arch["image_width"], target_height=arch["image_height"]) else: I = imread(anno.imageName) try: jit_image, jit_anno = annotation_jitter(I, anno, target_width=arch["image_width"], target_height=arch["image_height"], jitter_scale_min=1.0, jitter_scale_max=1.0, jitter_offset=0) except: import traceback print(traceback.format_exc()) continue boxes, box_flags = annotation_to_h5( jit_anno, arch["grid_width"], arch["grid_height"], arch["rnn_len"]) yield {"imname": anno.imageName, "raw": [], "image": jit_image, "boxes": boxes, "box_flags": box_flags}
def load_idl_tf(idlfile, H, jitter): """Take the idlfile and net configuration and create a generator that outputs a jittered version of a random image from the annolist that is mean corrected.""" annolist = al.parse(idlfile) annos = [x for x in annolist] for anno in annos: anno.imageName = os.path.join( os.path.dirname(os.path.realpath(idlfile)), anno.imageName) random.seed(0) if H['data']['truncate_data']: annos = annos[:10] while True: random.shuffle(annos) for anno in annos: I = imread(anno.imageName) if I.shape[0] != H["arch"]["image_height"] or I.shape[1] != H["arch"]["image_width"]: I, anno = rescale_boxes(I, anno, H["arch"]["image_height"], H["arch"]["image_width"]) if jitter: jitter_scale_min=0.9 jitter_scale_max=1.1 jitter_offset=16 I, anno = annotation_jitter(I, anno, target_width=H["arch"]["image_width"], target_height=H["arch"]["image_height"], jitter_scale_min=jitter_scale_min, jitter_scale_max=jitter_scale_max, jitter_offset=jitter_offset) boxes, flags = annotation_to_h5(anno, H["arch"]["grid_width"], H["arch"]["grid_height"], H["arch"]["rnn_len"]) yield {"image": I, "boxes": boxes, "flags": flags}
def run_eval(H, checkpoint_dir , hypes_file, output_path): """Do Evaluation with full epoche of data. Args: H: Hypes checkpoint_dir: directory with checkpoint files output_path: path to save results """ #Load GT true_idl = H['data']['test_idl'] true_annos = al.parse(true_idl) # define output files pred_file = 'val_%s.idl' % os.path.basename(hypes_file).replace('.json', '') pred_idl = os.path.join(output_path, pred_file) true_file = 'true_%s.idl' % os.path.basename(hypes_file).replace('.json', '') true_idl_scaled = os.path.join(output_path, true_file) data_folder = os.path.dirname(os.path.realpath(true_idl)) #Load Graph Model tf.reset_default_graph() googlenet = googlenet_load.init(H) x_in = tf.placeholder(tf.float32, name='x_in') if H['arch']['use_lstm']: lstm_forward = build_lstm_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) pred_boxes, pred_logits, pred_confidences = lstm_forward else: overfeat_forward = build_overfeat_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test') pred_boxes, pred_logits, pred_confidences = overfeat_forward start_time = time.time() saver = tf.train.Saver() with tf.Session() as sess: logging.info("Starting Evaluation") sess.run(tf.initialize_all_variables()) # Restore Checkpoints ckpt = tf.train.get_checkpoint_state(checkpoint_dir) if ckpt and ckpt.model_checkpoint_path: logging.info(ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) annolist = al.AnnoList() trueanno = al.AnnoList() #shuffle true_annos to randomize plottet Images shuffle(true_annos) for i in range(len(true_annos)): true_anno = true_annos[i] img = imread( os.path.join(data_folder, true_anno.imageName)) # Rescale Boxes trueanno.append(rescale_boxes(img.shape, true_annos[i], H["arch"]["image_height"], H["arch"]["image_width"])) # Rescale Images img = imresize(img, (H["arch"]["image_height"], H["arch"]["image_width"]), interp='cubic') feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() pred_anno.imageName = true_anno.imageName new_img, rects = add_rectangles([img], np_pred_confidences, np_pred_boxes, H["arch"], use_stitching=True, rnn_len=H['arch']['rnn_len'], min_conf=0.3) pred_anno.rects = rects annolist.append(pred_anno) if i % 20 == 0: # Draw every 20th Image; # plotted Image is randomized due to shuffling duration = time.time() - start_time duration = float(duration)*1000/20 out_img = os.path.join(output_path, 'test_%i.png'%i) scp.misc.imsave(out_img, new_img) logging.info('Step %d: Duration %.3f ms' % (i, duration)) start_time = time.time() annolist.save(pred_idl) trueanno.save(true_idl_scaled) # write results to disk iou_threshold = 0.5 rpc_cmd = './utils/annolist/doRPC.py --minOverlap %f %s %s' % (iou_threshold, true_idl_scaled, pred_idl) rpc_output = subprocess.check_output(rpc_cmd, shell=True) txt_file = [line for line in rpc_output.split('\n') if line.strip()][-1] output_png = os.path.join(output_path, "roc.png") plot_cmd = './utils/annolist/plotSimple.py %s --output %s' % (txt_file, output_png) plot_output = subprocess.check_output(plot_cmd, shell=True)
def still_image_TENSORBOX(idl_filename, frames_list,folder_path_det_frames,folder_path_det_result,folder_path_frames,path_video_folder,hypes_file,weights_file,pred_idl): print("Starting DET Phase") if not os.path.exists(path_video_folder+'/'+folder_path_det_frames): os.makedirs(path_video_folder+'/'+folder_path_det_frames) print("Created Folder: %s"%path_video_folder+'/'+folder_path_det_frames) if not os.path.exists(path_video_folder+'/'+folder_path_det_result): os.makedirs(path_video_folder+'/'+folder_path_det_result) print("Created Folder: %s"% path_video_folder+'/'+folder_path_det_result) det_frames_list=[] #### START TENSORBOX CODE ### ### Opening Hypes file for parameters with open(hypes_file, 'r') as f: H = json.load(f) ### Get Annotation List of all the image to test test_annos = al.parse(idl_filename) ### Building Network tf.reset_default_graph() googlenet = googlenet_load.init(H) x_in = tf.placeholder(tf.float32, name='x_in', shape=[H['arch']['image_height'], H['arch']['image_width'], 3]) if H['arch']['use_rezoom']: pred_boxes, pred_logits, pred_confidences, pred_confs_deltas, pred_boxes_deltas = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) grid_area = H['arch']['grid_height'] * H['arch']['grid_width'] pred_confidences = tf.reshape(tf.nn.softmax(tf.reshape(pred_confs_deltas, [grid_area * H['arch']['rnn_len'], 2])), [grid_area, H['arch']['rnn_len'], 2]) if H['arch']['reregress']: pred_boxes = pred_boxes + pred_boxes_deltas else: pred_boxes, pred_logits, pred_confidences = build_forward(H, tf.expand_dims(x_in, 0), googlenet, 'test', reuse=None) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.initialize_all_variables()) saver.restore(sess, weights_file )##### Restore a Session of the Model to get weights and everything working annolist = al.AnnoList() import time; t = time.time() #### Starting Evaluating the images lenght=int(len(frames_list)) print("%d Frames to DET"%len(frames_list)) progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) for i in progress(range(0, len(frames_list)-1)): img = imread(frames_list[i]) feed = {x_in: img} (np_pred_boxes, np_pred_confidences) = sess.run([pred_boxes, pred_confidences], feed_dict=feed) pred_anno = al.Annotation() #pred_anno.imageName = test_anno.imageName new_img, rects = add_rectangles(H, [img], np_pred_confidences, np_pred_boxes,H["arch"], use_stitching=True, rnn_len=H['arch']['rnn_len'], min_conf=0.5) pred_anno.rects = rects bb_img = Image.open(frames_list[i]) for bb_rect in rects: ################ Adding Rectangle ################### dr = ImageDraw.Draw(bb_img) cor = (bb_rect.x1,bb_rect.y1,bb_rect.x2 ,bb_rect.y2) # DA VERIFICARE Try_2 (x1,y1, x2,y2) cor = (bb_rect.left() ,bb_rect.right(),bb_rect.bottom(),bb_rect.top()) Try_1 dr.rectangle(cor, outline="red") bb_img_det_name = frames_list[i].replace(folder_path_frames,folder_path_det_frames) bb_img.save(bb_img_det_name) det_frames_list.append(bb_img_det_name) annolist.append(pred_anno) annolist.save(pred_idl) #### END TENSORBOX CODE ### return det_frames_list