def test_rgba2rgb_conversion(self):
     rgba = self.img_rgba
     rgb = rgba2rgb(rgba)
     expected = np.array([[[1, 1, 1],
                           [0, 0.5, 1],
                           [0.5, 0.75, 1]]]).astype(np.float)
     self.assertEqual(rgb.shape, expected.shape)
     assert_almost_equal(rgb, expected)
Exemple #2
0
def punchhole_removal(im):
    import numpy as np
    from PIL import Image
    from skimage import io
    from skimage.color import rgba2rgb, rgb2gray
    from skimage.transform import hough_circle, hough_circle_peaks
    from skimage.feature import canny
    from skimage.draw import circle
    from skimage.util import img_as_ubyte

    ''' check for punch holes and remove  '''
    
    max_peaks =  24 #maximum number of peaks to be found. changed from 99 to 24 for reducing the unnecessary punch holes being filled.

    img = np.array(im)# Load picture .
    img_rgb = rgba2rgb(img)# convert to RGB
    img_gray = rgb2gray(img_rgb)# convert to gray
    image = img_as_ubyte(img_gray)
    width, height = image.shape
    x1 =  punchhole_margin
    x2 =  (int)(width - punchhole_margin)
    y1 =  (int)(height - punchhole_margin)
    y2 =  punchhole_margin

    edges = canny(image, 3, 10, 40) # perform canny to detect the edges
    hough_radii = np.arange(31, 34, 1) #get the radius range with step as 1.
    hough_res = hough_circle(edges, hough_radii) # detect the circles centres coordinates

    # Select the most prominent circles based on the max_peaks
    accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii,total_num_peaks=max_peaks)

    for center_y, center_x, radius in zip(cy, cx, radii):

        #if the circles centres fall in the border regions, 
        #get the dominant color near the hole and fill the hole with a linear gradient of the dominant color
        if(((0 < center_y < width) and (0 < center_x < y2)) or \
           ((0 < center_y < width) and (y1 < center_x < height)) or\
           ((0 < center_y < x1) and (0 < center_x < height)) or \
           ((x2 < center_y < width) and (0 < center_x < height))):

            index=0
            rr, cc= circle(center_y, center_x, radius+1, img.shape)
            dominantpix = dominantcolor(center_x, center_y, radius, img)           
            dark_grad = [dominantpix[0], dominantpix[1],dominantpix[2]]
            light_grad = [dominantpix[0]+1, dominantpix[1]+1, dominantpix[2]+1]
            #white_grad = [255,255,255]
            RGBA_list = lineargradient(dark_grad,light_grad,len(list(rr)))   
          
            for i , j in zip(list(rr), list(cc)):
                pixlist = RGBA_list[index]
                pixtuple = tuple(pixlist)
                img[i,j]= (pixtuple[0], pixtuple[1], pixtuple[2], 255)
                index += 1
           
    finalimage=Image.fromarray(img)

    return finalimage
Exemple #3
0
def read_image(filename, number_of_channels):
    """ read_image using skimage
        The output is a 3-dim image [H, W, C]
    """
    if number_of_channels == 1:
        image = io.imread(filename, as_gray=True)
        image = imgproc.toUINT8(image)
        assert len(image.shape) == 2
        image = np.expand_dims(image, axis=2)  # H,W,C
        assert len(image.shape) == 3 and image.shape[2] == 1
    elif number_of_channels == 3:
        image = io.imread(filename)
        if len(image.shape) == 2:
            image = color.gray2rgb(image)
        elif image.shape[2] == 4:
            image = color.rgba2rgb(image)
        image = imgproc.toUINT8(image)
        assert len(image.shape) == 3 and image.shape[2] == 3
    else:
        raise ValueError("number_of_channels must be 1 or 3")
    if not os.path.exists(filename):
        raise ValueError(filename + " does not exist!")
    return image
def crf(original_image, annotated_image):
    rd.seed(123)
    if len(original_image.shape) < 3:
        original_image = gray2rgb(original_image)
    if len(original_image.shape) == 3 and original_image.shape[2] == 4:
        original_image = rgba2rgb(original_image)
    original_image = img_as_ubyte(original_image)
    annotated_image = np.moveaxis(annotated_image, -1, 0)
    annotated_image = annotated_image.copy(order='C')

    d = dcrf.DenseCRF2D(
        original_image.shape[1], original_image.shape[0], 3)

    U = unary_from_softmax(annotated_image)
    d.setUnaryEnergy(U)

    d.addPairwiseGaussian(sxy=(3, 3), compat=3, kernel=dcrf.DIAG_KERNEL,
                                normalization=dcrf.NORMALIZE_SYMMETRIC)

    d.addPairwiseBilateral(sxy=(80, 80), srgb=(13, 13, 13), rgbim=original_image,
                                compat=10,
                                kernel=dcrf.DIAG_KERNEL,
                                normalization=dcrf.NORMALIZE_SYMMETRIC)

    Q = d.inference(NB_ITERATIONS)

    MAP = np.argmax(Q, axis=0).reshape(
        original_image.shape[0], original_image.shape[1])

    result = np.zeros((MAP.shape[0], MAP.shape[1], 3))
    result[:, :, 2] = MAP

    result[:, :, 2][result[:, :, 2] == 2] = 4
    result[:, :, 2][result[:, :, 2] == 1] = 2
    result[:, :, 2][result[:, :, 2] == 0] = 1

    return result
Exemple #5
0
 def __init__(self, fname, format=None, resize=None, as_gray=False,
              electrodes=None, metadata=None, compress=False):
     img = imread(fname, format=format)
     # Build the metadata container:
     if metadata is None:
         metadata = {}
     metadata['source'] = fname
     metadata['source_shape'] = img.shape
     # Convert to grayscale if necessary:
     if as_gray:
         if img.shape[-1] == 4:
             # Convert the transparent background to black:
             img = rgba2rgb(img, background=(0, 0, 0))
         img = rgb2gray(img)
     # Resize if necessary:
     if resize is not None:
         img = img_resize(img, resize)
     # Store the original image shape for resizing and color conversion:
     self.img_shape = img.shape
     # Convert to float array in [0, 1] and call the Stimulus constructor:
     super(ImageStimulus, self).__init__(img_as_float(img).ravel(),
                                         time=None, electrodes=electrodes,
                                         metadata=metadata,
                                         compress=compress)
def rgb_to_od(img: PIL.Image.Image) -> PIL.Image.Image:
    """Convert from RGB to optical density (OD_RGB) space.

    Parameters
    ----------
    img : PIL.Image.Image
        Input image

    Returns
    -------
    PIL.Image.Image
        Image in OD space
    """
    if img.mode == "RGBA":
        img_arr = np.array(sk_color.rgba2rgb(img))
        warn("Input image must be RGB. "
             "NOTE: the image will be converted to RGB before HED conversion.")
    else:
        img_arr = np.array(img)
    mask = img_arr == 0
    img_arr[mask] = 1
    od_arr = np.maximum(-1 * np.log10(img_arr / 255), 1e-6).round(10)
    od_arr = sk_exposure.rescale_intensity(od_arr)
    return np_to_pil(od_arr)
Exemple #7
0
def test_lab_encode():
    img = read_image(test_image)
    img = transform.resize(img, (64, 64))
    img = color.rgba2rgb(img)

    img = convert_rgb_to_lab(img)

    # plot_image(img)
    start = time.time()

    encoded = soft_encode_lab_img(img)

    for row in range(encoded.shape[0]):
        for col in range(encoded.shape[1]):
            dist = encoded[row, col, :]
            if not np.isclose(1, dist.sum()):
                print(dist)
                print(dist.sum())
                exit()

    end = time.time()
    total = end - start

    print("encoding took", total, "seconds")
Exemple #8
0
def felzen_segment():
    global segments, orig
    img_b64 = request.values['data'].split("base64,")[1]
    val = 600 - int(float(request.values['val']))
    image_result = open('orig.png', 'wb')
    image_result.write(base64.b64decode(img_b64))
    img = io.imread('orig.png')
    img = color.rgba2rgb(img)
    segments = segmentation.felzenszwalb(img, min_size=val)
    segmented_img = segmentation.mark_boundaries(img, segments)
    back_img = np.zeros((segmented_img.shape[0], segmented_img.shape[1], 4))
    back_img[:, :, 0] = img[:, :, 0] * 255
    back_img[:, :, 1] = img[:, :, 1] * 255
    back_img[:, :, 2] = img[:, :, 2] * 255
    back_img[:, :, 3] = 255
    orig = img
    for file in os.listdir("./masks/"):
        os.remove("./masks/" + file)
    plt.imsave("masks/back.png", back_img.astype(np.uint8))
    plt.imsave("segment.jpg", segmented_img)
    np.save("segments", segments)
    print('Image received: {}'.format(img.shape))
    response = jsonify({'message': 'Happy Noises'})
    return response
Exemple #9
0
    def test_rgba2rgb_dtype(self):
        rgba = self.img_rgba.astype('float64')
        rgba32 = img_as_float32(rgba)

        assert rgba2rgb(rgba).dtype == rgba.dtype
        assert rgba2rgb(rgba32).dtype == rgba32.dtype
