예제 #1
0
def image_proposal(img_path):
    img = skimage.io.imread(img_path)
    img_lbl, regions = selectivesearch.selective_search(
                       img, scale=500, sigma=0.9, min_size=10)
    candidates = set()
    images = []
    vertices = []
    for r in regions:
	# excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
	if r['size'] < 220:
            continue
	# resize to 224 * 224 for input
        proposal_img, proposal_vertice = prep.clip_pic(img, r['rect'])
        # Delete Empty array
	if len(proposal_img) == 0:
	    continue
        # Ignore things contain 0 or not C contiguous array
	x, y, w, h = r['rect']
	if w == 0 or h == 0:
	    continue
        # Check if any 0-dimension exist
	[a, b, c] = np.shape(proposal_img)
	if a == 0 or b == 0 or c == 0:
	    continue
	im = Image.fromarray(proposal_img)
	resized_proposal_img = resize_image(im, 224, 224)
	candidates.add(r['rect'])
	img_float = pil_to_nparray(resized_proposal_img)
        images.append(img_float)
        vertices.append(r['rect'])
    return images, vertices
예제 #2
0
def image_proposal(img_path):
    '''
    Enter the picture to be candidate box extraction
    The candidate box is extracted by using the characteristics of each pixel of the picture, 
    because the number of candidate boxes is too many and the size of candidate boxes required for different problem backgrounds is not the same.
    Therefore, it is necessary to go through a series of rules to further reduce the number of feature frames.
    '''
    img = cv2.imread(img_path)
    img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10)
    candidates = set()
    images = []
    vertices = []
    for r in regions:
        if r['rect'] in candidates:
            continue
        if r['size'] < 220:
            continue
        if (r['rect'][2] * r['rect'][3]) < 500:
            continue
        proposal_img, proposal_vertice = clip_pic(img, r['rect'])
        if len(proposal_img) == 0:
            continue
        x, y, w, h = r['rect']
        if w == 0 or h == 0:
            continue
        [a, b, c] = np.shape(proposal_img)
        if a == 0 or b == 0 or c == 0:
            continue
        resized_proposal_img = resize_image(proposal_img, cfg.Image_size, cfg.Image_size)
        candidates.add(r['rect'])
        img_float = np.asarray(resized_proposal_img, dtype="float32")
        images.append(img_float)
        vertices.append(r['rect'])
    return images, vertices
예제 #3
0
def main():

    # loading astronaut image
    # img = skimage.data.coffee()
    img = np.asarray(Image.open('berlin2.jpg'))

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=700,
                                                        sigma=0.8,
                                                        min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 800:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        # if w / h > 1.2 or h / w > 1.2:
        #     continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    f = open(time.ctime(), 'wr')
    pickle.dump(candidates, f)
    f.close()
예제 #4
0
def predict(img, pngFilePath=None):

    # loading lena image
    # img = skimage.data.chelsea() # => numpy.ndarray
    # print img[0][0]

    # 書きの方法はダメ!ネガポジ画像になってしまう。
    # img = Image.open("images/fruit.jpg")
    # img.load()
    # img = np.asarray( img, dtype="int32" )

    # predictでは、data/bbox_image/n07747607_631_box2.JPEGは正解できた。
    # ということは、n07747607_631.jpg でselective search すると正解できるかも?
    # tgt_img_path = "data/image/n07747607_631.jpg"

    if pngFilePath is not None:
        im = Image.open(pngFilePath)
        bg = Image.new("RGB", im.size, (255,255,255))
        bg.paste(im,im)
        bg.save(pngFilePath + ".jpg")

        img = io.imread(pngFilePath + ".jpg")
        print "get img from file"

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    print candidates

    # img = Image.open(tgt_img_path)
    # imgs = [resize(img.crop(rect)) for rect in candidates]
    res = []
    img = Image.open(pngFilePath + ".jpg")
    for rect in candidates:
        print rect
        cropped_img = resize(img.crop(rect))
        if cropped_img is None:
            continue
        print "cropped!"
        (answer, prob) = pd.predict_by_data(cropped_img)
        print "get answer and prob!"
        if prob > 0.9:
            res.append((rect,answer))

    print "send back answers!"
    return res
예제 #5
0
def img_proposal(img_path):
    """ 将给定的图片转换成 (框图片, 所在位置)的列表 """
    # img = skimage.io.imread(img_path)
    img = RCNN.load_img(img_path)
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=10)

    candidates = set()
    images = list()
    vertices = list()

    for r in regions:
        if r['rect'][2] == 0 or r['rect'][3] == 0:
            continue
        if r['size'] < 220:
            continue
        if r['rect'] in candidates:
            continue
        candidates.add(r['rect'])

        proposal_img, proposal_vertice = clip_img(img, r['rect'])
        resized_proposal_img = RCNN.resize_img(proposal_img, (224, 224))
        images.append(resized_proposal_img)
        vertices.append(proposal_vertice)

    return images, vertices
예제 #6
0
def predict_img(img, model, place_list, input_shape):
    # 画像から特徴部分を抽出
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=10)
    # 分類器を使用して判別
    candidates = set()
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for r in regions:
        # 特徴量抽出
        if r['rect'] in candidates:
            continue
        # ある一定pixelより小さいものについては除外する
        if r['size'] < 300:
            continue
        # ある一定pixelより大きいものについては除外する
        if r['size'] > 750:
            continue
        # 特徴量部分の座標を取得
        x, y, w, h = r['rect']
        if 2 * h > w:
            continue
        if h < 5:
            continue
        #print("hの高さ%s" % h)
        img_part = img[y:y + h, x:x + w]
        ax, place_name_list = local_classification(ax, img_part, input_shape,
                                                   x, y, w, h)
    ax.imshow(img)
    return place_name_list
예제 #7
0
def predict_selective_search(img, n_rect=7, n_rect_search=200):
    rects = selectivesearch.selective_search(img,
                                             scale=500,
                                             sigma=0.9,
                                             min_size=10)[1][:n_rect_search]
    centers = []
    candidates = set()
    for r in rects:
        if r['rect'] in candidates:
            continue
        if r['size'] < 1200:
            continue
        x, y, w, h = r['rect']
        candidates.add(r['rect'])
    candidates = list(candidates)[:n_rect]
    for box in candidates:
        x, y, w, h = box
        centers += [[x, y, x + w, y + h]]
    if len(centers) == 0:  # if no object detected, center crop
        tx1 = (img.shape[1] - cfg.crop_size) / 2
        ty1 = (img.shape[0] - cfg.crop_size) / 2
        tx2 = (img.shape[1] + cfg.crop_size) / 2
        ty2 = (img.shape[0] + cfg.crop_size) / 2
        centers = np.array([[tx1, ty1, tx2, ty2]], dtype=np.float32)
    return np.asarray(centers, dtype=np.float32), np.zeros(len(centers),
                                                           dtype=np.float32)
예제 #8
0
def get_regions(img_path):
    # loading  image
    img = io.imread(img_path)

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=10)

    print(len(img_lbl))
    print(np.shape(img))
    candidates = set()

    for r in regions:
        # excluding same rectangle (withsl different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 100:
            continue
        candidates.add(r['rect'])

    # draw rectangles on the original image

    for i, R in enumerate(candidates):
        x, y, w, h = R
        nor_img = transform.resize(img[y:y + h, x:x + w, :], (224, 224))
        io.imsave("./region/" + str(i) + '.jpg', nor_img)
