Exemplo n.º 1
0
def test_jpg_more():
    need_internet()

    # Test broken JPEG
    fname = fnamebase + "_broken.jpg"
    open(fname, "wb").write(b"this is not an image")
    raises(Exception, imageio.imread, fname)
    #
    bb = imageio.imsave(imageio.RETURN_BYTES, get_ref_im(3, 0, 0), "JPEG")
    with open(fname, "wb") as f:
        f.write(bb[:400])
        f.write(b" ")
        f.write(bb[400:])
    raises(Exception, imageio.imread, fname)

    # Test EXIF stuff
    fname = get_remote_file("images/rommel.jpg")
    im = imageio.imread(fname)
    assert im.shape[0] > im.shape[1]
    im = imageio.imread(fname, exifrotate=False)
    assert im.shape[0] < im.shape[1]
    im = imageio.imread(fname, exifrotate=2)  # Rotation in Python
    assert im.shape[0] > im.shape[1]
    # Write the jpg and check that exif data is maintained
    if sys.platform.startswith("darwin"):
        return  # segfaults on my osx VM, why?
    imageio.imsave(fnamebase + "rommel.jpg", im)
    im = imageio.imread(fname)
    assert im.meta.EXIF_MAIN
Exemplo n.º 2
0
    def __init__(self, img, ismask=False, transparent=True,
                 fromalpha=False, duration=None):
        VideoClip.__init__(self, ismask=ismask, duration=duration)

        if PY3:
           if isinstance(img, str):
              img = imread(img)
        else:
           if isinstance(img, (str, unicode)):
              img = imread(img)

        if len(img.shape) == 3:  # img is (now) a RGB(a) numpy array

            if img.shape[2] == 4:
                if fromalpha:
                    img = 1.0 * img[:, :, 3] / 255
                elif ismask:
                    img = 1.0 * img[:, :, 0] / 255
                elif transparent:
                    self.mask = ImageClip(
                        1.0 * img[:, :, 3] / 255, ismask=True)
                    img = img[:, :, :3]
            elif ismask:
                img = 1.0 * img[:, :, 0] / 255

        # if the image was just a 2D mask, it should arrive here
        # unchanged
        self.make_frame = lambda t: img
        self.size = img.shape[:2][::-1]
        self.img = img
Exemplo n.º 3
0
def maybe_preprocess(data_dir):

    npz_file1 = os.path.join(data_dir, 'train.npy')
    npz_file2 = os.path.join(data_dir, 'test.npy')
    if os.path.exists(npz_file1) and os.path.exists(npz_file2):
        return # all good

    
    trainx = []
    train_dir = os.path.join(data_dir, 'train')
    for f in os.listdir(train_dir):
        if f.endswith('.png'):
            #print('reading', f)
            filepath = os.path.join(train_dir, f)
            trainx.append(imread(filepath).reshape((1,32,32,3)))
    trainx = np.concatenate(trainx, axis=0)
    

    testx = []
    test_dir = os.path.join(data_dir, 'test')
    for f in os.listdir(test_dir):
        if f.endswith('.png'):
            #print('reading', f)
            filepath = os.path.join(test_dir, f)
            testx.append(imread(filepath).reshape((1,32,32,3)))
    testx = np.concatenate(testx, axis=0)

    np.save(npz_file1, trainx)
    np.save(npz_file2, testx)
Exemplo n.º 4
0
def example():
   
   from matplotlib import pyplot as plt
   import numpy as np 

   # todo: read the example image from the papers source directory   
   image1=imread(os.path.join(wrapper.source_directory,'data','tsukuba0.png')) 
   image2=imread(os.path.join(wrapper.source_directory,'data','tsukuba1.png')) 
  

   output=wrapper.stereoGuidedFilter(image1,image2,dmin=-15,dmax=0)
   print ('found disparities :')
   v,c=np.unique(output['disparity'],return_counts=True)
   plt.plot(v,c)
   plt.title('nb pixel per disparity')
   # todo: display the results
   plt.figure()
   plt.subplot(2,2,1)   
   plt.imshow(output['disparity'].astype(np.float),cmap='Greys_r')
   plt.subplot(2,2,2)   
   plt.imshow(output['occlusion'].astype(np.float),cmap='Greys_r')
   plt.subplot(2,2,3)   
   plt.imshow(output['occlusion_filled'].astype(np.float),cmap='Greys_r')
   plt.subplot(2,2,4)   
   plt.imshow(output['occlusion_filled_smoothed'].astype(np.float),cmap='Greys_r')
   plt.show()
   print ('done' )
Exemplo n.º 5
0
def test_defaults():
    path = 'mlxtend/image/tests/data/'
    eyepad = EyepadAlign()
    target_image = imageio.imread(os.path.join(path,
                                               'celeba-subset/000001.jpg'))
    eyepad.fit_image(target_image)

    img = imageio.imread(os.path.join(path, 'lena-small.png'))
    img_tr = eyepad.transform(img)

    landmarks_tr = extract_face_landmarks(img_tr)

    true_vals = np.array([[35, 113],
                          [33, 126],
                          [34, 140],
                          [36, 154],
                          [41, 166],
                          [51, 176],
                          [61, 184],
                          [72, 189],
                          [82, 190],
                          [90, 186]], dtype=np.int32)

    if os.name == 'nt':
        # on windows, imageio parses jpgs sometimes differently so pixel values
        # maybe slightly different
        assert np.sum(np.abs(landmarks_tr[:10] - true_vals) > 2) == 0
    else:
        assert np.sum(np.abs(landmarks_tr[:10] - true_vals) > 0) == 0, \
                np.sum(np.abs(landmarks_tr[:10] - true_vals))
        np.testing.assert_array_equal(landmarks_tr[:10], true_vals)
Exemplo n.º 6
0
def test_ico():
    
    for float in (False, True):
        for crop in (0, 1, 2):
            for colors in (0, 1, 3, 4):
                fname = fnamebase + '%i.%i.%i.ico' % (float, crop, colors)
                rim = get_ref_im(colors, crop, float)
                imageio.imsave(fname, rim)
                im = imageio.imread(fname)
                mul = 255 if float else 1
                assert_close(rim * mul, im, 0.1)  # lossless
    
    # Meta data
    R = imageio.read(fnamebase + '0.0.0.ico')
    assert isinstance(R.get_meta_data(0), dict)
    assert isinstance(R.get_meta_data(None), dict)  # But this print warning
    writer = imageio.save(fnamebase + 'I.ico')
    writer.set_meta_data({})
    writer.close()
    
    # Parameters. Note that with makealpha, RGBA images are read in incorrectly
    im = imageio.imread(fnamebase + '0.0.0.ico', makealpha=True)
    assert im.ndim == 3 and im.shape[-1] == 4
    
    # Parameter fail
    raises(TypeError, imageio.imread, fname, notavalidkwarg=True)
    raises(TypeError, imageio.imsave, fnamebase + '1.gif', im, notavalidk=True)
    
    # Multiple images
    im = get_ref_im(4, 0, 0)
    ims1 = im, np.column_stack([im, im]), np.row_stack([im, im])
    imageio.mimsave(fnamebase + 'I.ico', ims1)
    ims2 = imageio.mimread(fnamebase + 'I.ico')
    for im1, im2 in zip(ims1, ims2):
        assert_close(im1, im2, 0.1)
Exemplo n.º 7
0
def compare_images(ref_fname, act_fname, no_print=True, isize=None):
    """Compare two images by calculating Manhattan and Zero norms."""
    # Source: http://stackoverflow.com/questions/189943/
    # how-can-i-quantify-difference-between-two-images
    ref_img = Image.open(ref_fname)
    act_img = Image.open(act_fname)
    if (ref_img.size != act_img.size) or ((isize and (act_img.size != isize))):
        m_norm, z_norm = 2 * [2 * IMGTOL]
    else:
        # Element-wise for Scipy arrays
        ref_img = imread(ref_fname).astype(float)
        act_img = imread(act_fname).astype(float)
        diff = ref_img - act_img
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", category=RuntimeWarning)
            # Manhattan norm
            m_norm = scipy.sum(np.abs(diff))
            # Zero norm
            z_norm = scipy.linalg.norm(diff.ravel(), 0)
    result = bool((m_norm < IMGTOL) and (z_norm < IMGTOL))
    if not no_print:
        print(
            "Image 1: {0}, Image 2: {1} -> ({2}, {3}) [{4}]".format(
                ref_fname, act_fname, m_norm, z_norm, result
            )
        )
    return result
Exemplo n.º 8
0
def example():
   
   from matplotlib import pyplot as plt
   import numpy as np 

   # todo: read the example image from the papers source directory   
   image1=imread(wrapper.source_directory+'/adam1.png') 
   image2=imread(wrapper.source_directory+'/adam2.png')
   
   # todo: call the wrapped function(s) with a valid set of arguments
   matches,keys1,keys2=wrapper.asift(image1,image2)

   # todo: display the results
   fig = plt.figure(figsize=(10,5))
   ax1 = fig.add_subplot(121)
   plt.imshow(image1,cmap='Greys_r') 
   ax2 = fig.add_subplot(122)
   plt.imshow(image2,cmap='Greys_r')   


   for x1,y1,x2,y2 in matches[::1,:]:
      con = ConnectionPatch(xyA=(x2, y2), xyB=(x1,y1),  coordsA="data", coordsB="data"  ,
                            axesA=ax2, axesB=ax1,
                            shrinkB=5,color='r')
      ax2.add_artist(con)
   plt.draw()
   plt.show()

   print ('done' )
Exemplo n.º 9
0
def test_singleton():

    im1 = imageio.imread("imageio:chelsea.png")

    fname = os.path.join(test_dir, "chelsea.bsdf")
    imageio.imsave(fname, im1)

    # Does it look alright if we open it in bsdf without extensions?
    raw = bsdf.load(fname, [])
    assert isinstance(raw, dict)
    assert set(raw.keys()) == set(["meta", "array"])
    assert isinstance(raw["meta"], dict)
    assert isinstance(raw["array"], dict)
    assert raw["array"]["shape"] == list(im1.shape)
    assert isinstance(raw["array"]["data"], bytes)

    # Read singleton image as singleton
    im2 = imageio.imread(fname)
    assert np.all(im1 == im2)

    # Read singleton image as series
    ims = imageio.mimread(fname)
    assert len(ims) == 1 and np.all(im1 == ims[0])

    # Read + write back without image extensions
    bsdf.save(fname, bsdf.load(fname))
    im3 = imageio.mimread(fname)
    assert np.all(im1 == im3)
