def main(): ''' Parse command line arguments and execute the code ''' parser = argparse.ArgumentParser() parser.add_argument('--dataset_path', required=True, type=str) parser.add_argument('--newext', default='.PNG', type=str) parser.add_argument('--oldext', default='.JPEG', type=str) args = parser.parse_args() start = time.time() image_list= Utils_Image.get_Image_List(args.dataset_path, args.oldext) progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) print "Start Processing... May take a while..." for image_path in progress(image_list): Utils_Image.resizeImage(image_path) Utils_Image.change_extension(image_path,args.oldext,args.newext) end = time.time() print("Parsed: %d Image of the Dataset"%(len(image_list))) print("Elapsed Time:%d Seconds"%(end-start)) print("Running Completed with Success!!!")
def main(): ''' Parse command line arguments and execute the code ''' ######### TENSORBOX PARAMETERS start = time.time() parser = argparse.ArgumentParser() # parser.add_argument('--result_folder', default='summary_result/', type=str) # parser.add_argument('--summary_file', default='results.txt', type=str) parser.add_argument('--output_name', default='output.mp4', type=str) parser.add_argument('--hypes', default='./TENSORBOX/hypes/overfeat_rezoom.json', type=str) parser.add_argument('--weights', default='./TENSORBOX/data/save.ckpt-1090000', type=str) parser.add_argument('--perc', default=2, type=int) parser.add_argument('--path_video', required=True, type=str) args = parser.parse_args() # hypes_file = './hypes/overfeat_rezoom.json' # weights_file= './output/save.ckpt-1090000' path_video_folder = os.path.splitext(os.path.basename(args.path_video))[0] pred_idl = './%s/%s_val.idl' % (path_video_folder, path_video_folder) idl_filename = path_video_folder + '/' + path_video_folder + '.idl' frame_list = [] frame_list = Utils_Video.extract_idl_from_frames(args.path_video, args.perc, path_video_folder, 'frames/', idl_filename) progress = progressbar.ProgressBar(widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage(), ' ', progressbar.ETA() ]) for image_path in progress(frame_list): Utils_Image.resizeImage(image_path) Utils_Image.resizeImage(-1) det_frame_list = Utils_Tensorbox.still_image_TENSORBOX_singleclass( frame_list, path_video_folder, args.hypes, args.weights, pred_idl) Utils_Video.make_video_from_list(args.output_name, det_frame_list) end = time.time() print("Elapsed Time:%d Seconds" % (end - start)) print("Running Completed with Success!!!")
def main(): ''' Parse command line arguments and execute the code ''' ######### TENSORBOX PARAMETERS start = time.time() parser = argparse.ArgumentParser() parser.add_argument('--det_frames_folder', default='det_frames/', type=str) parser.add_argument('--det_result_folder', default='det_results/', type=str) parser.add_argument('--frames_folder', default='frames/', type=str) # parser.add_argument('--result_folder', default='summary_result/', type=str) # parser.add_argument('--summary_file', default='results.txt', type=str) parser.add_argument('--output_name', default='output.mp4', type=str) parser.add_argument('--hypes', default='./TENSORBOX/hypes/overfeat_rezoom.json', type=str) parser.add_argument('--weights', default='./TENSORBOX/output/save.ckpt-1090000', type=str) parser.add_argument('--perc', default=2, type=int) parser.add_argument('--path_video', required=True, type=str) args = parser.parse_args() # hypes_file = './hypes/overfeat_rezoom.json' # weights_file= './output/save.ckpt-1090000' path_video_folder = os.path.splitext(os.path.basename(args.path_video))[0] pred_idl = './%s/%s_val.idl' % (path_video_folder, path_video_folder) idl_filename=path_video_folder+'/'+path_video_folder+'.idl' frame_list=[] frame_list = Utils_Video.extract_idl_from_frames(args.path_video, args.perc, path_video_folder, args.frames_folder, idl_filename ) progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) for image_path in progress(frame_list): Utils_Image.resizeImage(image_path) det_frame_list=still_image_TENSORBOX(idl_filename, frame_list, args.det_frames_folder, args.det_result_folder, args.frames_folder, path_video_folder, args.hypes, args.weights, pred_idl) Utils_Video.make_video_from_list(args.output_name, det_frame_list) end = time.time() print("Elapsed Time:%d Seconds"%(end-start)) print("Running Completed with Success!!!")
def make_video_from_list(out_vid_path, frames_list): if frames_list[0] is not None: img = cv2.imread(frames_list[0], True) print frames_list[0] h, w = img.shape[:2] fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') out = cv2.VideoWriter(out_vid_path,fourcc, 20.0, (w, h), True) print("Start Making File Video:%s " % out_vid_path) print("%d Frames to Compress"%len(frames_list)) progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) for i in progress(range(0,len(frames_list))): if Utils_Image.check_image_with_pil(frames_list[i]): out.write(img) img = cv2.imread(frames_list[i], True) out.release() print("Finished Making File Video:%s " % out_vid_path)
def make_video_from_list(out_vid_path, frames_list): if frames_list[0] is not None: img = cv2.imread(frames_list[0], True) print frames_list[0] h, w = img.shape[:2] fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') out = cv2.VideoWriter(out_vid_path, fourcc, 20.0, (w, h), True) print("Start Making File Video:%s " % out_vid_path) print("%d Frames to Compress" % len(frames_list)) progress = progressbar.ProgressBar(widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage(), ' ', progressbar.ETA() ]) for i in progress(range(0, len(frames_list))): if Utils_Image.check_image_with_pil(frames_list[i]): out.write(img) img = cv2.imread(frames_list[i], True) out.release() print("Finished Making File Video:%s " % out_vid_path)
def make_tracked_video(out_vid_path, labeled_video_frames): if labeled_video_frames[0] is not None: img = cv2.imread(labeled_video_frames[0], True) print "Reading Filename: %s" % labeled_video_frames[0] h, w = img.shape[:2] print "Video Size: width: %d height: %d" % (h, w) fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') out = cv2.VideoWriter(out_vid_path, fourcc, 20.0, (w, h), True) print("Start Making File Video:%s " % out_vid_path) print("%d Frames to Compress" % len(labeled_video_frames)) progress = progressbar.ProgressBar(widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage(), ' ', progressbar.ETA() ]) for i in progress(range(0, len(labeled_video_frames))): if Utils_Image.check_image_with_pil(labeled_video_frames[i]): out.write(img) img = cv2.imread(labeled_video_frames[i], True) out.release() print("Finished Making File Video:%s " % out_vid_path)
def parse_XML_to_multiclass_txt(bb_XML_file_list, path_val_folder, path_dataset): count_rect=0 count_img=0 progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) print "Start Processing & Building the MultiClass Dataset... May take a while..." path_mltcl_bb_file=path_dataset+'/'+string_mltcl_bb_file # Create this file in .dataset/airplane/airplane_bb_mltcl_file_list.txt path_mltcl_class_code_file=path_dataset+'/'+string_mltcl_class_code_file path_mltcl_class_name_file=path_dataset+'/'+string_mltcl_class_name_file path_mltcl_chall_code_file=path_dataset+'/'+string_mltcl_chall_code_file for file_name in progress(bb_XML_file_list): with open(file_name, 'rt') as f: tree = ElementTree.parse(f) for obj in tree.findall('object'): name = obj.find('name').text class_code= name name = Classes.code_to_class_string(name) if name in ["nothing"]: continue else: path_orig_file=path_val_folder jump=0 image_multi_class= Picture_Info() image_multi_class.dataset_path= path_val_folder rectangle_multi= BB_Rectangle() #xmin x1 letf #ymin y1 bottom #xmax x2 right #ymax y2 top for node in tree.iter(): tag=str(node.tag) if tag in ["folder"]: path_orig_file=path_orig_file+'/'+str(node.text) image_multi_class.folder= str(node.text) if tag in ["filename"]: image_multi_class.filename=str(node.text)+'.PNG' path_orig_file=path_orig_file+'/'+str(node.text)+'.JPEG' if tag in ["width"]: image_multi_class.width= int(node.text) if tag in ["height"]: image_multi_class.height= int(node.text) if tag in ["trackid"]: image_multi_class.frame= int(os.path.basename(file_name).replace('.xml', '')) if tag in ['name']: if str(Classes.code_to_class_string(str(node.text))) in ["nothing"]: jump = 1 else : jump=0 rectangle_multi.label_chall=int(Classes.class_string_to_comp_code(str(Classes.code_to_class_string(str(node.text))))) rectangle_multi.label_code=str(node.text) rectangle_multi.label=Classes.code_to_class_string(str(node.text)) if tag in ["xmax"]: if jump == 0: rectangle_multi.x2=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), True)) if tag in ["xmin"]: if jump == 0: rectangle_multi.x1=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), True)) if tag in ["ymin"]: if jump == 0: rectangle_multi.y2=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) if tag in ["ymax"]: if jump == 0: rectangle_multi.y1=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) image_multi_class.append_rect(rectangle_multi) count_rect=count_rect+1 if jump == 0: count_img=count_img+1 out_stream = open(path_mltcl_bb_file, "a") out_stream.write(image_multi_class.get_info_string()+ os.linesep) out_stream = open(path_mltcl_class_code_file, "a") out_stream.write(image_multi_class.get_rects_code()+ os.linesep) out_stream = open(path_mltcl_class_name_file, "a") out_stream.write(image_multi_class.get_rects_labels()+ os.linesep) out_stream = open(path_mltcl_chall_code_file, "a") out_stream.write(image_multi_class.get_rects_chall() + os.linesep) break out_stream = open('dataset/summary_multiclass.txt', "w") out_stream.write( "SUMMARY:" + os.linesep+ ("Parsed: %d BB Files"%len(bb_XML_file_list)) + os.linesep+ ("Added: %d Object Rectangles"%count_rect) + os.linesep+("Added: %d Images"%count_img)+os.linesep) print "Ended with Success the Process" print "SUMMARY:" print "Parsed: %d BB Files"%len(bb_XML_file_list) print "Added: %d Object Rectangles"%count_rect print "Added: %d Images"%count_img if count_img>0: print "Ratio BB_Files/Images: %.2f"%(float(count_img)/float(len(bb_XML_file_list))) out_stream.write(("Ratio BB_Files/Images: %.2f"%(float(count_img)/float(len(bb_XML_file_list))))+os.linesep) if count_rect>0: print "Ratio Images/Object_Rectangles: %.2f"%(float(count_img)/float(count_rect)) out_stream.write(("Ratio Images/Object_Rectangles: %.2f"%(float(count_img)/float(count_rect)))+os.linesep)
def still_image_TENSORBOX_multiclass(frames_list,path_video_folder,hypes_file,weights_file,pred_idl): from train import build_forward 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 ### idl_filename=path_video_folder+'/'+path_video_folder+'.idl' ### Opening Hypes file for parameters with open(hypes_file, 'r') as f: H = json.load(f) ### Building Network tf.reset_default_graph() googlenet = googlenet_load.init(H) 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), 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'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) pred_logits = tf.reshape(tf.nn.softmax(tf.reshape(pred_logits, [grid_area * H['rnn_len'], H['num_classes']])), [grid_area, H['rnn_len'], H['num_classes']]) 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, weights_file )##### Restore a Session of the Model to get weights and everything working annolist = al.AnnoList() #### 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()]) frameNr=0 skipped=0 for i in progress(range(0, len(frames_list))): if Utils_Image.isnotBlack(frames_list[i]) & Utils_Image.check_image_with_pil(frames_list[i]): img = imread(frames_list[i]) feed = {x_in: img} (np_pred_boxes,np_pred_logits, np_pred_confidences) = sess.run([pred_boxes,pred_logits, pred_confidences], feed_dict=feed) # print_logits(np_pred_confidences) pred_anno = al.Annotation() #pred_anno.imageName = test_anno.imageName # print "np_pred_confidences shape" + str(np_pred_confidences.shape) # print "np_pred_boxes shape" + str(np_pred_boxes.shape) # for i in range(0, np_pred_confidences.shape[0]): # print np_pred_confidences[i] # for j in range(0, np_pred_confidences.shape[2]): # print np_pred_confidences[i][0][j] rects = get_multiclass_rectangles(H, np_pred_confidences, np_pred_boxes, rnn_len=H['rnn_len']) pred_anno.rects = rects pred_anno.imageName = frames_list[i] pred_anno.frameNr = frameNr frameNr=frameNr+1 det_frames_list.append(frames_list[i]) pick = NMS(rects) draw_rectangles(frames_list[i],frames_list[i], pick) annolist.append(pred_anno) else: skipped=skipped+1 saveTextResults(idl_filename,annolist) annolist.save(pred_idl) print("Skipped %d Black Frames"%skipped) #### END TENSORBOX CODE ### return det_frames_list
def main(): ''' Parse command line arguments and execute the code ''' ######### TENSORBOX PARAMETERS start = time.time() parser = argparse.ArgumentParser() # parser.add_argument('--result_folder', default='summary_result/', type=str) # parser.add_argument('--summary_file', default='results.txt', type=str) parser.add_argument('--output_name', default='output.mp4', type=str) parser.add_argument('--hypes', default='./TENSORBOX/hypes/overfeat_rezoom.json', type=str) parser.add_argument('--weights', default='./TENSORBOX/data/save.ckpt-1250000', type=str) parser.add_argument('--perc', default=100, type=int) parser.add_argument('--path_video', default='DAVIS-2017-TrainVal.mp4', type=str) # required=True, type=str) args = parser.parse_args() # hypes_file = './hypes/overfeat_rezoom.json' # weights_file= './output/save.ckpt-1090000' path_video_folder = os.path.splitext(os.path.basename(args.path_video))[0] pred_idl = './%s/%s_val.idl' % (path_video_folder, path_video_folder) idl_filename = path_video_folder + '/' + path_video_folder + '.idl' frame_tensorbox = [] frame_inception = [] frame_tensorbox, frame_inception = Utils_Video.extract_frames_incten( args.path_video, args.perc, path_video_folder, idl_filename) progress = progressbar.ProgressBar(widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage(), ' ', progressbar.ETA() ]) for image_path in progress(frame_tensorbox): Utils_Image.resizeImage(image_path) Utils_Image.resizeImage(-1) video_info = Utils_Tensorbox.bbox_det_TENSORBOX_multiclass( frame_tensorbox, path_video_folder, args.hypes, args.weights, pred_idl) tracked_video = Utils_Video.recurrent_track_objects(video_info) # tracked_video=utils_video.track_objects(video_info) # labeled_video=Utils_Imagenet.label_video(tracked_video, frame_inception) labeled_video = Utils_Imagenet.recurrent_label_video( tracked_video, frame_inception) # tracked_video=utils_video.track_objects(video_info) # tracked_video=utils_video.track_and_label_objects(video_info) labeled_frames = Utils_Video.draw_rectangles(path_video_folder, labeled_video) Utils_Video.make_tracked_video(args.output_name, labeled_frames) frame.saveVideoResults(idl_filename, labeled_video) # utils_video.make_tracked_video(args.output_name, labeled_video) end = time.time() print("Elapsed Time:%d Seconds" % (end - start)) print("Running Completed with Success!!!")
def parse_XML_lightweight_txt(bb_XML_file_list, path_val_folder, path_dataset): count_rect = 0 count_img = 0 progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()]) print "Start Processing & Building Dataset... may take a while..." path_mltcl_bb_file=path_dataset+'/'+string_mltcl_bb_file # Create this file in .dataset/airplane/airplane_bb_mltcl_file_list.txt path_mltcl_class_code_file=path_dataset+'/'+string_mltcl_class_code_file path_mltcl_class_name_file=path_dataset+'/'+string_mltcl_class_name_file path_mltcl_chall_code_file=path_dataset+'/'+string_mltcl_chall_code_file for file_name in progress(bb_XML_file_list): with open(file_name, 'rt') as f: tree = ElementTree.parse(f) for obj in tree.findall('object'): name = obj.find('name').text class_code= name name = Classes.code_to_class_string(name) if name in ["nothing"]: continue else: same_label=0 #The files with the original data path are made in both: multiclass e single class path_bb_file=path_dataset+'/'+name+'/'+ name+string_bb_file path_class_code_file= path_dataset+'/'+name+'/'+name+string_class_code_file path_class_name_file= path_dataset+'/'+name+'/'+name+string_class_name_file path_chall_code_file= path_dataset+'/'+name+'/'+name+string_chall_code_file path_orig_file=path_val_folder jump=0 image_single_class= Picture_Info() image_single_class.dataset_path= path_val_folder image_multi_class= Picture_Info() image_multi_class.dataset_path= path_val_folder rectangle_single= BB_Rectangle() rectangle_multi= BB_Rectangle() #xmin x1 letf #ymin y1 bottom #xmax x2 right #ymax y2 top for node in tree.iter(): tag=str(node.tag) if tag in ["folder"]: path_orig_file=path_orig_file+'/'+str(node.text) image_single_class.folder= str(node.text) image_multi_class.folder= str(node.text) if tag in ["filename"]: image_single_class.filename=str(node.text)+'.PNG' image_multi_class.filename=str(node.text)+'.PNG' path_orig_file=path_orig_file+'/'+str(node.text)+'.JPEG' if tag in ['name']: if str(Classes.code_to_class_string(str(node.text))) in ["nothing"]: jump = 1 else : jump=0 rectangle_multi.label_chall=int(Classes.class_string_to_comp_code(str(Classes.code_to_class_string(str(node.text))))) rectangle_multi.label_code=str(node.text) rectangle_multi.label=Classes.code_to_class_string(str(node.text)) if str(node.text) == class_code: same_label = 1 rectangle_single.label_chall=int(Classes.class_string_to_comp_code(str(Classes.code_to_class_string(str(node.text))))) rectangle_single.label_code=str(node.text) rectangle_single.label=Classes.code_to_class_string(str(node.text)) else: same_label = 0 if tag in ["xmax"]: if jump == 0: rectangle_multi.x2=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) if same_label==1: rectangle_single.x2=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) if tag in ["xmin"]: if jump == 0: rectangle_multi.x1=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) if same_label==1: rectangle_single.x1=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) if tag in ["ymax"]: if jump == 0: rectangle_multi.y2=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) image_multi_class.append_rect(rectangle_multi) count_rect=count_rect+1 if same_label==1: rectangle_single.y2=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) image_single_class.append_rect(rectangle_single) if tag in ["ymin"]: if jump == 0: rectangle_multi.y1=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) if same_label==1: rectangle_single.y1=float(Utils_Image.transform_point(image_multi_class.width,image_multi_class.height,width, height,float(node.text), False)) if jump == 0: count_img=count_img+1 out_stream = open(path_mltcl_bb_file, "a") out_stream.write(image_multi_class.get_info_string()+ os.linesep) out_stream = open(path_mltcl_class_code_file, "a") out_stream.write(image_multi_class.get_rects_code()+ os.linesep) out_stream = open(path_mltcl_class_name_file, "a") out_stream.write(image_multi_class.get_rects_labels()+ os.linesep) out_stream = open(path_mltcl_chall_code_file, "a") out_stream.write(image_multi_class.get_rects_chall() + os.linesep) if same_label==1: out_stream = open(path_bb_file, "a") out_stream.write(image_single_class.get_info_string()+ os.linesep) out_stream = open(path_class_code_file, "a") out_stream.write(image_single_class.get_rects_chall()+ os.linesep) out_stream = open(path_class_name_file, "a") out_stream.write(image_single_class.get_rects_labels()+ os.linesep) out_stream = open(path_chall_code_file, "a") out_stream.write(image_single_class.get_rects_code() + os.linesep) break print "SUMMARY:" print "Parsed: %d BB Files"%len(bb_XML_file_list) print "Added: %d Object Rectangles"%count_rect print "Added: %d Images"%count_img if count_img>0: print "Ratio BB_Files/Images: %.2f"%(float(count_img)/float(len(bb_XML_file_list))) if count_rect>0: print "Ratio Images/Object_Rectangles: %.2f"%(float(count_img)/float(count_rect))