def track_and_label_objects(video_info): previous_frame= None previous_num_obj=-1 cropped_img_array=[] tracked_video=[] for frame_info in video_info: print "Tracking Frame Nr: %d"%frame_info.frame print len(frame_info.rects) current_frame = frame.Frame_Info() current_frame=frame_info.duplicate() current_frame.rects=[] print len(frame_info.rects) if previous_frame is not None: print "Previous Frame obj:%d"%previous_num_obj for rect in frame_info.rects: print "Entered into the rect check" max_rect=None max_iou=0 current_rect= Rectangle_Multiclass() trackID=-1 if previous_num_obj >0: ### If i come here means that there's the same number of object between the previous and the current frame print "Entered into the rect check with :%d objects"%previous_num_obj id_rect=0 max_id=0 for prev_rect in previous_frame.rects: print "Entered" if rect.iou(prev_rect)>max_iou: max_iou=rect.iou(prev_rect) max_id=id_rect id_rect=id_rect+1 print "Lenght previous rects array: %d"%len(previous_frame.rects) print "max_rect track ID: %d"%previous_frame.rects[max_id].trackID print "max_rect label: %s"%previous_frame.rects[max_id].label current_rect.load_labeled_rect(previous_frame.rects[max_id].trackID, previous_frame.rects[max_id].true_confidence, previous_frame.rects[max_id].label_confidence, previous_frame.rects[max_id].x1,previous_frame.rects[max_id].y1,previous_frame.rects[max_id].x2 ,previous_frame.rects[max_id].y2, previous_frame.rects[max_id].label, previous_frame.rects[max_id].label_chall, previous_frame.rects[max_id].label_code) current_frame.append_labeled_rect(current_rect) rect.load_label(previous_frame.rects[max_id].trackID,previous_frame.rects[max_id].label_confidence, previous_frame.rects[max_id].label, previous_frame.rects[max_id].label_chall, previous_frame.rects[max_id].label_code) previous_frame.rects.pop(max_id) previous_num_obj=previous_num_obj-1 else: ### If i come here means that there's more objects in the current frame respect to che previous if previous_num_obj == 0: trackID = len(frame_info.rects) previous_num_obj = -1 current_rect= Rectangle_Multiclass() img= Image.open(frame_info.filename) cor = (rect.x1,rect.y1,rect.x2 ,rect.y2) cropped_img=img.crop(cor) cropped_img_name="cropped_frame_%d.JPEG"%(frame_info.frame) cropped_img.save(cropped_img_name) cropped_img_array.append(cropped_img_name) label, confidence = Utils_Imagenet.run_inception_once(cropped_img_name) rect.load_label(trackID,confidence, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) current_rect.load_labeled_rect(trackID, rect.true_confidence, confidence, rect.x1,rect.y1,rect.x2 ,rect.y2, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) print "current_rect track ID: %d"%current_rect.trackID print "current_rect label: %s"%current_rect.label current_frame.append_labeled_rect(current_rect) else: trackID=1 for rect in frame_info.rects: current_rect= Rectangle_Multiclass() img= Image.open(frame_info.filename) cor = (rect.x1,rect.y1,rect.x2 ,rect.y2) cropped_img=img.crop(cor) cropped_img_name="cropped_frame_%d.JPEG"%(frame_info.frame) cropped_img.save(cropped_img_name) cropped_img_array.append(cropped_img_name) label, confidence = Utils_Imagenet.run_inception_once(cropped_img_name) rect.load_label(trackID,confidence, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) current_rect.load_labeled_rect(trackID, rect.true_confidence, confidence, rect.x1,rect.y1,rect.x2 ,rect.y2, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) current_frame.append_labeled_rect(current_rect) trackID=trackID+1 previous_num_obj=len(frame_info.rects) previous_frame=frame_info.duplicate() previous_frame.duplicate_rects(frame_info.rects) print previous_frame print "Previous Frame obj:%d"%previous_num_obj print "prev_rect 0 track ID: %d"%previous_frame.rects[0].trackID print "prev_rect 0 label: %s"%previous_frame.rects[0].label tracked_video.insert(len(tracked_video), current_frame) return tracked_video
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 = vid_classes.code_to_class_string(name) if name in ["nothing"]: continue else: path_orig_file = path_val_folder jump = 0 image_multi_class = Frame_Info() image_multi_class.dataset_path = path_val_folder rectangle_multi = Rectangle_Multiclass() #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( vid_classes.code_to_class_string( str(node.text))) in ["nothing"]: jump = 1 else: jump = 0 print str(node.text) rectangle_multi.label_chall = int( vid_classes.code_to_code_chall( str(node.text))) rectangle_multi.label_code = str(node.text) rectangle_multi.label = vid_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 track_and_label_objects(video_info): previous_frame = None previous_num_obj = -1 cropped_img_array = [] tracked_video = [] for frame_info in video_info: print("Tracking Frame Nr: %d" % frame_info.frame) print(len(frame_info.rects)) current_frame = frame.Frame_Info() current_frame = frame_info.duplicate() current_frame.rects = [] print(len(frame_info.rects)) if previous_frame is not None: print("Previous Frame obj:%d" % previous_num_obj) for rect in frame_info.rects: print("Entered into the rect check") max_rect = None max_iou = 0 current_rect = Rectangle_Multiclass() trackID = -1 if previous_num_obj > 0: ### If i come here means that there's the same number of object between the previous and the current frame print("Entered into the rect check with :%d objects" % previous_num_obj) id_rect = 0 max_id = 0 for prev_rect in previous_frame.rects: print("Entered") if rect.iou(prev_rect) > max_iou: max_iou = rect.iou(prev_rect) max_id = id_rect id_rect = id_rect + 1 print("Lenght previous rects array: %d" % len(previous_frame.rects)) print("max_rect track ID: %d" % previous_frame.rects[max_id].trackID) print("max_rect label: %s" % previous_frame.rects[max_id].label) current_rect.load_labeled_rect( previous_frame.rects[max_id].trackID, previous_frame.rects[max_id].true_confidence, previous_frame.rects[max_id].label_confidence, previous_frame.rects[max_id].x1, previous_frame.rects[max_id].y1, previous_frame.rects[max_id].x2, previous_frame.rects[max_id].y2, previous_frame.rects[max_id].label, previous_frame.rects[max_id].label_chall, previous_frame.rects[max_id].label_code) current_frame.append_labeled_rect(current_rect) rect.load_label( previous_frame.rects[max_id].trackID, previous_frame.rects[max_id].label_confidence, previous_frame.rects[max_id].label, previous_frame.rects[max_id].label_chall, previous_frame.rects[max_id].label_code) previous_frame.rects.pop(max_id) previous_num_obj = previous_num_obj - 1 else: ### If i come here means that there's more objects in the current frame respect to che previous if previous_num_obj == 0: trackID = len(frame_info.rects) previous_num_obj = -1 current_rect = Rectangle_Multiclass() img = Image.open(frame_info.filename) cor = (rect.x1, rect.y1, rect.x2, rect.y2) cropped_img = img.crop(cor) cropped_img_name = "cropped_frame_%d.JPEG" % ( frame_info.frame) cropped_img.save(cropped_img_name) cropped_img_array.append(cropped_img_name) label, confidence = Utils_Imagenet.run_inception_once( cropped_img_name) rect.load_label( trackID, confidence, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) current_rect.load_labeled_rect( trackID, rect.true_confidence, confidence, rect.x1, rect.y1, rect.x2, rect.y2, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) print("current_rect track ID: %d" % current_rect.trackID) print("current_rect label: %s" % current_rect.label) current_frame.append_labeled_rect(current_rect) else: trackID = 1 for rect in frame_info.rects: current_rect = Rectangle_Multiclass() img = Image.open(frame_info.filename) cor = (rect.x1, rect.y1, rect.x2, rect.y2) cropped_img = img.crop(cor) cropped_img_name = "cropped_frame_%d.JPEG" % (frame_info.frame) cropped_img.save(cropped_img_name) cropped_img_array.append(cropped_img_name) label, confidence = Utils_Imagenet.run_inception_once( cropped_img_name) rect.load_label(trackID, confidence, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) current_rect.load_labeled_rect( trackID, rect.true_confidence, confidence, rect.x1, rect.y1, rect.x2, rect.y2, vid_classes.code_to_class_string(label), vid_classes.code_to_code_chall(vid_classes), label) current_frame.append_labeled_rect(current_rect) trackID = trackID + 1 previous_num_obj = len(frame_info.rects) previous_frame = frame_info.duplicate() previous_frame.duplicate_rects(frame_info.rects) print(previous_frame) print("Previous Frame obj:%d" % previous_num_obj) print("prev_rect 0 track ID: %d" % previous_frame.rects[0].trackID) print("prev_rect 0 label: %s" % previous_frame.rects[0].label) tracked_video.insert(len(tracked_video), current_frame) return tracked_video
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 = vid_classes.code_to_class_string(name) if name in ["nothing"]: continue else: path_orig_file=path_val_folder jump=0 image_multi_class= Frame_Info() image_multi_class.dataset_path= path_val_folder rectangle_multi= Rectangle_Multiclass() #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(vid_classes.code_to_class_string(str(node.text))) in ["nothing"]: jump = 1 else : jump=0 print str(node.text) rectangle_multi.label_chall=int(vid_classes.code_to_code_chall(str(node.text))) rectangle_multi.label_code=str(node.text) rectangle_multi.label=vid_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)