def main():
    list_of_colors = read_color_codes("classes.csv")
    image_names_list = os.listdir(in_dir_images)

    image_index = 3  # 18
    target_index = 42
    img = io.imread(os.path.join(in_dir_images, image_names_list[image_index]))
    print("IMG --> ", image_names_list[image_index])
    print("CODED TARGET --> ", target_index + 1)

    if img.shape[2] == 4:
        img = color.rgba2rgb(img)
    elif img.shape[2] < 3 or img.shape[2] > 5:
        print("Our img non RGB or RGBA. We don't know, what to do...")
        return 1

    mask_coded_target = np.all(img == list_of_colors[target_index], axis=-1)

    img_edges = feature.canny(mask_coded_target)

    # Проходим по строкам
    line_1_points = list()
    line_1_xs = list()
    line_1_ys = list()
    epsilon = 2
    for row in range(img_edges.shape[0]):
        row_edges = np.nonzero(img_edges[row])
        row_edges = row_edges[0]
        if len(row_edges):
            avr_in_row = (row_edges[0] + row_edges[-1]) / 2
            if row <= 0 + epsilon or row >= img_edges.shape[0] - epsilon:
                print("BAD ROW. CODED TARGET --> ", target_index)
                line_1_points.clear()
                line_1_xs.clear()
                line_1_ys.clear()
                break
            line_1_points.append([avr_in_row, row])
            line_1_xs.append(avr_in_row)
            line_1_ys.append(row)
    # Проходим по столбцам
    line_2_points = list()
    line_2_xs = list()
    line_2_ys = list()
    epsilon = 2
    for col in range(img_edges.shape[1]):
        col_edges = np.nonzero(img_edges[:, col])
        col_edges = col_edges[0]
        if len(col_edges):
            avr_in_col = (col_edges[0] + col_edges[-1]) / 2
            if col <= 0 + epsilon or col >= img_edges.shape[1] - epsilon:
                print("BAD COL. CODED TARGET --> ", target_index)
                line_2_points.clear()
                line_2_xs.clear()
                line_2_ys.clear()
                break
            line_2_points.append([col, avr_in_col])
            line_2_xs.append(col)
            line_2_ys.append(avr_in_col)

    # находим пересечение двух прямых
    xs = None
    ys = None
    if len(line_1_xs) * len(line_1_ys) and len(line_2_xs) * len(line_2_ys):
        m1, b1 = np.polyfit(line_1_xs, line_1_ys, 1)
        m2, b2 = np.polyfit(line_2_xs, line_2_ys, 1)

        A = np.array([[m1, -1], [m2, -1]])
        B = np.array([[-b1], [-b2]])

        xs, ys = np.linalg.solve(A, B)

    print("xs = ", xs, "ys = ", ys)
    fig, (ax1, ax2) = plt.subplots(nrows=1,
                                   ncols=2,
                                   figsize=(8, 3),
                                   sharex=True,
                                   sharey=True)
    ax1.imshow(img)
    ax1.axis("off")
    ax1.set_title("Image")

    ax2.imshow(img_edges)
    # Отображаем точки первой линии
    for point in line_1_points:
        plt.scatter(point[0], point[1], s=1, facecolor='red')
    # Отображаем точки второй линии
    for point in line_2_points:
        plt.scatter(point[0], point[1], s=1, facecolor='red')

    if xs is not None and ys is not None:
        plt.scatter(xs, ys, s=2, facecolor='green')

    ax2.axis("off")
    ax2.set_title("Edges")

    fig.tight_layout()

    plt.show()
Exemple #11
0
def recolor(request):
    if request.method == 'POST':
        json_name = request.POST['case']
        banner = getImageFromJson(os.path.join('./case' , json_name + '.json'))
        #banner = getImageFromJson('0073_1760.json')
        target = banner.paint()
        target.save('./static/img/target.png')

        palette_gen = request.POST['paletteGen']
        render_op = request.POST['render']
        # iseva = request.POST['iseva']
        keys = request.POST.keys()
        iseva = False
        if 'iseva' in keys:
            iseva = True
        # assert False
        orig_score = banner.evaluate()
        # change background and text
        childrens = banner.childrens
        background = childrens[0]
        element = background
        for item in childrens:
            if item.category == 'background':
                background = item
            elif item.category == 'element':
                element = item


        
        url_elem = 'http:' + element.src
        elem = io.imread(url_elem)
        mask = np.ones(elem[:,:,0].shape) * 255
        if elem.shape[2] == 4:
            mask = elem[:,:,3]
            elem = color.rgba2rgb(elem)
            elem = img_as_ubyte(elem)

        elem_color = getProductColor(elem , mask)
        print elem_color
        elem_palette_img = getImagePalette(elem_color)
        scipy.misc.imsave('./static/img/elem_palette.jpg' , elem_palette_img)

        
        pal = palettes.Palette(elem_color)
        
        if palette_gen == '0':
            palette = pal.getRecColorFromColormind()
        else:
            pass
        # palette = [[49,47,49],[91,83,81],[133,155,143],[226,209,167],[235,198,126]]
        score = pal.palette_score(palette)
        scipy.misc.imsave('./static/img/palette.jpg' , getImagePalette(palette))

        contrasts = []
        order = []
        scores = []
        for item in permutations(range(2 , 5) , 2):
            index_bak , index_text = item
            bak_l , _ , _ = colormap.rgb2lab(palette[index_bak][0] , palette[index_bak][1] , palette[index_bak][2])
            text_l , _, _ = colormap.rgb2lab(palette[index_text][0] , palette[index_text][1] , palette[index_text][2])
            contrast = abs(bak_l - text_l)
            contrasts.append(contrast)
            #order.append(item)
            banner.childChange(bak_change(background , palette[index_bak:index_bak + 1] , render_op))

            for item in childrens:
                print item.category
                print item.filename
                if 'title' in item.category:
                    # banner.childChange(title_change(item , palette[index_text:index_text + 1]))
                    # print 'adasd'
                    if color_select(item):
                        banner.childChange(title_change(item , palette[index_text:index_text + 1]))
                    else:
                        banner.childChange(bak_change(item , palette[index_text:index_text + 1] , render_op))
            img = banner.paint()
            if iseva:
                scores.append(banner.evaluate())

            img.save(os.path.join('./static/img/' , str(index_bak) + '_' + str(index_text) + '.png'))
            order.append(str(index_bak) + '_' + str(index_text) + '.png')

        print contrasts
        print order
        # print score , scores
        print score
        if iseva:
            res = zip(order , scores)
        else:
            res = order

        return render_to_response('index.html' , {'flag':True , 'pics':res , 'score':score , 'iseval':iseva , 'orgin_score':orig_score})
Exemple #12
0
        self.r1 = min(mask[:, 0])
        self.r2 = max(mask[:, 0])
        self.c1 = min(mask[:, 1])
        self.c2 = max(mask[:, 1])
        self.data = np.ones((28, 28), dtype=np.float64)
        for bb in mask:
            print(type(o[bb[0], bb[1]]))
            self.data[bb[0] - self.r1 + 4, bb[1] - self.c1 + 4] = o[bb[0],
                                                                    bb[1]]
        self.pixel = 255 - img_as_ubyte(self.data)
        self.pixel = np.array(self.pixel).reshape(28 * 28)


filename = os.path.join('4.png')
moon = io.imread(filename)
moon = rgb2gray(rgba2rgb(moon))
block_size = 11
local_thresh = threshold_local(moon, block_size, offset=0.001)
binary = moon >= local_thresh
a = np.argwhere(binary == False)
kmeans = KMeans(n_clusters=4, random_state=0).fit(a)
b = []
for i in range(4):
    b.append(figure(a[kmeans.predict(a) == i], moon))
b = sorted(b, key=operator.attrgetter('c1'))