Exemplo n.º 10
0
def test_series():

    im1 = imageio.imread("imageio:chelsea.png")
    ims1 = [im1, im1 * 0.8, im1 * 0.5]

    fname = os.path.join(test_dir, "chelseam.bsdf")
    imageio.mimsave(fname, ims1)

    # Does it look alright if we open it in bsdf without extensions?
    raw = bsdf.load(fname, [])
    assert isinstance(raw, list) and len(raw) == 3
    for r in raw:
        assert set(r.keys()) == set(["meta", "array"])
        assert isinstance(r["meta"], dict)
        assert isinstance(r["array"], dict)
        assert r["array"]["shape"] == list(im1.shape)
        assert isinstance(r["array"]["data"], bytes)

    # Read multi-image as singleton
    im2 = imageio.imread(fname)
    assert np.all(im1 == im2)

    # Read multi-image as series
    ims2 = imageio.mimread(fname)
    assert len(ims2) == 3 and all(np.all(ims1[i] == ims2[i]) for i in range(3))

    # Read + write back without image extensions
    bsdf.save(fname, bsdf.load(fname))
    ims3 = imageio.mimread(fname)
    assert len(ims3) == 3 and all(np.all(ims1[i] == ims3[i]) for i in range(3))
Exemplo n.º 11
0
def example():
   
   
 
   # todo: read the example image from the papers source directory   
   image1=imread(wrapper.source_directory+'/examples/gobelet.png') 
   image2=imread(wrapper.source_directory+'/examples/gobelet2.png')
   
   # todo: call the wrapped function(s) with a valid set of arguments
   #keys1=wrapper.
   keys1=wrapper.extract_surf(image1)
   keys2=wrapper.extract_surf(image2)
   matches=wrapper.match_surf(keys1,keys2)

   # todo: display the results
   fig = plt.figure(figsize=(10,5))
   ax1 = fig.add_subplot(121)
   plt.imshow(image1) 
   ax2 = fig.add_subplot(122)
   plt.imshow(image2)     
  
 
   for x1,y1,x2,y2 in matches:
      con = ConnectionPatch(xyA=(x2, y2), xyB=(x1,y1),  coordsA="data", coordsB="data"  ,
                         axesA=ax2, axesB=ax1,
                         shrinkB=5,color='r')
      ax2.add_artist(con)
   plt.draw()
   plt.show()
 
   
   
   print ('done' )
Exemplo n.º 12
0
def test_png():

    for float in (False, True):
        for crop in (0, 1, 2):
            for colors in (0, 1, 3, 4):
                fname = fnamebase + "%i.%i.%i.png" % (float, crop, colors)
                rim = get_ref_im(colors, crop, float)
                imageio.imsave(fname, rim)
                im = imageio.imread(fname)
                mul = 255 if float else 1
                assert_close(rim * mul, im, 0.1)  # lossless

    # Run exact same test, but now in pypy backup mode
    try:
        imageio.plugins._freeimage.TEST_NUMPY_NO_STRIDES = True
        for float in (False, True):
            for crop in (0, 1, 2):
                for colors in (0, 1, 3, 4):
                    fname = fnamebase + "%i.%i.%i.png" % (float, crop, colors)
                    rim = get_ref_im(colors, crop, float)
                    imageio.imsave(fname, rim)
                    im = imageio.imread(fname)
                    mul = 255 if float else 1
                    assert_close(rim * mul, im, 0.1)  # lossless
    finally:
        imageio.plugins._freeimage.TEST_NUMPY_NO_STRIDES = False

    # Parameters
    im = imageio.imread("chelsea.png", ignoregamma=True)
    imageio.imsave(fnamebase + ".png", im, interlaced=True)

    # Parameter fail
    raises(TypeError, imageio.imread, "chelsea.png", notavalidkwarg=True)
    raises(TypeError, imageio.imsave, fnamebase + ".png", im, notavalidk=True)

    # Compression
    imageio.imsave(fnamebase + "1.png", im, compression=0)
    imageio.imsave(fnamebase + "2.png", im, compression=9)
    s1 = os.stat(fnamebase + "1.png").st_size
    s2 = os.stat(fnamebase + "2.png").st_size
    assert s2 < s1
    # Fail
    raises(ValueError, imageio.imsave, fnamebase + ".png", im, compression=12)

    # Quantize
    if sys.platform.startswith("darwin"):
        return  # quantization segfaults on my osx VM
    imageio.imsave(fnamebase + "1.png", im, quantize=256)
    imageio.imsave(fnamebase + "2.png", im, quantize=4)

    im = imageio.imread(fnamebase + "2.png")  # touch palette read code
    s1 = os.stat(fnamebase + "1.png").st_size
    s2 = os.stat(fnamebase + "2.png").st_size
    assert s1 > s2
    # Fail
    fname = fnamebase + "1.png"
    raises(ValueError, imageio.imsave, fname, im[:, :, :3], quantize=300)
    raises(ValueError, imageio.imsave, fname, im[:, :, 0], quantize=100)
Exemplo n.º 13
0
def make_arrays(labelsAndFiles, ratio):
    global height, width
    images = []
    labels = []
    imShape = imageio.imread(labelsAndFiles[0][1]).shape
    if len(imShape) > 2:
        height, width, channels = imShape
    else:
        height, width = imShape
        channels = 1
    for i in range(0, len(labelsAndFiles)):
        # display progress, since this can take a while
        if (i % 100 == 0):
            sys.stdout.write("\r%d%% complete" %
                             ((i * 100) / len(labelsAndFiles)))
            sys.stdout.flush()

        filename = labelsAndFiles[i][1]
        try:
            image = imageio.imread(filename)
            images.append(image)
            labels.append(labelsAndFiles[i][0])
        except:
            # If this happens we won't have the requested number
            print("\nCan't read image file " + filename)

    if ratio == 'train':
        ratio = 0
    elif ratio == 'test':
        ratio = 1
    else:
        ratio = float(ratio) / 100
    count = len(images)
    trainNum = int(count * (1 - ratio))
    testNum = count - trainNum
    if channels >= 1:
        trainImagedata = numpy.zeros(
            (trainNum, height, width, channels), dtype=numpy.uint8)
        testImagedata = numpy.zeros(
            (testNum, height, width, channels), dtype=numpy.uint8)
    else:
        trainImagedata = numpy.zeros(
            (trainNum, height, width), dtype=numpy.uint8)
        testImagedata = numpy.zeros(
            (testNum, height, width), dtype=numpy.uint8)
    trainLabeldata = numpy.zeros(trainNum, dtype=numpy.uint8)
    testLabeldata = numpy.zeros(testNum, dtype=numpy.uint8)

    for i in range(trainNum):
        trainImagedata[i] = images[i]
        trainLabeldata[i] = labels[i]

    for i in range(0, testNum):
        testImagedata[i] = images[trainNum + i]
        testLabeldata[i] = labels[trainNum + i]
    print("\n")
    return trainImagedata, trainLabeldata, testImagedata, testLabeldata
def setup(flist):
    N = len(flist)-1
    r, c = imageio.imread(str(flist[0])).shape
    I = empty((N+1, r, c), dtype=uint8)

    for i, f in enumerate(flist):
        I[i, ...] = imageio.imread(str(f))

    return I, N, r, c
Exemplo n.º 15
0
def test_jpg():
    
    for float in (False, True):
        for crop in (0, 1, 2):
            for colors in (0, 1, 3):
                fname = fnamebase + '%i.%i.%i.jpg' % (float, crop, colors)
                rim = get_ref_im(colors, crop, float)
                imageio.imsave(fname, rim)
                im = imageio.imread(fname)
                mul = 255 if float else 1
                assert_close(rim * mul, im, 1.1)  # lossy
    
    # No alpha in JPEG
    raises(Exception, imageio.imsave, fname, im4)
    
    # Parameters
    imageio.imsave(fnamebase + '.jpg', im3, progressive=True, optimize=True, 
                   baseline=True)
    
    # Parameter fail
    raises(TypeError, imageio.imread, fnamebase + '.jpg', notavalidkwarg=True)
    raises(TypeError, imageio.imsave, fnamebase + '.jpg', im, notavalidk=True)
    
    # Compression
    imageio.imsave(fnamebase + '1.jpg', im3, quality=10)
    imageio.imsave(fnamebase + '2.jpg', im3, quality=90)
    s1 = os.stat(fnamebase + '1.jpg').st_size
    s2 = os.stat(fnamebase + '2.jpg').st_size
    assert s2 > s1 
    raises(ValueError, imageio.imsave, fnamebase + '.jpg', im, quality=120)
    
    # Test broken JPEG
    fname = fnamebase + '_broken.jpg'
    open(fname, 'wb').write(b'this is not an image')
    raises(Exception, imageio.imread, fname)
    #
    bb = imageio.imsave(imageio.RETURN_BYTES, get_ref_im(3, 0, 0), 'JPEG')
    with open(fname, 'wb') as f:
        f.write(bb[:400])
        f.write(b' ')
        f.write(bb[400:])
    raises(Exception, imageio.imread, fname)
    
    # Test EXIF stuff
    fname = get_remote_file('images/rommel.jpg')
    im = imageio.imread(fname)
    assert im.shape[0] > im.shape[1]
    im = imageio.imread(fname, exifrotate=False)
    assert im.shape[0] < im.shape[1]
    im = imageio.imread(fname, exifrotate=2)  # Rotation in Python
    assert im.shape[0] > im.shape[1]
    # Write the jpg and check that exif data is maintained
    if sys.platform.startswith('darwin'):
        return  # segfaults on my osx VM, why?
    imageio.imsave(fnamebase + 'rommel.jpg', im)
    im = imageio.imread(fname)
    assert im.meta.EXIF_MAIN
Exemplo n.º 16
0
def test_inside_zipfile():
    need_internet()

    fname = os.path.join(test_dir, "pillowtest.zip")
    with ZipFile(fname, "w") as z:
        z.writestr("x.png", open(get_remote_file("images/chelsea.png"), "rb").read())
        z.writestr("x.jpg", open(get_remote_file("images/rommel.jpg"), "rb").read())

    for name in ("x.png", "x.jpg"):
        imageio.imread(fname + "/" + name)
Exemplo n.º 17
0
def test_images_with_transparency():
    # Not alpha channel, but transparent pixels, see issue #245 and #246
    need_internet()

    fname = get_remote_file("images/imageio_issue245.gif")
    im = imageio.imread(fname)
    assert im.shape == (24, 30, 4)

    fname = get_remote_file("images/imageio_issue246.png")
    im = imageio.imread(fname)
    assert im.shape == (24, 30, 4)
