Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_read_empty_dir():
# Create an empty dir
empty = os.path.join(test_dir, "empty_dir")
if not os.path.isdir(empty):
os.mkdir(empty)
# Test that no format is found, but no error is raised
request = core.Request(empty, "ri")
assert imageio.formats.search_read_format(request) is None
def test_invalid():
need_internet()
fname1 = get_remote_file("images/stent.swf", test_dir)
fname2 = fname1[:-4] + ".invalid.swf"
# Empty file
with open(fname2, "wb"):
pass
assert not imageio.formats.search_read_format(core.Request(fname2, "rI"))
raises(IOError, imageio.mimread, fname2, "swf")
# File with BS data
with open(fname2, "wb") as f:
f.write(b"x" * 100)
assert not imageio.formats.search_read_format(core.Request(fname2, "rI"))
raises(IOError, imageio.mimread, fname2, "swf")
with raises(IndexError):
[im for im in R]
# Test writer no format
raises(ValueError, imageio.get_writer, "foo.unknownext")
# Test streaming reader
R = F.get_reader(Request(filename1, "ri"))
R._stream_mode = True
assert R.get_length() == np.inf
ims = [im for im in R]
assert len(ims) == 5
# Test using writer
im1 = np.zeros((10, 10))
im2 = imageio.core.Image(im1, {"foo": 1})
W = F.get_writer(Request(filename2, "wi"))
W.append_data(im1)
W.append_data(im2)
W.append_data(im1, {"bar": 1})
W.append_data(im2, {"bar": 1})
# Test that no data is copies (but may be different views)
assert len(W._written_data) == 4
for im in W._written_data:
assert (im == im1).all()
im1[2, 2] == 99
for im in W._written_data:
assert (im == im1).all()
# Test meta
assert W._written_meta[0] == {}
assert W._written_meta[1] == {"foo": 1}
assert W._written_meta[2] == {"bar": 1}
def test_select():
fname1 = get_remote_file("images/cockatoo.mp4", test_dir)
F = imageio.formats["avbin"]
assert F.name == "AVBIN"
assert F.can_read(core.Request(fname1, "rI"))
assert not F.can_write(core.Request(fname1, "wI"))
assert not F.can_read(core.Request(fname1, "ri"))
assert not F.can_read(core.Request(fname1, "rv"))
def setup_module():
try:
imageio.plugins.avbin.download()
except imageio.core.InternetNotAllowedError:
pass
def test_freeimage_format():
# Format
F = imageio.formats["PNG-FI"]
assert F.name == "PNG-FI"
# Reader
R = F.get_reader(core.Request("imageio:chelsea.png", "ri"))
assert len(R) == 1
assert isinstance(R.get_meta_data(), dict)
assert isinstance(R.get_meta_data(0), dict)
raises(IndexError, R.get_data, 2)
raises(IndexError, R.get_meta_data, 2)
# Writer
W = F.get_writer(core.Request(fnamebase + ".png", "wi"))
W.append_data(im0)
W.set_meta_data({"foo": 3})
raises(RuntimeError, W.append_data, im0)
if decoded.get("detection_classes_names"):
for cls in decoded["detection_classes_names"]:
if cls in statistics[cur_scene_idx]["cur_scene_statistics"]:
statistics[cur_scene_idx]["cur_scene_statistics"][cls] += 1
else:
statistics[cur_scene_idx]["cur_scene_statistics"][cls] = 1
# Extract feature
if frame_idx in middle_frame:
# Create new scene
index_scene_feature(rpc_client, video, img, frame, frame_idx, middle_time, scene_list)
# Update pbar
pbar.update(1)
frame_idx += 1
except imageio.core.format.CannotReadFrameError:
print("io error caught")
pass
# Close feature client
rpc_client.close()
# Save the pickle file
pkl_name = str(video.video_file).split("/")[-1].split(".")[0] + "_index.pkl"
pkl_path = osp.join(settings.MEDIA_ROOT, "multi_features", pkl_name)
with open(pkl_path, "wb") as f:
pickle.dump(scene_list, f)
video.pkl_path = pkl_path
# Save the statistics
statistics_name = str(video.video_file).split("/")[-1].split(".")[0] + "_statistics.json"
statistics_path = osp.join(settings.MEDIA_ROOT, "statistics/" + statistics_name)
with open(statistics_path, "w") as f:
json.dump(statistics, f)
if op.isdir(resource_dir):
shutil.rmtree(resource_dir)
os.mkdir(resource_dir)
open(op.join(resource_dir, "shipped_resources_go_here"), "wb")
# Load images
for fname in [
"images/chelsea.png",
"images/chelsea.zip",
"images/astronaut.png",
"images/newtonscradle.gif",
"images/cockatoo.mp4",
"images/realshort.mp4",
"images/stent.npz",
]:
imageio.core.get_remote_file(fname, resource_dir, force_download=True)
def prepare_reader_and_witer():
""" Prepare Format.Reader and Format.Writer for doc generation.
"""
# Create Reader and Writer subclasses that are going to be placed
# in the format module so that autoclass can find them. They need
# to be new classes, otherwise sphinx considers them aliases.
# We create the class using type() so that we can copy the __doc__.
Reader = type(
"Reader",
(imageio.core.format.Format.Reader,),
{"__doc__": imageio.core.format.Format.Reader.__doc__},
)
Writer = type(
"Writer",
(imageio.core.format.Format.Writer,),
{"__doc__": imageio.core.format.Format.Writer.__doc__},
)
imageio.core.format.Reader = Reader
imageio.core.format.Writer = Writer
# We set the docs of the original classes, and remove the original
# classes so that Reader and Writer do not show up in the docs of
# the Format class.
imageio.core.format.Format.Reader = None # .__doc__ = ''
imageio.core.format.Format.Writer = None # .__doc__ = ''
def run(self):
# Download images and libs
import imageio
resource_dir = imageio.core.resource_dirs()[0]
_set_crossplatform_resources(resource_dir)
_set_platform_resources(resource_dir, imageio.core.get_platform())
# Build as normal
build_py.run(self)