im = Image.fromarray(img_as_ubyte(b[0].data))
im.save("./figures/1.jpg")
im = Image.fromarray(img_as_ubyte(b[1].data))
im.save("./figures/2.png")
im = Image.fromarray(img_as_ubyte(b[2].data))
Exemple #13
0
def wmcnn_hdf5(path, full=False):
    def expanddims(array, expand=2):
        for i in range(expand):
            array = np.expand_dims(array, 0)
        return array

    # from matplotlib import pyplot as plt
    # from skimage import measure

    scale = 2
    if not full:
        size_label = 96
        stride = 48
    else:
        size_label = 400

    size_input = size_label / scale
    batch_size = 400
    classes = 7
    # downsizes = [1, 0.7, 0.5]

    id_files = join(path, 'id_files.txt')
    # id_files = join(path, 'test_id.txt')
    with open(id_files, 'r') as f:
        article = f.readlines()
        fnum = len(article) // classes
        order = np.random.permutation(fnum)
        training = order[:int(fnum * 0.7)]
        testing = order[len(training):]

        img_list = []
        for i, line in enumerate(article):
            img = io.imread(path + line[2:].strip('\n'))
            if i % fnum in testing:
                cls = line.split('/')[1]
                mkdir_if_not_exist(path + 'test/' + cls)
                io.imsave(path + 'test/' + line[2:].strip('\n'), img)
            else:
                if img.shape[2] == 4:
                    img = color.rgba2rgb(img)
                img_ycbcr = color.rgb2ycbcr(img) / 255
                (rows, cols, channel) = img_ycbcr.shape
                img_y, img_cb, img_cr = np.split(img_ycbcr,
                                                 indices_or_sections=channel,
                                                 axis=2)
                img_list.append(img_y)

    for idx, img_gt in enumerate(img_list):
        if idx == 0:
            hf = h5py.File('D:/data/rsscn7/full_wdata.h5', 'w')
            d_data = hf.create_dataset("data",
                                       (batch_size, 1, size_input, size_input),
                                       maxshape=(None, 1, size_input,
                                                 size_input),
                                       dtype='float32')
            d_ca = hf.create_dataset("CA",
                                     (batch_size, 1, size_input, size_input),
                                     maxshape=(None, 1, size_input,
                                               size_input),
                                     dtype='float32')
            d_ch = hf.create_dataset("CH",
                                     (batch_size, 1, size_input, size_input),
                                     maxshape=(None, 1, size_input,
                                               size_input),
                                     dtype='float32')
            d_cv = hf.create_dataset("CV",
                                     (batch_size, 1, size_input, size_input),
                                     maxshape=(None, 1, size_input,
                                               size_input),
                                     dtype='float32')
            d_cd = hf.create_dataset("CD",
                                     (batch_size, 1, size_input, size_input),
                                     maxshape=(None, 1, size_input,
                                               size_input),
                                     dtype='float32')
        else:
            hf = h5py.File('D:/data/rsscn7/full_wdata.h5', 'a')
            d_data = hf['data']
            d_ca = hf['CA']
            d_ch = hf['CH']
            d_cv = hf['CV']
            d_cd = hf['CD']

        count = 0
        if not full:
            d_data.resize([idx * batch_size + 392, 1, size_input, size_input])
            d_ca.resize([idx * batch_size + 392, 1, size_input, size_input])
            d_ch.resize([idx * batch_size + 392, 1, size_input, size_input])
            d_cv.resize([idx * batch_size + 392, 1, size_input, size_input])
            d_cd.resize([idx * batch_size + 392, 1, size_input, size_input])

            for flip in range(2):
                for degree in range(4):
                    # for downsize in downsizes:
                    img = img_gt.squeeze()
                    if flip == 1:
                        img = np.fliplr(img)

                    for turn in range(degree):
                        img = np.rot90(img)

                    # img = imresize(img, scalar_scale=downsize)
                    hei, wid = img.shape
                    # fig = plt.figure(figsize=(6, 3))
                    for x in range(0, hei - size_label, stride):
                        for y in range(0, wid - size_label, stride):
                            subim_label = img[x:x + size_label,
                                              y:y + size_label]
                            subim_data = imresize(subim_label,
                                                  scalar_scale=1 / scale)
                            coeffs2 = pywt.dwt2(subim_label, 'bior1.1')
                            LL, (LH, HL, HH) = coeffs2

                            d_data[idx * batch_size + count] = expanddims(
                                subim_data, expand=2)
                            d_ca[idx * batch_size + count] = expanddims(
                                LL, expand=2)
                            d_ch[idx * batch_size + count] = expanddims(
                                LH, expand=2)
                            d_cv[idx * batch_size + count] = expanddims(
                                HL, expand=2)
                            d_cd[idx * batch_size + count] = expanddims(
                                HH, expand=2)
                            count += 1
        else:
            d_data.resize([idx * batch_size + 1, 1, size_input, size_input])
            d_ca.resize([idx * batch_size + 1, 1, size_input, size_input])
            d_ch.resize([idx * batch_size + 1, 1, size_input, size_input])
            d_cv.resize([idx * batch_size + 1, 1, size_input, size_input])
            d_cd.resize([idx * batch_size + 1, 1, size_input, size_input])

            img = img_gt.squeeze()
            im_data = imresize(img, scalar_scale=1 / scale)
            coeffs2 = pywt.dwt2(img, 'bior1.1')
            LL, (LH, HL, HH) = coeffs2

            d_data[idx * batch_size + count] = expanddims(im_data, expand=2)
            d_ca[idx * batch_size + count] = expanddims(LL, expand=2)
            d_ch[idx * batch_size + count] = expanddims(LH, expand=2)
            d_cv[idx * batch_size + count] = expanddims(HL, expand=2)
            d_cd[idx * batch_size + count] = expanddims(HH, expand=2)
            count += 1

        batch_size = count

        hf.close()
def GetPoseandCostsF(
    cfg,
    dlc_cfg,
    sess,
    inputs,
    outputs,
    cap,
    nframes,
    batchsize,
    shelf_path,
):
    """Batchwise prediction of pose"""
    strwidth = int(np.ceil(np.log10(nframes)))  # width for strings
    batch_ind = 0  # keeps track of which image within a batch should be written to
    batch_num = 0  # keeps track of which batch you are at
    if cfg["cropping"]:
        cap.set_bbox(cfg["x1"], cfg["x2"], cfg["y1"], cfg["y2"])
    nx, ny = cap.dimensions

    frames = np.empty((batchsize, ny, nx, 3),
                      dtype="ubyte")  # this keeps all frames in a batch
    pbar = tqdm(total=nframes)
    counter = 0
    inds = []

    if shelf_path:
        db = shelve.open(
            shelf_path,
            protocol=pickle.DEFAULT_PROTOCOL,
        )
    else:
        db = dict()
    db["metadata"] = {
        "nms radius":
        dlc_cfg["nmsradius"],
        "minimal confidence":
        dlc_cfg["minconfidence"],
        "sigma":
        dlc_cfg.get("sigma", 1),
        "PAFgraph":
        dlc_cfg["partaffinityfield_graph"],
        "PAFinds":
        dlc_cfg.get("paf_best",
                    np.arange(len(dlc_cfg["partaffinityfield_graph"]))),
        "all_joints": [[i] for i in range(len(dlc_cfg["all_joints"]))],
        "all_joints_names": [
            dlc_cfg["all_joints_names"][i]
            for i in range(len(dlc_cfg["all_joints"]))
        ],
        "nframes":
        nframes,
    }
    while cap.video.isOpened():
        frame = cap.read_frame(crop=cfg["cropping"])
        key = "frame" + str(counter).zfill(strwidth)
        if frame is not None:
            # Avoid overwriting data already on the shelf
            if isinstance(db, shelve.Shelf) and key in db:
                continue
            frame = img_as_ubyte(frame)
            if frame.shape[-1] == 4:
                frame = rgba2rgb(frame)
            frames[batch_ind] = frame
            inds.append(counter)
            if batch_ind == batchsize - 1:
                D = predict.predict_batched_peaks_and_costs(
                    dlc_cfg,
                    frames,
                    sess,
                    inputs,
                    outputs,
                )
                for ind, data in zip(inds, D):
                    db["frame" + str(ind).zfill(strwidth)] = data
                del D
                batch_ind = 0
                inds.clear()
                batch_num += 1
            else:
                batch_ind += 1
        elif counter >= nframes:
            if batch_ind > 0:
                D = predict.predict_batched_peaks_and_costs(
                    dlc_cfg,
                    frames,
                    sess,
                    inputs,
                    outputs,
                )
                for ind, data in zip(inds, D):
                    db["frame" + str(ind).zfill(strwidth)] = data
                del D
            break
        counter += 1
        pbar.update(1)

    cap.close()
    pbar.close()
    try:
        db.close()
    except AttributeError:
        pass
    return db, nframes
def get_volume_main(filepath, plate_color, ref_len, shape_type, additional_info = 0, debug = False, showing = False):
    
    """
    shape type:
        1: cube (e.g. cake)
        2: ball (e.g. apple)
        3: half-ball (e.g. bun)
        4: cone (e.g. fried rice in the plate)
        5: fixed-height (e.g. pizza)
        6: irregular but nearly fixed shape (e.g. banana)
        
    additional_info:
        height, for type 5
        volume per unit area, for type 6
    """
    
    image_rgb = io.imread(filepath) 
    if (image_rgb.shape[2] == 4):
        image_rgb = color.rgba2rgb(image_rgb)
    image_rgb = transform.resize(image_rgb, (int(100*image_rgb.shape[0]/image_rgb.shape[1]), 100))
    food, plate_long, plate_short = get_food_region(image_rgb, plate_color)
    
    if showing:
        io.imsave('original_image.jpg', image_rgb)
        
    if debug:    
        f, ((ax0, ax1, ax2, ax3), (ax4, ax5, ax6, ax7)) = plt.subplots(ncols=4, nrows=2, figsize=(22, 8))
        ax0.set_title('food')
        ax0.imshow(image_rgb)

    if shape_type == 1:           
        labels, labels2 = segment_food (image_rgb, food)
        area, height = get_height_and_area(labels2)
        volume = cal_volume_1(plate_long, plate_short, ref_len, area, height)
        
        if debug:
            ax2.set_title('segment')
            ax2.imshow(labels)
    
            ax3.set_title('segment2')
            ax3.imshow(labels2)
        
    if shape_type == 2:
        volume = cal_volume_2(plate_long, plate_short, ref_len, food)
    
    if shape_type == 3:
        volume = cal_volume_3(plate_long, plate_short, ref_len, food)
    
    if shape_type == 4:
        volume = cal_volume_4(plate_long, plate_short, ref_len, food)
        
    if shape_type == 5:
        volume = cal_volume_5(plate_long, plate_short, ref_len, food, additional_info)
    
    
    if shape_type == 6:
        volume = cal_volume_6(plate_long, plate_short, ref_len, food, additional_info)
    
    if debug:
        print('The estimated volume is', volume, 'cm^3.\n(Plate size:', ref_len, 'cm; type of shape: #', shape_type, '.)')
      
        for i in range(0, image_rgb.shape[0]):
            for j in range(0, image_rgb.shape[1]):
                if (food[i][j] == 0):
                    image_rgb[i][j] = [0,0,0]    
                
        ax1.set_title('food')
        ax1.imshow(image_rgb)
            
    
    if showing:
        if shape_type == 1:
            io.imsave('mid_result.jpg', labels2)
        else:
            io.imsave('mid_result.jpg', food)
    
    return volume  
  plt.axis('off')
  plt.title('orginal', fontsize = 12)

  location = [2, 3, 4, 6, 7, 8]
  for inter in range(6):
    time1 = time.time()*1000
    I1 = resize(img, (height*factor, length*factor, 3), order = inter)
    time2 = time.time()*1000
    diff = time2 - time1
    plt.subplot(2, 4, location[inter])
    plt.imshow(I1)
    plt.axis('off')
    plt.title('order: %i\n time: %.2fs' %(inter, diff), fontsize = 10)
    
