コード例 #1
0
def parse_multi_file(filen, ret=None, groundtruth=None):
    file = open(filen, "r")
    if ret == None:
        ret = DataSetMulti()
    if drop_neg:
        negs_files = [f[:f.rfind(".")] for f in os.listdir(negs)]
    i = 0
    for line in file:
        line = line.strip().rstrip()
        splited = line.split()
        filename = splited[0]
        # filename = filename[filename.rfind("/")+1:]
        # filename = filename[:filename.rfind(".")]
        if drop_neg:
            if filename in negs_files:
                continue
        height = int(splited[1])
        width = int(splited[2])
        class_id = int(splited[3])
        (confidence, x, y, x2, y2) = tuple([float(a) for a in splited[4:]])
        if hratio != None:
            height = y2 - y
            height2 = height * hratio
            y += (height - height2) / 2.0
            y2 = y + height2
        if wratio != None:
            width = x2 - x
            width2 = width * wratio
            x += (width - width2) / 2.0
            x2 = x + width2
        if whratio != None:
            height = y2 - y
            width = x2 - x
            width2 = height * whratio
            x += (width - width2) / 2.0
            x2 = x + width2
        bb = BoundingBox(x, y, x2, y2, confidence)
        if groundtruth != None:
            img = groundtruth[filename]
            if img == []:
                print "warning: " + filename + " not found in groundtruth."
                continue
            # r = bb.area() / (img.height * img.width)
            # if (min_area_ratio != None and r < min_area_ratio) or \
            #     (max_area_ratio != None and r > max_area_ratio):
            #     print "not adding ratio " + str(r)
            #     continue
        area = bb.area()
        if (min_area == None or area >= min_area) and \
                (max_area == None or area <= max_area):
            ret.add_obj(confidence, filename, bb)
            i = i + 1
    file.close()
    return ret
コード例 #2
0
ファイル: eblearn.py プロジェクト: fireae/visiongrader
def parse_multi_file(filen, ret = None, groundtruth = None):
    file = open(filen, "r")
    if ret == None:
        ret = DataSetMulti()
    if drop_neg:
        negs_files = [f[:f.rfind(".")] for f in os.listdir(negs)]
    i = 0
    for line in file:
        line = line.strip().rstrip()
        splited = line.split()
        filename = splited[0]
        # filename = filename[filename.rfind("/")+1:]
        # filename = filename[:filename.rfind(".")]
        if drop_neg:
            if filename in negs_files:
                continue
        height = int(splited[1])
        width = int(splited[2])
        class_id = int(splited[3])
        (confidence, x, y, x2, y2) = tuple([float(a) for a in splited[4:]])
        if hratio != None:
            height = y2 - y
            height2 = height * hratio
            y += (height - height2) / 2.0
            y2 = y + height2
        if wratio != None:
            width = x2 - x
            width2 = width * wratio
            x += (width - width2) / 2.0
            x2 = x + width2
        if whratio != None:
            height = y2 - y
            width = x2 - x
            width2 = height * whratio
            x += (width - width2) / 2.0
            x2 = x + width2
        bb = BoundingBox(x, y, x2, y2, confidence)
        if groundtruth != None:
            img = groundtruth[filename]
            if img == []:
                print "warning: " + filename + " not found in groundtruth."
                continue ;
            # r = bb.area() / (img.height * img.width)
            # if (min_area_ratio != None and r < min_area_ratio) or \
            #     (max_area_ratio != None and r > max_area_ratio):
            #     print "not adding ratio " + str(r)
            #     continue            
        area = bb.area()
        if (min_area == None or area >= min_area) and \
                (max_area == None or area <= max_area):
            ret.add_obj(confidence, filename, bb)
            i = i + 1
    file.close()
    return ret
コード例 #3
0
ファイル: inria.py プロジェクト: fireae/visiongrader
def get_object(data, i):
    xmin, xmax, ymin, ymax = None, None, None, None
    object_char = data.find("object %d" % (i, ))
    while object_char != -1:
        begin_line = data[:object_char].rfind("\n") + 1
        end_line = begin_line + 1 + data[begin_line + 1:].find("\n")
        line = data[begin_line:end_line]
        if line.find("Bounding box") != -1:
            # if this happens, there are other objects than pedestrians
            # in the annotation :
            assert (line.find("PASperson") != -1)
            box_begin = line.rfind(":") + 1
            box_str = line[box_begin:]
            box_str = box_str[box_str.find("(") + 1:].strip()
            xmin = int(box_str[:box_str.find(",")].strip().rstrip())
            box_str = box_str[box_str.find(",") + 1:].strip()
            ymin = int(box_str[:box_str.find(")")].strip().rstrip())
            box_str = box_str[box_str.find("(") + 1:].strip()
            xmax = int(box_str[:box_str.find(",")].strip().rstrip())
            box_str = box_str[box_str.find(",") + 1:].strip()
            ymax = int(box_str[:box_str.find(")")].strip().rstrip())
            break
        object_char = object_char+1 \
            + data[object_char+1:].find("object %d"%(i,))
    if xmin == None:
        return None
    if whratio != None:
        height = ymax - ymin
        width = xmax - xmin
        width2 = height * whratio
        xmin += (width - width2) / 2.0
        xmax = xmin + width2
    return BoundingBox(xmin, ymin, xmax, ymax, 1.0)
コード例 #4
0
 def on_button_release(self, widget, event):
     if event.button == 1:
         self.button1 = False
         o = self.button1_origin
         w0 = max(0, min(o[1], event.x))
         h0 = max(0, min(o[0], event.y))
         w1 = max(w0, max(o[1], event.x))
         h1 = max(h0, max(o[0], event.y))
         self.boxes.append(BoundingBox(w0, h0, w1, h1))
         self.displayer.draw(self.boxes)
