-
Notifications
You must be signed in to change notification settings - Fork 0
/
efficiency.py
67 lines (53 loc) · 1.68 KB
/
efficiency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import importlib
import sys
import time
from typing import Callable, Optional
from numpy import ndarray
from skimage import io, color
import skimage
def load(path: str) -> ndarray:
raw = io.imread(path)
if raw.ndim == 3 and raw.shape[-1] == 4:
raw = color.rgba2rgb(raw)
return raw
path = [f'images/{x}' for x in ['office.jpg',
'chelsea.png',
'cameraman.tif',
'gantrycrane.png',
'chessboard.png',
'coins.png']]
images = [load(p) for p in path]
images.append(skimage.data.page())
images.append(skimage.data.text())
def count_time(func: Callable):
def wrapper(*args):
start = time.time()
func(*args)
end = time.time()
print(round(end - start, 2))
return wrapper
@count_time
def run_single(image_func: Callable):
for img in images:
result = image_func(img)
def parse_func(mod: str) -> Optional[Callable]:
tokens = mod.lstrip('.\\').split('.')
if len(tokens) >= 2:
module = importlib.import_module(tokens[0])
return vars(module).get(tokens[1], None)
def to_gray_scale() -> None:
global images
for i, img in enumerate(images):
if img.ndim == 3:
images[i] = color.rgb2gray(img)
if __name__ == '__main__':
for arg in sys.argv[1:]:
if arg == '-g':
to_gray_scale()
continue
func = parse_func(arg)
if func is not None:
print(f'{func.__module__}.{func.__name__}')
run_single(func)
else:
print(f'cannot load module {arg}', file=sys.stderr)