예제 #9
0
def visualize(img):
    # loading astronaut image
    # img = skimage.data.astronaut()

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=500, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 220:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        # if w / h > 1.2 or h / w > 1.2:
        #    continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for i, r in enumerate(candidates):
        x, y, w, h = r

        print(i, x, y, w, h)
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)

    plt.show()
예제 #10
0
def main():

    # loading astronaut image
    #img = skimage.data.astronaut()

    # loading your own image
    img = io.imread('./sheep.jpg')

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=100,
                                                        sigma=0.8,
                                                        min_size=10)
    print(regions[0])
    """
    parameter explanation
    sigma:
        In general we use a Gaussian filter to smooth the image slightly before 
        computing the edge weights, in order to compensate for digitization artifacts.
        We always use a Gaussian with σ = 0.8, which does not produce any 
        visible change to the image but helps remove artifacts.
        (The smaller the sigma, the finer the segmentation result)
    
    min_size:
        If the rect size is reached on min_size, the calculation is stopped.
        
    scale:
        a larger scale causes a preference for larger components
    """

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions lager than 1600 pixels
        if r['size'] > 1600 or r['size'] < 50:
            continue
        # distorted rects
        x, y, w, h = r['rect']

        if w / h > 1.2 or h / w > 1.2:
            continue

        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        #print(x, y, w, h)
        rect = mpatches.Rectangle((x, y),
                                  w,
                                  h,
                                  fill=False,
                                  edgecolor='red',
                                  linewidth=1)
        ax.add_patch(rect)

    plt.show()
예제 #11
0
def selectiveSearch(img, pos, template):
    """
  Args:
    Image: the orginal image. Used for selective search
    Pos: (x,y,w,h) of the bounding box
    Template: the template captured in the first frame
  Output:
    proposed regions
  """
    x, y, w, h = pos
    min_size = np.floor(w * h / 4).astype(int)
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=min_size)
    candidates = []
    rect_img = img.copy()
    for item in regions:
        rect = item['rect']
        pt1 = rect[0], rect[1]
        pt2 = rect[0] + rect[2], rect[1] + rect[3]
        cv2.rectangle(rect_img, pt1, pt2, (0, 255, 0), 2)
        if rect[2] > 1.75 * h or rect[3] > 1.75 * w or rect[
                2] < 0.25 * h or rect[3] < 0.25 * w:
            continue
        if np.abs(rect[0] -
                  y) > img.shape[1] / 1.5 or np.abs(rect[1] -
                                                    x) > img.shape[0] / 1.5:
            continue
        candidates.append(rect)
    #cv2.imshow("test", rect_img)
    #cv2.waitKey(0)
    best_rect = findPatch(img, candidates, template)
    return best_rect
예제 #12
0
파일: RCNN.py 프로젝트: JungHY/RCNN
    def TestRCNN(self, input_image):

        predicted_classes = []
        predicted_bboxes = []

        if type(input_image) == type(''):
            input_image = skimage.io.imread(input_image)

        _, sc_regions = selectivesearch.selective_search(input_image)
        proposal_regions = []
        for region in sc_regions:
            proposal_regions.append([
                region['rect'][0], region['rect'][1],
                region['rect'][2] - region['rect'][0],
                region['rect'][3] - region['rect'][1]
            ])

        warped_region = []
        for region in proposal_regions:
            warped_region.append(self.RegionWarp(input_image, region))

        for region in warped_region:
            bbox, class_ = self.TOTAL_model.predict(region)
            predicted_bboxes.append(bbox)
            predicted_classes.append(class_)

        predict_results = []
        for idx in range(len(predicted_bboxes)):
            predict_results.append([
                predicted_bboxes[idx][0], predicted_bboxes[idx][1],
                predicted_bboxes[idx][2], predicted_bboxes[idx][3],
                predicted_classes[idx]
            ])

        return predict_results
def get_object_proposals(img, scale=500, sigma=0.9, min_size=10):
    # Selective search
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=scale,
                                                        sigma=sigma,
                                                        min_size=min_size)
    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 500 pixels
        x, y, w, h = r['rect']
        if r['size'] < 500 or w > 0.95 * img.shape[1] or h > 0.95 * img.shape[
                0]:
            continue
        # excluding the zero-width or zero-height box
        if r['rect'][2] == 0 or r['rect'][3] == 0:
            continue
        # distorted rects
        if w / h > 5 or h / w > 5:
            continue
        candidates.add(r['rect'])

    return candidates
def customized_selective_search(img):
    assert img.shape[-1] == 3, "imgs.shape should be [B, H, W, 3]"
    ss_boxes = []
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 500 pixels
        if r['size'] < 50:
            continue
        x, y, w, h = r['rect']
        if w < 5 or h < 5 or w / h > 2.0 or h / w > 2.0:
            continue
        candidates.add(r['rect'])

    data = [list(a) for a in candidates]
    data = np.array(data)
    if data.shape[0] == 0:
        print("this image has no proposals")
    else:
        data[:, 2:] = data[:, :2] + data[:, 2:]
    ss_boxes.append(list(data))
    return ss_boxes
예제 #15
0
def load_regions(image_index, number):
    image_name = image_index + ".jpg"
    xml_name = image_index + ".xml"
    image_dir = os.path.join(IMAGE_DIR, image_name)
    xml_dir = os.path.join(ANNOTATION_DIR, xml_name)
    # get regions proposals with selective search
    image = skimage.io.imread(image_dir)
    _, regions = selectivesearch.selective_search(image)
    # get ground truth
    dom_tree = xml.dom.minidom.parse(xml_dir)
    annotation = dom_tree.documentElement
    object_list = annotation.getElementsByTagName('object')
    for object in object_list:
        dom_name = object.getElementsByTagName('name')
        object_name = dom_name[0].childNodes[0].data
        xmin = int(object.getElementsByTagName('xmin')[0].childNodes[0].data)
        ymin = int(object.getElementsByTagName('ymin')[0].childNodes[0].data)
        xmax = int(object.getElementsByTagName('xmax')[0].childNodes[0].data)
        ymax = int(object.getElementsByTagName('ymax')[0].childNodes[0].data)
        ground_truth = [xmin, ymin, xmax, ymax]
        region_proposal = []
        background = []
        for region in regions:
            roi = region['rect']
            iou = utils.get_IoU(ground_truth, roi)
            if iou > 0.5 and len(region_proposal) < number * 0.25:
                label = int(classes_num[classes == object_name])
                region_proposal.append([roi, [label, ground_truth]])
            elif iou > 0.1 and len(background) < number * 0.75:
                background.append([roi, [20, -1]])
            if len(region_proposal) is 16 and len(background) is 48:
                break
    rois = region_proposal + background
    return image, rois
예제 #16
0
def pp_nd_ss(image_dir):
    ss_arr = []

    img = skimage.io.imread(image_dir)
    img = Image.fromarray(img).resize((640, 480))
    img = np.array(img)

    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0,
                                                        min_size=500)

    candidates = []

    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if h is 0 or w is 0:
            continue
        if w / h > 2 or h / w > 2:
            continue
        candidates.append(r['rect'])

        image = Image.fromarray(img).crop((x, y, x + w, h + y)).resize(
            (64, 64))
        ss_arr.append(np.array(image))

    ss_arr = np.array(ss_arr) / 255
    return ss_arr, candidates