Exemplo n.º 18
0
def run_feeimage_test_suite():
    """ Run freeimage test suite.
    Lots of images. Berrer done locally and then checking the result.
    Not so much suited for CI, I think.
    """
    
    if not os.path.isdir(TESTDIR):
        os.mkdir(TESTDIR)
    if not os.path.isdir(ZIPDIR):
        os.mkdir(ZIPDIR)
    
    for name in names:
        fname = os.path.join(ZIPDIR, name+'.zip')
        # Make sure that the file is there
        if not os.path.isfile(fname):
            print('Downloading %s.zip' % name)
            f1 = urlopen(ulr+name+'.zip')
            f2 = open(fname, 'wb')
            shutil.copyfileobj(f1, f2)
            f1.close()
            f2.close()
        
        # Check contents
        zf = zipfile.ZipFile(fname, 'r')
        subnames = zf.namelist()
        zf.extractall(TESTDIR)
        zf.close()
        
        # Read and write each one
        for subname in subnames:
            if subname in FAILS:
                continue
            fname_zip = fname+'/%s' % subname
            subname_, ext = os.path.splitext(subname)
            fname_dst1 = os.path.join(TESTDIR, subname+'_1'+ext)
            fname_dst2 = os.path.join(TESTDIR, subname+'_2'+ext)
            if os.path.splitext(subname)[1].lower() in NOT_WRITABLE:
                fname_dst1 += '.png'
                fname_dst2 += '.png'
            print('Reading+saving %s' % subname)
            try:
                # Read from zip, save to file
                im = imageio.imread(fname_zip)
                imageio.imsave(fname_dst1, im)
                # Read from file, save to file
                im = imageio.imread(fname_dst1)
                imageio.imsave(fname_dst2, im)
            except Exception:
                e_type, e_value, e_tb = sys.exc_info()
                del e_tb
                err = str(e_value)
                print('woops! ' + fname_zip)
                print('  ' + err)
Exemplo n.º 19
0
def test_npz_reading_writing():
    """ Test reading and saveing npz """
    
    if IS_PYPY:
        return  # no support for npz format :(
    
    im2 = np.ones((10, 10), np.uint8) * 2
    im3 = np.ones((10, 10, 10), np.uint8) * 3
    im4 = np.ones((10, 10, 10, 10), np.uint8) * 4
    
    filename1 = os.path.join(test_dir, 'test_npz.npz')

    # One image
    imageio.imsave(filename1, im2)
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert (im == im2).all()
    assert len(ims) == 1
    
    # Multiple images
    imageio.mimsave(filename1, [im2, im2, im2])
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert (im == im2).all()
    assert len(ims) == 3
    
    # Volumes
    imageio.mvolsave(filename1, [im3, im3])
    im = imageio.volread(filename1)
    ims = imageio.mvolread(filename1)
    assert (im == im3).all()
    assert len(ims) == 2
    
    # Mixed
    W = imageio.save(filename1)
    assert W.format.name == 'NPZ'
    W.append_data(im2)
    W.append_data(im3)
    W.append_data(im4)
    raises(RuntimeError, W.set_meta_data, {})  # no meta data support
    W.close()
    #
    R = imageio.read(filename1)
    assert R.format.name == 'NPZ'
    ims = list(R)  # == [im for im in R]
    assert (ims[0] == im2).all()
    assert (ims[1] == im3).all()
    assert (ims[2] == im4).all()
    # Fail
    raises(IndexError, R.get_data, -1)
    raises(IndexError, R.get_data, 3)
    raises(RuntimeError, R.get_meta_data, None)  # no meta data support
    raises(RuntimeError, R.get_meta_data, 0)  # no meta data support
Exemplo n.º 20
0
def test_tifffile_reading_writing():
    """ Test reading and saving tiff """
    
    need_internet()  # We keep a test image in the imageio-binary repo
    
    im2 = np.ones((10, 10, 3), np.uint8) * 2

    filename1 = os.path.join(test_dir, 'test_tiff.tiff')

    # One image
    imageio.imsave(filename1, im2)
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert (im == im2).all()
    assert len(ims) == 1

    # Multiple images
    imageio.mimsave(filename1, [im2, im2, im2])
    im = imageio.imread(filename1)
    ims = imageio.mimread(filename1)
    assert (im == im2).all()
    assert len(ims) == 3, ims[0].shape

    # remote multipage rgb file
    filename2 = get_remote_file('images/multipage_rgb.tif')
    img = imageio.mimread(filename2)
    assert len(img) == 2
    assert img[0].shape == (3, 10, 10)

    # Mixed
    W = imageio.save(filename1)
    W.set_meta_data({'planarconfig': 'planar'})
    assert W.format.name == 'TIFF'
    W.append_data(im2)
    W.append_data(im2)
    W.close()
    #
    R = imageio.read(filename1)
    assert R.format.name == 'TIFF'
    ims = list(R)  # == [im for im in R]
    assert (ims[0] == im2).all()
    meta = R.get_meta_data()
    assert meta['orientation'] == 'top_left'
    # Fail
    raises(IndexError, R.get_data, -1)
    raises(IndexError, R.get_data, 3)

    # Ensure imwrite write works round trip
    filename3 = os.path.join(test_dir, 'test_tiff2.tiff')
    R = imageio.imread(filename1)
    imageio.imwrite(filename3, R)
    R2 = imageio.imread(filename3)
    assert (R == R2).all()
Exemplo n.º 21
0
def test_png():
    
    for isfloat in (False, True):
        for crop in (0, 1, 2):
            for colors in (0, 1, 3, 4):
                fname = fnamebase+'%i.%i.%i.png' % (isfloat, crop, colors)
                rim = get_ref_im(colors, crop, isfloat)
                imageio.imsave(fname, rim)
                im = imageio.imread(fname)
                mul = 255 if isfloat else 1
                assert_close(rim * mul, im, 0.1)  # lossless
    
    # Parameters
    im = imageio.imread('imageio:chelsea.png', ignoregamma=True)
    imageio.imsave(fnamebase + '.png', im, interlaced=True)
    
    # Parameter fail
    raises(TypeError, imageio.imread, 'imageio:chelsea.png', notavalidk=True)
    raises(TypeError, imageio.imsave, fnamebase + '.png', im, notavalidk=True)
    
    # Compression
    imageio.imsave(fnamebase + '1.png', im, compression=0)
    imageio.imsave(fnamebase + '2.png', im, compression=9)
    s1 = os.stat(fnamebase + '1.png').st_size
    s2 = os.stat(fnamebase + '2.png').st_size
    assert s2 < s1
    # Fail
    raises(ValueError, imageio.imsave, fnamebase + '.png', im, compression=12)
    
    # Quantize
    imageio.imsave(fnamebase + '1.png', im, quantize=256)
    imageio.imsave(fnamebase + '2.png', im, quantize=4)
    
    im = imageio.imread(fnamebase + '2.png')  # touch palette read code
    s1 = os.stat(fnamebase + '1.png').st_size
    s2 = os.stat(fnamebase + '2.png').st_size
    assert s1 > s2
    # Fail
    fname = fnamebase + '1.png'
    raises(ValueError, imageio.imsave, fname, im[:, :, :3], quantize=300)
    raises(ValueError, imageio.imsave, fname, im[:, :, 0], quantize=100)
    
    # 16b bit images
    im = imageio.imread('imageio:chelsea.png')[:, :, 0]
    imageio.imsave(fnamebase + '1.png', im.astype('uint16')*2)
    imageio.imsave(fnamebase + '2.png', im)
    s1 = os.stat(fnamebase + '1.png').st_size
    s2 = os.stat(fnamebase + '2.png').st_size
    assert s2 < s1
    im2 = imageio.imread(fnamebase + '1.png')
    assert im2.dtype == np.uint16
Exemplo n.º 22
0
def test_png_dtypes():
    # See issue #44

    # Two images, one 0-255, one 0-200
    im1 = np.zeros((100, 100, 3), dtype="uint8")
    im2 = np.zeros((100, 100, 3), dtype="uint8")
    im1[20:80, 20:80, :] = 255
    im2[20:80, 20:80, :] = 200

    fname = fnamebase + ".dtype.png"

    # uint8
    imageio.imsave(fname, im1)
    assert_close(im1, imageio.imread(fname))
    imageio.imsave(fname, im2)
    assert_close(im2, imageio.imread(fname))

    # float scaled
    imageio.imsave(fname, im1 / 255.0)
    assert_close(im1, imageio.imread(fname))
    imageio.imsave(fname, im2 / 255.0)
    assert_close(im2, imageio.imread(fname))

    # float not scaled
    imageio.imsave(fname, im1 * 1.0)
    assert_close(im1, imageio.imread(fname))
    imageio.imsave(fname, im2 * 1.0)
    assert_close(im1, imageio.imread(fname))  # scaled

    # int16
    imageio.imsave(fname, im1.astype("int16"))
    assert_close(im1, imageio.imread(fname))
    imageio.imsave(fname, im2.astype("int16"))
    assert_close(im1, imageio.imread(fname))  # scaled
Exemplo n.º 23
0
def test_different_read_modes():
    
    dname1, dname2, fname1, fname2 = _prepare()
    
    for fname, dname, n in [(fname1, dname1, 1), (fname2, dname2, 2)]:
        
        # Test imread()
        im = imageio.imread(fname)
        assert isinstance(im, np.ndarray)
        assert im.shape == (512, 512)
        
        # Test mimread()
        ims = imageio.mimread(fname)
        assert isinstance(ims, list)
        assert ims[0].shape == im.shape
        assert len(ims) > 1
        #    
        ims2 = imageio.mimread(dname)
        assert len(ims) == len(ims2)
        
        # Test volread()
        vol = imageio.volread(dname)
        assert vol.ndim == 3
        assert vol.shape[0] > 10
        assert vol.shape[1:] == (512, 512)
        #
        vol2 = imageio.volread(fname)  # fname works as well
        assert (vol == vol2).all()
        
        # Test mvolread()
        vols = imageio.mvolread(dname)
        assert isinstance(vols, list)
        assert len(vols) == n
        assert vols[0].shape == vol.shape
        assert sum([v.shape[0] for v in vols]) == len(ims)
def make_arrays(labelsAndFiles):
  images = []
  labels = []
  for i in range(0, len(labelsAndFiles)):

    # display progress, since this can take a while
    if (i % 100 == 0):
      sys.stdout.write("\r%d%% complete" % ((i * 100)/len(labelsAndFiles)))
      sys.stdout.flush()

    filename = labelsAndFiles[i][1]
    try:
      image = imageio.imread(filename)
      images.append(image)
      labels.append(labelsAndFiles[i][0])
    except:
      # If this happens we won't have the requested number
      print("\nCan't read image file " + filename)

  count = len(images)
  imagedata = numpy.zeros((count,28,28), dtype=numpy.uint8)
  labeldata = numpy.zeros(count, dtype=numpy.uint8)
  for i in range(0, len(labelsAndFiles)):
    imagedata[i] = images[i]
    labeldata[i] = labels[i]
  print("\n")
  return imagedata, labeldata