I = imread('Images/tree.png')
I = rgba2rgb(I)
img = resize(I, (np.floor(0.2*I.shape[0]), np.floor(0.2*I.shape[1]), 3))
enlarge(img)


# The order terms are different methods used to extrapolate the colors of the new added pixels in the resizing. the order terms correspond to 
# 
# 0 = Nearest-neighbor,
# 1 = Bi-linear,
# 2 = Bi-quadratic,
# 3 = Bi-cubic,
# 4 = Bi-quartic,
# 5 = Bi-quintic,
# 
# The time complexity increases with the order term respectively
Exemple #17
0
    async def tex(self, ctx: Context, *message: clean_content):
        await ctx.trigger_typing()
        print(message)
        # Input filtering
        if not message:
            await ctx.send("Your message contained nothing to render")

        if message[0] == "```tex":
            message = ("```", *message[1:])
        combined = " ".join([x.lstrip("@") for x in message])
        if combined[0] != "`" or combined[-1] != "`":
            await ctx.send("Please place your input in an inline code block")
            return

        tex_code = combined.lstrip("`").rstrip("`")
        # May want to consider enforcing the $message$ requirement here
        # May also want to disallow/convert $$message$$ environments here

        # Matplotlib preamble
        plt.clf()
        plt.rc("text", usetex=True)
        plt.rc("text.latex", preamble=r"\usepackage{amsmath}")
        plt.rc("font", **{
            "family": "serif",
            "serif": ["Palatino"],
            "size": 16
        })
        plt.axis("off")

        # Generate the filename
        filename = (tex_code + "-" +
                    str(hex(int(datetime.utcnow().timestamp()))).lstrip("0x") +
                    ".png").replace(" ", "")
        path_png = "{path}/{filename}".format(
            path=CONFIG["FIG_SAVE_PATH"].rstrip("/"), filename=filename)
        path_jpg = path_png.replace(".png", ".jpg")
        try:
            # Plot the latex and save it.
            plt.text(0, 1, tex_code, color="white")
            plt.savefig(path_png,
                        dpi=300,
                        bbox_inches="tight",
                        transparent=True)
        except RuntimeError as r:
            # Failed to render latex. Report error
            print(r)
            await ctx.send(
                "Unable to render LaTeX. Please check that it's correct")
        else:
            # Generate a mask of the transparent regions in the image
            img_arr = img_as_float(io.imread(path_png))
            transparent_mask = np.array([1, 1, 1, 0])
            img_mask = np.abs(img_arr - transparent_mask).sum(axis=2) < 1

            # Generate the bounding box for the mask
            mask_coords = np.array(np.nonzero(~img_mask))
            top_left = np.min(mask_coords, axis=1) - [15, 15]
            bottom_right = np.max(mask_coords, axis=1) + [15, 15]

            # Crop the image and add a background layer
            img_cropped = img_arr[top_left[0]:bottom_right[0],
                                  top_left[1]:bottom_right[1]]
            img_cropped = color.rgba2rgb(img_cropped,
                                         background=IMAGE_BACKGROUND)

            # Save the image, delete the PNG and set the permissions for the JPEG
            io.imsave(path_jpg, img_cropped, quality=100)
            os.chmod(path_jpg, 0o644)
            os.remove(path_png)

            # Load the image as a file to be attached to an image
            img_file = File(path_jpg, filename="tex_output.jpg")
            display_name = get_name_string(ctx.message)
            await ctx.send(f"Here you go, {display_name}! :abacus:",
                           file=img_file)
Exemple #18
0
    def create_bags(self, dir_list):
        bag_list = []
        labels_list = []
        for dir in dir_list:
            # Get image name

            img_name = os.path.basename(dir)

            # bmp to pillow
            img_dir = os.path.join(dir, img_name + '.bmp')
            img = io.imread(img_dir)
            if img.shape[2] == 4:
                img = color.rgba2rgb(img)

            if self.location_info:
                xs = np.arange(0, 500)
                xs = np.asarray([xs for i in range(500)])
                ys = xs.transpose()
                img = np.dstack((img, xs, ys))

            # crop malignant cells
            dir_epithelial = os.path.join(dir, img_name + '_epithelial.mat')
            with open(dir_epithelial, 'rb') as f:
                mat_epithelial = scipy.io.loadmat(f)

            cropped_cells_epithelial = []
            for (x, y) in mat_epithelial['detection']:
                x = np.round(x)
                y = np.round(y)

                if self.data_augmentation:
                    x = x + np.round(np.random.normal(0, 3, 1))
                    y = y + np.round(np.random.normal(0, 3, 1))

                if x < 13:
                    x_start = 0
                    x_end = 27
                elif x > 500 - 14:
                    x_start = 500 - 27
                    x_end = 500
                else:
                    x_start = x - 13
                    x_end = x + 14

                if y < 13:
                    y_start = 0
                    y_end = 27
                elif y > 500 - 14:
                    y_start = 500 - 27
                    y_end = 500
                else:
                    y_start = y - 13
                    y_end = y + 14

                cropped_cells_epithelial.append(img[int(y_start):int(y_end), int(x_start):int(x_end)])

            # crop all other cells
            dir_inflammatory = os.path.join(dir, img_name + '_inflammatory.mat')
            dir_fibroblast = os.path.join(dir, img_name + '_fibroblast.mat')
            dir_others = os.path.join(dir, img_name + '_others.mat')

            with open(dir_inflammatory, 'rb') as f:
                mat_inflammatory = scipy.io.loadmat(f)
            with open(dir_fibroblast, 'rb') as f:
                mat_fibroblast = scipy.io.loadmat(f)
            with open(dir_others, 'rb') as f:
                mat_others = scipy.io.loadmat(f)

            all_coordinates = np.concatenate(
                (mat_inflammatory['detection'], mat_fibroblast['detection'], mat_others['detection']), axis=0)

            cropped_cells_others = []
            for (x, y) in all_coordinates:
                x = np.round(x)
                y = np.round(y)

                if self.data_augmentation:
                    x = x + np.round(np.random.normal(0, 3, 1))
                    y = y + np.round(np.random.normal(0, 3, 1))

                if x < 13:
                    x_start = 0
                    x_end = 27
                elif x > 500 - 14:
                    x_start = 500 - 27
                    x_end = 500
                else:
                    x_start = x - 13
                    x_end = x + 14

                if y < 13:
                    y_start = 0
                    y_end = 27
                elif y > 500 - 14:
                    y_start = 500 - 27
                    y_end = 500
                else:
                    y_start = y - 13
                    y_end = y + 14

                cropped_cells_others.append(img[int(y_start):int(y_end), int(x_start):int(x_end)])

            # generate bag
            bag = cropped_cells_epithelial + cropped_cells_others

            # store single cell labels
            labels = np.concatenate((np.ones(len(cropped_cells_epithelial)), np.zeros(len(cropped_cells_others))),
                                    axis=0)

            # shuffle
            if self.shuffle_bag:
                zip_bag_labels = list(zip(bag, labels))
                random.shuffle(zip_bag_labels)
                bag, labels = zip(*zip_bag_labels)

            # append every bag two times if training
            if self.train:
                for _ in [0, 1]:
                    bag_list.append(bag)
                    labels_list.append(labels)
            else:
                bag_list.append(bag)
                labels_list.append(labels)

        return bag_list, labels_list