예제 #17
0
    def generate_labels(self):
        with codecs.open(self.all_list, 'r', 'utf-8') as f:
            lines = f.readlines()
            for num, image_idx in enumerate(lines):
                ground_truth_dic = self.load_annotation(image_idx)
                image_path = os.path.join(self.images_path,
                                          image_idx.strip() + '.jpg')
                img = cv2.imread(image_path)
                img_lbl, regions = selectivesearch.selective_search(
                    img, scale=1000, sigma=0.9, min_size=1000)
                labels = []
                for r in regions:
                    x, y, w, h = r['rect']
                    proposal_vertice = [x + 1, y, x + w, y + h, w, h]
                    proposal_bbox = [x, y, (x + w - 1), (y + h - 1)]
                    label = np.zeros(self.class_num * 5 - 4, dtype=np.float32
                                     )  # 假设包括背景有5类,0:5是判断类别,5:5+4*4=21 是位置框信息
                    iou_val = 0
                    for ground_truth, class_idx in ground_truth_dic.items():
                        #ground_truth = list(ground_truth)
                        xmin = (2 * ground_truth[0] - ground_truth[2]) / 2.0
                        ymin = (2 * ground_truth[1] - ground_truth[3]) / 2.0
                        ground_truth = [
                            xmin, ymin, ground_truth[2], ground_truth[3]
                        ]
                        iou_val = IOU(ground_truth, proposal_bbox)
                        px = float(proposal_vertice[0]) + float(
                            proposal_vertice[4] / 2.0)  # 中心点X
                        py = float(proposal_vertice[1]) + float(
                            proposal_vertice[5] / 2.0)  # 中心点Y
                        pw = float(proposal_vertice[4])  # w
                        ph = float(proposal_vertice[5])  # h

                        gx = float(ground_truth[0])  # 中心点X
                        gy = float(ground_truth[1])  # 中心点Y
                        gw = float(ground_truth[2])  # W
                        gh = float(ground_truth[3])  # H

                        if iou_val < self.roi_threshold:
                            label[0] = 1
                        elif iou_val > self.roi_threshold:
                            label[0] = 0
                            label[class_idx] = 1
                            label[self.class_num + (class_idx-1)*4 : self.class_num + (class_idx-1)*4 + 4] = \
                                [((gx - px) / pw), ((gy - py) / ph), (np.log(gw / pw)), (np.log(gh / ph))]
                            break
                    for i in range(len(proposal_bbox)):
                        proposal_bbox[i] = (proposal_bbox[i] / 16.0)
                    proposal_bbox.insert(0, 0)
                    proposal_bbox.insert(0, iou_val)
                    proposal_bbox.extend(label)
                    labels.append(proposal_bbox)
                view_bar("Process image of %s" % image_path, num + 1,
                         len(lines))
                if self.is_save:
                    if not os.path.exists(self.processed_path):
                        os.makedirs(self.processed_path)
                    np.save((os.path.join(self.processed_path,
                                          image_idx.split('.')[0].strip()) +
                             '_data.npy'), labels)
예제 #18
0
def extract_regions(image, scale=500, min_pixels=1000):
    '''
    img: np.narray in tf format
    return: list [[x, y, w, h], ...,]
    '''
    img = image.copy()
    img = img.astype('uint8')  # for fast
    img = np.rollaxis(img, 0, 3)  # change to dim_ordering to tf

    # get roi
    img_lbl, region = sls.selective_search(img,
                                           scale=scale,
                                           sigma=0.8,
                                           min_size=50)

    candidates = set()
    for r in region:
        # excluding same rectangle (with different segments)
        if r['rect'] in set():
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < min_pixels:
            continue
        candidates.add(r['rect'])

    return list(candidates)
예제 #19
0
def _get_ssinfo_(image):
    # 单通道 to 三通道
    image = np.expand_dims(image, axis=2)
    image = np.concatenate((image, image, image), axis=-1)
    img_lbl, regions = selectivesearch.selective_search(image,
                                                        scale=100,
                                                        sigma=0.1,
                                                        min_size=10)
    # 计算一共分割了多少个原始候选区域
    temp = set()
    for i in range(img_lbl.shape[0]):
        for j in range(img_lbl.shape[1]):
            temp.add(img_lbl[i, j, 3])
    # 创建一个集合 元素不会重复,每一个元素都是一个list(左上角x,左上角y,宽,高),表示一个候选区域的边框
    candidates = set()  # 注意,set只能遍历
    for r in regions:
        x, y, w, h = r['rect']
        # 排除小于 2000 pixels的候选区域(并不是bounding box中的区域大小)
        if r['size'] > 120:
            continue
        # 排除扭曲的候选区域边框  即只保留近似正方形的
        x, y, w, h = r['rect']
        if w == 0 or h == 0:
            continue
        try:
            if w / h < 0.25 or h / w < 0.25:
                continue
        except ZeroDivisionError:
            print(1)
            continue
        candidates.add(r['rect'])
    return candidates
예제 #20
0
파일: example.py 프로젝트: kzktsan/MyTools
def main():

    # loading lena image
    #img = skimage.data.lena()
    img = skimage.data.imread("moto.png")

    # perform selective search 500 0.9 10
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=400, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        print x, y, w, h
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)

    plt.show()
예제 #21
0
def image_proposal(img_path):
    '''
    输入要进行候选框提取的图片
    利用图片的各像素点的特点进行候选框的提取,由于候选框数量太多且针对不同的问题背景所需要的候选框的尺寸是不一样的
    因此要经过一系列的规则加以限制来进一步减小特征框的数量
    '''
    img = cv2.imread(img_path)
    img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10)
    candidates = set()
    images = []
    vertices = []
    for r in regions:
        if r['rect'] in candidates:
            continue
        if r['size'] < 220:
            continue
        if (r['rect'][2] * r['rect'][3]) < 500:
            continue
        proposal_img, proposal_vertice = clip_pic(img, r['rect'])
        if len(proposal_img) == 0:
            continue
        x, y, w, h = r['rect']
        if w == 0 or h == 0:
            continue
        [a, b, c] = np.shape(proposal_img)
        if a == 0 or b == 0 or c == 0:
            continue
        resized_proposal_img = resize_image(proposal_img, cfg.Image_size, cfg.Image_size)
        candidates.add(r['rect'])
        img_float = np.asarray(resized_proposal_img, dtype="float32")
        images.append(img_float)
        vertices.append(r['rect'])
    return images, vertices
예제 #22
0
def _generate_region_proposals(img):
    """generate region proposals"""
    _, regions = selectivesearch.selective_search(img,
                                                  scale=500,
                                                  sigma=0.9,
                                                  min_size=100)
    return regions
