-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr.py
70 lines (56 loc) · 2.38 KB
/
sr.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
68
69
70
import click
import cv2
from sr.code import EncodeError
from sr.code import generate as _generate
from sr.decode import decode as _decode
from sr.decode import decode_video
from sr.image import horizontal_concat, pressed_quit
@click.group()
def cli():
pass
@cli.command()
@click.option("--size", "-s", type=int, default=600, help="Size in pixels")
@click.option("--message", "-m", type=str, required=True, help="The message to encode")
@click.argument("output_file")
def generate(size, message, output_file):
"""Generate an SR code."""
try:
image = _generate(size, message)
except EncodeError as e:
click.secho(e, fg='red')
else:
cv2.imwrite(output_file, image)
@cli.command()
@click.option("--visualize/--no-visualize", default=True, help="Show/hide OpenCV visualization.")
@click.option("--find-all/--no-find-all", default=True, help="Find all SR codes.")
@click.option("--use-bw/--no-use-bw", default=False, help="Show blakc&white or rgb visualization.")
@click.argument("input_file", type=click.File())
def decode(input_file, visualize, find_all, use_bw):
"""Decode from an image."""
image = cv2.imread(input_file.name)
result = _decode(image, find_all, use_bw)
click.echo(result)
if not visualize:
return
cv2.namedWindow('SR code', cv2.WINDOW_KEEPRATIO)
visualization = result.contour_visualization
if result.success:
for vis in result.get_visualizations():
visualization = horizontal_concat(visualization, vis)
cv2.imshow('SR code', visualization)
while not pressed_quit():
pass
cv2.destroyAllWindows()
@cli.command()
@click.option("--visualize/--no-visualize", default=True, help="Show/hide OpenCV visualization.")
@click.option("--verbose/--no-verbose", default=True, help="Print decode result for every frame.")
@click.option("--stop-on-success/--no-stop-on-success", default=False, help="Stop on a successful read.")
@click.argument("input_file", type=click.File(), required=False)
def video(input_file, visualize, verbose, stop_on_success):
"""Decode from a video/camera stream.
To open a camera stream, leave the INPUT_FILE empty.
Press 'b' while running to switch between color and black&white mode.
"""
# '0' signals OpenCV to open a camera stream
filename = input_file.name if input_file else 0
decode_video(filename, visualize, verbose, stop_on_success)