Exemplo n.º 25
0
def test_simpleitk_reading_writing():
    """ Test reading and saveing tiff """
    im2 = np.ones((10, 10, 3), np.uint8) * 2

    filename1 = os.path.join(test_dir, 'test_tiff.tiff')

    # One image
    imageio.imsave(filename1, im2, 'itk')
    im = imageio.imread(filename1, 'itk')
    ims = imageio.mimread(filename1, 'itk')
    assert (im == im2).all()
    assert len(ims) == 1

    # Mixed
    W = imageio.save(filename1, 'itk')
    raises(RuntimeError, W.set_meta_data, 1)
    assert W.format.name == 'ITK'
    W.append_data(im2)
    W.append_data(im2)
    W.close()
    #
    R = imageio.read(filename1, 'itk')
    assert R.format.name == 'ITK'
    ims = list(R)  # == [im for im in R]
    assert (ims[0] == im2).all()
    # Fail
    raises(IndexError, R.get_data, -1)
    raises(IndexError, R.get_data, 3)
    raises(RuntimeError, R.get_meta_data)
Exemplo n.º 26
0
    def read(self):
        print "reading %s" % self.file_path
        TEMP = paths.TEMP_DIR + '/images/'
        try:
            shutil.rmtree(TEMP)
        except OSError as e:
            pass
        utils.create_dirs_if_not_exists(TEMP)

        # TODO: use piplines instead of savings files to disk
        command = ['ffmpeg', '-n', '-i', self.file_path,
                   '-vf', 'fps=1, scale=' +
                   str(IMAGE_SIZE) + ':' + str(IMAGE_SIZE),
                   TEMP + '%d.png']
        sp.call(command)

        image_files = os.listdir(TEMP)
        max_examples = len(image_files)

        dataset = np.ndarray(shape=(max_examples, IMAGE_SIZE,
                                    IMAGE_SIZE, 3), dtype=np.float32)
        labels = np.ndarray(shape=(max_examples), dtype=np.int32)

        idx = 0
        for file in image_files:
            seq = int(file.split(".")[0])
            src = TEMP + file
            try:
                image = imageio.imread(src)
                if image.shape != (IMAGE_SIZE,
                                   IMAGE_SIZE, 3):
                    raise Exception('Image incorrectly resized',
                                    src, image.shape)
                dataset[idx] = image
                if seq in self.positives:
                    labels[idx] = 1
                else:
                    labels[idx] = 0
                idx += 1
            except IOError as e:
                print "Could not read", src, e

        # Remove unused space
        dataset = dataset[0:idx, :, :]
        labels = labels[0:idx]

        sample_range = self.__random_sample(dataset)
        print "creating test set from", sample_range[0], "to", sample_range[1]
        test_dataset, test_labels = self.__extract_dataset(dataset,
                                                           labels,
                                                           sample_range)
        dataset, labels = self.__delete_range(dataset, labels, sample_range)
        shutil.rmtree(TEMP)

        return {
            'train_dataset': dataset,
            'train_labels': labels,
            'test_dataset': test_dataset,
            'test_labels': test_labels
        }
Exemplo n.º 27
0
    def animate(self):

        """
        This function ...
        :return:
        """

        # Inform the user
        log.info("Creating an animation of the SED fitting procedure ...")

        # Create an Animation instance
        self.animation = Animation()

        # Loop over the entries of the chi squared table (sorted by decreasing chi squared)
        for i in range(len(self.chi_squared)):

            # Get the name of the simulation
            simulation_name = self.chi_squared["Simulation name"][i]

            # Determine the path to the corresponding SED plot file
            path = fs.join(self.fit_plot_path, simulation_name, "sed.png")

            # Load the image (as a NumPy array)
            image = imageio.imread(path)

            # Add the image to the animation
            self.animation.add_frame(image)
Exemplo n.º 28
0
    def gen_video_preview(elem, output_dir):
        """
        Copy temporary image to specified output filepath.

        :param elem: Data element to get the preview image for.
        :type elem: smqtk.data_rep.DataElement

        :param output_dir: Directory to save generated image to.
        :type output_dir: str

        """
        output_fp = os.path.join(output_dir,
                                 "%s.gif" % elem.md5())
        if not os.path.isfile(output_fp):
            tmp_vid_fp = elem.write_temp()
            interval = 0.5  # ~2fps gif
            fm = video_utils.ffmpeg_extract_frame_map(
                tmp_vid_fp, second_interval=interval
            )
            img_arrays = []
            for frm_num in sorted(fm.keys()):
                img_arrays.append(imageio.imread(fm[frm_num]))
            imageio.mimwrite(output_fp, img_arrays, duration=interval)
            elem.clean_temp()
        return output_fp
Exemplo n.º 29
0
def make_gif(gif_name):
    frames = []
    for x in range(len(input_image)):
        #for j in range(3):
        frames.append(imageio.imread('{0}.jpg'.format(x)))
    # Save them as frames into a gif 
    imageio.mimsave(gif_name, frames, 'GIF', duration = 1.4)
Exemplo n.º 30
0
def load_letter(folder, min_num_images):
  """Load the data for a single letter label."""
  image_files = os.listdir(folder)
  dataset = np.ndarray(shape=(len(image_files), image_size, image_size),
                         dtype=np.float32)
  print(folder)
  num_images = 0
  for image in image_files:
    image_file = os.path.join(folder, image)
    try:
      image_data = (imageio.imread(image_file).astype(float) - 
                    pixel_depth / 2) / pixel_depth
      if image_data.shape != (image_size, image_size):
        raise Exception('Unexpected image shape: %s' % str(image_data.shape))
      dataset[num_images, :, :] = image_data
      num_images = num_images + 1
    except (IOError, ValueError) as e:
      print('Could not read:', image_file, ':', e, '- it\'s ok, skipping.')
    
  dataset = dataset[0:num_images, :, :]
  if num_images < min_num_images:
    raise Exception('Many fewer images than expected: %d < %d' %
                    (num_images, min_num_images))
    
  print('Full dataset tensor:', dataset.shape)
  print('Mean:', np.mean(dataset))
  print('Standard deviation:', np.std(dataset))
  return dataset
# -*- coding: utf-8 -*-
"""
Created on Thu Apr  9 15:05:50 2020

@author: Abubakar
"""

import cv2
import imageio as im
import matplotlib.pyplot as plt
import numpy as np

img = im.imread('strawberries.jpg')
plt.title("Original_Image")
plt.imshow(img, cmap="gray")
plt.show()
plt.close()
plt.title("Interpolation_Nearest_Image")
near_img = cv2.resize(img,
                      None,
                      fx=0.1,
                      fy=0.1,
                      interpolation=cv2.INTER_NEAREST)
