Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Efficient-image-loading/show_image.py
3118 views
1
from argparse import ArgumentParser
2
3
import cv2
4
5
from loader import (
6
CV2Loader,
7
LmdbLoader,
8
PILLoader,
9
TFRecordsLoader,
10
TurboJpegLoader,
11
methods,
12
)
13
14
15
def show_image(method, image):
16
cv2.imshow(f"{method} image", image)
17
k = cv2.waitKey(0) & 0xFF
18
if k == 27: # check ESC pressing
19
return True
20
else:
21
return False
22
23
24
def show_images(loader):
25
num_images = len(loader)
26
loader = iter(loader)
27
for idx in range(num_images):
28
image, time = next(loader)
29
print_info(image, time)
30
stop = show_image(type(loader).__name__, image)
31
if stop:
32
cv2.destroyAllWindows()
33
return
34
35
36
def print_info(image, time):
37
print(
38
f"Image with {image.shape[0]}x{image.shape[1]} size has been loading for {time} seconds",
39
)
40
41
42
def demo(method, path):
43
loader = methods[method](path) # get the image loader
44
show_images(loader)
45
46
47
if __name__ == "__main__":
48
parser = ArgumentParser()
49
50
parser.add_argument(
51
"--path",
52
"-p",
53
type=str,
54
help="path to image, folder of images, lmdb environment path or tfrecords database path",
55
)
56
parser.add_argument(
57
"--method",
58
required=True,
59
choices=["cv2", "pil", "turbojpeg", "lmdb", "tfrecords"],
60
help="Image loading methods to use in benchmark",
61
)
62
63
args = parser.parse_args()
64
65
demo(args.method, args.path)
66
67