def analysis_xmls(dir_xml, ext, file_names):
    dict_obj = {}
    for file_name in file_names:
        file_path = dir_xml + '/' + file_name + '.xml'
        print(file_path)
        pvr_obj = PascalVocReader(file_path)
        shapes = pvr_obj.getShapes()
        im_info = pvr_obj.get_imageInfo()
        im_w = im_info[0][0]
        im_h = im_info[0][1]
        # im_c = im_info[0][2]
        for shape in shapes:
            key = shape[0]
            left = shape[1][0]
            top = shape[1][1]
            right = shape[1][2]
            bottom = shape[1][3]

            if 1 >= left or 1 >= top:
                print('error 0 !')
                continue
            if right >= (im_w - 2) or bottom >= (im_h - 2):
                print('error 1 !')
                continue
            if left >= right or top >= bottom:
                print('error 2 !')
                continue

            if key not in dict_obj:
                dict_obj[key] = 1
            else:
                dict_obj[key] += 1
    return dict_obj
Exemple #2
0
    def calculate_images(self, lis=None):

        if lis == None:
            lis = self.lis

        img_count = {}

        for file in lis:

            xml_name = self.anno_path + '/' + file + '.xml'

            if os.path.exists(xml_name):
                pr = PR(xml_name)
                shapes = pr.getShapes()
                #
                label_list = [s[0] for s in shapes]
            else:
                print('file not exist')
            # else:
            #     lis.remove(file)
            #     continue

            for k in self.label_count.keys():
                if k in label_list:
                    try:
                        img_count[k] += 1
                    except:
                        img_count[k] = 1

        return img_count, lis
Exemple #3
0
    def del_label(self, label_del):

        flist = os.listdir(self.anno_path)
        for filename in flist:
            filepath = self.anno_path + '/' + filename
            pr = PR(filepath)
            shapes = pr.getShapes()
            sl = pr.getSources()

            localpath = os.path.join(self.im_path, sl['filename'])

            pw = PW(sl['folder'],
                    sl['filename'],
                    sl['size'],
                    databaseSrc='Unknown',
                    localImgPath=localpath)
            pw.verified = True

            for shape in shapes:
                if shape[0] != label_del:
                    label = shape[0]
                    difficult = 0
                    bndbox = shape[1]
                    pw.addBndBox(bndbox[0][0], bndbox[0][1],
                                 bndbox[2][0], bndbox[2][1], label, difficult)

            pw.save(filepath)
Exemple #4
0
    def label_filter(self, labels, new_dir):
        flist = []
        for lb in labels:
            stemp = self.find_label(lb)
            for fname in stemp:
                if (fname in flist) == False:
                    flist.append(fname)

        for filename in flist:
            filepath = self.anno_path + '/' + filename + '.xml'
            pr = PR(filepath)
            shapes = pr.getShapes()
            sl = pr.getSources()
            localpath = os.path.join(self.im_path, sl['filename'])

            pw = PW(sl['folder'],
                    sl['filename'],
                    sl['size'],
                    databaseSrc='Unknown',
                    localImgPath=localpath)
            pw.verified = True

            for shape in shapes:
                if shape[0] in labels:
                    label = shape[0]
                    difficult = 0
                    bndbox = shape[1]
                    pw.addBndBox(bndbox[0][0], bndbox[0][1],
                                 bndbox[2][0], bndbox[2][1], label, difficult)

            pw.save(new_dir + '/' + filename + '.xml')
Exemple #5
0
def model_test(net, test_list, input_dir, iter_num=1):
    total_score = 0.0
    score = 0.0
    class_score = {}
    for im_name in test_list:
        if DEBUG:
            print "############## scoring {0} ##############".format(im_name)
        im_file = os.path.join(input_dir, 'img', im_name)

        label_file = os.path.join(input_dir, 'annotation', im_name)
        index = label_file.rindex('.')
        label_file = label_file[:index] + ".xml"

        if not os.path.isfile(label_file):
            sys.stderr.write("can't find label file for {0}".format(im_file))
            continue

        label_reader = PascalVocReader(label_file)
        size = label_reader.getSize()
        shapes = label_reader.getShapes()
        true_label = convert_shapes(shapes)

        pre_label = predict_label(net, im_file)

        t_s, s = compare_label(true_label, pre_label, class_score)
        print "scoring {0}".format(im_name), t_s, s, s / t_s
        total_score += t_s
        score += s
    print "iter {0} total_score".format(
        iter_num), score, total_score, score / total_score
    for i in class_score.iterkeys():
        print "iter {0} class_score:".format(
            iter_num), i, class_score[i], class_score[i][0] / class_score[i][1]
    return total_score, score