def preprocess_data(file_path, num_classes, save_path="./select_img_data",
                    svm=False, save=False, img_size=224, threshold=0.5):
    with open(file_path, "r") as f:
        lines = f.readlines()
    i = 0
    for line in lines:
        labels = []
        img_datas = []
        temp = line.strip().split(" ")
        img_path = temp[0]
        img_label = int(temp[1])
        img_rect = [int(i) for i in temp[2].split(",")]
        img_data = cv2.imread(img_path)
        # img_data_np = np.array(img_data, dtype="float32")
        select_img_datas, select_img_rects = selectivesearch.selective_search(
            img_data, scale=500, sigma=0.9, min_size=10)
        rects = set()
        for r in select_img_rects:
            if r["rect"] in rects:
                continue
            if r['size'] < 220:
                continue
            if (r['rect'][2] * r['rect'][3]) < 500:
                continue
            x, y, w, h = r["rect"]
            if w == 0 or h == 0:
                continue
            select_img_data = img_cutting(img_data, r["rect"])
            if len(select_img_data) == 0:
                continue
            [a, b, c] = np.shape(select_img_data)
            if a == 0 or b == 0 or c == 0:
                continue
            rects.add(r["rect"])
            try:
                select_img_data_resize = cv2.resize(select_img_data, (img_size, img_size), cv2.INTER_CUBIC)
            except:
                print(select_img_data.shape)
            select_img_data_resize_np = np.array(select_img_data_resize, dtype="float32")
            img_datas.append(select_img_data_resize_np)
            label = np.zeros(num_classes)
            iou_val = iou(r["rect"], img_rect)
            if svm:
                if iou_val < threshold:
                    labels.append(0)
                else:
                    labels.append(img_label)
            else:
                if iou_val < threshold:
                    label[0] = 1
                else:
                    label[img_label] = 1
                labels.append(label)
        print("\rreading file: %d" % i, end='', flush=True)
        i += 1
        if save:
            if not os.path.exists(save_path):
                os.mkdir(save_path)
            with open((os.path.join(save_path, img_path.split('/')[-1].split('.')[0].strip()) + '.pkl'), "wb") as f:
                pkl.dump((img_datas, labels), f)
예제 #24
0
def regionProposal(image_filename, size, scale=250, sigma=1):
    true_image = skimage.io.imread(image_filename)
    if true_image is None:
        print("CANNOT FIND IMAGE TO CONDUCT DETECTION")
        return None

    img = cv2.resize(
        true_image,
        (300,
         int(300.0 * float(true_image.shape[0]) / float(true_image.shape[1]))),
        interpolation=cv2.INTER_AREA)

    img_size = true_image.shape
    boundingBoxInfo = []
    images = []
    aspect_ratio = float(img_size[1]) / 300.0

    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=scale,
                                                        sigma=sigma)

    candidates = set()

    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # # # excluding regions smaller than 400 pixels
        if r['size'] < 400:
            continue
        # # distorted rects
        x, y, w, h = r['rect']
        if (w > 280 or h > 280) or (w < 15 or h < 15):
            continue

        candidates.add(r['rect'])

    for x, y, w, h in candidates:

        window = img[y:y + h, x:x + w]

        window = cv2.resize(window, (size, size), interpolation=cv2.INTER_AREA)

        images.append(window)

        new_x = int(x * aspect_ratio)
        new_y = int(y * aspect_ratio)
        new_w = int(w * aspect_ratio)
        new_h = int(h * aspect_ratio)

        boundingBoxInfo.append([new_x, new_y, new_w, new_h])

        cv2.rectangle(true_image, (new_x, new_y),
                      (new_x + new_w, new_y + new_h), (0, 0, 255), 1)

    # cv2.imshow("Window", true_image)
    # cv2.waitKey(1000)
    # time.sleep(10)

    return [true_image, np.array(images), boundingBoxInfo]
def get_component_position(img, scale, sigma, min_size, is_using_thumb=True):
    thumb_img = img
    scale_index = 1
    if is_using_thumb:
        pixels = 300 * 400
        original_w = img.shape[0]
        original_h = img.shape[1]
        original_pixels = original_w * original_h
        scale_index = math.sqrt(original_pixels / pixels)
        new_w = int(original_w / scale_index)
        new_h = int(original_h / scale_index)
        thumb_img = transform.resize(img, (new_w, new_h))
    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        thumb_img, scale=scale, sigma=sigma, min_size=min_size)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 200:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if h == 0 or w == 0:
            continue
        # if w / h > 4 or h / w > 4:
        #     continue
        candidates.add(r['rect'])
    if is_using_thumb:
        candidates = {tuple(int(loc * scale_index) for loc in candidate) for candidate in candidates}
    # print(candidates)
    return candidates
예제 #26
0
def main():
    img = io.imread('cat.jpg')

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=500, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 1000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)

    plt.show()
def selective_search(img_path):
    # https://github.com/AlpacaDB/selectivesearch
    img = cv2.imread(img_path)
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=10)
    # 创建一个集合 元素不会重复,每一个元素都是一个list(左上角x,左上角y,宽,高),表示一个候选区域的边框
    candidates = set()
    for r in regions:
        # 排除重复的候选区
        if r['rect'] in candidates:
            continue
        # 排除小于 2000 pixels的候选区域(并不是bounding box中的区域大小)
        if r['size'] < 2000:
            continue
        # 排除扭曲的候选区域边框  即只保留近似正方形的
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])
    img_regions = []
    for candidate in candidates:
        x, y, w, h = candidate
        img_regions.append(img[x:x + w][y:y + h])
    return img_regions
예제 #28
0
def cropROI(filename):
    global width, height
    image = skimage.io.imread(filename)

    width = len(image[0])
    height = len(image)
    # area = width * height

    # if (area < 256*256 or area > 512*512) and ((height > 512 and width > 512) or (height < 256 and width <256)):
    #     height = int(512 * height/width)
    #     width = 512

    num = 1
    for sc in [350, 450, 500]:
        for sig in [0.8]:
            for mins in [30, 60, 120]:
                img = skimage.io.imread(filename)[:, :, :3]
                # if height == len(img) and width == len(img[0]):
                #     pass
                # else:
                #     img = skimage.transform.resize(img, (height, width))
                # perform selective search
                img_lbl, regions = selectivesearch.selective_search(
                    img, scale=sc, sigma=sig, min_size=mins)

                for r in regions:
                    # excluding same rectangle (with different segments)
                    if r['rect'] in candidates:
                        continue
                    # excluding regions smaller than 2000 pixels
                    if r['size'] < 2000:
                        continue
                    # distorted rects
                    x, y, w, h = r['rect']
                    if w / h > 1.2 or h / w > 1.2:
                        continue
                    if w >= (img.shape[0] - 1) * (0.9) and h >= (img.shape[1] -
                                                                 1) * (0.9):
                        continue
                    candidates.add(r['rect'])
        print("Stage " + str(num) + " Done.")
        num += 1

    merge()
    draw_superbox()

    img = skimage.io.imread(filename)
    # if height == len(img) and width == len(img[0]):
    #     pass
    # else:
    #     img = skimage.transform.resize(img, (height, width))
    i = 1
    for x, y, w, h in refined:
        if str(sys.version)[:3] == "3.4":
            skimage.io.imsave(cmd_folder + "Temp/" + str(i) + ".jpg",
                              img[y:y + h, x:x + w])  # crop
        else:
            skimage.io.imsave(cmd_folder + "Temp/" + str(i) + ".jpg",
                              img[y:y + h, x:x + w])  # crop
        i += 1
예제 #29
0
def image_proposal(img_path):
    img = skimage.io.imread(img_path)
    img_lbl, regions = selectivesearch.selective_search(img,
                                                        scale=500,
                                                        sigma=0.9,
                                                        min_size=10)
    candidates = set()
    images = []
    vertices = []
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        if r['size'] < 220:
            continue
