Exemplo n.º 1
0
def parse_XML_to_data(xml_list_video):
    frames_list=[]
    video_list=[]
    # image_multi_class= None
    # rectangle_multi = None
    progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()])
    for i in progress(list(range(0, len(xml_list_video)))):
        # print "Iterating on Video:"+ str(xml_list_video[i][0][0])
        for j in range(0, len(xml_list_video[i])):
            # print "Iterating on Frame:"+ str(xml_list_video[i][j][0])
            with open(xml_list_video[i][j][0], '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:
                        #The files with the original data path are made in both: multiclass e single class
                        jump=0
                        image_multi_class= fm.Frame_Info()
                        image_multi_class.frame= xml_list_video[i][j][1]
                        # print image_multi_class.frame
                        rectangle_multi= multiclass_rectangle.Rectangle_Multiclass()
                        for node in tree.iter():
                            tag=str(node.tag)
                            if tag in ['name']:
                                if str(vid_classes.code_to_class_string(str(node.text))) in ["nothing"]:
                                    jump = 1
                                else : 
                                    jump=0
                                    rectangle_multi.label_chall=int(vid_classes.class_string_to_comp_code(str(vid_classes.code_to_class_string(str(node.text)))))
                                    # print rectangle_multi.label_chall
                                    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(node.text)
                            if tag in ["xmin"]:
                                if jump == 0:
                                    rectangle_multi.x1=float(node.text)
                            if tag in ["ymax"]:
                                if jump == 0:
                                    rectangle_multi.y2=float(node.text)                            
                            if tag in ["ymin"]:
                                if jump == 0:    
                                    rectangle_multi.y1=float(node.text)
                                    image_multi_class.append_rect(rectangle_multi)
                        if jump == 0:
                            image_multi_class.append_labeled_rect(rectangle_multi)
                        break
                frames_list.append(image_multi_class)
        video_list.append(frames_list)
        # frames_list=None
        # frames_list=[]        
    return video_list
def parse_XML_to_data(xml_list_video):
    frames_list=[]
    video_list=[]
    # image_multi_class= None
    # rectangle_multi = None
    progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()])
    for i in progress(range(0, len(xml_list_video))):
        # print "Iterating on Video:"+ str(xml_list_video[i][0][0])
        for j in range(0, len(xml_list_video[i])):
            # print "Iterating on Frame:"+ str(xml_list_video[i][j][0])
            with open(xml_list_video[i][j][0], '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:
                        #The files with the original data path are made in both: multiclass e single class
                        jump=0
                        image_multi_class= fm.Frame_Info()
                        image_multi_class.frame= xml_list_video[i][j][1]
                        # print image_multi_class.frame
                        rectangle_multi= multiclass_rectangle.Rectangle_Multiclass()
                        for node in tree.iter():
                            tag=str(node.tag)
                            if tag in ['name']:
                                if str(vid_classes.code_to_class_string(str(node.text))) in ["nothing"]:
                                    jump = 1
                                else : 
                                    jump=0
                                    rectangle_multi.label_chall=int(vid_classes.class_string_to_comp_code(str(vid_classes.code_to_class_string(str(node.text)))))
                                    # print rectangle_multi.label_chall
                                    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(node.text)
                            if tag in ["xmin"]:
                                if jump == 0:
                                    rectangle_multi.x1=float(node.text)
                            if tag in ["ymax"]:
                                if jump == 0:
                                    rectangle_multi.y2=float(node.text)                            
                            if tag in ["ymin"]:
                                if jump == 0:    
                                    rectangle_multi.y1=float(node.text)
                                    image_multi_class.append_rect(rectangle_multi)
                        if jump == 0:
                            image_multi_class.append_labeled_rect(rectangle_multi)
                        break
                frames_list.append(image_multi_class)
        video_list.append(frames_list)
        # frames_list=None
        # frames_list=[]        
    return video_list
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 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
Exemplo n.º 5
0
def recurrent_label_video(video_info, frames):

    progress = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ',progressbar.Percentage(), ' ',progressbar.ETA()])
    decomposed_path =frames[0].split("/")
    folder = decomposed_path[len(decomposed_path)-2]
    if not os.path.exists(folder+"/cropped_rects/"):
    	os.makedirs(folder+"/cropped_rects/")
        print("Created Folder: %s"%(folder+"/cropped_rects/"))

    # Loads label file, strips off carriage return
    label_lines = [line.rstrip() for line in tf.gfile.GFile(label_file)]
    # Creates graph from saved GraphDef.
    create_graph()
    saver = tf.train.Saver()  # defaults to saving all variables - in this case w and b
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        # load_checkpoint(sess, saver)
        softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
        idx=0
        video_labels=[]
        for frame_info in progress(video_info):
            print "Tracking Frame Nr: %d"%frame_info.frame
            print len(frame_info.rects)
            rect_id=0
            frame_labels=[]
            frame_info.filename = frames[idx]
            for rect in frame_info.rects:
                
                img= Image.open(frames[idx])
                width, height= utils_image.get_Image_Size(frames[idx])
                print rect.x1,rect.y1,rect.x2 ,rect.y2
                x1,y1,x2,y2=utils_image.get_orig_rect(width, height, 640, 480, rect.x1,rect.y1,rect.x2 ,rect.y2)
                print x1,y1,x2,y2
                cor = (min(x1,x2),min(y1,y2),max(x1,x2),max(y1,y2))
                print cor
                cropped_img=img.crop(cor)
                cropped_img_name=folder+"/cropped_rects/cropped_frame_%d_rect_%d.JPEG"%(frame_info.frame, rect_id)
                cropped_img.save(cropped_img_name)
                print "Frame: %d Rect: %d conf: %.2f"%(frame_info.frame, rect_id, rect.true_confidence)
                if not tf.gfile.Exists(cropped_img_name):
                    tf.logging.fatal('File does not exist %s', cropped_img_name)
                    sys.exit()
                image_data = tf.gfile.FastGFile(cropped_img_name, 'rb').read()

                predictions = sess.run(softmax_tensor,{'DecodeJpeg/contents:0': image_data})
                predictions = np.squeeze(predictions)

                top_k = predictions.argsort()[-3:][::-1]  # Getting top 5 predictions

                #CHECK OUTPUT
                for node_id in top_k:
                    human_string = label_lines[node_id]
                    score = predictions[node_id]
                    print('%s (score = %.5f)' % (human_string, score))

                if(len(video_labels)>0):
                    if(video_labels[idx-1][rect_id][0]==top_k[0]):
                        # CHECK BEST LABEL
                        print "Best Label: %s with conf: %.5f"%(vid_classes.code_to_class_string(label_lines[top_k[0]]),predictions[top_k[0]])
                        rect.set_rect_coordinates(x1,x2,y1,y2)
                        rect.set_label(predictions[top_k[0]], vid_classes.code_to_class_string(label_lines[top_k[0]]), top_k[0], label_lines[top_k[0]])
                        frame_labels.append((top_k[0], predictions[top_k[0]]))
                    else:
                        label = video_labels[idx-1][rect_id][0] 
                        print "Best Label setted recurrently: %s "%(vid_classes.code_to_class_string(label_lines[label]))
                        rect.set_rect_coordinates(x1,x2,y1,y2)
                        rect.set_label(video_labels[idx-1][rect_id][1], vid_classes.code_to_class_string(label_lines[label]), label, label_lines[label])
                        frame_labels.append((label, video_labels[idx-1][rect_id][1]))
                else:
                    # CHECK BEST LABEL
                    print "Best Label: %s with conf: %.5f"%(vid_classes.code_to_class_string(label_lines[top_k[0]]),predictions[top_k[0]])
                    rect.set_rect_coordinates(x1,x2,y1,y2)
                    rect.set_label(predictions[top_k[0]], vid_classes.code_to_class_string(label_lines[top_k[0]]), top_k[0], label_lines[top_k[0]])
                    frame_labels.append((top_k[0], predictions[top_k[0]]))
                    
                rect_id=rect_id+1
            video_labels.append(frame_labels)
            idx=idx+1
    return video_info
Exemplo n.º 6
0
def pre_process_dataset(bb_XML_file_list, path_val_folder, path_dataset):

    with open(path_dataset + '/dataset_summary.txt', 'w') as summary:
        for class_code in CL.class_code_string_list:
            start_string = "Starting making files for class code: %s ; name: %s, May take a while.... " % (
                class_code, vid_classes.code_to_class_string(class_code))
            staring_xml_string = "Strating xml files: %d" % len(
                bb_XML_file_list)
            print start_string + '/n', staring_xml_string + '/n'
            summary.write(start_string + os.linesep + staring_xml_string +
                          os.linesep)
            progress = progressbar.ProgressBar(widgets=[
                progressbar.Bar('=', '[', ']'), ' ',
                progressbar.Percentage(), ' ',
                progressbar.ETA()
            ])
            tot_rect_class = 0
            count_xml = 0
            bb_folder_path = path_dataset + '/annotations/' + class_code + '/'
            data_folder_path = path_dataset + '/data/' + class_code + '/'
            for file_name in progress(bb_XML_file_list):
                removed = 0
                delete = False
                with open(file_name, 'rt') as f:
                    #print "File Opened: %s"%file_name
                    tree_new = ElementTree.parse(f)
                    root_new = tree_new.getroot()
                    parent_map_new = dict(
                        (c, p) for p in tree_new.getiterator() for c in p)
                    count_rect = 0
                    # list so that we don't mess up the order of iteration when removing items.
                    for obj in list(tree_new.findall('object')):
                        obj_class_code = obj.find('name').text
                        if obj_class_code == str(class_code):
                            #print "Founded Object: %s"%vid_classes.code_to_class_string(obj_class_code)
                            #count_rect+1 there's an object of that class in the xml file
                            count_rect = count_rect + 1
                            delete = True
                        else:
                            #eliminate node
                            #print "Eliminated Node: %s"%vid_classes.code_to_class_string(obj_class_code)
                            parent_map_new[obj].remove(obj)
                            removed = removed + 1
                    if count_rect > 0:
                        ### Means the file belongs to the class so we change filename and directory name and we copy the image to the dataset
                        for node in tree_new.iter():
                            tag = str(node.tag)
                            if tag in ["folder"]:
                                path_orig_file = path_val_folder + '/' + str(
                                    node.text)
                                node.text = data_folder_path
                                # print "Changed folder from: %s to : %s"%(path_orig_file, node.text)
                            if tag in ["filename"]:
                                path_orig_file = path_orig_file + '/' + str(
                                    node.text) + '.JPEG'
                                new_filename = class_code + '_' + str(
                                    000000 + count_xml) + '.JPEG'
                                path_new_file = data_folder_path + new_filename
                                node.text = new_filename
                                # print "Changed Name from: %s to : %s"%(path_orig_file, path_new_file)
                        xml_filename = bb_folder_path + class_code + '_' + str(
                            000000 + count_xml) + '.xml'
                        tree_new.write(xml_filename)
                        shutil.copy2(path_orig_file, path_new_file)
                        # print "Saved New .xml file: %s"%xml_filename
                        # print "Saved New .jpeg image: %s"%path_new_file
                        if (removed == 0) & delete:
                            os.remove(path_orig_file)
                            # print "Removed image: %s"%(path_orig_file)
                        count_xml = count_xml + 1
                        tot_rect_class = tot_rect_class + count_rect
                        ##TODO: Copy Image
                # print "Count_Rect %d"%count_rect
                # print "Removed %d"%removed
                if (removed == 0) & delete:
                    os.remove(file_name)
                    # print "Removed XML: %s"%(file_name)
                    bb_XML_file_list.remove(file_name)
                if (removed > 0) & (count_rect > 0):
                    with open(file_name, 'rt') as f:
                        tree_new = ElementTree.parse(f)
                        root_new = tree_new.getroot()
                        parent_map_new = dict(
                            (c, p) for p in tree_new.getiterator() for c in p)
                        for obj in list(tree_new.findall('object')):
                            obj_class_code = obj.find('name').text
                            if obj_class_code == str(class_code):
                                parent_map_new[obj].remove(obj)
                        tree_new.write(file_name)
            end_string = "Ended with Success Process for class:%s" % class_code
            parsed_bb_string = "Parsed: %d BB Files" % count_xml
            added_rect_String = "Added: %d Object Rectangles" % tot_rect_class
            print end_string + '/n', parsed_bb_string + '/n', added_rect_String + '/n'
            summary.write(end_string + os.linesep + parsed_bb_string +
                          os.linesep + added_rect_String + os.linesep)
Exemplo n.º 7
0
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 = vid_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 = Frame_Info()
                    image_single_class.dataset_path = path_val_folder

                    image_multi_class = Frame_Info()
                    image_multi_class.dataset_path = path_val_folder

                    rectangle_single = Rectangle_Multiclass()
                    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_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(
                                    vid_classes.code_to_class_string(
                                        str(node.text))) in ["nothing"]:
                                jump = 1
                            else:
                                jump = 0
                                rectangle_multi.label_chall = int(
                                    vid_classes.class_string_to_comp_code(
                                        str(
                                            vid_classes.code_to_class_string(
                                                str(node.text)))))
                                rectangle_multi.label_code = str(node.text)
                                rectangle_multi.label = vid_classes.code_to_class_string(
                                    str(node.text))

                                if str(node.text) == class_code:
                                    same_label = 1
                                    rectangle_single.label_chall = int(
                                        vid_classes.class_string_to_comp_code(
                                            str(
                                                vid_classes.
                                                code_to_class_string(
                                                    str(node.text)))))
                                    rectangle_single.label_code = str(
                                        node.text)
                                    rectangle_single.label = vid_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))
Exemplo n.º 8
0
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 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 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 = vid_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= Frame_Info()
                    image_single_class.dataset_path= path_val_folder

                    image_multi_class= Frame_Info()
                    image_multi_class.dataset_path= path_val_folder


                    rectangle_single= Rectangle_Multiclass()
                    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_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(vid_classes.code_to_class_string(str(node.text))) in ["nothing"]:
                                jump = 1
                            else : 
                                jump=0
                                rectangle_multi.label_chall=int(vid_classes.class_string_to_comp_code(str(vid_classes.code_to_class_string(str(node.text)))))
                                rectangle_multi.label_code=str(node.text)
                                rectangle_multi.label=vid_classes.code_to_class_string(str(node.text))

                                if str(node.text) == class_code: 
                                    same_label = 1
                                    rectangle_single.label_chall=int(vid_classes.class_string_to_comp_code(str(vid_classes.code_to_class_string(str(node.text)))))
                                    rectangle_single.label_code=str(node.text)
                                    rectangle_single.label=vid_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))