Exemple #6
0
    def test_upper(self):
        dir_name = os.path.abspath(os.path.dirname(__file__))
        libs_path = os.path.join(dir_name, '..', 'libs')
        sys.path.insert(0, libs_path)
        from pascal_voc_io import PascalVocWriter
        from pascal_voc_io import PascalVocReader

        # Test Write/Read
        writer = PascalVocWriter('tests',
                                 'test', (512, 512, 1),
                                 localImgPath='tests/test.bmp')
        writer.addBndBox(60, 40, 430, 504, 'person', dict(difficult=1))
        writer.addBndBox(113, 40, 450, 403, 'face', dict(difficult=1))
        writer.save('tests/test.xml')

        reader = PascalVocReader('tests/test.xml')
        shapes = reader.getShapes()

        personBndBox = shapes[0]
        face = shapes[1]
        self.assertEqual(personBndBox[0], 'person')
        self.assertEqual(personBndBox[1], [(60, 40), (430, 40), (430, 504),
                                           (60, 504)])
        self.assertEqual(face[0], 'face')
        self.assertEqual(face[1], [(113, 40), (450, 40), (450, 403),
                                   (113, 403)])
Exemple #7
0
    def loadPascalXMLByFilename(self, xmlPath):
        if self.filename is None:
            return
        if os.path.isfile(xmlPath) is False:
            return

        tVocParseReader = PascalVocReader(xmlPath)
        shapes = tVocParseReader.getShapes()
        self.loadLabels(shapes)
Exemple #8
0
    def loadPascalXMLByFilename(self, xmlPath):
        if self.filePath is None:
            return
        if os.path.isfile(xmlPath) is False:
            return

        tVocParseReader = PascalVocReader(xmlPath)
        shapes = tVocParseReader.getShapes()
        self.loadLabels(shapes)
Exemple #9
0
    def check_label(self):

        flist = os.listdir(self.anno_path)

        for filename in flist:
            filepath = self.anno_path + '/' + filename
            pr = PR(filepath)
            shapes = pr.getShapes()
            sl = pr.getSources()

            if len(shapes) == 0:
                print(filepath)
def convert(image_path, annotation_path):

    save_path = os.path.join(os.path.dirname(annotation_path), "xml_converted")
    if not os.path.exists(save_path): os.makedirs(save_path)

    for file in os.listdir(annotation_path):
        if file.endswith(".xml"):
            annotation_no_xml = os.path.splitext(file)[0]

            imagePath = image_path + "/" + annotation_no_xml + ".jpg"

            image = QImage()
            image.load(imagePath)
            imageShape = [
                image.height(),
                image.width(), 1 if image.isGrayscale() else 3
            ]
            imgFolderName = os.path.basename(image_path)
            imgFileName = os.path.basename(imagePath)

            writer = YOLOWriter(imgFolderName,
                                imgFileName,
                                imageShape,
                                localImgPath=imagePath)

            # Read classes.txt
            classListPath = annotation_path + "/" + "classes.txt"
            classesFile = open(classListPath, 'r')
            classes = classesFile.read().strip('\n').split('\n')
            classesFile.close()

            # Read VOC file
            filePath = annotation_path + "/" + file
            tVocParseReader = PascalVocReader(filePath)
            shapes = tVocParseReader.getShapes()
            num_of_box = len(shapes)

            for i in range(num_of_box):
                label = classes.index(shapes[i][0])
                xmin = shapes[i][1][0][0]
                ymin = shapes[i][1][0][1]
                x_max = shapes[i][1][2][0]
                y_max = shapes[i][1][2][1]

                writer.addBndBox(xmin, ymin, x_max, y_max, label, 0)

            writer.save(targetFile=save_path + "/" + annotation_no_xml +
                        ".txt")