def GetPoseandCostsF_from_assemblies(
    cfg,
    dlc_cfg,
    sess,
    inputs,
    outputs,
    cap,
    nframes,
    batchsize,
    assemblies,
    feature_dict,
    extra_dict,
):
    """Batchwise prediction of pose"""
    strwidth = int(np.ceil(np.log10(nframes)))  # width for strings
    batch_ind = 0  # keeps track of which image within a batch should be written to
    batch_num = 0  # keeps track of which batch you are at
    if cfg["cropping"]:
        cap.set_bbox(cfg["x1"], cfg["x2"], cfg["y1"], cfg["y2"])
    nx, ny = cap.dimensions

    frames = np.empty((batchsize, ny, nx, 3),
                      dtype="ubyte")  # this keeps all frames in a batch
    pbar = tqdm(total=nframes)
    counter = 0
    inds = []

    PredicteData = {}

    while cap.video.isOpened():
        frame = cap.read_frame(crop=cfg["cropping"])
        key = "frame" + str(counter).zfill(strwidth)
        if frame is not None:
            # Avoid overwriting data already on the shelf
            if key in feature_dict:
                continue

            frame = img_as_ubyte(frame)
            if frame.shape[-1] == 4:
                frame = rgba2rgb(frame)
            frames[batch_ind] = frame
            inds.append(counter)

            if batch_ind == batchsize - 1:

                preds = predict.predict_batched_peaks_and_costs(
                    dlc_cfg,
                    frames,
                    sess,
                    inputs,
                    outputs,
                    extra_dict=extra_dict)
                if not preds:
                    continue

                D, features = preds
                for i, (ind, data) in enumerate(zip(inds, D)):
                    PredicteData["frame" + str(ind).zfill(strwidth)] = data
                    raw_coords = assemblies.get(ind)
                    if raw_coords is None:
                        continue
                    fname = "frame" + str(ind).zfill(strwidth)
                    feature_dict[fname] = _get_features_dict(
                        raw_coords,
                        features[i],
                        dlc_cfg["stride"],
                    )

                batch_ind = 0
                inds.clear()
                batch_num += 1
            else:
                batch_ind += 1
        elif counter >= nframes:
            if batch_ind > 0:

                preds = predict.predict_batched_peaks_and_costs(
                    dlc_cfg,
                    frames,
                    sess,
                    inputs,
                    outputs,
                    extra_dict=extra_dict)
                if not preds:
                    continue

                D, features = preds
                for i, (ind, data) in enumerate(zip(inds, D)):
                    PredicteData["frame" + str(ind).zfill(strwidth)] = data
                    raw_coords = assemblies.get(ind)
                    if raw_coords is None:
                        continue
                    fname = "frame" + str(ind).zfill(strwidth)
                    feature_dict[fname] = _get_features_dict(
                        raw_coords,
                        features[i],
                        dlc_cfg["stride"],
                    )

            break
        counter += 1
        pbar.update(1)

    cap.close()
    pbar.close()
    feature_dict.close()
    PredicteData["metadata"] = {
        "nms radius":
        dlc_cfg["nmsradius"],
        "minimal confidence":
        dlc_cfg["minconfidence"],
        "sigma":
        dlc_cfg.get("sigma", 1),
        "PAFgraph":
        dlc_cfg["partaffinityfield_graph"],
        "PAFinds":
        dlc_cfg.get("paf_best",
                    np.arange(len(dlc_cfg["partaffinityfield_graph"]))),
        "all_joints": [[i] for i in range(len(dlc_cfg["all_joints"]))],
        "all_joints_names": [
            dlc_cfg["all_joints_names"][i]
            for i in range(len(dlc_cfg["all_joints"]))
        ],
        "nframes":
        nframes,
    }
    return PredicteData, nframes
Exemple #20
0
    def create_bags_one_type(self, dir_list):
        """Create bags containing only one type of nucleus."""
        bag_list = []
        labels_list = []
        for dir in dir_list:
            # Get image name
            img_name = dir.split('/')[-1]

            # bmp to pillow
            img_dir = dir + '/' + img_name + '.bmp'
            img = io.imread(img_dir)

            if img.shape[2] == 4:
                img = color.rgba2rgb(img)

            if self.location_info:
                xs = np.arange(0, 500)
                xs = np.asarray([xs for i in range(500)])
                ys = xs.transpose()
                img = np.dstack((img, xs, ys))

            # crop nucleus_type cells
            dir_nucleus_type = dir + '/' + img_name + '_' + self.nucleus_type + '.mat'
            with open(dir_nucleus_type, 'rb') as f:
                mat_nucleus_type = scipy.io.loadmat(f)

            cropped_cells = []
            for (x, y) in mat_nucleus_type['detection']:
                x = np.round(x)
                y = np.round(y)

                if self.data_augmentation:
                    x = x + np.round(np.random.normal(0, 3, 1))
                    y = y + np.round(np.random.normal(0, 3, 1))

                if x < 13:
                    x_start = 0
                    x_end = 27
                elif x > 500 - 14:
                    x_start = 500 - 27
                    x_end = 500
                else:
                    x_start = x - 13
                    x_end = x + 14

                if y < 13:
                    y_start = 0
                    y_end = 27
                elif y > 500 - 14:
                    y_start = 500 - 27
                    y_end = 500
                else:
                    y_start = y - 13
                    y_end = y + 14

                cropped_cells.append(img[int(y_start):int(y_end), int(x_start):int(x_end)])

            # if image doesn't contain any specific type nucleus, move to the next image
            if cropped_cells == []:
                continue

            # generate bag
            bag = cropped_cells

            # store single cell labels
            if self.nucleus_type == 'epithelial':
                labels = np.ones(len(cropped_cells))
            else:
                labels = np.zeros(len(cropped_cells))

            # shuffle
            if self.shuffle_bag:
                zip_bag_labels = list(zip(bag, labels))
                random.shuffle(zip_bag_labels)
                bag, labels = zip(*zip_bag_labels)

            # append every bag two times if training
            if self.train:
                for _ in [0, 1]:
                    bag_list.append(bag)
                    labels_list.append(labels)
            else:
                bag_list.append(bag)
                labels_list.append(labels)

            # bag_list.append(bag)
            # labels_list.append(labels)

        return bag_list, labels_list
def GetPoseandCostsS(cfg, dlc_cfg, sess, inputs, outputs, cap, nframes,
                     shelf_path):
    """Non batch wise pose estimation for video cap."""
    strwidth = int(np.ceil(np.log10(nframes)))  # width for strings
    if cfg["cropping"]:
        cap.set_bbox(cfg["x1"], cfg["x2"], cfg["y1"], cfg["y2"])

    if shelf_path:
        db = shelve.open(
            shelf_path,
            protocol=pickle.DEFAULT_PROTOCOL,
        )
    else:
        db = dict()
    db["metadata"] = {
        "nms radius":
        dlc_cfg["nmsradius"],
        "minimal confidence":
        dlc_cfg["minconfidence"],
        "sigma":
        dlc_cfg.get("sigma", 1),
        "PAFgraph":
        dlc_cfg["partaffinityfield_graph"],
        "PAFinds":
        dlc_cfg.get("paf_best",
                    np.arange(len(dlc_cfg["partaffinityfield_graph"]))),
        "all_joints": [[i] for i in range(len(dlc_cfg["all_joints"]))],
        "all_joints_names": [
            dlc_cfg["all_joints_names"][i]
            for i in range(len(dlc_cfg["all_joints"]))
        ],
        "nframes":
        nframes,
    }
    pbar = tqdm(total=nframes)
    counter = 0
    while cap.video.isOpened():
        frame = cap.read_frame(crop=cfg["cropping"])
        key = "frame" + str(counter).zfill(strwidth)
        if frame is not None:
            # Avoid overwriting data already on the shelf
            if isinstance(db, shelve.Shelf) and key in db:
                continue
            frame = img_as_ubyte(frame)
            if frame.shape[-1] == 4:
                frame = rgba2rgb(frame)
            dets = predict.predict_batched_peaks_and_costs(
                dlc_cfg,
                np.expand_dims(frame, axis=0),
                sess,
                inputs,
                outputs,
            )
            db[key] = dets[0]
            del dets
        elif counter >= nframes:
            break
        counter += 1
        pbar.update(1)

    pbar.close()
    try:
        db.close()
    except AttributeError:
        pass
    return db, nframes