# resize to 224 * 224 for input
        proposal_img, proposal_vertice = prep.clip_pic(img, r['rect'])
        # Delete Empty array
        if len(proposal_img) == 0:
            continue
        # Ignore things contain 0 or not C contiguous array
        x, y, w, h = r['rect']
        if w == 0 or h == 0:
            continue
        # Check if any 0-dimension exist
        [a, b, c] = np.shape(proposal_img)
        if a == 0 or b == 0 or c == 0:
            continue
        im = Image.fromarray(proposal_img)
        resized_proposal_img = resize_image(im, 224, 224)
        candidates.add(r['rect'])
        img_float = pil_to_nparray(resized_proposal_img)
        images.append(img_float)
        vertices.append(r['rect'])
    return images, vertices
예제 #30
0
def my_selective_search(img):
    img_array = np.array(img)
    img_size = img_array.size / 3  #the number of img pixel
    min_region_size = img_size / 100.0
    min_component_size = int(math.sqrt(img_size) / 10)

    #img_array = data.astronaut()
    img_lbl, regions = select.selective_search(img_array,
                                               scale=300,
                                               sigma=0.9,
                                               min_size=min_component_size)
    #draw = ImageDraw.Draprint get_IOU([50, 50, 100 ,0], [0,0,100,100])w(img)
    #print regions.__len__()
    candidate = set()
    n = 0
    for i in regions:
        if i['rect'] in candidate:
            continue
        if i['size'] < min_region_size:
            continue
        x, y, w, h = i['rect']
        if w / h > 2 or h / w > 2:
            continue
        rect = (x, y, x + w, y + h)
        candidate.add(rect)
    return candidate
예제 #31
0
def getSelectiveSearchRois(img, ssScale, ssSigma, ssMinSize, maxDim):
    # Selective Search
    #     Parameters
    #     ----------
    #         im_orig : ndarray
    #             Input image
    #         scale : int
    #             Free parameter. Higher means larger clusters in felzenszwalb segmentation.
    #         sigma : float
    #             Width of Gaussian kernel for felzenszwalb segmentation.
    #         min_size : int
    #             Minimum component size for felzenszwalb segmentation.
    #     Returns
    #     -------
    #         img : ndarray
    #             image with region label
    #             region label is stored in the 4th value of each pixel [r,g,b,(region)]
    #         regions : array of dict
    #             [
    #                 {
    #                     'rect': (left, top, right, bottom),
    #                     'labels': [...]
    #                 },
    #                 ...
    #             ]
    # inter_area seems to give much better results esp when upscaling image
    img, scale = imresizeMaxDim(img, maxDim, boUpscale=True, interpolation = cv2.INTER_AREA)
    _, ssRois = selectivesearch.selective_search(img, scale=ssScale, sigma=ssSigma, min_size=ssMinSize)
    rects = []
    for ssRoi in ssRois:
        x, y, w, h = ssRoi['rect']
        rects.append([x,y,x+w,y+h])
    return rects, img, scale
예제 #32
0
def find_rois_selective_search(image_pixels, scale=200, sigma=0.9, min_size=10):
    """
    Uses the selective search library to find rois

    :param
        image_path: path to an image
        image_pixels: pixels from the image
    """
    # Higher scale means higher preference for larger components (k / |C|, where |C| is the
    # number of pixels in the component and k is the scale; for a large k, it would be difficult
    # for a small component to be have a separation boundary with the neighboring components since
    # the division is large). Smaller components are allowed when there is a sufficiently large
    # difference between neighboring components (the higher k / |C|, the higher the difference
    # between neighboring components has to be)
    img_lbl, regions = \
        selectivesearch.selective_search(image_pixels, scale=scale, sigma=sigma, min_size=min_size)

    unique_rois = {}

    # Deduplicating rois
    for region in regions:
        # rect format: [x, y, w, h]
        rect = region["rect"]
        key = str(rect[0]) + str(rect[1]) + str(rect[2]) + str(rect[3])
        if key not in unique_rois:
            # From [x, y, w, h] to {x, y, w, h}
            unique_rois[key] = rect

    return np.array(unique_rois.values())
예제 #33
0
    def find_potential_regions(self, img, img_path):
        # A crucial part of the program. It performs the selective search rather than a sliding window
        # search. All areas that are found are stored in the regions structure.
        img_lbl, regions = selectivesearch.selective_search(img,
                                                            scale=500,
                                                            sigma=0.5,
                                                            min_size=20)

        # Used for filtering only.
        candidates = []

        # Keep any region of proper size
        # Consider to remove detections for the full picture
        coordinates = set()
        for r in regions:
            if (r['rect'] in coordinates):
                continue
            if (r['size'] < 2000):
                continue

            x, y, w, h = r['rect']
            if (w / h > 1.2 or h / w > 1.2):
                continue
            print("Added somehting: ", x, " ", y, " ", x + w, " ", y + h)
            coordinates.add(r['rect'])
            candidates.append((img, np.array([[x, y, x + w, y + h]])))

        # Last, evaluate if they are vehicles or somehting else
        detected_vehicles = self.evaluate_detections(img, candidates)

        # Then return the found vehicles
        return detected_vehicles
예제 #34
0
def getSelectiveSearchRois(img, ssScale, ssSigma, ssMinSize, maxDim):
    # Selective Search
    #     Parameters
    #     ----------
    #         im_orig : ndarray
    #             Input image
    #         scale : int
    #             Free parameter. Higher means larger clusters in felzenszwalb segmentation.
    #         sigma : float
    #             Width of Gaussian kernel for felzenszwalb segmentation.
    #         min_size : int
    #             Minimum component size for felzenszwalb segmentation.
    #     Returns
    #     -------
    #         img : ndarray
    #             image with region label
    #             region label is stored in the 4th value of each pixel [r,g,b,(region)]
    #         regions : array of dict
    #             [
    #                 {
    #                     'rect': (left, top, right, bottom),
    #                     'labels': [...]
    #                 },
    #                 ...
    #             ]
    # inter_area seems to give much better results esp when upscaling image
    img, scale = imresizeMaxDim(img, maxDim, boUpscale=True, interpolation = cv2.INTER_AREA)
    _, ssRois = selectivesearch.selective_search(img, scale=ssScale, sigma=ssSigma, min_size=ssMinSize)
    rects = []
    for ssRoi in ssRois:
        x, y, w, h = ssRoi['rect']
        rects.append([x,y,x+w,y+h])
    return rects, img, scale
예제 #35
0
def image_proposal(img_path):
	img = skimage.io.imread(img_path)
	img_lbl, regions = selectivesearch.selective_search(img, scale=15, sigma=0.5, min_size=500)
	candidates = set()
	images = []
	vertices = []
	for r in regions:
		if r['rect'] in candidates:
			continue
		proposal_img, proposal_vertice = prep.clip_pic(img, r['rect'])
		if len(proposal_img) == 0:
			continue
		x, y, w, h = r['rect']
		if w == 0 or h == 0:
			continue
		[a, b, c] = np.shape(proposal_img)
		if a == 0 or b == 0 or c == 0:
			continue
		im = Image.fromarray(proposal_img)
		array_img = Image.fromarray(proposal_img) 
		resize_img = resize_image(array_img, 227, 227)
		img_float = pil_to_nparray(resize_img)
		images.append(img_float)
		vertices.append(r['rect'])
	return images, vertices