Exemple #11
0
    def split_single_image(self, img_id):

        xml_file = self.anno_path / f"{img_id}.xml"
        objects = PascalVocReader(xml_file).get_shapes()
        img_file = self.img_path / f"{img_id}{self.suffix}"

        # img = cv2.imread(str(img_file))
        # img_height, img_width = img.shape[:2]
        img = gdal.Open(str(img_file))
        img_height = img.RasterYSize
        img_width = img.RasterXSize

        top, left = 0, 0
        # start_positions = []
        patch_ids = []
        while (top < img_height):

            # print(top)
            if (top + self.patch_size >= img_height):
                top = max(img_height - self.patch_size, 0)
            left = 0
            while (left < img_width):
                # print(f"left = {left}")
                if (left + self.patch_size >= img_width):
                    left = max(img_width - self.patch_size, 0)

                right = min(left + self.patch_size, img_width)
                down = min(top + self.patch_size, img_height)
                patch_name = f"{img_id}__{top}__{left}"
                self.save_patches(img, objects, patch_name, left, top, right,
                                  down)
                patch_ids.append(patch_name)

                if left + self.patch_size >= img_width:
                    break
                else:
                    left += self.slide_step
            if top + self.patch_size >= img_height:
                break
            else:
                top += self.slide_step

        img = None
        print(img_file)

        if img_id in self.train_sets:
            # self.train_ids += patch_ids
            with (self.set_out_path / 'train.txt').open('a') as fp:
                fp.write('\n'.join(patch_ids) + '\n')
        else:
            # self.val_ids += patch_ids
            with (self.set_out_path / 'val.txt').open('a') as fp:
                fp.write('\n'.join(patch_ids) + '\n')
Exemple #12
0
    def test_upper(self):
        dir_name = os.path.abspath(os.path.dirname(__file__))
        libs_path = os.path.join(dir_name, '..', 'libs')
        sys.path.insert(0, libs_path)
        from pascal_voc_io import PascalVocWriter
        from pascal_voc_io import PascalVocReader

        # Test Write/Read
        writer = PascalVocWriter('tests', 'test', (512, 512, 1), localImgPath='tests/test.bmp')
        difficult = 1
        writer.addBndBox(60, 40, 430, 504, 'person', difficult)
        writer.addBndBox(113, 40, 450, 403, 'face', difficult)
        writer.save('tests/test.xml')

        reader = PascalVocReader('tests/test.xml')
        shapes = reader.getShapes()

        personBndBox = shapes[0]
        face = shapes[1]
        self.assertEqual(personBndBox[0], 'person')
        self.assertEqual(personBndBox[1], [(60, 40), (430, 40), (430, 504), (60, 504)])
        self.assertEqual(face[0], 'face')
        self.assertEqual(face[1], [(113, 40), (450, 40), (450, 403), (113, 403)])
Exemple #13
0
        imgFolderName = os.path.basename(imgFolderPath)
        imgFileName = os.path.basename(imagePath)

        writer = YOLOWriter(imgFolderName,
                            imgFileName,
                            imageShape,
                            localImgPath=imagePath)

        # Read classes.txt
        classListPath = imgFolderPath + "/" + "classes.txt"
        classesFile = open(classListPath, 'r')
        classes = classesFile.read().strip('\n').split('\n')
        classesFile.close()

        # Read VOC file
        filePath = imgFolderPath + "/" + file
        tVocParseReader = PascalVocReader(filePath)
        shapes = tVocParseReader.getShapes()
        num_of_box = len(shapes)

        for i in range(num_of_box):
            label = classes.index(shapes[i][0])
            xmin = shapes[i][1][0][0]
            ymin = shapes[i][1][0][1]
            x_max = shapes[i][1][2][0]
            y_max = shapes[i][1][2][1]

            writer.addBndBox(xmin, ymin, x_max, y_max, label, 0)

        writer.save(targetFile=imgFolderPath + "/" + annotation_no_xml +
                    ".txt")
Exemple #14
0
from unittest import TestCase

import sys
import os
dir_name = os.path.abspath(os.path.dirname(__file__))
libs_path = os.path.join(dir_name, '..', 'libs')
sys.path.insert(0, libs_path)
from pascal_voc_io import PascalVocWriter
from pascal_voc_io import PascalVocReader

