def transcode(filename): tmpname = test_utils.temporary_filename(suffix=os.path.splitext(filename)[1]) tmpnam_ = test_utils.temporary_filename(suffix=os.path.splitext(filename)[1]) try: # complete transcoding test image = load(filename) # save with the same extension write(image, tmpname) # reload the image from the file image2 = load(tmpname) assert numpy.array_equal(image, image2) # test getting part of the image as well if len(image.shape) == 3: subsample = image[:,::2,::2] else: subsample = image[::2,::2] assert not subsample.flags.contiguous write(subsample, tmpnam_) image3 = load(tmpnam_) assert numpy.array_equal(subsample, image3) finally: if os.path.exists(tmpname): os.unlink(tmpname) if os.path.exists(tmpnam_): os.unlink(tmpnam_)
def test_image_load(): # test that the generic bob.io.image.load function works as expected for filename in ( "test.jpg", "cmyk.jpg", "test.pbm", "test_corrupted.pbm", "test.pgm", "test_corrupted.pgm", "test.ppm", "test_corrupted.ppm", "test.gif", ): full_file = datafile(filename, __name__) # load with just image name i1 = load(full_file) assert i1.shape == (6, 4) # Loading the last pgm file full_file = datafile("test_spaces.pgm", __name__) load(full_file).shape == (100, 100) # Testing exception with pytest.raises(RuntimeError): load(os.path.splitext(full_file)[0] + ".unknown")
def transcode(filename): tmpname = test_utils.temporary_filename( suffix=os.path.splitext(filename)[1]) tmpnam_ = test_utils.temporary_filename( suffix=os.path.splitext(filename)[1]) try: # complete transcoding test image = load(filename) # save with the same extension write(image, tmpname) # reload the image from the file image2 = load(tmpname) assert numpy.array_equal(image, image2) # test getting part of the image as well if len(image.shape) == 3: subsample = image[:, ::2, ::2] else: subsample = image[::2, ::2] assert not subsample.flags.contiguous write(subsample, tmpnam_) image3 = load(tmpnam_) assert numpy.array_equal(subsample, image3) finally: if os.path.exists(tmpname): os.unlink(tmpname) if os.path.exists(tmpnam_): os.unlink(tmpnam_)
def test_open_file_leak(): import psutil import tempfile import gc from . import reader from bob.io.base import save my_video = numpy.random.random_integers(0, 255, (10, 3, 256 * 4, 256 * 4)) process = psutil.Process() def open_files_count(path): return len([f for f in process.open_files() if f.path == path]) with tempfile.NamedTemporaryFile(suffix='.avi') as f: path = f.name save(my_video.astype('uint8'), path) # Get a count before doing anything. This following count is 1 because # NamedTemporaryFile keeps a pointer to this file. We will check if the # count goes to 0 in the end! last_count = open_files_count(path) # load the video at once and see if the count is constant load(path) gc.collect() new_count = open_files_count(path) assert new_count == last_count, \ ('We did not close all the files when loading the video at once. ' 'old count: {}, new count: {}'.format(last_count, new_count)) # iterate over all frames and see if the count is constant for frame in reader(path): pass gc.collect() new_count = open_files_count(path) assert new_count == last_count, \ ('We did not close all the files when iterating over all frames. ' 'old count: {}, new count: {}'.format(last_count, new_count)) # iterate over one frame and see if the count is constant for frame in reader(path): break gc.collect() new_count = open_files_count(path) assert new_count == last_count, \ ('We did not close all the files when iterating over one frame. ' 'old count: {}, new count: {}'.format(last_count, new_count)) count = open_files_count(path) assert count == 0, 'The temporary file was not closed! {}'.format(count)
def test_png_rgba_color(): # Read an indexed color PNG image, and compared with hardcoded values img = load(PNG_RGBA_COLOR) assert img.shape == (3, 22, 32) assert img[0, 0, 0] == 255 assert img[0, 17, 17] == 117
def test_get_skin_pixels(): """ Test the skin colored pixels detection """ # to run face detection import bob.ip.facedetect mod = sys.modules.get(__name__) or loader.load_module(__name__) # load face image face = load(datafile('001.jpg', 'bob.rppg.base')) from bob.rppg.ssr.ssr_utils import get_skin_pixels # zero threshold -> the number of skin pixels is the number of pixels in the cropped face skin_pixels = get_skin_pixels(face, 0, True, 0.0) bbox, quality = bob.ip.facedetect.detect_single_face(face) assert skin_pixels.shape[1] == (bbox.size[0] - 1) * bbox.size[1] # -1 because of the cropping # same as before, but with provided bbox bounding_boxes = [bbox] skin_pixels = get_skin_pixels(face, 0, True, 0, bounding_boxes) assert skin_pixels.shape[1] == (bbox.size[0] - 1) * bbox.size[1] # -1 because of the cropping # threshold of 1.0 -> zero skin pixels skin_pixels = get_skin_pixels(face, 0, True, 1, bounding_boxes) assert skin_pixels.shape[1] == 0
def test_can_use_array_interface(): from . import reader array = load(INPUT_VIDEO) iv = reader(INPUT_VIDEO) for frame_id, frame in zip(range(array.shape[0]), iv.__iter__()): assert numpy.array_equal(array[frame_id, :, :, :], frame)
def test_slicing_0(): from . import reader f = reader(INPUT_VIDEO) objs = f[:] for i, k in enumerate(load(INPUT_VIDEO)): assert numpy.allclose(k, objs[i])
def test_png_gray_alpha(): # Read a gray+alpha PNG image, and compared with hardcoded values img = load(PNG_GRAY_ALPHA) assert img.shape == (22, 32) assert img[0, 0] == 255 assert img[17, 17] == 51
def test_can_use_array_interface(): from . import reader array = load(INPUT_VIDEO) iv = reader(INPUT_VIDEO) for frame_id, frame in zip(range(array.shape[0]), iv.__iter__()): assert numpy.array_equal(array[frame_id,:,:,:], frame)
def test_iteration(): from . import reader f = reader(INPUT_VIDEO) objs = load(INPUT_VIDEO) nose.tools.eq_(len(f), len(objs)) for l, i in zip(objs, f): assert numpy.allclose(l, i)
def transcode(filename): tmpname = test_utils.temporary_filename(suffix=os.path.splitext(filename)[1]) try: # complete transcoding test image = load(filename) # save with the same extension write(image, tmpname) # reload the image from the file image2 = load(tmpname) assert numpy.array_equal(image, image2) finally: if os.path.exists(tmpname): os.unlink(tmpname)
def read_write_check(data, numpy_assert=True): """Testing loading and save different file types""" with tempfile.NamedTemporaryFile(prefix="bobtest_", suffix=".hdf5") as f: save(data, f.name) data2 = load(f.name) if numpy_assert: assert np.allclose(data, data2, atol=10e-5, rtol=10e-5) else: assert data == data2
def transcode(filename): tmpname = test_utils.temporary_filename( suffix=os.path.splitext(filename)[1]) try: # complete transcoding test image = load(filename) # save with the same extension write(image, tmpname) # reload the image from the file image2 = load(tmpname) assert numpy.array_equal(image, image2) finally: if os.path.exists(tmpname): os.unlink(tmpname)
def test_base_load_on_unicode(): try: # Writing temp file for testing tmpname = write_unicode_temp_file() from . import reader f = reader(tmpname) objs = load(tmpname) nose.tools.eq_(len(f), len(objs)) for l, i in zip(objs, f): assert numpy.allclose(l.shape, i.shape) finally: if os.path.exists(tmpname): os.unlink(tmpname)
from bob.ip.draw import box, cross from bob.ip.color import rgb_to_gray from bob.ip.facedetect import detect_single_face from bob.io.base import load import bob.io.image import matplotlib.pyplot as plt def get_data(f): from os.path import join from pkg_resources import resource_filename from bob.io.base import load return load(resource_filename('bob.ip.flandmark', join('data', f))) lena = load("Aaron_Eckhart_0001.jpg") lena_gray = rgb_to_gray(lena) bounding_box, quality = detect_single_face(lena) bounding_box = bounding_box.scale(1.2, True) y, x = bounding_box.topleft height, width = bounding_box.size width = height # x, y, width, height = [214, 202, 183, 183] # Manual annotations localizer = Flandmark() keypoints = localizer.locate(lena_gray, y, x, height, width) for k in keypoints: print(int(k[0])) print(int(k[1])) # draw the keypoints and bounding box box(lena, (y, x), (height, width), (255, 0, 0)) # red bounding box print(int(y))
def test_png_indexed_trns(): # Read an tRNS PNG image (without alpha), and compared with hardcoded values img = load(PNG_tRNS) assert img.shape == (3, 22, 32) assert img[0, 0, 0] == 255 assert img[0, 17, 17] == 117
def test_png_indexed_color_alpha(): # Read an indexed color+alpha PNG image, and compared with hardcoded values img = load(PNG_INDEXED_COLOR_ALPHA) assert img.shape == (3, 22, 32) assert img[0, 0, 0] == 255 assert img[0, 17, 17] == 117
def test_mat_file_io_does_not_crash(): data = load(test_utils.datafile('test_cell.mat', __name__))
def test_png_gray_alpha(): # Read a gray+alpha PNG image, and compared with hardcoded values img = load(PNG_GRAY_ALPHA) assert img.shape == (22,32) assert img[0,0] == 255 assert img[17,17] == 51
def test_png_indexed_trns(): # Read an tRNS PNG image (without alpha), and compared with hardcoded values img = load(PNG_tRNS) assert img.shape == (3,22,32) assert img[0,0,0] == 255 assert img[0,17,17] == 117
def test_png_indexed_color_alpha(): # Read an indexed color+alpha PNG image, and compared with hardcoded values img = load(PNG_INDEXED_COLOR_ALPHA) assert img.shape == (3,22,32) assert img[0,0,0] == 255 assert img[0,17,17] == 117
def file_loader(files, original_directory, original_extension): data = [] for path in files: d = load(os.path.join(original_directory, path + original_extension)) data.append(d) return data
def get_data(f): from os.path import join from pkg_resources import resource_filename from bob.io.base import load import bob.io.image return load(resource_filename('bob.ip.flandmark', join('data', f)))