Exemple #22
0
def analyze(imgpath, model):

    imgname = os.path.basename(imgpath)
    kernelSize = 64
    kernelStepSize = 1
    bufferVal = 8  # will load kernelSize x bufferVal
    stepsize = 1
    starttime = time.time()

    slide = openslide.open_slide(imgpath)
    #tissue detection
    thumbnail = np.array(
        slide.get_thumbnail((slide.level_dimensions[0][0] / 64,
                             slide.level_dimensions[0][1] / 64)))
    thumbnailGray = color.rgb2gray(thumbnail)
    val = filters.threshold_otsu(thumbnailGray)
    tissueMask = thumbnailGray < max(val, 0.8)
    plt.imsave('tissue.png', tissueMask)  #save the thumb of tissue mask

    buffersize = kernelSize * bufferVal
    resultMask = thumbnail.astype(numpy.uint8) * 0
    if stepsize > 1:
        resultMask = np.resize(
            resultMask,
            (int(resultMask.shape[0] / stepsize),
             int(resultMask.shape[1] / stepsize), resultMask.shape[2]))
    counter1 = 0
    counter2 = 0
    expectedStep = tissueMask.shape[0] / bufferVal
    outputsVec = []
    for i in range(0, tissueMask.shape[0] - bufferVal, bufferVal):  #Height

        curMod = i % (bufferVal * max(5, int(expectedStep / 20)))
        if curMod == 0:
            print('.', end='', flush=True)
        for j in range(0, tissueMask.shape[1] - bufferVal, bufferVal):  #Width

            if np.mean(tissueMask[i:i + bufferVal, j:j + bufferVal]) < (
                    8 / 16):  #most of them are background
                continue
            bigTile = numpy.array(
                slide.read_region((j * kernelSize, i * kernelSize), 0,
                                  [buffersize, buffersize]))
            bigTile = color.rgba2rgb(bigTile)
            sz = bigTile.itemsize
            h, w, cs = bigTile.shape
            bh, bw = kernelSize, kernelSize
            shape = (int(h / bh / stepsize), int(w / bw / stepsize), bh, bw,
                     cs)
            strides = (stepsize * w * bh * sz * cs, stepsize * sz * cs * bw,
                       w * sz * cs, sz * cs, sz)
            blocks = np.lib.stride_tricks.as_strided(bigTile,
                                                     shape=shape,
                                                     strides=strides)
            blocks = blocks.reshape(blocks.shape[0] * blocks.shape[1],
                                    blocks.shape[2], blocks.shape[3],
                                    blocks.shape[4])
            predictions = model.predict(blocks)
            outputsVec = outputsVec + predictions
            qwe = np.array(predictions)
            qwe = qwe.reshape(int(h / bh / stepsize), int(w / bw / stepsize),
                              2)
            counter1 = counter1 + sum(np.array(predictions)[:, 1] > 0.5)
            counter2 = counter2 + len(predictions) - sum(
                np.array(predictions)[:, 1] > 0.5)
            resultMask[int(i / stepsize):int((i + bufferVal) / stepsize),
                       int(j / stepsize):int((j + bufferVal) / stepsize),
                       0] = 255 * qwe[:, :, 1]
            resultMask[int(i / stepsize):int((bufferVal + i) / stepsize),
                       int(j / stepsize):int((bufferVal + j) / stepsize),
                       1] = 255 * qwe[:, :, 0]
    endtime = time.time()
    elapsedtime = endtime - starttime
    print('elapsed time ' + str(elapsedtime))
    outputname = 'output/' + imgname + '-f' + str(counter2) + '-o' + str(
        counter1) + '.png'
    plt.imsave(outputname, resultMask, cmap=plt.cm.gray)
    outputname2 = 'output/' + imgname + '-f' + str(counter2) + '-o' + str(
        counter1) + '.csv'
    writeXML(outputname2, outputsVec)
    return (counter2, counter1)
Exemple #23
0
def recommend_similarity_item(item):
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  
  ### hyper parameter
  k     = 3    ### 출력할 유사한 item의 갯수
  rows  = 1    ### 출력할 item image의 행
  cols  = k    ### 출력할 item image의 열
  space = 1    ### 출력할 item이 놓일 위치

  ### 경로 설정
  root_path    = "../../experiment/"    ### file 경로의 공통된 root_path      

  feature_path = "data/polyvore/dataset/"    ### feature vector를 저장한 파일의 위치

  train_json = 'data/polyvore/jsons/train_no_dup.json'    ### train data의 위치
  valid_json = 'data/polyvore/jsons/valid_no_dup.json'    ### validation data의 위치
  test_json  = 'data/polyvore/jsons/test_no_dup.json'     ### test data의 위치

  image_folder_path = 'data/polyvore/images/'    ### dataset의 image들이 저장된 위치

  ### item 간의 유사도를 계산하기 위한 cosine similarity 함수
  def cos_sim(A, B):
    return np.dot(A,B)/(np.linalg.norm(A)*np.linalg.norm(B))

  ### 미리 학습한 training set에 있는 item들의 feature vector 불러오기
  with open(root_path+feature_path+'imgs_featdict_train.pkl', 'rb') as train_feat:
    train_data = pickle.load(train_feat)

  ### 미리 학습한 validation set에 있는 item들의 feature vector 불러오기
  with open(root_path+feature_path+'imgs_featdict_valid.pkl', 'rb') as valid_feat:
    valid_data = pickle.load(valid_feat)
    
  ### 미리 학습한 test set에 있는 item들의 feature vector 불러오기
  with open(root_path+feature_path+'imgs_featdict_test.pkl', 'rb') as test_feat:
    test_data = pickle.load(test_feat)

  save_feat = []     ### save_id: 각 item을 구별할 수 있는 id만 저장하기 위한 list
  save_id   = []     ### save_feat: 각 item의 feature vector만 저장하기 위한 list

  ### train data에 대하여...
  for key, value in train_data.items():
    save_feat.append(value)
    save_id.append(key)

  ### validation data에 대하여...
  for key, value in valid_data.items():
    save_feat.append(value)
    save_id.append(key)

  ### test data에 대하여...
  for key, value in test_data.items():
    save_feat.append(value)
    save_id.append(key)

  print('nums of total item: {}\n'.format(len(save_id)))

  ###############################################
  ### 모든 item들의 id와 feature vector를 저장 ###
  ###############################################
  data_dict = {}    ### data_dict은 전체 data에 있는 item에 대하여
                    ### key: item_id, value: feature vector를 갖는다.
  
  for i in range(len(save_id)):
    data_dict[save_id[i]] = save_feat[i]
  ###############################################

  ###############################################################################
  ###                         user가 등록한 이미지 출력                        ###
  ###############################################################################
  ### 임의의 user item 등록
  item_path = item

  ### 등록 이미지 출력
  user_img = cv2.imread(item_path)
  user_img = cv2.resize(user_img, dsize=(256,256), interpolation=cv2.INTER_AREA)
  user_img = plt.imshow(cv2.cvtColor(user_img, cv2.COLOR_BGR2RGB))
  plt.axis("off")
  ###############################################################################

  print("Start loading pretrained resnet50 model...")
  model = models.resnet50(pretrained=True)
  model = nn.Sequential(*list(model.children())[:-1])   
  model = model.to(device)
  model.eval()
  print("Finish loading pretrained restnet50 model!!!\n")

  normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225])

  transform = transforms.Compose([
              transforms.ToPILImage(),
              transforms.Resize(256), transforms.CenterCrop(224),
              transforms.ToTensor(), normalize
              ])

  ### resnet50을 통해 user image의 feature vector를 추출하는 함수
  def process_image(im):
    im  = transform(im)
    im  = im.unsqueeze_(0)
    im  = im.to(device)
    out = model(im)
    return out.squeeze()

  print("Start extract user items feature vector")
  with torch.no_grad():
    im = skimage.io.imread(item_path)
    if len(im.shape) == 2:
      im = gray2rgb(im)
    if im.shape[2] == 4:
      im = rgba2rgb(im)
    
    im = resize(im, (256,256))
    im = img_as_ubyte(im)
    
    feats = process_image(im).cpu().numpy()
  print("Finish extract user items feature vector\n")

  res_list = []    ### res_list에는 user의 item과 data의 item의 유사도를 저장하는 list  
  res_dict = {}    ### key: polyvore item, value: similarity score

  print("Start calculating similarity scores...")
  for i in range(len(save_feat)):
    res = cos_sim(feats,save_feat[i])
    res_list.append(res)
    res_dict[save_id[i]] = res_list[i]
  print("Finish calculating simiarity scores!!!\n")
  
  print("Start sorting similarity score...")
  sort_list = sorted(res_dict.items(), key=lambda item:item[1], reverse=True)
  print("Finish sorting similarity score!!!\n")
  
  best_sim_list = []    ### k개의 가장 유사성이 높은 item을 저장하는 list
  
  for i in range (k):
    best_sim_list.append(sort_list[i][0])

  ######################################################
  ###                json data load                  ###
  ######################################################
  ### train.json에 대하여...
  with open(root_path+train_json, "r") as train_file:
    train_in_outfit_id = json.load(train_file)

  ### valid.json에 대하여...
  with open(root_path+valid_json, "r") as valid_file:
    valid_in_outfit_id = json.load(valid_file)

  ### test.json에 대하여...
  with open(root_path+test_json, "r") as test_file:
    test_in_outfit_id = json.load(test_file)
  #######################################################

  outfit2item  = {}    ### outfit2item은 outfit을 조합하는 item을 저장한다.
                       ### key: outfit_id, value: outfit을 구성하는 item id들
  outfit2index = {}    ### outf2index는 outfit을 조합하는 index를 저장한다.
                       ### key: outfit_id, value: outfit을 구성하는 index들

  ################################################################################
  ###         각 outfit 마다 item_id와 index를 딕셔너리 형태로 각각 저장         ###
  ################################################################################
  ### 17,316개의 outfit을 갖고 있는 train set에ㅔ 대하여...
  for i in range(len(train_in_outfit_id)):    ### len(train_in_outfit_id: 17316)
    outfit_id = train_in_outfit_id[i]["set_id"]
    
    item_index = []    ### train data에서 outfit을 구성하는 index를 저장하는 list
    item_id    = []    ### train data에서 outfit을 구성하는 item id를 저장하는 list
    
    for j in range(len(train_in_outfit_id[i]["items"])):
      index = train_in_outfit_id[i]["items"][j]["index"]
      item_index.append(index)

      _, each_item_id = train_in_outfit_id[i]["items"][j]["image"].split('id=')
      item_id.append(each_item_id)
        
    outfit2index[outfit_id] = item_index
    outfit2item[outfit_id]  = item_id

  ### 1,497개의 outfit을 갖고 있는 validation set에 대하여...
  for i in range(len(valid_in_outfit_id)):    ### len(train_in_outfit_id: 17316)
    outfit_id = valid_in_outfit_id[i]["set_id"]
    
    item_index = []    ### validation data에서 outfit을 구성하는 index를 저장하는 list
    item_id    = []    ### validation data에서 outfit을 구성하는 item id를 저장하는 list  

    for j in range(len(valid_in_outfit_id[i]["items"])):
      index = valid_in_outfit_id[i]["items"][j]["index"]
      item_index.append(index)

      _, each_item_id = valid_in_outfit_id[i]["items"][j]["image"].split('id=')
      item_id.append(each_item_id)
      
    outfit2index[outfit_id] = item_index
    outfit2item[outfit_id]  = item_id
      
  ### 3,076개의 outfit을 갖고 있는 test set에 대하여....
  for i in range(len(test_in_outfit_id)):    ### len(train_in_outfit_id: 17316)
    outfit_id = test_in_outfit_id[i]["set_id"]
    
    item_index = []    ### test data에서 outfit을 구성하는 index를 저장하는 list
    item_id    = []    ### test data에서 outfit을 구성하는 item id를 저장하는 list
  
    for j in range(len(test_in_outfit_id[i]["items"])):
      index = test_in_outfit_id[i]["items"][j]["index"]
      item_index.append(index)

      _, each_item_id = test_in_outfit_id[i]["items"][j]["image"].split('id=')
      item_id.append(each_item_id)
        
    outfit2index[outfit_id] = item_index
    outfit2item[outfit_id] = item_id
  ################################################################################

  outfit_id = []    ### outfit의 id를 저장하는 list
  index_id  = []    ### outfit의 index 갯수를 저장하는 list

  for i in range(k):
    for key in outfit2item.keys():
      for j in range(len(outfit2item[key])):
        if best_sim_list[i] == outfit2item[key][j]:
          outfit_id.append(str(key))
          index_id.append(str(j+1))
    
  sim_save_file = []    ### 유사한 item의 image 파일 경로를 저장하는 list
  for i in range(k):
      sim_save_file.append(image_folder_path+outfit_id[i]+'/'+index_id[i]+'.jpg')

  ### http://blog.daum.net/geoscience/1263 참고
  fig = plt.figure()

  for i in range(k):
    img = cv2.imread(root_path+sim_save_file[i])
    img = cv2.resize(img, dsize=(256,256), interpolation=cv2.INTER_AREA)
    ax = fig.add_subplot(rows,cols,space)
    ax.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    space += 1
    ax.axis("off")
  plt.show()
