示例#1
0
    def savePascalVocFormat(self, filename, shapes, imagePath, imageData,
            lineColor=None, fillColor=None, databaseSrc=None):
        imgFolderPath = os.path.dirname(imagePath)

        # Folder name is implicitly set to VOC2007 for purpose of annotation
        # used in detection using py_faster_rcnn.
        #imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFolderName = "VOC2007"

        imgFileName = os.path.basename(imagePath)
        imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]
        imageShape = numpy.asarray(Image.open(imagePath)).shape
        # Martin Kersner, 2015/11/03
        writer = PascalVocWriter(imgFolderName, imgFileName,\
                                 imageShape, localImgPath=imagePath)
        bSave = False
        for shape in shapes:
            points = shape['points']
            label = shape['label']
            bndbox = LabelFile.convertPoints2BndBox(points)
            print bndbox # KERSNEER
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
            bSave = True

        if bSave:
            writer.save(targetFile = filename)
        return
示例#2
0
    def savePascalVocFormat(self,
                            filename,
                            shapes,
                            imagePath,
                            imageData,
                            lineColor=None,
                            fillColor=None,
                            databaseSrc=None):
        imgFolderPath = os.path.dirname(imagePath)
        imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFileName = os.path.basename(imagePath)
        imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]
        imageShape = numpy.asarray(Image.open(imagePath)).shape
        writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt,\
                                 imageShape, localImgPath=imagePath)
        bSave = False
        for shape in shapes:
            points = shape['points']
            label = shape['label']
            bndbox = LabelFile.convertPoints2BndBox(points)
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
            bSave = True

        if bSave:
            writer.save(targetFile=filename)
        return
示例#3
0
    def savePascalVocFormat(self, filename, shapes, imagePath, imageData,
            lineColor=None, fillColor=None, databaseSrc=None):
        imgFolderPath = os.path.dirname(imagePath)
        imgFolderName = os.path.split(imgFolderPath)[-1]
        imgFileName = os.path.basename(imagePath)
        imgFileNameWithoutExt = os.path.splitext(imgFileName)[0]
        imageShape = numpy.asarray(Image.open(imagePath)).shape
        writer = PascalVocWriter(imgFolderName, imgFileNameWithoutExt,\
                                 imageShape, localImgPath=imagePath)
        bSave = False
        for shape in shapes:
            points = shape['points']
            label = shape['label']
            bndbox = LabelFile.convertPoints2BndBox(points) 
            writer.addBndBox(bndbox[0], bndbox[1], bndbox[2], bndbox[3], label)
            bSave = True

        if bSave:
            writer.save(targetFile = filename)
        return
示例#4
0
def main():
    do_draw = False
    image_list = []

    img_db_path = "../annotator/static/images"
    annotations_dir_name = "ann"
    images_dir_name = "img"

    ROI = {"x": 0, "y": 0, "w": 1200, "h": 370}

    ##############################################################################

    args = getParameters(sys.argv)
    db_annotations, db_image_host, db_image_list, db_dirname = getImageListInformation(
        args.db_path, args.id)

    # TODO remove already existing
    # TODO warning
    # create output directory with image list name
    mkdir(args.output_path)
    output_full_path = os.path.join(args.output_path, db_dirname)
    mkdir(output_full_path)

    db_name = "HY"
    dims = (370, 1200, 3)

    # create ann and img subdirectories
    mkdir(os.path.join(output_full_path, annotations_dir_name))
    mkdir(os.path.join(output_full_path, images_dir_name))

    # path to directory of input images
    # img_input_path = os.path.join(img_db_path, db_dirname)
    img_input_path = os.path.join(img_db_path,
                                  os.path.split(db_image_host)[-1])

    annotation_converted = {}
    for ann in db_annotations:
        keyframe = ann["keyframes"][0]["frame"]
        if keyframe in annotation_converted:
            annotation_converted[keyframe].append(ann)
        else:
            annotation_converted[keyframe] = [(ann)]

    for keyframe in annotation_converted:
        image_list.append(keyframe)
        image_name = db_image_list[keyframe].split(".")[0]

        # drawRectangle(frame, ROI["x"], ROI["y"], ROI["w"], ROI["h"], YELLOW_COLOR_) # ROI
        pascal_writer = PascalVocWriter(output_full_path,
                                        str(image_name),
                                        dims,
                                        databaseSrc=db_name)

        for ann in annotation_converted[keyframe]:
            rect = getRectImageList(
                ann["keyframes"]
                [0])  # each annotated object has its won annotation
            obj_type = ann["type"]

            # if do_draw:
            # drawRectangle(frame, rect["x"], rect["y"], rect["w"], rect["h"], getColor(obj_type)) # object
            # drawText(frame, obj_type, rect["x"], rect["y"], getColor(obj_type))

            # image
            # cropped_frame = cropImage(clear_frame, ROI["x"], ROI["y"], ROI["w"], ROI["h"])
            # saveCrop(cropped_frame, os.path.join(output_path, images_dir_name), tmp_img_name)

            # annotation
            if args.kitti_format:
                with open(
                        os.path.join(output_full_path, annotations_dir_name,
                                     str(image_name) + ".txt"), "a") as f:
                    f.write(
                        "{} 0.0 0 0.0 {} {} {} {} 0.0 0.0 0.0 0.0 0.0 0.0 0.0\n"
                        .format(obj_type, rect["x"], rect["y"],
                                rect["x"] + rect["w"], rect["y"] + rect["h"]))
            elif args.voc_format:
                pascal_writer.addBndBox(float(rect["x"] + 1),
                                        float(rect["y"] + 1),
                                        float(rect["x"] + rect["w"]),
                                        float(rect["y"] + rect["h"]), obj_type,
                                        0)

                pascal_writer.save(
                    os.path.join(output_full_path, annotations_dir_name))

            # if do_draw:
            # cv2.imshow("frame", frame)
            # if cv2.waitKey(0) & 0xFF == ord('q'):
            # break

    # copy images
    for idx in image_list:
        shutil.copyfile(
            os.path.join(img_input_path, db_image_list[idx]),
            os.path.join(output_full_path, images_dir_name,
                         db_image_list[idx]))