示例#1
0
def _process_image(filename):
    """Process a image and annotation file.

    Args:
      filename: string, path to an image file e.g., '/path/to/example.JPG'.
    """
    # Read the image file.
    # filename = '../../.jpg'
    image_data = tf.gfile.FastGFile(filename, 'rb').read()
    import cv2
    img=cv2.imread(filename)
    shape=img.shape

    # # 解码RGB JPEG.
    # image = tf.image.decode_jpeg(image_data)

    '''读取单张图片的信息'''
    imgid = filename.split('.')[0].split('/')[-1]
    imgdata = anno_func.load_img(anns, DATADIR, imgid)
    imgdata_draw, mask_ellipse, img = anno_func.draw_all(anns, DATADIR, imgid, imgdata)
    img_info = img['objects']
    label_classes = []
    label_text = []
    bbox_list = []
    ymin = []
    xmin = []
    ymax = []
    xmax = []
    for obj in range(len(img_info)):
        label_text.append(img_info[obj]['category'].encode())
        bbox = img_info[obj]['bbox']
        assert len(bbox) == 4
        bbox_list.append([bbox['xmin'], bbox['ymin'], bbox['xmax'], bbox['ymax']])
        label_classes.append(int(target_classes[img_info[obj]['category']][0]))
    return image_data,  label_classes, label_text,bbox_list,shape
    def get_actual_data_from_xml(img_path):
        actual_item = []
        img2 = []
        img1 = []
        try:
            img_id = img_path.split('\\')[-1].split('.')[0]
            # datadir = os.path.join(dataset_dir, os.path.pardir)
            imgdata, img_msg = anno_func.load_img(anns, dataset_dir, img_id)
            imgdata = imgdata[400:1424, :, :]
            split_list = np.hsplit(imgdata, 2)
            split_interval = len(split_list[0])
            shape = split_list[0].shape
            img_width, img_height, depth = shape
            img_info = img_msg['objects']

            for info in img_info:
                label = info['category'].strip()
                label = classes[label][0]
                xmin = info['bbox']['xmin']
                xmax = info['bbox']['ymin']
                ymin = info['bbox']['xmax'] - 400.0
                ymax = info['bbox']['ymax'] - 400.0
                # 1024.0为设定的剪切图片的大小
                if ymin > 1024.0:
                    break
                if ymax > 1024.0:
                    ymax = 1024.0
                if xmin < split_interval and xmax < split_interval:
                    xmin, xmax = xmin, xmax
                    img1.append([((xmin + xmax) / 2 / img_width), ((ymin + ymax) / 2 / img_height),
                                 ((xmin - xmax) / img_width),
                                 ((ymin - ymax) / img_height), label])
                elif xmin < split_interval and xmax < 2 * split_interval:
                    xmin, xmax = xmin, split_interval
                    img1.append([((xmin + xmax) / 2 / img_width), ((ymin + ymax) / 2 / img_height),
                                 ((xmin - xmax) / img_width),
                                 ((ymin - ymax) / img_height), label])
                elif (xmin >= split_interval and xmin < 2 * split_interval) and (
                        xmax < 2 * split_interval):  # 超越第一张图片的大小
                    xmin, xmax = xmin - split_interval, xmax - split_interval
                    img2.append([((xmin + xmax) / 2 / img_width), ((ymin + ymax) / 2 / img_height),
                                 ((xmin - xmax) / img_width),
                                 ((ymin - ymax) / img_height), label])
                elif (xmin >= split_interval and xmin < 2 * split_interval) and (xmax >= 2 * split_interval):  # 一倍间距
                    xmin, xmax = xmin - split_interval, xmax - split_interval
                    img2.append([((xmin + xmax) / 2 / img_width), ((ymin + ymax) / 2 / img_height),
                                 ((xmin - xmax) / img_width),
                                 ((ymin - ymax) / img_height), label])
                else:
                    continue
            actual_item.extend([img1, img2])
            return actual_item
        except:
            return None