def main():

    # loading lena image
    # img = skimage.data.chelsea() # => numpy.ndarray
    # print img[0][0]

    # 書きの方法はダメ!ネガポジ画像になってしまう。
    # img = Image.open("images/fruit.jpg")
    # img.load()
    # img = np.asarray( img, dtype="int32" )

    # predictでは、data/bbox_image/n07747607_631_box2.JPEGは正解できた。
    # ということは、n07747607_631.jpg でselective search すると正解できるかも?
    tgt_img_path = "data/image/n07747607_631.jpg"
    img = io.imread(tgt_img_path)

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    img = Image.open(tgt_img_path)
    # imgs = [resize(img.crop(rect)) for rect in candidates]
    res = []
    for rect in candidates:
        print rect
        cropped_img = resize(img.crop(rect))
        (answer, prob) = predict.predict_by_data(cropped_img)
        if prob > 0.9:
            res.append((rect,answer))

    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)

    for v in res:
        x, y, w, h = v[0] # rect object
        print v[1] # answer
        print x, y, w, h
        rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)
    plt.show()
예제 #37
0
def main():

    # loading lena image
    #img = skimage.data.lena()
    imagefilename = 'TextImage.jpg'
    img = np.float32(PIL.Image.open(imagefilename))/256.0
    #showarray(img)


    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=500, sigma=0.1, min_size=2)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])


    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        print x, y, w, h
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)


    # create separate cropped images
    i = 0
    for x, y, w, h in candidates:
	with Image(filename=imagefilename) as img:
	     print x, y, w, h
	     i = i + 1
	     oldimg = img
	     print img.size
	     img.crop(x,y,width=w,height=h)
	     img.format = 'jpeg'
	     img.save(filename='frame_'+str(i)+'.jpg')
	     img = oldimg

    plt.show()
예제 #38
0
파일: Data.py 프로젝트: ausk/Fast-RCNN
    def generate_labels(self):
        with codecs.open(self.all_list, 'r', 'utf-8') as f:
            lines = f.readlines()
            for num, image_idx in enumerate(lines):
                ground_truth_dic = self.load_annotation(image_idx)
                image_path = os.path.join(self.images_path, image_idx.strip() + '.jpg')
                img = cv2.imread(image_path)
                img_lbl, regions = selectivesearch.selective_search(img, scale=1000, sigma=0.9, min_size=1000)
                labels = []
                for r in regions:
                    x, y, w, h = r['rect']
                    proposal_vertice = [x + 1, y, x +  w, y +  h, w, h]
                    proposal_bbox = [x, y, (x + w - 1), (y + h - 1)]
                    label = np.zeros(self.class_num * 5 - 4, dtype=np.float32)  # 假设包括背景有5类,0:5是判断类别,5:5+4*4=21 是位置框信息
                    iou_val=0
                    for ground_truth, class_idx in ground_truth_dic.items():
                        #ground_truth = list(ground_truth)
                        xmin=(2*ground_truth[0]-ground_truth[2])/2.0
                        ymin=(2*ground_truth[1]-ground_truth[3])/2.0
                        ground_truth=[xmin,ymin,ground_truth[2],ground_truth[3]]
                        iou_val = IOU(ground_truth, proposal_bbox)
                        px = float(proposal_vertice[0]) + float(proposal_vertice[4] / 2.0)  # 中心点X
                        py = float(proposal_vertice[1]) + float(proposal_vertice[5] / 2.0)  # 中心点Y
                        pw = float(proposal_vertice[4])  # w
                        ph = float(proposal_vertice[5])  # h

                        gx = float(ground_truth[0])  # 中心点X
                        gy = float(ground_truth[1])  # 中心点Y
                        gw = float(ground_truth[2])  # W
                        gh = float(ground_truth[3])  # H

                        if iou_val < self.roi_threshold  :
                            label[0] = 1
                        elif iou_val > self.roi_threshold:
                            label[0] = 0
                            label[class_idx] = 1
                            label[self.class_num + (class_idx-1)*4 : self.class_num + (class_idx-1)*4 + 4] = \
                                [((gx - px) / pw), ((gy - py) / ph), (np.log(gw / pw)), (np.log(gh / ph))]
                            break
                    for i in range(len(proposal_bbox)):
                        proposal_bbox[i] = (proposal_bbox[i] / 16.0)
                    proposal_bbox.insert(0, 0)
                    proposal_bbox.insert(0, iou_val)
                    proposal_bbox.extend(label)
                    labels.append(proposal_bbox)
                view_bar("Process image of %s" % image_path, num + 1, len(lines))
                if self.is_save:
                    if not os.path.exists(self.processed_path):  os.makedirs(self.processed_path)
                    np.save((os.path.join(self.processed_path, image_idx.split('.')[0].strip())
                             + '_data.npy'), labels)
def main():

    csv = open("bboxes.csv", "w")
    csv.write("filename,ymin,xmin,ymax,xmax\n")
    images = glob.iglob('test/*.png')

    for i in images:

        # loading lena image
        img = skimage.io.imread(i)
        # img = skimage.data.astronaut()

        # perform selective search
        img_lbl, regions = selectivesearch.selective_search(
            img, scale=500, sigma=0.9, min_size=10)

        candidates = set()
        for r in regions:
            # excluding same rectangle (with different segments)
            if r['rect'] in candidates:
                continue
            # excluding regions smaller than 20 pixels
            if r['size'] < 20:
                continue
            # distorted rects
            x, y, w, h = r['rect']

            if x is 0 or y is 0 or w is 0 or h is 0:
                continue
            if w / h > 1.5 or h / w > 1.5:
                continue
            candidates.add(r['rect'])

        # draw rectangles on the original image
        fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
        ax.imshow(img)
        for x, y, w, h in candidates:
            #print x, y, w, h
            csv.write('"nums/' + str(i) + '.png",' + str(y) + "," + str(x) + \
                      "," + str(y + h) + "," + str(x + w) + "\n")

            rect = mpatches.Rectangle(
                (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
            ax.add_patch(rect)

        plt.show()
        #plt.savefig("nums/" + str(i) + "boxes.png")
        plt.close()
예제 #40
0
def selective_window(img,scale=300,sigma=0.8,mins=100):
    #this is a time bottleneck
    im_lables,regions = selective_search(img,scale=scale,sigma=sigma,min_size=mins);
    true_regions = set([])
    print '# of regions: {}'.format(len(regions))

    for reg in regions:
        if reg['rect'] in true_regions:
            continue
        if reg['size'] < 100:
            continue
        (x,y,w,h) = reg['rect']
        if not (w*h > 0 and w/h < 4 and  h/w < 4):
            continue
        true_regions.add(reg['rect'])
    return true_regions
예제 #41
0
def selective(image_path):
    # loading lena image

    image_array = pre_process(image_path)

    # perform selective search
    # img = selectivesearch._generate_segments(
    #     image_array,
    #     scale=SELECTIVESEARCH_SCALE,
    #     sigma=SELECTIVESEARCH_SIGMA,
    #     min_size=SELECTIVESEARCH_MIN_SIZE
    # )
    # print('seg')
    # print(img)

    img_lbl, regions = selectivesearch.selective_search(
        image_array,
        scale=SELECTIVESEARCH_SCALE,
        sigma=SELECTIVESEARCH_SIGMA,
        min_size=SELECTIVESEARCH_MIN_SIZE
    )

    candidates = set()
    for r in regions:
        print('size')
        x, y, w, h = r['rect']
        print(r['size'])
        print(w*h)
        print(r['size'] == w*h)
        
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        # print('size')
        # print(r['size'])
        if r['size'] < 15*15:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 3 or h / w > 3:
            continue
        candidates.add(r['rect'])

    return post_process(candidates)
def main():

    # loading lena image
    img = skimage.data.chelsea() # => numpy.ndarray
    print img[0][0]

    # 書きの方法はダメ!ネガポジ画像になってしまう。
    # img = Image.open("images/fruit.jpg")
    # img.load()
    # img = np.asarray( img, dtype="int32" )

    # 参考:http://www.scipy-lectures.org/packages/scikit-image/
    img = io.imread('images/fruit.jpg')

    print img[0][0]

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
            img, scale=500, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        print x, y, w, h
        rect = mpatches.Rectangle(
                (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)

    plt.show()
예제 #43
0
파일: demo.py 프로젝트: nboyd/detection
def process(net, image_folder, classes):
    for image_path in sorted(glob.glob(image_folder+"/*.jpg")):
        print 'Processing frame {}'.format(image_path)
        im = cv2.imread(image_path)
        # Detect all object classes and regress object bounds
        timer = Timer()
        timer.tic()
        img_lbl, regions = selectivesearch.selective_search(im, scale=500, sigma=0.8, min_size=200)
        regions_array = np.zeros((len(regions),4))
        for idx in xrange(0,len(regions)):
            rect = regions[idx]['rect']
            regions_array[idx,:] = [rect[0],rect[1], rect[0]+rect[2], rect[1] + rect[3]]
        obj_proposals  = regions_array
        timer.toc()
        print ('Proposals took {:.3f}s for '
               '{:d} object proposals').format(timer.total_time, len(regions))
        timer = Timer()
        timer.tic()
        scores, boxes = im_detect(net, im, obj_proposals)
        timer.toc()
        print ('Detection took {:.3f}s for '
               '{:d} object proposals').format(timer.total_time, boxes.shape[0])

        # Visualize detections for each class
        CONF_THRESH = 0.6
        NMS_THRESH = 0.3
        for cls in classes:
            cls_ind = CLASSES.index(cls)
            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
            cls_scores = scores[:, cls_ind]
            keep = np.where(cls_scores >= CONF_THRESH)[0]
            cls_boxes = cls_boxes[keep, :]
            cls_scores = cls_scores[keep]
            dets = np.hstack((cls_boxes,
                              cls_scores[:, np.newaxis])).astype(np.float32)
            keep = nms(dets, NMS_THRESH)
            dets = dets[keep, :]
            print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls,
                                                                        CONF_THRESH)
            vis_detections(im, cls, dets, image_path, thresh=CONF_THRESH)
예제 #44
0
def main():

    # loading lena image
    #img = skimage.data.lena()

    #print "oh hi"

    img = np.float32(PIL.Image.open('sky1024px.jpg'))/256.0

    #print "sup brah?"

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=500, sigma=0.95, min_size=50)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        print x, y, w, h
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)

    plt.show()
