Ejemplo n.º 1
0
def worker(output_dir, video_dir):
    image_names = glob(os.path.join(video_dir, '*.JPEG'))
    image_names = sorted(image_names,
                        key=lambda x:int(x.split('/')[-1].split('.')[0]))
    video_name = video_dir.split('/')[-1]
    save_folder = os.path.join(output_dir, video_name)
    if not os.path.exists(save_folder):
        os.mkdir(save_folder)
    trajs = {}
    for image_name in image_names:
        img = cv2.imread(image_name)
        img_mean = tuple(map(int, img.mean(axis=(0, 1))))
        anno_name = image_name.replace('Data', 'Annotations')
        anno_name = anno_name.replace('JPEG', 'xml')
        tree = ET.parse(anno_name)
        root = tree.getroot()
        bboxes = []
        filename = root.find('filename').text
        for obj in root.iter('object'):
            bbox = obj.find('bndbox')
            bbox = list(map(int, [bbox.find('xmin').text,
                                  bbox.find('ymin').text,
                                  bbox.find('xmax').text,
                                  bbox.find('ymax').text]))
            trkid = int(obj.find('trackid').text)
            if trkid in trajs:
                trajs[trkid].append(filename)
            else:
                trajs[trkid] = [filename]
            instance_img, _, _ = get_instance_image(img, bbox,
                    config.exemplar_size, config.instance_size, config.context_amount, img_mean)
            instance_img_name = os.path.join(save_folder, filename+".{:02d}.x.jpg".format(trkid))
            cv2.imwrite(instance_img_name, instance_img)
    return video_name, trajs
Ejemplo n.º 2
0
def worker(output_dir, video_dir):

    image_names = glob(os.path.join(video_dir, '*.JPEG'))

    #sort函数
    #sorted()作用于任意可以迭代的对象,而sort()一般作用于列表;
    #sort()函数不需要复制原有列表,消耗的内存较少,效率也较高: b=sorted(a)并不改变a的排序,a.sort() 会改变a的排序
    #sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)  #key依据某一列为排序依据
    image_names = sorted(
        image_names,
        key=lambda x: int(x.split('/')[-1].split('.')[0]))  #从小到大进行排列

    video_name = video_dir.split('/')[-1]

    save_folder = os.path.join(output_dir, video_name)

    if not os.path.exists(save_folder):
        os.mkdir(save_folder)

    trajs = {}

    for image_name in image_names:
        img = cv2.imread(image_name)
        #axis=0,表示shape第0个元素被压缩成1,即求每一列的平均值,axis=1,表示输出矩阵是1列(shape第一个元素被压缩成1),求每一行的均值,axis=(0,1)表示shape的第0个元素和第1个元素被压缩成了1
        #元组和列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组的创建很简单,只需要在括号中添加元素,并使用逗号间隔开
        #map(int, img.mean(axis=(0, 1)))将数据全部转换为int类型列表
        img_mean = tuple(map(int, img.mean(axis=(0, 1))))
        anno_name = image_name.replace(
            'Data', 'Annotations')  #str.replace('a','b')将str中的a替换为字符串中的b
        anno_name = anno_name.replace('JPEG', 'xml')
        tree = ET.parse(anno_name)  #解析xml文件
        root = tree.getroot(
        )  #获取根节点; 作为一个元素,root有一个标签和一个属性字典,它也有子节点,for child in root
        bboxes = []
        filename = root.find(
            'filename'
        ).text  #查找指定标签的文本内容,对于任何标签都可以有三个特征,标签名root.tag,标签属性root.attrib,标签的文本内容root.text
        for obj in root.iter('object'):  #迭代所有的object属性
            bbox = obj.find('bndbox')  #找到objecet中的 boundbox 坐标值
            bbox = list(
                map(int, [
                    bbox.find('xmin').text,
                    bbox.find('ymin').text,
                    bbox.find('xmax').text,
                    bbox.find('ymax').text
                ]))
            trkid = int(obj.find('trackid').text)
            if trkid in trajs:
                trajs[trkid].append(filename)  #如果已经存在,就append
            else:  #添加
                trajs[trkid] = [filename]
            instance_img, _, _ = get_instance_image(img, bbox,
                                                    config.exemplar_size,
                                                    config.instance_size,
                                                    config.context_amount,
                                                    img_mean)
            instance_img_name = os.path.join(
                save_folder, filename + ".{:02d}.x.jpg".format(trkid))
            cv2.imwrite(instance_img_name, instance_img)
    return video_name, trajs