plt.imshow(near_img)
plt.show()
plt.close()
plt.title("Interpolation_BiLinear_Image")
bilinear_img = cv2.resize(img,
                          None,
                          fx=0.1,
                          fy=0.1,
import PIL
from PIL import Image

SCALE_IMGS_TO = 64

#%% Load dataset
#
#outputPath = "/media/macramole/stuff/Data/sauvage/"
outputPath = "/media/macramole/stuff/Data/open_images/Rose/"
imagesPath = "/media/macramole/stuff/Data/open_images/Rose/goodFiles/resized_1024/*.jpg"
h5Path = outputPath + "rose.hdf5"

if not os.path.isfile(h5Path):
    images = []
    imagePathList = glob(imagesPath)
    img = imageio.imread(imagePathList[0])
    originalShape = img.shape

    h5pyFile = h5py.File(h5Path, "w")
    #    df = h5pyFile.create_dataset("df", (len(imagePathList), originalShape[0]*originalShape[1]*originalShape[2] ), dtype='uint8')
    #    df = h5pyFile.create_dataset("df", (len(imagePathList), SCALE_IMGS_TO*SCALE_IMGS_TO*3 ), dtype='uint8')
    df = h5pyFile.create_dataset(
        "df", (len(imagePathList), SCALE_IMGS_TO, SCALE_IMGS_TO, 3),
        dtype='float32')
    df.attrs['shape'] = originalShape
    filenames = []
    filenamesErrors = []

    #h5pyFile.close()
    def loadImage(pathImage):
        #        print(pathImage)
Exemplo n.º 33
0
def labeling():
    image_path = "H:/workspace/cplusplus_opencv/Casecadenet/dataset/Image"
    test_image_path = "C:/Users/tkawn/Desktop/test"

    files = glob.glob(path.join(image_path, '*.jpg'))
    test_files = glob.glob(path.join(test_image_path, '*.jpg'))
    img = []
    img_test = []
    for i in range(30):
        img.append(files[i][63:])
        # img_test.append(test_files)
        img[i] = img[i].replace(".jpg", "")
        img[i] = int(img[i])
    img.sort()
    for i in range(30):
        img[i] = "H:/workspace/cplusplus_opencv/Casecadenet/dataset/Image\\" + "setting" + str(
            img[i]) + ".jpg"

    images = [imageio.imread(path) for path in img]
    test_images = [imageio.imread(path) for path in test_files]

    X_train = np.array(images)
    X_test = np.asarray(test_images)

    Y_train_orig = []
    # list = [0,1,2,3,4]
    # for i in range(6):#6000
    #     out = random.sample(list, 5)
    #     Y_train_orig.append(out)
    for _ in range(6):
        Y_train_orig.append(0)
    for _ in range(6):
        Y_train_orig.append(1)
    for _ in range(6):
        Y_train_orig.append(2)
    for _ in range(6):
        Y_train_orig.append(3)
    for _ in range(6):
        Y_train_orig.append(4)
    # for i in range(300000)
    # #라벨값 저장
    f = open("Y_train_label.txt", 'w')
    f.write(str(Y_train_orig))
    f.close()
    Y_train_orig = np.array(Y_train_orig).reshape(1, 30)
    np.expand_dims(Y_train_orig, axis=0)

    Y_test_orig = [[
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
        4, 4, 4, 4, 4, 4
    ]]
    #test 라벨 저장
    f = open("Y_test_label.txt", 'w')
    f.write(str(Y_test_orig))
    f.close()
    Y_test_orig = np.array(Y_test_orig)
    Y_train = convert_to_one_hot(Y_train_orig, 5).T
    Y_test = convert_to_one_hot(Y_test_orig, 5).T

    dict = {
        "X_train": X_train,
        "X_test": X_test,
        "Y_train": Y_train,
        "Y_test": Y_test
    }

    return dict
Exemplo n.º 34
0
            torch.save(checkpoint, path_checkpoint)

    # plot loss
    plt.figure(figsize=(10, 5))
    plt.title("Generator and Discriminator Loss During Training")
    plt.plot(G_losses, label="G")
    plt.plot(D_losses, label="D")
    plt.xlabel("iterations")
    plt.ylabel("Loss")
    plt.legend()
    # plt.show()
    plt.savefig(os.path.join(out_dir, "loss.png"))

    # save gif
    imgs_epoch = [
        int(name.split("_")[0]) for name in list(
            filter(lambda x: x.endswith("epoch.png"), os.listdir(out_dir)))
    ]
    imgs_epoch = sorted(imgs_epoch)

    imgs = list()
    for i in range(len(imgs_epoch)):
        img_name = os.path.join(out_dir, "{}_epoch.png".format(imgs_epoch[i]))
        imgs.append(imageio.imread(img_name))

    imageio.mimsave(os.path.join(out_dir, "generation_animation.gif"),
                    imgs,
                    fps=2)

    print("done")
Exemplo n.º 35
0
 def read(self, filename: str, **kwargs) -> np.ndarray:
     return imageio.imread(filename)
Exemplo n.º 36
0
    def __init__(self, **kwargs):
        """
        source_image_path
        target_image_path
        patch_size
        cover_size
        """
        source_image_path = kwargs.get('source_image_path', None)
        target_image_path = kwargs.get('target_image_path', None)

        # NOTE: sanity check, the source image must exist
        if source_image_path is None:
            raise Exception('source_image_path must exist')

        if not tf.gfile.Exists(source_image_path):
            raise Exception('source_image_path must exist')

        if target_image_path is None:
            target_image_uint8 = None
        elif not tf.gfile.Exists(target_image_path):
            raise Exception('target_image_path is invalid')
        else:
            self._target_image_uint8 = imageio.imread(target_image_path)

        self._source_image_uint8 = imageio.imread(source_image_path)
        self._source_image_uint8 = scipy.ndimage.gaussian_filter(
            self._source_image_uint8, sigma=0.5, mode='nearest')
        self._result_image_uint8 = np.zeros_like(self._source_image_uint8)

        self._patch_size = patch_size = kwargs.get('patch_size', 64)
        self._cover_size = cover_size = kwargs.get('cover_size', 16)

        # NOTE: sanity check, patch_size must be greater then 2 x cover_size
        if self._patch_size <= 2 * self._cover_size:
            raise Exception('invalid patch_size & cover_size')

        image_h, image_w, _ = self._source_image_uint8.shape

        num_patch_h = (image_h + patch_size - 4 * cover_size - 1) \
            // (patch_size - 2 * cover_size)
        num_patch_w = (image_w + patch_size - 4 * cover_size - 1) \
            // (patch_size - 2 * cover_size)

        self._source_patches_f32 = []

        for y, x in itertools.product(range(num_patch_h), range(num_patch_w)):
            # NOTE: crop a patch
            patch_x = \
                min(x * (patch_size - 2 * cover_size), image_w - patch_size)
            patch_y = \
                min(y * (patch_size - 2 * cover_size), image_h - patch_size)

            patch_4x = self._source_image_uint8[
                patch_y:patch_y+patch_size, patch_x:patch_x+patch_size]

            patch_1x = scipy.misc.imresize(
                patch_4x, (patch_size // 4, patch_size // 4), interp='bicubic')

            patch_1x = patch_1x.astype(np.float32) / 127.5 - 1.0

            patch_2x = scipy.misc.imresize(
                patch_4x, (patch_size // 2, patch_size // 2), interp='bicubic')

            patch_2x = patch_2x.astype(np.float32) / 127.5 - 1.0

            patch_4x = patch_4x.astype(np.float32) / 127.5 - 1.0

            # NOTE: mapping information
            if x == 0 or x + 1 == num_patch_w:
                map_w = patch_size - cover_size
            else:
                map_w = patch_size - cover_size * 2

            if y == 0 or y + 1 == num_patch_h:
                map_h = patch_size - cover_size
            else:
                map_h = patch_size - cover_size * 2

            map_source_x = (0 if x == 0 else cover_size)
            map_source_y = (0 if y == 0 else cover_size)

            if x == 0:
                map_target_x = 0
            else:
                map_target_x = patch_x + cover_size

            if y == 0:
                map_target_y = 0
            else:
                map_target_y = patch_y + cover_size

            self._source_patches_f32.append({
                'patch_1x': patch_1x,
                'patch_2x': patch_2x,
                'patch_4x': patch_4x,
                'map_source_x': map_source_x,
                'map_source_y': map_source_y,
                'map_target_x': map_target_x,
                'map_target_y': map_target_y,
                'map_w': map_w,
                'map_h': map_h})
Exemplo n.º 37
0
    def load_image(self, image_file_name):
        image = imageio.imread(image_file_name)
        image = cv2.resize(image, dsize=self.out_image_size)

        return image
#Start a session
sess = tf.Session()	

#Reshaping and normalizing image
def reshape_and_normalize_image(image):    
    image = np.reshape(image, ((1,) + image.shape))
	#Means for VGG-16: [123.68, 116.779, 103.939]
    image = image - np.array([123.68, 116.779, 103.939]).reshape((1,1,1,3)) 
    return image

#Changing working directory
#next_wd = os.path.join(previous_wd,"outputs")
#os.chdir(next_wd)
#Reading content image, resizing and reshaping
content_image = imageio.imread(string_content)
content_image = cv2.resize(content_image, (IMAGE_WIDTH,IMAGE_HEIGHT))
content_image = reshape_and_normalize_image(content_image)

#Reading style image, resizing and reshaping
style_image = imageio.imread(string_style)
style_image = cv2.resize(style_image, (IMAGE_WIDTH,IMAGE_HEIGHT))
style_image = reshape_and_normalize_image(style_image)

#Changing working directory to previous directory
#os.chdir(previous_wd)
#Function for a noise image
def generate_noise_image(content_image, noise_ratio = NOISE_RATIO):    
    noise_image = np.random.uniform(-20, 20, (1, IMAGE_HEIGHT, IMAGE_WIDTH, COLOR_CHANNELS)).astype('float32')
    input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio)    
    return input_image
Exemplo n.º 39
0
    def __init__(self,
                 data_dir,
                 data_split,
                 device='cpu',
                 scale=1,
                 coll_th=0.2):
        """
        Args:
        - data_dir: Directory containing dataset files in the format
        <frame_id> <ped_id> <x> <y>
        - obs_len: Number of time-steps in input trajectories
        - pred_len: Number of time-steps in output trajectories
        - skip: Number of frames to skip while making the dataset
        - threshold: Minimum error to be considered for non linear traj
        when using a linear predictor
        - min_ped: Minimum number of pedestrians that should be in a seqeunce
        - delim: Delimiter in the dataset files
        """
        super(TrajectoryDataset, self).__init__()

        self.obs_len = 8
        self.pred_len = 12
        skip = 1
        self.scale = scale
        self.seq_len = self.obs_len + self.pred_len
        self.device = device
        delim = ','
        dt = 0.4
        min_ped = 0
        data_dir = data_dir.replace('\\', '/')

        n_state = 6
        # root_dir = '/dresden/users/ml1323/crowd/datasets/Trajectories'
        # root_dir = os.path.join(data_dir, data_name)
        # root_dir = 'D:\crowd\datasets\Trajectories\Trajectories'

        all_files = [
            e for e in os.listdir(data_dir)
            if ('.csv' in e) and ('h**o' not in e)
        ]
        all_files = np.array(
            sorted(all_files, key=lambda x: int(x.split('.')[0])))

        if data_split == 'train':
            all_files = all_files[:30]
        elif data_split == 'val':
            all_files = all_files[[42, 44]]
        else:
            all_files = all_files[[43, 47, 48, 49]]

        # with open(os.path.join(root_dir, 'exit_wc.json')) as data_file:
        #     all_exit_wc = json.load(data_file)

        num_peds_in_seq = []
        seq_list = []

        obs_frame_num = []
        fut_frame_num = []
        map_file_names = []
        inv_h_ts = []

        for path in all_files:
            # exit_wc = np.array(all_exit_wc[path])
            num_data_from_one_file = 0
            path = os.path.join(data_dir, path.rstrip().replace('\\', '/'))
            print('data path:', path)
            # if 'Pathfinding' not in path:
            #     continue
            map_file_name = path.replace('.csv', '.png')
            print('map path: ', map_file_name)
            h = np.loadtxt(path.replace('.csv', '_homography.csv'),
                           delimiter=',')
            inv_h_t = np.linalg.pinv(np.transpose(h))

            loaded_data = read_file(path, delim)

            data = pd.DataFrame(loaded_data)
            data.columns = ['f', 'a', 'pos_x', 'pos_y']
            # data.sort_values(by=['f', 'a'], inplace=True)
            data.sort_values(by=['f', 'a'], inplace=True)

            frames = data['f'].unique().tolist()

            frame_data = []
            # data.sort_values(by=['f'])
            for frame in frames:
                frame_data.append(data[data['f'] == frame].values)
            num_sequences = int(
                math.ceil((len(frames) - self.seq_len + 1) / skip))
            # print('num_sequences: ', num_sequences)

            # all frames를 seq_len(kernel size)만큼씩 sliding해가며 볼것. 이때 skip = stride.
            for idx in range(0, num_sequences * skip + 1, skip):
                curr_seq_data = np.concatenate(
                    frame_data[idx:idx + self.seq_len], axis=0
                )  # frame을 seq_len만큼씩 잘라서 볼것 = curr_seq_data. 각 frame이 가진 데이터(agent)수는 다를수 잇음. 하지만 각 데이터의 길이는 4(frame #, agent id, pos_x, pos_y)
                peds_in_curr_seq = np.unique(
                    curr_seq_data[:, 1])  # unique agent id

                curr_seq = np.zeros(
                    (len(peds_in_curr_seq), n_state, self.seq_len))
                num_peds_considered = 0
                ped_ids = []
                for _, ped_id in enumerate(
                        peds_in_curr_seq
                ):  # current frame sliding에 들어온 각 agent에 대해
                    curr_ped_seq = curr_seq_data[
                        curr_seq_data[:, 1] ==
                        ped_id, :]  # frame#, agent id, pos_x, pos_y
                    curr_ped_seq = np.around(curr_ped_seq, decimals=4)
                    pad_front = frames.index(
                        curr_ped_seq[0, 0]
                    ) - idx  # sliding idx를 빼주는 이유?. sliding이 움직여온 step인 idx를 빼줘야 pad_front=0 이됨. 0보다 큰 pad_front라는 것은 현ped_id가 처음 나타난 frame이 desired first frame보다 더 늦은 경우.
                    pad_end = frames.index(curr_ped_seq[
                        -1, 0]) - idx + 1  # pad_end까지선택하는 index로 쓰일거라 1더함
                    if pad_end - pad_front != self.seq_len:  # seq_len만큼의 sliding동안 매 프레임마다 agent가 존재하지 않은 데이터였던것.
                        continue
                    ped_ids.append(ped_id)
                    # x,y,x',y',x'',y''
                    x = curr_ped_seq[:, 2]
                    y = curr_ped_seq[:, 3]
                    vx = derivative_of(x, dt)
                    vy = derivative_of(y, dt)
                    ax = derivative_of(vx, dt)
                    ay = derivative_of(vy, dt)

                    # Make coordinates relative
                    _idx = num_peds_considered
                    curr_seq[_idx, :, pad_front:pad_end] = np.stack(
                        [x, y, vx, vy, ax, ay])  # (1,6,20)

                    num_peds_considered += 1
                if num_peds_considered > min_ped:  # 주어진 하나의 sliding(16초)동안 등장한 agent수가 min_ped보다 큼을 만족하는 경우에만 이 slide데이터를 채택
                    seq_traj = curr_seq[:num_peds_considered][:, :2]

                    exclude_idx = []
                    for i in range(20):
                        curr1 = seq_traj[:, :,
                                         i].repeat(num_peds_considered,
                                                   0)  # AAABBBCCC
                        curr2 = np.stack(
                            [seq_traj[:, :, i]] * num_peds_considered).reshape(
                                -1, 2)  # ABCABC
                        dist = np.linalg.norm(curr1 - curr2, axis=1)
                        dist = dist.reshape(num_peds_considered,
                                            num_peds_considered)

                        diff_agent_idx = np.triu_indices(num_peds_considered,
                                                         k=1)
                        dist[diff_agent_idx] = 0
                        under_th_idx = np.array(
                            np.where((dist > 0) & (dist < coll_th)))
                        # under_th_idx = np.where((dist > 0) & (dist < coll_th))

                        # print(len(np.array(np.where((dist > 0.5) & (dist < 1))[0])))

                        # for elt in np.unique(under_th_idx):
                        #     np.count_nonzero(under_th_idx == elt)
                        for j in range(len(under_th_idx[0])):
                            idx_pair = under_th_idx[:, j]
                            exclude_idx.append(idx_pair[0])
                    exclude_idx = np.unique(exclude_idx)

                    if len(exclude_idx) == num_peds_considered:
                        continue
                    if len(exclude_idx) > 0:
                        # print(len(exclude_idx), '/', num_peds_considered)
                        valid_idx = [
                            i for i in range(num_peds_considered)
                            if i not in exclude_idx
                        ]
                        seq_traj = curr_seq[valid_idx]
                        num_peds_considered = len(valid_idx)

                        #======================
                        # for i in range(20):
                        #     curr1 = seq_traj[:, :2, i].repeat(num_peds_considered, 0)  # AAABBBCCC
                        #     curr2 = np.stack([seq_traj[:, :2, i]] * num_peds_considered).reshape(-1, 2)  # ABCABC
                        #     dist = np.linalg.norm(curr1 - curr2, axis=1)
                        #     dist = dist.reshape(num_peds_considered, num_peds_considered)
                        #
                        #     diff_agent_idx = np.triu_indices(num_peds_considered, k=1)
                        #     dist[diff_agent_idx] = 0
                        #     print(len(np.array(np.where((dist > 0) & (dist < 2))[0])))
                        #======================
                    else:
                        seq_traj = curr_seq[:num_peds_considered]

                    # find the agent with max num of neighbors at the beginning of future steps
                    curr1 = seq_traj[:, :2, self.obs_len].repeat(
                        num_peds_considered, 0)  # AAABBBCCC
                    curr2 = np.stack([seq_traj[:, :2, self.obs_len]] *
                                     num_peds_considered).reshape(-1,
                                                                  2)  # ABCABC
                    dist = np.linalg.norm(curr1 - curr2, axis=1)
                    dist = dist.reshape(num_peds_considered,
                                        num_peds_considered)
                    target_agent_idx = []
                    for d in range(len(dist)):
                        neighbor_idx = np.where((dist[d] < 5))[0]
                        if len(neighbor_idx) > len(target_agent_idx):
                            target_agent_idx = neighbor_idx
                    seq_traj = seq_traj[target_agent_idx]

                    num_peds_considered = len(target_agent_idx)
                    print(num_peds_considered)

                    seq_list.append(seq_traj)
                    num_data_from_one_file += num_peds_considered
                    num_peds_in_seq.append(num_peds_considered)
                    obs_frame_num.append(
                        np.ones((num_peds_considered, self.obs_len)) *
                        frames[idx:idx + self.obs_len])
                    fut_frame_num.append(
                        np.ones((num_peds_considered, self.pred_len)) *
                        frames[idx + self.obs_len:idx + self.seq_len])
                    # map_file_names.append(num_peds_considered*[map_file_name])
                    map_file_names.append(map_file_name)
                    inv_h_ts.append(inv_h_t)
                if num_data_from_one_file > 1000:
                    break
            cum_start_idx = [0] + np.cumsum(num_peds_in_seq).tolist()
            aa = np.array([
                (start, end)
                for start, end in zip(cum_start_idx, cum_start_idx[1:])
            ])
            print('num data,  min/avg/max #agent')
            print(num_data_from_one_file,
                  np.round((aa[:, 1] - aa[:, 0]).min(), 2),
                  np.round((aa[:, 1] - aa[:, 0]).mean(), 2),
                  np.round((aa[:, 1] - aa[:, 0]).max(), 2))

        seq_list = np.concatenate(seq_list, axis=0)  # (32686, 2, 16)
        self.obs_frame_num = np.concatenate(obs_frame_num, axis=0)
        self.fut_frame_num = np.concatenate(fut_frame_num, axis=0)

        # Convert numpy -> Torch Tensor
        self.obs_traj = seq_list[:, :, :self.obs_len]
        self.fut_traj = seq_list[:, :, self.obs_len:]

        # frame seq순, 그리고 agent id순으로 쌓아온 데이터에 대한 index를 부여하기 위해 cumsum으로 index생성 ==> 한 슬라이드(16 seq. of frames)에서 고려된 agent의 data를 start, end로 끊어내서 index로 골래내기 위해
        cum_start_idx = [0] + np.cumsum(num_peds_in_seq).tolist(
        )  # num_peds_in_seq = 각 slide(16개 frames)별로 고려된 agent수.따라서 len(num_peds_in_seq) = slide 수 = 2692 = self.num_seq
        self.seq_start_end = [
            (start, end)
            for start, end in zip(cum_start_idx, cum_start_idx[1:])
        ]  # [(0, 2),  (2, 4),  (4, 7),  (7, 10), ... (32682, 32684),  (32684, 32686)]

        # self.num_seq = len(self.seq_start_end)
        self.num_seq = len(self.obs_traj)
        self.map_file_name = map_file_names
        self.inv_h_t = inv_h_ts
        self.local_map = []
        self.local_homo = []
        self.local_ic = []
        print(self.seq_start_end[-1])

        for seq_i in range(len(self.seq_start_end)):
            start, end = self.seq_start_end[seq_i]
            global_map = imageio.imread(self.map_file_name[seq_i])

            local_maps = []
            local_ics = []
            local_homos = []
            for idx in range(start, end):
                all_traj = np.concatenate(
                    [self.obs_traj[idx, :2], self.fut_traj[idx, :2]],
                    axis=1).transpose(1, 0)
                # plt.imshow(global_map)
                # plt.scatter(all_traj[:8,0], all_traj[:8,1], s=1, c='b')
                # plt.scatter(all_traj[8:,0], all_traj[8:,1], s=1, c='r')
                # plt.show()
                local_map, local_ic, local_h = get_local_map_ic(global_map,
                                                                all_traj,
                                                                zoom=10,
                                                                radius=8)
                local_maps.append(local_map)
                local_ics.append(local_ic)
                local_homos.append(local_h)

                # plt.imshow(local_map[0])
                # plt.scatter(local_ic[:,1], local_ic[:,0], s=1, c='r')
                # plt.show()
            self.local_map.append(np.stack(local_maps))
            self.local_ic.append(np.stack(local_ics))
            self.local_homo.append(np.stack(local_homos))
        self.local_map = np.concatenate(self.local_map)
        self.local_ic = np.concatenate(self.local_ic)
        self.local_homo = np.concatenate(self.local_homo)

        all_data = \
            {'seq_start_end': self.seq_start_end,
             'obs_traj': self.obs_traj,
             'fut_traj': self.fut_traj,
             'obs_frame_num': self.obs_frame_num,
             'fut_frame_num': self.fut_frame_num,
             'map_file_name': self.map_file_name,
             'inv_h_t': self.inv_h_t,
             'local_map': self.local_map,
             'local_ic': self.local_ic,
             'local_homo': self.local_homo,
             }

        save_path = os.path.join(
            data_dir, data_split + '_threshold' + str(coll_th) + '.pkl')
        with open(save_path, 'wb') as handle:
            pickle5.dump(all_data, handle, protocol=pickle5.HIGHEST_PROTOCOL)
Exemplo n.º 40
0
 def _check_and_load(self, ext, img, f, verbose=True):
     if not os.path.isfile(f) or ext.find('reset') >= 0:
         if verbose:
             print('Making a binary: {}'.format(f))
         with open(f, 'wb') as _f:
             pickle.dump(imageio.imread(img), _f)
Exemplo n.º 41
0
 #endfileidx = 89
 calib_idx = calib_map[scene_idx]
 calib_extrinsic = calib_extrinsics[calib_idx].copy()
 calib_extrinsic[2,3] += 1.65
 calib_projection = calib_projections[calib_idx]
 calib_intrinsic = calib_projection.dot(np.linalg.inv(calib_extrinsic))
 
 view_angle = view_by_day[calib_idx]
     
 for fileidx in range(startfileidx, endfileidx):
     lidarfile = lidar_files.format(scene_idx, fileidx)
     if not isfile(lidarfile):
         continue
     data = np.fromfile(lidarfile, dtype=np.float32).reshape((-1,4))[:,:3]
     data = data.dot(calib_extrinsic[:3,:3].T) + calib_extrinsic[:3,3]
     img = grayer(imread(img_files.format(scene_idx, fileidx))[:,:,::-1])
     ground = np.load(ground_files.format(scene_idx, fileidx))
     
     # shade by elevation
     plotimg = plotImgKitti(view_angle)
     max_elev = 5.
     pixel_to_ground = np.mgrid[40:640., :640]
     pixel_to_ground[0] *= -60. / 640
     pixel_to_ground[1] *= -60. / 640
     pixel_to_ground[0] += 60.
     pixel_to_ground[1] += 30.
     pixel_to_ground = pixel_to_ground.transpose((1,2,0))
     quantized = floor(pixel_to_ground[:,:,:2]/grndstep)-grndstart
     planes = ground[quantized[:,:,0], quantized[:,:,1]]
     heights = (planes[:,:,3] - planes[:,:,0]*pixel_to_ground[:,:,0] -
                planes[:,:,1]*pixel_to_ground[:,:,1])
"""
Slide Show - Make an application that shows various pictures in a slide show format.
Optional: Try adding various effects like fade in/out, star wipe and window blinds transitions.
"""

import imageio
filenames = ["Image-Slideshow/mountains.jpg","Image-Slideshow/lakes.png"]
images = []
for filename in filenames:
    images.append(imageio.imread(filename))
imageio.mimsave('Image-Slideshow/movie.gif', images,'GIF',duration=1)
  image : numpy array
      [w, h, c]
  image_path : str
      path

  """
  try:  # RGB
    imageio.imwrite(image_path, image)
  except Exception:  # Greyscale
    imageio.imwrite(image_path, image[:, :, 0])


if __name__ == '__main__':

  for parent, _, files in os.walk('./output_img'):
    for file in files:
      im_name = file
      im_path = os.path.join(parent, file)
      xml_path = os.path.join(
          './output_xml', file[:-4] + '.xml')
      coords, classes_list = get_xml_info(xml_path)
      # image = cv2.imread(im_path)
      image = imageio.imread(im_path)
      print('Draw Box for %s' % im_name)
      try:
        draw_boxes_and_labels_to_image(image, coords, [], classes_list, is_center=False,
                                       is_rescale=False, save_name='./ground_truth/%s' % im_name)
      except Exception as e:
        pass
      continue
Exemplo n.º 44
0
import os
import imageio

duration = 0.25
path_images = input(
    'pfad?: ')  #r'C:\Users\x123069\Desktop\D\pys\images_to_gif\images'
path_result = r'C:\Users\x123069\Desktop\D\pys\images_to_gif'
all_filenames = os.listdir(path_images)

filenames = []
images = []

for file in all_filenames:
    if file.endswith('jpg'):
        images.append(imageio.imread(os.path.join(path_images, file)))

imageio.mimsave(os.path.join(path_result, 'result.gif'),
                images,
                duration=duration)
Exemplo n.º 45
0
        # Read aligned faces of targeted person
        file_list = os.listdir(args.target_dic)
        for i in range(len(file_list)):
            file_list[i] = os.path.join(args.target_dic, file_list[i])
        faces_target = util_crop.read_face_from_aligned(file_list=file_list,
                                                        model=args.model)

    # Read aligned faces of the original person
    file_list = os.listdir(args.self_dic)
    for i in range(len(file_list)):
        file_list[i] = os.path.join(args.self_dic, file_list[i])
    faces_self = util_crop.read_face_from_aligned(file_list=file_list,
                                                  model=args.model)

    # Read original image for the attack
    img = imageio.imread(args.attack_img)
    face, det = util_crop.crop_face(img=img, model=args.model)
    face = np.array([face])

    with tf.Session() as sess:
        if args.model == 'center':
            print("Loading Center Face Model")
            FRmodel = CenterFace()
        elif args.model == 'triplet':
            print("Loading Triplet FaceNet Model")
            FRmodel = FaceNetModel()
        print('Model loaded')

        # Run the attack
        adv, delta, l2, const = util_attack.find_adv(sess,
                                                     face,
Exemplo n.º 46
0
import os
import imageio
# imageio.mimwrite()

List=os.listdir('gif')
imgList=[]
for name in List:
    img=imageio.imread(f"gif/{name}")
    imgList.append(img)

imageio.mimwrite('yang.gif',imgList,duration=0.15)
display_data(X_norm[:100, :])
plt.title('Original faces')
plt.axis('equal')

# Display reconstructed data from only k eigenfaces
plt.subplot(1, 2, 2)
display_data(X_rec[:100, :])
plt.title('Recovered faces')
plt.axis('equal')

input('Program paused. Press enter to continue.\n')

# === Part 8(a): Optional (ungraded) Exercise: PCA for Visualization ===
plt.close()

A = imread('bird_small.png')

A = A/255
img_size = A.shape
X = A.reshape(img_size[0]*img_size[1], 3)
K = 16
max_iters = 10
initial_centroids = kmeans_init_centroids(X, K)
centroids, idx = run_kmeans(X, initial_centroids, max_iters)

# %  Sample 1000 random indexes (since working with all the data is
# %  too expensive. If you have a fast computer, you may increase this.
sel = np.random.randint(X.shape[0], size=1000)

# Setup Color Palette
cm = plt.cm.get_cmap('RdYlBu')
Exemplo n.º 48
0
import numpy as np
from imageio import imread
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from ML_classes_real_data import *

# Load the terrain
terrain1 = imread('SRTM_data_Norway_1.tif')
terrain1 = terrain1[:500, :500]
# Show the terrain
plt.figure()
plt.title('Terrain over Norway 1')
plt.imshow(terrain1, cmap='gray')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

for i in range(5):
    A = Ridge_main(i + 1, 0.1, terrain1)
    A.variance_in_beta()
Exemplo n.º 49
0
def img_file_to_gif(img_list, output_file_name):
    ## imge 파일 리스트로부터 gif 생성 
    imgs_array = [np.array(imageio.imread(img_file)) for img_file in img_list]

    imageio.mimsave(output_file_name, imgs_array, duration=0.4) # 수정
Exemplo n.º 50
0
folders = sorted(os.listdir(glyphFolder))
Bar = ProgressBar(len(folders), 30, 'Create gif ')

for folder in folders:
    gifName = folder + '.gif'
    gifList.append(gifName)

    with imageio.get_writer(outputFolder + "/" + gifName, mode='I') as writer:
        if os.path.isdir(glyphFolder + folder):
            files = [
                glyphFolder + folder + "/" + f
                for f in sorted(os.listdir(glyphFolder + folder))
                if f.split(".")[-1] == "png"
            ]
            for filename in files:
                image = imageio.imread(filename)
                writer.append_data(image)

    Bar.update()

with open(outputFolder + "gif.html", "w") as fp:
    html = """
    <html>
    <head><meta charset="UTF-8"></head>
    <body>
    """

    for g in gifList:
        html += """
        <img src='{}' onerror="this.style.display='none'"/>
        """.format(g)
Exemplo n.º 51
0
    A = np.matmul(np.transpose(J), J)

    # b matirx
    b = np.matmul(np.transpose(J), delX)

    # p matrix
    p = np.matmul(np.linalg.inv(A), b)
    p = np.append(p, np.vstack([0, 0]), axis=0)

    return p


if __name__ == '__main__':

    #Traget image timg
    timg = imageio.imread("samples_as3/input/target/hallway.png")
    #Target image coordinates tcoord
    tcoord = [(0, 492), (1100, 54), (1100, 1975), (0, 1225)]

    #Source image simg
    simg = imageio.imread("samples_as3/input/source/nerdy.png")
    #Source image coordinates scoord
    scoord = [(0, 0), (599, 0), (599, 349), (0, 349)]

    p = H(scoord, tcoord)

    # For bigger image
    #p = np.asarray([1, 0, 0, 0, 1, 0, 0, 0], dtype=float).reshape((8, 1))
    H = H_iter(scoord, tcoord, p, 1000, 0.1)

    src_h = simg.shape[0]
Exemplo n.º 52
0
def get_ellipse(n=256,
                image='ellipse',
                alpha=20,
                Ncount=1000,
                filter='Gaussian_local',
                noise=True):
    N = n**2
    if image == 'ellipse':
        ax1s = [0.2, 0.5]  # solution (anything else is fine)
        ax2s = [0.9, 0.7]  # solution
        thetas = torch.tensor([0.4 * math.pi, 0.4 * math.pi])  # solution
        im = make_im(n, thetas, ax1s,
                     ax2s).numpy().reshape(n, n).T  # ground truth image
        imres = im.reshape(N, )
    elif image == 'phanton':
        im = imageio.imread('phanton.png')[:, :, 0]
        im = rescale(im, scale=n / 512, mode='reflect')
    elif image == 'chessboard':
        im1 = imageio.imread('chessboard.jpg')[:, :, 0]
        im1 = rescale(im1, scale=n / 612, mode='reflect')
        im2 = im1[33:256, 33:256]
        im = rescale(im2, scale=n / 223, mode='reflect')
    elif image == 'eyechart':
        im1 = imageio.imread('eyechart.jpg')
        im = rescale(im1, scale=n / 1280, mode='reflect')

    A2d = makeA2d(n, alpha)
    imres = im.reshape(N, )
    Sigma = sp.sparse.diags(
        np.real(scipy.fft.fft(A2d[0, :].toarray().reshape(N, ))))
    mode1 = 'wrap'
    if filter == 'Gaussian':
        mres = np.real(
            sp.fft.fft(
                (Sigma**Ncount @ sp.fft.fft(imres).conjugate()).reshape(N, )) /
            N)
        meas = mres.reshape(n, n)
    elif filter == 'average':
        meas = ndimage.uniform_filter(im, size=20)
    elif filter == 'maximum':
        meas = ndimage.maximum_filter(im, size=20)
    elif filter == 'pyramid':
        size = 10
        P = pyramid(size)
        P = P / np.sum(P)
        meas = convolve(im, P, mode=mode1)
        print('size', size)
    elif filter == 'miscellaneous':
        size = 20
        P = pyramid(size)**0.5
        P = P / np.sum(P)
        meas = convolve(im, P, mode=mode1)
    elif filter == 'Gaussian_local':
        sigma = 2
        size = 40
        x1, y1 = np.meshgrid(np.linspace(-sigma * 3, sigma * 3, size),
                             np.linspace(-sigma * 3, sigma * 3, size))
        dst = np.sqrt((x1 * x1 + y1 * y1))
        muu = 0.000
        gauss = np.exp(-((dst - muu)**2 / (2.0 * sigma**2)))
        gauss = gauss / np.sum(gauss)
        meas = convolve(im, gauss, mode=mode1)
        print('kernel shape', gauss.shape, 'sigma', sigma)
    elif filter == 'mixed':
        size = 10
        P = pyramid(size)
        P = P / np.sum(P)
        meas1 = convolve(im, P, mode=mode1)

        x1, y1 = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
        dst = np.sqrt((x1 * x1 + y1 * y1))
        sigma = 0.5
        muu = 0.000
        gauss = np.exp(-((dst - muu)**2 / (2.0 * sigma**2)))
        gauss = gauss / np.sum(gauss)
        meas2 = convolve(im, gauss, mode=mode1)

        meas = meas1.copy()
        meas[int(n / 2):, :] = meas2[int(n / 2):, :]
    if noise == True:
        meas += 0.05 * np.random.randn(meas.shape[0], meas.shape[1])
    return im, meas, Sigma
Exemplo n.º 53
0
    def run_episode(self,
                    env,
                    i_episode,
                    log_metrics=True,
                    log_animation=False):
        env.reset()

        for t in count():
            obs, reward, is_done = env.observe()

            if log_animation:
                world, rmap = env.render()
                plt.imsave(
                    f'/tmp/{wandb.run.id}_episode_{i_episode}_step_{t}.png',
                    world)

            if is_done:
                break

            window, vector = obs
            window = torch.from_numpy(window[None])
            window = window.float().to(device=self.agent.device)
            vector = torch.from_numpy(
                vector[None]).float().to(device=self.agent.device)
            # Select and perform an action
            action = self.agent.make_action((window, vector))
            env.act(action)

        if env.task_length != 0:
            path_difference = (env.path_length -
                               env.task_length) / env.task_length
        else:
            path_difference = 0

        if log_metrics:
            wandb.log(
                {
                    'duration': t,
                    'success': env.is_success,
                    'path_diff': path_difference,
                    'task_length': env.task_length
                },
                step=i_episode)

        if log_animation:
            with imageio.get_writer(
                    f'/tmp/{wandb.run.id}_episode_{i_episode}.gif',
                    mode='I',
                    fps=3) as writer:
                for i in range(t):
                    image = imageio.imread(
                        f'/tmp/{wandb.run.id}_episode_{i_episode}_step_{i}.png'
                    )
                    writer.append_data(image)
            wandb.log(
                {
                    f'animation':
                    wandb.Video(f'/tmp/{wandb.run.id}_episode_{i_episode}.gif',
                                fps=3,
                                format='gif')
                },
                step=i_episode)

        return env.is_success, t, path_difference, env.task_length
Exemplo n.º 54
0
def calOpt(height=240,
           width=320,
           maxdisp=256,
           fac=1,
           modelpath='finetune_67999.tar'):
    # Calculate model hyperparameters
    # Resize to 64X
    maxh = height
    maxw = width
    max_h = int(
        maxh // 64 * 64
    )  # Basically this is performing an integer division and modulo operation
    max_w = int(maxw // 64 * 64)  # if modulo is not zero, then round it up
    if max_h < maxh:  # The rounded-up integer is multiplied by 64x
        max_h += 64
    if max_w < maxw:
        max_w += 64

    # load model
    if (MODEL_OPTION == 'base'):
        model = VCN([1, max_w, max_h],
                    md=[int(4 * (maxdisp / 256)), 4, 4, 4, 4],
                    fac=fac)
    else:
        model = VCN_small([1, max_w, max_h],
                          md=[int(4 * (maxdisp / 256)), 4, 4, 4, 4],
                          fac=fac)
    model = nn.DataParallel(model, device_ids=[0])
    model.cuda()

    # load weights
    pretrained_dict = torch.load(modelpath)
    mean_L = pretrained_dict['mean_L']
    mean_R = pretrained_dict['mean_R']
    model.load_state_dict(pretrained_dict['state_dict'], strict=False)
    model.eval()
    print('Number of model parameters: {}'.format(
        sum([p.data.nelement() for p in model.parameters()])))

    start_time = time.time()

    # Load image and Resize
    # Note that the images are loaded as [H,W,C] i.e. [H,W,3]
    imgL_o = imageio.imread(
        'image1.png'
    )[:, :, :
      3]  # In some cases, image files include alpha channel (the 4th channel)
    imgR_o = imageio.imread(
        'image2.png')[:, :, :3]  # Only get the RGB channels (1st 3 channels)
    input_size = imgL_o.shape
    imgL = cv2.resize(imgL_o, (max_w, max_h))
    imgR = cv2.resize(imgR_o, (max_w, max_h))

    read_time = time.time()

    # For gray input images
    # The model expects RGB images, in other words, 3 channels
    # This repeats H*W spatial values over the channel layer [H,W,1] -> [H,W,3]
    if len(imgL_o.shape) == 2:
        imgL_o = np.tile(imgL_o[:, :, np.newaxis], (1, 1, 3))
        imgR_o = np.tile(imgR_o[:, :, np.newaxis], (1, 1, 3))

    # Flip channel, subtract mean
    # The model expects inputs of format [C,H,W] instead of [H,W,C]
    imgL = imgL[:, :, ::-1].copy() / 255. - np.asarray(mean_L).mean(0)[
        np.newaxis, np.newaxis, :]
    imgR = imgR[:, :, ::-1].copy() / 255. - np.asarray(mean_R).mean(0)[
        np.newaxis, np.newaxis, :]
    imgL = np.transpose(imgL, [2, 0, 1])[np.newaxis]
    imgR = np.transpose(imgR, [2, 0, 1])[np.newaxis]

    # Image to Torch tensor
    imgL = torch.FloatTensor(imgL).cuda()
    imgR = torch.FloatTensor(imgR).cuda()

    # Forward
    with torch.no_grad():
        imgLR = torch.cat([imgL, imgR], 0)
        time1 = time.time()
        rts = model(imgLR)
        pred_disp, entropy = rts
        time2 = time.time()

    print(time2 - time1)

    forward_time = time.time()

    # Upsampling
    pred_disp = torch.squeeze(pred_disp).data.cpu().numpy(
    )  # Remove batch dimension, torch tensor to numpy ndarray
    pred_disp = cv2.resize(
        np.transpose(pred_disp, (1, 2, 0)), (input_size[1], input_size[0])
    )  # Resize to the original size, and transpose from [C,H,W] -> [H,W,C]
    pred_disp[:, :, 0] *= input_size[1] / max_w
    pred_disp[:, :, 1] *= input_size[0] / max_h
    flow = np.ones([pred_disp.shape[0], pred_disp.shape[1], 3])
    flow[:, :, :2] = pred_disp
    entropy = torch.squeeze(entropy).data.cpu().numpy()
    entropy = cv2.resize(entropy, (input_size[1], input_size[0]))

    upsample_time = time.time()

    print("Read: {}s".format(read_time - start_time))
    print("Forward: {}s".format(forward_time - start_time))
    print("Upsample: {}s".format(upsample_time - start_time))

    # Show results
    showImage(flow_to_image(flow), "flow_to_image.png")
    showImage(point_vec(imgL_o, flow)[:, :, ::-1], "vector_on_image.png")
    showImage(entropy, "entropy.png")
Exemplo n.º 55
0
def load_as_float(path):
    return imread(path).astype(np.float32)
Exemplo n.º 56
0
from pts.core.tools import filesystem as fs
from pts.core.basics.configuration import ConfigurationDefinition, parse_arguments

# -----------------------------------------------------------------

# Create the definition
definition = ConfigurationDefinition()
definition.add_required("filename", "file_path",
                        "name of the input image file")

# Parse the command line arguments
config = parse_arguments("invert", definition)

# -----------------------------------------------------------------

# Open the original image
image = imageio.imread(config.filename)

# Invert the colours
invert_colors(image)

# Determine output name
name = fs.strip_extension(fs.name(config.filename))
extension = fs.get_extension(config.filename)
newname = name + "_inverted." + extension

# Write the inverted image
imageio.imwrite(newname, image)

# -----------------------------------------------------------------
judgement = Dense(units=1,
                  activation='sigmoid',
                  name='discriminator_second_output_layer')(discrim)
discriminator = Model(discriminator_input, [Disl, judgement],
                      name='discriminator')
discriminator.summary()

total = Model(encoder_input,
              discriminator(decoder(encoder(encoder_input)[2])),
              name='total')
total.load_weights('model.h5')

x = []
random_indices = np.random.randint(202001, 202599, size=8)
for i in random_indices:
    x += [imageio.imread('64x64_celeba\%06d.jpg' % i)]
x_test = np.array(x)

z_test = encoder.predict(x_test)[2]
reconstructed = decoder.predict(z_test)
z_from_noise = np.random.normal(size=(8, 2048))
new_images = decoder.predict(z_from_noise)

reconstructed = list(((reconstructed + 1) * 128).astype(np.uint8))
new_images = list(((new_images + 1) * 128).astype(np.uint8))
x_test = list(x_test)
random_indices = random_indices.tolist()

t = 0
while t < 8:
    imageio.imwrite('test\%06d.jpg' % random_indices[t],
# projection file indices, we need to read in the projection in reverse order due to the portrait mode acquision 
projs_idx  = range(1199,-1, -angluar_sub_sampling)

n_pro = vecs.shape[0]

# create the numpy array which will receive projection data from tiff files
projs = np.zeros((n_pro, projs_rows, projs_cols), dtype=np.float32)

# transformation to apply to each image, we need to get the image from
# the way the scanner reads it out into to way described in the projection
# geometry
trafo = lambda image : np.transpose(np.flipud(image))

# load flat-field and dark-fields
# there are two flat-field images (taken before and after acquisition), we simply average them
dark = trafo(imageio.imread(os.path.join(data_path_full, dark_name)))
flat = np.zeros((2, projs_rows, projs_cols), dtype=np.float32)

for i, fn in enumerate(flat_name):
    flat[i] = trafo(imageio.imread(os.path.join(data_path_full, fn)))
flat =  np.mean(flat,axis=0)

# load projection data
for i in range(n_pro):
    projs[i] = trafo(imageio.imread(os.path.join(data_path_full, projs_name.format(projs_idx[i]))))

print(np.round_(time.time() - t, 3), 'sec elapsed')



### pre-process data ###########################################################
Exemplo n.º 59
0
def load_image(path):
    return imageio.imread(path)
Exemplo n.º 60
0

def dct2(x):
    return spfft.dct(spfft.dct(x.T, norm='ortho', axis=0).T,
                     norm='ortho',
                     axis=0)


def idct2(x):
    return spfft.idct(spfft.idct(x.T, norm='ortho', axis=0).T,
                      norm='ortho',
                      axis=0)


# Xorig = imageio.imread('Escher_Waterfall.jpg', as_gray=True, pilmode='L')
Xorig = imageio.imread('rome.jpg', as_gray=True, pilmode='L')
# ny, nx = Xorig.shape
# print(ny,nx)
X = spimg.zoom(Xorig, 0.04)
ny, nx = X.shape
print(X.shape)
# plt.imshow(Xorig)
# plt.imshow(X)
# plt.show()

# print(X.mean(),X.max(),X.min())
X = X - 128
# X = X/128
# print(X.mean(),X.max(),X.min())

# extract small sample of signal