예제 #45
0
파일: util.py 프로젝트: satojkovic/DeepLogo
def get_object_proposals(img, scale=500, sigma=0.9, min_size=10):
    # Selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=scale, sigma=sigma, min_size=min_size)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 500 pixels
        x, y, w, h = r['rect']
        if r['size'] < 2000 or w > 0.95 * img.shape[1] or h > 0.95 * img.shape[0]:
            continue
        # excluding the zero-width or zero-height box
        if r['rect'][2] == 0 or r['rect'][3] == 0:
            continue
        # distorted rects
        if w / h > 5 or h / w > 5:
            continue
        candidates.add(r['rect'])

    return candidates
예제 #46
0
def main():

    # loading lena image
    img = skimage.data.lena()

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=500, sigma=0.9, min_size=10)

    print dir(regions)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue
        # excluding regions smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    # save all candidates

    i = 0
    for c in candidates:
        print i
        x, y, w, h = c  # x, y, width, height

        print x, y, w, h
        cropped = img[x:x+w, y:y+h]
        fname = 'regions/region_{}.jpg'.format(i)
        i += 1
        skimage.io.imsave(fname, cropped)
예제 #47
0
파일: main.py 프로젝트: indspug/TensorFlow
  
    if argc != 2:
        print_usage(argvs[0])
        quit()

    # コマンドライン引数でファイルパスを指定
    filepath = argvs[1]

    # 画像読込
    img = skimage.io.imread(filepath)
    img = skimage.transform.resize(img, (64,64), mode='edge')

    print('aaaaaaaaaa')

    # selective search実行
    label, regions = selectivesearch.selective_search(img, scale=64, sigma=0.8, min_size=2)

    print('bbbbbbbbbb')

    # 元画像にselective searchで取得した短形を追加して表示
    candidates = set()
    for reg in regions:
        if reg['rect'] in candidates:
            continue

        if reg['size'] < 100:
            continue

        x, y, w, h = reg['rect']

        #if float(w)*h > float(256)*256*0.95:
예제 #48
0
    def load_2flowers(self):
        with codecs.open(self.fineturn_list, 'r', 'utf-8') as f:
            lines = f.readlines()
            for num, line in enumerate(lines):
                labels = []
                labels_bbox = []
                images = []
                context = line.strip().split(' ')
                image_path = context[0]
                ref_rect = context[2].split(',')
                ground_truth = [int(i) for i in ref_rect]
                img = cv2.imread(image_path)
                img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10)
                candidate = set()
                for r in regions:
                    if r['rect'] in candidate:
                        continue
                    if r['size'] < 200 :
                        continue
                    if (r['rect'][2] * r['rect'][3]) <500:
                        continue
                    proposal_img, proposal_vertice = clip_pic(img, r['rect'])
                    if len(proposal_img) == 0:
                        continue
                    x, y, w, h = r['rect']
                    if w==0 or h==0 :
                        continue
                    [a, b, c] =np.shape(proposal_img)
                    if a==0 or b==0 or c==0 :
                        continue
                    resized_proposal_img = resize_image(proposal_img, self.image_size,self.image_size)
                    candidate.add(r['rect'])
                    img_float = np.asarray(resized_proposal_img, dtype="float32")
                    if self.is_svm:
                        feature = self.solver.predict([img_float])
                        images.append(feature[0])
                    else :
                        images.append(img_float)

                    iou_val = IOU(ground_truth, proposal_vertice)
                    px = float(proposal_vertice[0]) + float(proposal_vertice[4] / 2.0)
                    py = float(proposal_vertice[1]) + float(proposal_vertice[5] / 2.0)
                    ph = float(proposal_vertice[5])
                    pw = float(proposal_vertice[4])

                    gx = float(ref_rect[0])
                    gy = float(ref_rect[1])
                    gw = float(ref_rect[2])
                    gh = float(ref_rect[3])

                    index = int(context[1])
                    if self.is_svm:
                        if iou_val < self.svm_threshold:
                            labels.append(0)
                        else:
                            labels.append(index)
                        label = np.zeros(5)
                        label[1:5] = [(gx - px) / pw, (gy - py) / ph, np.log(gw / pw), np.log(gh / ph)]
                        if iou_val < self.reg_threshold:
                            label[0] = 0
                        else:
                            label[0] = 1
                        labels_bbox.append(label)

                    else:
                        label = np.zeros(self.F_class_num )
                        if iou_val < self.fineturn_threshold :
                            label[0] = 1
                        else:
                            label[index] = 1
                        labels.append(label)
                view_bar("Process SVM_and_Reg_image of %s" % image_path, num + 1, len(lines))
                if self.is_save:
                    if self.is_svm:
                        if not os.path.exists(os.path.join(self.SVM_and_Reg_save_path, str(context[1]))):
                            os.makedirs(os.path.join(self.SVM_and_Reg_save_path, str(context[1])))
                        np.save((os.path.join(self.SVM_and_Reg_save_path, str(context[1]), context[0].split('/')[-1].split('.')[0].strip())
                                                    + '_data.npy'),[images, labels, labels_bbox])
                    else:
                        np.save((os.path.join(self.fineturn_save_path, context[0].split('/')[-1].split('.')[0].strip()) +
                                                     '_data.npy'),[images, labels])