コード例 #5
0
def parse(filen, crawl=False):
    file = open(filen, "r")
    ret = DataSet()
    for line in file:
        line = line.strip().rstrip()
        splited = line.split()
        filename = splited[0]
        # filename = filename[filename.rfind("/")+1:]
        # filename = filename[:filename.rfind(".")]
        height = int(splited[1])
        width = int(splited[2])
        class_id = int(splited[3])
        (confidence, x, y, x2, y2) = tuple([float(a) for a in splited[4:]])
        #if confidence > parse_confidence_min: #TODO
        if hratio != None:
            height = y2 - y
            height2 = height * hratio
            y += (height - height2) / 2.0
            y2 = y + height2
        if wratio != None:
            width = x2 - x
            width2 = width * wratio
            x += (width - width2) / 2.0
            x2 = x + width2
        if whratio != None:
            height = y2 - y
            width = x2 - x
            width2 = height * whratio
            x += (width - width2) / 2.0
            x2 = x + width2
        bb = BoundingBox(x, y, x2, y2)
        area = bb.area()
        if (min_area == None or area >= min_area) and \
                (max_area == None or area <= max_area):
            ret.add_obj(filename, bb)
    file.close()
    # print summary
    print 'Dataset ' + str(filen) + ' has ' + str(len(ret)) + ' images and ' \
          + str(ret.get_nobjs()) + ' positive objects.'
    return ret
コード例 #6
0
ファイル: eblearn.py プロジェクト: fireae/visiongrader
def parse(filen, crawl = False):
    file = open(filen, "r")
    ret = DataSet()
    for line in file:
        line = line.strip().rstrip()
        splited = line.split()
        filename = splited[0]
        # filename = filename[filename.rfind("/")+1:]
        # filename = filename[:filename.rfind(".")]
        height = int(splited[1])
        width = int(splited[2])
        class_id = int(splited[3])
        (confidence, x, y, x2, y2) = tuple([float(a) for a in splited[4:]])
        #if confidence > parse_confidence_min: #TODO
        if hratio != None:
            height = y2 - y
            height2 = height * hratio
            y += (height - height2) / 2.0
            y2 = y + height2
        if wratio != None:
            width = x2 - x
            width2 = width * wratio
            x += (width - width2) / 2.0
            x2 = x + width2
        if whratio != None:
            height = y2 - y
            width = x2 - x
            width2 = height * whratio
            x += (width - width2) / 2.0
            x2 = x + width2
        bb = BoundingBox(x, y, x2, y2)
        area = bb.area()
        if (min_area == None or area >= min_area) and \
                (max_area == None or area <= max_area):
            ret.add_obj(filename, bb)
    file.close()
    # print summary
    print 'Dataset ' + str(filen) + ' has ' + str(len(ret)) + ' images and ' \
          + str(ret.get_nobjs()) + ' positive objects.'
    return ret
コード例 #7
0
def paste_foreground_into_background(foreground_img, background_img, x_factor=0.5, y_factor=0.5, bounding_box=True):
    """
        Paste a foreground/object image onto a background image, translated by the given x and y factors. If parameter
        'bounding_box' is true, then also the bounding box is returned.

        :param foreground_img: foreground/object image
        :param background_img: background image
        :param x_factor: value for x translation
        :param y_factor: value for y translation
        :param bounding_box: boolean, whether bounding box should be returned
        :return: merged_img, (bounding_box): merged image and optional the bounding box of the foreground object
    """
    # if foreground image is bigger than background image, then adjust foreground's resolution to background's
    if foreground_img.shape[0] > background_img.shape[0]:
        foreground_img = crop_image(foreground_img, new_height=background_img.shape[0])
    if foreground_img.shape[1] > background_img.shape[1]:
        foreground_img = crop_image(foreground_img, new_width=background_img.shape[1])

    merged_img = background_img.copy()
    fg_img_height, fg_img_width = foreground_img.shape[:2]
    bg_img_height, bg_img_width = background_img.shape[:2]

    x_offset = int((bg_img_width - fg_img_width) * x_factor)
    y_offset = int((bg_img_height - fg_img_height) * y_factor)

    # calc corners of foreground image
    y1, y2 = y_offset, y_offset + foreground_img.shape[0]
    x1, x2 = x_offset, x_offset + foreground_img.shape[1]

    # calc alpha value (opacity)
    alpha_s = foreground_img[:, :, 3] / 255.0
    alpha_l = 1.0 - alpha_s

    # do actual merge of fore- in background image
    for c in range(0, 3):
        merged_img[y1:y2, x1:x2, c] = (alpha_s * foreground_img[:, :, c] +
                                    alpha_l * background_img[y1:y2, x1:x2, c])

    logging.info("   => merge object and background")

    if bounding_box:
        return merged_img, BoundingBox(x_min=x1, x_max=x2, y_min=y1, y_max=y2)
    else:
        return merged_img
コード例 #8
0
ファイル: caltech.py プロジェクト: fireae/visiongrader
def parse_file_multi(file, filename, dataset):
    for line in file:
        (x, y, w, h, score) = \
            tuple([float(a) for a in line.strip().rstrip().split(",")])
        if filename not in corresp:
            print 'warning: image ' + filename + ' not found in groundtruth'
        else:
            if hratio != None:
                h2 = h * hratio
                y += (h - h2) / 2.0
                h = h2
            if wratio != None:
                w2 = w * wratio
                x += (w - w2) / 2.0
                w = w2
            if whratio != None:
                w2 = h * whratio
                x += (w - w2) / 2.0
                w = w2
            dataset.add_obj(score, corresp[filename], \
                                BoundingBox(x, y, x+w, y+h, score))