from skimage import io
imagergba = io.imread("/Users/sameriksson/temp/screensgen/1.png")
from skimage.color import rgba2rgb
image = rgba2rgb(imagergba)
from skimage import transform
img = transform.resize(image, (224, 224))  #224
import matplotlib.pyplot as plt
plt.imshow(img)
plt.show()
numpy_image = img.transpose((2, 0, 1))
print(numpy_image.shape)
import torch.nn as nn
conv1 = nn.Conv2d(3, 6, 5)
print(conv1)
#Conv2d(3, 6, kernel_size=(16, 16), stride=(1, 1))
import torch
tensor_image = torch.from_numpy(numpy_image)
tensor_images = tensor_image.unsqueeze(0)
conv1 = conv1.double()
c = conv1(tensor_images)
print(c.shape)
import torch.nn.functional as F
b = F.relu(c)
pool = nn.MaxPool2d(2, 2)
d = pool(b)
print(d.shape)
conv2 = nn.Conv2d(6, 16, 5)
conv2 = conv2.double()
e = conv2(d)
print(e.shape)
f = F.relu(e)
Exemple #25
0
    D = np.loadtxt('dltrainfiles/dl4_rgb_ds16_maps.txt')
    # D = np.loadtxt('dl8_rgb_ds192.txt')
    # D = io.imread('dict.png').astype(float) / 65535
    # dl.visualize_dictionary(D, 16, 8)

    dir_images = "/home/eduardo/Imagens/*.png"
    images = glob.glob(dir_images)

    for i, img in enumerate(images):
        print(i, img)
    imgidx = int(input("Escolha do id da imagem: "))

    # img_train = io.imread(images[imgidx], as_grey=True)
    image = io.imread(images[imgidx])
    if image.shape[-1] == 4:
        image = color.rgba2rgb(image)

    if image.max() > 1.0:
        img_train = image / 255.
    else:
        img_train = image
    # img_train = io.imread(images[imgidx])[:,:,:3]
    nl, nc, _ = img_train.shape
    ml = nl % m11
    mc = nc % m22
    img_train = img_train[:(nl - ml), :(nc - mc), :].astype(float)
    # print(color.rgba2rgb(io.imread(images[imgidx]))[0,0])

    # io.imshow(color.yuv2rgb(img_train))
    # io.show()