# Test Write/Read
writer = PascalVocWriter('tests',
                         'test', (512, 512, 1),
                         localImgPath='tests/test.bmp')
difficult = 1
writer.addBndBox(60, 40, 430, 504, 'person', difficult)
writer.addBndBox(113, 40, 450, 403, 'face', difficult)
writer.save('tests/test.xml')

reader = PascalVocReader('tests/test.xml')
shapes = reader.getShapes()
Exemple #15
0
from pascal_voc_io import PascalVocWriter
from pascal_voc_io import PascalVocReader
import os
import cv2

path = '/root/darknet/scripts/VOCdevkit/VOC2050back/Annotations/'
imgpath = '/root/darknet/scripts/VOCdevkit/VOC2050back/JPEGImages/'
pathdir = os.listdir(path)
for file in pathdir:
    filename = path + file
    print file
    dot = file.find('.')
    purename = file[:dot]
    reader = PascalVocReader(filename)
    reader.parseXML()
    boxes = []
    for i, shape in enumerate(reader.shapes):
        if i < (len(reader.shapes) / 2):
            boxes.append(shape)
    boxes = sorted(boxes, key=lambda item: item[1][0][0])
    readimgname = 'VOCdevkit/VOC2050/JPEGImages/{}.jpg'.format(purename)
    img = cv2.imread(readimgname)
    imgSize = [img.shape[0], img.shape[1], 1]
    foldername = 'JPEGImages'
    filename = purename + '.jpg'
    localImagePath = '/root/darknet/scripts/VOCdevkit/VOC2050back/JPEGImages/' + filename
    writer = PascalVocWriter(foldername,
                             filename,
                             imgSize,
                             localImgPath=localImagePath)
    #print type(boxes)
                L.append(os.path.join(file))
    return L


file_dir = '/root/darknet/scripts/VOCdevkit/VOC2050_corrected/JPEGImages/'
goal_dir = '/root/darknet/scripts/VOCdevkit/VOC2050_corrected/JPEGImages/'
xml_dir = '/root/darknet/scripts/VOCdevkit/VOC2050_corrected/Annotations/'
foldername = 'JPEGImages'
for name in file_name(file_dir):
    img = cv2.imread(file_dir + name)
    dot = name.find('.')
    namenum = int(name[:dot])
    newname = str(namenum + 200000)
    xmlfname = xml_dir + name[:dot] + '.xml'
    print xmlfname
    reader = PascalVocReader(xmlfname)
    reader.parseXML()

    boxes = []
    for i, shape in enumerate(reader.shapes):
        if i < (len(reader.shapes) / 2):
            boxes.append(shape)

    #print boxes
    for box in boxes:
        #print '--------'
        #print box
        section = random.randint(1, 4)
        xmin = box[1][0][0]
        ymin = box[1][0][1]
        xmax = box[1][2][0]
Exemple #17
0
    im = 128 * np.ones((300, 500, 3), dtype=np.uint8)
    for i in xrange(2):
        _, _= im_detect(net, im)

    im_names = os.listdir(input_dir+"/img")

    for im_name in im_names:
        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        print "auto label {0}".format(im_name)
        im_file = os.path.join(input_dir, 'img', im_name)

        label_file = os.path.join(input_dir, 'annotation', im_name)
        index = label_file.rindex('.')
        label_file = label_file[:index] + ".xml"
        if os.path.isfile(label_file):
            tmp_reader = PascalVocReader(label_file)
            size = tmp_reader.getSize()
            tmp_writer = PascalVocWriter(input_dir, im_name, size)
            shapes = tmp_reader.getShapes()
            for i in shapes:
                tmp_writer.addBndBox(i[1][0][0], i[1][0][1], i[1][2][0], i[1][2][1], i[0])
            print "load {0} labels from {1}".format(len(shapes), label_file)
        else:
            print "can not find label file {0}".format(label_file)
            size = cv2.imread(im_file).shape
            tmp_writer = PascalVocWriter(input_dir, im_name, size)
        
        n = auto_label(net, im_file, tmp_writer)
        
        tmp_writer.save(output_dir + '/' + os.path.basename(label_file))
        print "auto label {0} labels".format(n)