示例#3
0
def draw_pic(annos, datadir, imgid):
    """
    Draw boundary by annotation, function call of anno_func.py
    Library pylab is required
    :param annos: annotation of region
    :param datadir: image directory
    :param imgid: image id
    :return: null
    """
    imgdata = anno_func.load_img(annos, datadir, imgid)
    imgdata_draw = anno_func.draw_all(annos, datadir, imgid, imgdata)
    pl.figure(figsize=(20, 20))
    pl.imshow(imgdata_draw)
    pl.show()
def process_image(dataset_dir, filename):
    '''获取每张图片的信息'''
    filename_id = filename.split('\\')[-1].split('.')[0]
    ANNOTATIONS = os.path.join(dataset_dir, os.path.pardir, 'annotations.json')
    ids = open(dataset_dir + 'ids.txt').read().splitlines()
    annotations = json.loads(open(ANNOTATIONS).read())
    CLASSES = annotations['types']
    classes = dict()
    for i, name in enumerate(CLASSES):
        classes[name] = (i + 1, i + 1)
    classes['Background'] = (0, 0)
    # filename_id='571'
    dataset_dir = os.path.join(
        dataset_dir, os.path.pardir)  #为了配合annos存储的是'path': 'test/33132.jpg'
    image_data = tf.gfile.FastGFile(filename, 'rb').read()

    imgdata, img = anno_func.load_img(annotations, dataset_dir, filename_id)
    shape = imgdata.shape
    img_info = img['objects']
    bboxes = []
    labels = []
    labels_text = []
    xmin = []
    ymin = []
    xmax = []
    ymax = []
    for info in img_info:
        labels_text.append(info['category'].encode('utf-8'))
        labels.append(classes[info['category']][0])
        xmin.append(info['bbox']['xmin'])
        ymin.append(info['bbox']['ymin'])
        xmax.append(info['bbox']['xmax'])
        ymax.append(info['bbox']['ymax'])
        bbox = info['bbox']
        bboxes.append([
            bbox['xmin'] / shape[0], bbox['ymin'] / shape[1],
            bbox['xmax'] / shape[0], bbox['ymax'] / shape[1]
        ])
        # r_e_shape.append(info)
    return image_data, shape, labels, labels_text, bboxes
示例#5
0
def get_img_info(img):
    '''读取单张图片的信息'''
    imgid = img.split('.')[0]
    imgdata = anno_func.load_img(anns, DATADIR, imgid)
    imgdata_draw, mask_ellipse, img = anno_func.draw_all(
        anns, DATADIR, imgid, imgdata)
    img_info = img['objects']
    label_classes = []
    label_text = []
    bbox_list = []
    ymin = []
    xmin = []
    ymax = []
    xmax = []
    for obj in range(len(img_info)):
        label_text.append(img_info[obj]['category'].encode())
        bbox = img_info[obj]['bbox']
        assert len(bbox) == 4
        bbox_list.append(
            [bbox['xmin'], bbox['ymin'], bbox['xmax'], bbox['ymax']])
        label_classes.append(int(target_classes[img_info[obj]['category']]))
    return bbox_list, label_classes, label_text
import json
import pylab as pl
import random
import numpy as np
import cv2
import anno_func

datadir = "/home/richardchen123/Documents/data/YOLOv4/data"

filedir = datadir + "/annotations.json"
ids = open(datadir + "/test/ids.txt").read().splitlines()

annos = json.loads(open(filedir).read())

imgid = random.sample(ids, 1)[0]
print(imgid)

imgdata = anno_func.load_img(annos, datadir, imgid)
imgdata_draw = anno_func.draw_all(annos, datadir, imgid, imgdata)
pl.figure(figsize=(20, 20))
pl.imshow(imgdata_draw)



示例#7
0
 def loadInput(self):
     self.imgid = random.sample(self.ids, 1)[0]
     self.indata = anno_func.load_img(self.annos, self.datadir, self.imgid)
     return self.imgid