예제 #49
0
def get_ss_crops(img):
    orig_img_height, orig_img_width, channels = img.shape
    #print 'orig: ', str(orig_img_width), str(orig_img_height)
    img = misc.imresize(img, (IMG_SIZE, IMG_SIZE))
    img_height, img_width, channels = img.shape

    #print float(img.size)
    #print 'max size', float(img.size)/3*0.8
    #print 'min size', float(img.size)/3*0.005

    img_exp = img
    #img_exp = exposure.equalize_adapthist(img, clip_limit=0.03)
    
    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img_exp, scale=SCALE, sigma=SIGMA, min_size=MIN_SIZE)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue

        # distorted rects

        x, y, w, h = r['rect']
        if h == 0 or w == 0:
            continue
        if w / h > 2 or h / w > 2:
            continue
        if w > img_width*0.9 or h > img_height*0.9:
            continue

        rect_size = w*h
        #print rect_size
        #print x, y, w, h


        if rect_size > float(img.size)/3*0.8:
            #print 'too big'
            continue
        if rect_size < float(img.size)/3*0.005:
            #print 'too small'
            continue
        center_x = x + w/2
        center_y = y + h/2
        
        if center_x < img_width*0.15 or center_x > img_width*0.85 or center_y < img_height*0.15 or center_y > img_height*0.85:
            #print 'off edges'
            continue

        # non-max suppression
        candidates = [s for s in candidates if not (s[0] >= x or s[1] >= y or s[2] <= w or s[3] <= h)]

        too_small = False
        for larger in candidates:
            if larger[0] <= x and larger[1] <= y and larger[2] >= w and larger[3] >= h:

                #print x, y, w, h, " too small"
                too_small = True
        if too_small:
            continue

        new_width = int(w*WINDOW_EXPAND)
        new_height = int(h*WINDOW_EXPAND)
        x = max(0, center_x-new_width/2)
        y = max(0, center_y-new_height/2)
        w = min(img_width-x, new_width)
        h = min(img_height-y, new_height)
        #print x, y, w, h

        x_scale_factor = float(orig_img_width)/IMG_SIZE
        y_scale_factor = float(orig_img_height)/IMG_SIZE
        #print x_scale_factor, y_scale_factor
        x_scaled = x * x_scale_factor
        y_scaled = y * y_scale_factor
        w_scaled = w * x_scale_factor
        h_scaled = h * y_scale_factor

        candidates.append((x_scaled, y_scaled, w_scaled, h_scaled))
    return candidates
예제 #50
0
def get_search(file_name, main_dir, sub_dir):

    dir_path = main_dir + sub_dir

    file_path = dir_path + '/' + file_name
    open_img = Image.open('%s' % file_path)
    img = np.array(open_img)

    cropped_dir = crop_dir + sub_dir + '_crp'

    # if file has already been processed, skip to next
    if os.path.isfile(cropped_dir + '/' + file_name.strip('.jpg') + '.png'):
        print "%s already processed, moving to next one..." % (sub_dir + '/' + file_name)
        return

    print "Searching for a whale in %s" % file_path
    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(
        img, scale=500, sigma=0.9, min_size=10)

    print "Regions found: ", len(regions)
    print "Selective search done. Now finding largest area of interest."
    candidates = set()
    rect_size = 0
    # finds largest rectangle of interest - presumed to be whale

    if len(regions) > 1:
        for r in regions:
            if r['rect'] in candidates:
                continue
        # excluding regions smaller than 40k px, or larger than 400k px
            if r['size'] < rect_size or r['size'] < 40000 or r['size'] > 400000:
                continue
        # distorted rects
            x, y, w, h = r['rect']
            if w / h > 1.4 or h / w > 1.4:
                continue
            rect_size = r['size']
            img_rect = r['rect']

    if rect_size > 0:
        print "Largest Size: ", rect_size
        print "Rectangle = ", img_rect
        make_cropped_img(open_img, img_rect, file_name, cropped_dir)
    else:
        # try again under less stringent conditions
        for r in regions:
            if r['rect'] in candidates:
                continue
        # excluding regions smaller than 20000 pixels
            if r['size'] < rect_size or r['size'] < 20000 or r['size'] > 600000:
                continue
        # distorted rects
            x, y, w, h = r['rect']
            if w / h > 2.2 or h / w > 2.2:
                continue
            rect_size = r['size']
            img_rect = r['rect']

        if rect_size > 0:
            print "Largest Size: ", rect_size
            print "Rectangle = ", img_rect
            print "Second pass successful..."
            make_cropped_img(open_img, img_rect, file_name, cropped_dir)
        else:
            print "No potential whales found..."
예제 #51
0
		text=set()
		text_cut = set()
		no_text = set()
		stage = 1
		text_cut_final = set()

		for sc in [350,450,500]:
			for sig in [0.8]:
				for mins in [30,60,120]: # important
					img = skimage.io.imread(fname)[:,:,:3]
					if height == len(img) and width == len(img[0]):
						pass
					else:
						img = skimage.transform.resize(img, (height, width))

					img_lbl, regions = selectivesearch.selective_search(
					img, scale=sc, sigma= sig,min_size = mins)

					for r in regions:
						# excluding same rectangle (with different segments)
						if r['rect'] in candidates:
							continue
						# excluding regions smaller than 2000 pixels
						if r['size'] < 2000:
							continue
						# distorted rects
						x, y, w, h = r['rect']
						if w / h > 1.2 or h / w > 1.2:
							continue
						if w >= (img.shape[0]-1)*(0.7) and h >= (img.shape[1]-1)*(0.7):
							continue
						candidates.add(r['rect'])
예제 #52
0
from IPython.display import clear_output, Image, display
from google.protobuf import text_format

import caffe

import skimage.data
import selectivesearch

def showarray(a, fmt='jpeg'):
    a = np.uint8(np.clip(a, 0, 255))
    f = StringIO()
    PIL.Image.fromarray(a).show() #.save(f, fmt)


img = skimage.data.lena()
img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10)

showarray(regions)


'''
regions
=> [{'labels': [0.0], 'rect': (0, 0, 59, 511), 'size': 15633},
 {'labels': [1.0], 'rect': (58, 0, 1, 132), 'size': 173},
 {'labels': [2.0], 'rect': (60, 18, 1, 5), 'size': 8},
 {'labels': [3.0], 'rect': (5, 57, 25, 335), 'size': 507},
 {'labels': [4.0], 'rect': (27, 166, 0, 9), 'size': 10},
 {'labels': [5.0], 'rect': (59, 498, 0, 0), 'size': 1},
 {'labels': [6.0], 'rect': (35, 484, 0, 27), 'size': 28},
 {'labels': [7.0], 'rect': (58, 122, 4, 374), 'size': 581},
 {'labels': [8.0], 'rect': (59, 40, 0, 81), 'size': 82},