Exemple #26
0
def predict(model: nn.Module, slide_reader: OpenSlide, img_id, bbox,
            scale_factor):
    """

    :param model:
    :param slide_reader:
    :param img_id:
    :param bbox:
    :param scale_factor:
    :return:
    """

    minr, minc, maxr, maxc = bbox
    scf_row, scf_col = scale_factor
    row_st_origin, col_st_origin = int(minr * scf_row), int(minc * scf_col)
    row_ed_origin, col_ed_origin = int(maxr * scf_row), int(maxc * scf_col)

    row_length, col_length = row_ed_origin - row_st_origin, col_ed_origin - col_st_origin

    if row_length < 500 or col_length < 500:
        return

    # print('read region')
    # print(row_st_origin, col_st_origin)
    # print((col_length + (alpha - 1) * PATCH_STRIDE, row_length + (alpha - 1) * PATCH_STRIDE))
    if (row_length - PATCH_SIZE - (alpha - 1) * SD) % PATCH_STRIDE != 0:
        row_ed_origin += PATCH_STRIDE - (row_length - PATCH_SIZE -
                                         (alpha - 1) * SD) % PATCH_STRIDE
    if (col_length - PATCH_SIZE - (alpha - 1) * SD) % PATCH_STRIDE != 0:
        col_ed_origin += PATCH_STRIDE - (col_length - PATCH_SIZE -
                                         (alpha - 1) * SD) % PATCH_STRIDE
    # dpt_table
    # whole_dpt = []
    # cm_hot = mpl.cm.get_cmap('bwr')
    cm_hot = mpl.cm.get_cmap('coolwarm')

    patch_loader = data_utils.DataLoader(dataset=PredictPatchLoader(
        slide_reader, row_st_origin, row_ed_origin, col_st_origin,
        col_ed_origin, PATCH_SIZE, PATCH_STRIDE, alpha, SD, img_id),
                                         batch_size=1,
                                         num_workers=3)
    cols = (col_ed_origin - col_st_origin - PATCH_SIZE -
            (alpha - 1) * SD) // PATCH_STRIDE + 1
    all_dpt = []
    dpt_table_row = []
    t = time_sp()
    for data in patch_loader:
        # t.time_span('转为Tensor')
        # row_idx = idx // cols
        # col_idx = idx % cols
        data.squeeze_()
        opt_table = []
        with torch.no_grad():
            data = data.float()
            for opt_row in range(alpha):
                opt_table.append([])
                for opt_col in range(alpha):
                    current_data = data[opt_row * alpha + opt_col, :, :, :]
                    current_data.unsqueeze_(0)
                    # print(current_data.shape)
                    # t.time_span('Tensor 切片')
                    outputs = model(current_data)
                    opts = nn.functional.softmax(outputs, dim=1).cpu().numpy()
                    opts = opts[0, 1, :, :]
                    # print(opts.shape)
                    opt_table[-1].append(opts)

        # t.time_span('预测耗时')
        dpt_table = merge_opt(opt_table)
        # t.time_span('Merge OPT')
        dpt_table_row.append(dpt_table)

        if len(dpt_table_row) == cols:
            row_img = np.concatenate(dpt_table_row, axis=1)
            all_dpt.append(row_img)
            # for each in all_dpt:
            #     print(each.shape)
            # print(f'all_apt len {len(all_dpt)}')
            dpt_table_row = []

    if len(all_dpt) == 0:
        print(bbox)
        return
    whole_pred = np.concatenate(all_dpt)
    # with open('dpt.dump', 'wb') as outfile:
    #     pickle.dump(whole_pred, outfile)
    htop_map = cm_hot(whole_pred)

    dims = slide_reader.level_dimensions
    down_level = -5
    fr = dims[0][1] / dims[down_level][1]
    fc = dims[0][0] / dims[down_level][0]
    # d = int( PATCH_SIZE + (alpha-1) * PATCH_STRIDE / fc)

    # print(
    #     (col_st_origin , row_st_origin),
    #     len(dims) - 5,
    #     (int((col_ed_origin - col_st_origin) // fc), int((row_ed_origin - row_st_origin) // fr))
    # )
    region_img = slide_reader.read_region(
        (col_st_origin, row_st_origin),
        len(dims) + down_level,
        (int((col_ed_origin - col_st_origin) // fc),
         int((row_ed_origin - row_st_origin) // fr))).convert('RGB')

    # region_img = thumbnail[minr:maxr, minc:maxc]
    tr, tc = whole_pred.shape

    region_img = np.asarray(region_img)
    region_img = transform.resize(region_img, (tr, tc))
    # region_img = np.resize(region_img, (tr, tc, 3))

    save_image = 0.7 * region_img + 0.3 * color.rgba2rgb(htop_map)
    save_image = save_image.clip(0, 1.0)
    info = '_'.join(
        map(str, (row_st_origin, col_st_origin, row_ed_origin - row_st_origin,
                  col_ed_origin - col_st_origin)))
    if not path.exists(f'pred/{img_id}'):
        os.mkdir(f'pred/{img_id}')
    np.save(f'pred/{img_id}/{info}.npy', whole_pred)
    io.imsave(f'pred/{img_id}/{info}_gray.bmp', whole_pred)
    io.imsave(f'pred/{img_id}/{info}_region.bmp', region_img)
    io.imsave(f'pred/{img_id}/{info}_heatmap.bmp', htop_map)
    io.imsave(f'pred/{img_id}/{info}.bmp', save_image)
 def load_img(self, idx):
     img_rgb_path = self.rgb_paths[idx]
     img = io.imread(img_rgb_path)
     img = color.rgba2rgb(img)
     # img = img / 255.
     return img
Exemple #28
0
def determine_skew_dev(
    image: ImageType,
    sigma: float = 3.0,
    num_peaks: int = 20,
    num_angles: int = 180
) -> Tuple[Optional[np.float64], List[List[np.float64]], np.float64, Tuple[
        ImageTypeUint64, List[List[np.float64]], ImageTypeFloat64], ]:
    """Calculate skew angle."""
    imagergb = rgba2rgb(image) if len(
        image.shape) == 3 and image.shape[2] == 4 else image
    img = rgb2gray(imagergb) if len(imagergb.shape) == 3 else imagergb
    edges = canny(img, sigma=sigma)
    out, angles, distances = hough_line(
        edges, np.linspace(-np.pi / 2, np.pi / 2, num_angles, endpoint=False))
    hough_line_out = (out, angles, distances)

    _, angles_peaks, _ = hough_line_peaks(out,
                                          angles,
                                          distances,
                                          num_peaks=num_peaks,
                                          threshold=0.05 * np.max(out))

    absolute_deviations = [_calculate_deviation(k) for k in angles_peaks]
    average_deviation: np.float64 = np.mean(np.rad2deg(absolute_deviations))
    angles_peaks_degree = [np.rad2deg(x) for x in angles_peaks]

    bin_0_45 = []
    bin_45_90 = []
    bin_0_45n = []
    bin_45_90n = []

    for angle in angles_peaks_degree:

        deviation_sum = int(90 - angle + average_deviation)
        if _compare_sum(deviation_sum):
            bin_45_90.append(angle)
            continue

        deviation_sum = int(angle + average_deviation)
        if _compare_sum(deviation_sum):
            bin_0_45.append(angle)
            continue

        deviation_sum = int(-angle + average_deviation)
        if _compare_sum(deviation_sum):
            bin_0_45n.append(angle)
            continue

        deviation_sum = int(90 + angle + average_deviation)
        if _compare_sum(deviation_sum):
            bin_45_90n.append(angle)

    angles = [bin_0_45, bin_45_90, bin_0_45n, bin_45_90n]
    nb_angles_max = 0
    max_angle_index = -1
    for angle_index, angle in enumerate(angles):
        nb_angles = len(angle)
        if nb_angles > nb_angles_max:
            nb_angles_max = nb_angles
            max_angle_index = angle_index

    if nb_angles_max:
        ans_arr = _get_max_freq_elem(angles[max_angle_index])
        angle = np.mean(ans_arr)
    elif angles_peaks_degree:
        ans_arr = _get_max_freq_elem(angles_peaks_degree)
        angle = np.mean(ans_arr)
    else:
        return None, angles, average_deviation, hough_line_out

    rot_angle = (angle + 45) % 90 - 45

    return rot_angle, angles, average_deviation, hough_line_out
Exemple #29
0
def extract_trajectories(
    image_filenames, point_method_kwargs={}, flow_method_kwargs={}
):
    """
    Times where no trajectory point are found will have index -1
    """
    traj_points = []
    traj_files = []
    img_prev_arr = None

    for fn in image_filenames:
        img_src = Image.open(fn)
        img_gray_arr = np.array(rgb2gray(rgba2rgb(np.array(img_src))))
        if img_prev_arr is None:
            points = shitomasi_detection(img_gray_arr, **point_method_kwargs)
            if len(points) < MIN_CORNERS:
                continue
            else:
                traj_points.append(points)
                traj_files.append(fn)
        else:
            points = traj_points[-1]
            xy_end = track_features(
                img_prev_arr, img_gray_arr, points=points, **flow_method_kwargs
            )
            traj_points.append(xy_end)
            traj_files.append(fn)

        img_prev_arr = img_gray_arr
    traj_points = np.array(traj_points)

    # NB: images use (x,y) indexing order rather than (i,j)
    ny_img, nx_img = img_gray_arr.shape

    # make all invalid points have index -1 and round to nearest integer
    np.nan_to_num(traj_points, copy=False, nan=-1.0)
    traj_points = traj_points.round().astype(int)

    # y-indexing is from top-left for images
    traj_points[..., 1] = ny_img - 1 - traj_points[..., 1]

    # now we need to clean up the points. Some will be outside the valid range
    # [0, Nx] and [0, Ny]
    m_invalid = np.logical_or(
        # out of x index bounds
        np.logical_or(traj_points[..., 0] < 0, traj_points[..., 0] >= nx_img),
        # out of y index bounds
        np.logical_or(traj_points[..., 1] < 0, traj_points[..., 1] >= ny_img),
    )
    # make these all -1 (indicating invalid point)
    traj_points[..., 0] = np.where(m_invalid, -1, traj_points[..., 0])
    traj_points[..., 1] = np.where(m_invalid, -1, traj_points[..., 1])

    N_img, N_trajs, _ = traj_points.shape

    assert len(traj_files) == N_img

    ds = xr.Dataset(coords=dict(image_filename=traj_files, traj_id=np.arange(N_trajs)))
    ds["i"] = ("image_filename", "traj_id"), traj_points[..., 0]
    ds["i"].attrs["long_name"] = "x-index"
    ds["i"].attrs["units"] = "1"
    ds["j"] = ("image_filename", "traj_id"), traj_points[..., 1]
    ds["j"].attrs["long_name"] = "j-index"
    ds["j"].attrs["units"] = "1"
    return ds
    edges = canny(img_hsv_hue)

    # fill holes
    fill_holes = ndi.binary_fill_holes(edges)

    # label, 填充
    structure = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
    label_objects, nb_labels = ndi.label(fill_holes, structure)

    # 剔除额外的标签
    sizes = np.bincount(label_objects.ravel())
    mask_sizes = sizes > (height * width) * 1e-5
    mask_sizes[0] = 0
    bin_img_cleaned = mask_sizes[label_objects]

    # 提取边界点
    return extract_edges_points(bin_img_cleaned)


if __name__ == "__main__":
    from skimage import io
    img = io.imread('./img1.png')
    channel = img.shape[2]
    if channel == 4:
        img_rgb = rgba2rgb(img)
    points, edges_mask = get_edges_from_colouration(img_rgb)

    # 结果呈现
    # plt.plot(points[0],points[1],'.')     # 根据坐标点画边界曲线
    show_with_mask(img_rgb, edges_mask)
            
            filename = f"{item['id']}.jpg"
            image_path = os.path.join(args.image_dir, filename)
            
            if os.path.exists(image_path):
                try:
                    img = skimage.io.imread(image_path)
                except Exception as e:
                    print(e)
                    continue
            else:
                continue
            
            img = np.array(img)
            if len(img.shape) == 2:
                img = gray2rgb(img)
            if img.shape[2] == 4:
                img = rgba2rgb(img)

            img = resize(img, (256, 256))
            img = img_as_ubyte(img)

            feats = process_image(img).cpu().numpy()

            features[item['id']] = feats

print(f'Total: {len(features)}')

with open(save_dict, 'wb') as handle:
    pkl.dump(features, handle, protocol=pkl.HIGHEST_PROTOCOL)
Exemple #32
0
    # filter images
    if 0 < n_images < len(data_paths):
        data_paths = data_paths[:n_images]
    # repeat data
    elif n_images > len(data_paths):
        repeats = n_images // len(data_paths) + 1
        data_paths = base_data_paths * repeats
        data_paths = data_paths[:n_images]

    with Timer(store=stores.get("data pre-processing", []),
               ignore=time_inference):
        # read images to numpy arrays
        data = [io.imread(str(d)) for d in data_paths]
        # rgba to rgb
        data = [
            im if im.shape[-1] == 3 else uint8(rgba2rgb(im) * 255)
            for im in data
        ]
        # resize images to target_size or
        if keep_ratio:
            # to closest multiples of 128 <= max_im_width, keeping aspect ratio
            new_sizes = [to_128(d, max_im_width) for d in data]
            data = [
                resize(d, ns, anti_aliasing=True)
                for d, ns in zip(data, new_sizes)
            ]
        else:
            # to args.target_size
            data = [resize_and_crop(d, target_size) for d in data]
            new_sizes = [(target_size, target_size) for _ in data]
        # resize() produces [0, 1] images, rescale to [-1, 1]