-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathprocess_card.py
More file actions
executable file
·87 lines (60 loc) · 2.26 KB
/
process_card.py
File metadata and controls
executable file
·87 lines (60 loc) · 2.26 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
"""Process a card image to make it easier to match an unlabeled card to
the correct labeled card."""
import os
import sys
import cv2
import numpy as np
from common import PROCESS_CARD_OUT_DIR, write_im, display_im, shrink
from vendor import noteshrink
PROCESSED_CARD_FILENAME = "processed.jpg"
def to_pil(cv2_im):
pass
def to_cv2(pil_im):
pass
def blur_card(card_filename):
im = cv2.imread(card_filename, 1)
# filters to make it easier for opencv to find card
blur = cv2.GaussianBlur(im, (1, 1), 1)
filename = "%s_blurred.png" % (card_filename)
cv2.imwrite(filename, blur)
return filename
def noteshrink_card_from_im(card_im):
# TODO make this not have to write to file
tmp_file = write_im(card_im, "tmp.jpg")
noteshrunk_file = noteshrink_card_from_file(tmp_file)
noteshrunk_im = cv2.imread(noteshrunk_file, 1)
os.remove(noteshrunk_file)
return noteshrunk_im
def noteshrink_card_from_file(card_filename, shrink_max_dim=120):
img, dpi = noteshrink.load(card_filename)
img = shrink(img, max_dim=shrink_max_dim)
options = noteshrink.get_argument_parser(
# hack to give a required argument from outside sys.argv
filenames=[card_filename]
).parse_args()
options.num_colors = 2
options.white_bg = True
options.quiet = True
if img is None:
return
output_filename = "%s.out.png" % (card_filename)
samples = noteshrink.sample_pixels(img, options)
palette = noteshrink.get_palette(samples, options)
labels = noteshrink.apply_palette(img, palette, options)
noteshrink.save(output_filename, labels, palette, dpi, options)
return output_filename
def process_card(card_filename):
blurred = blur_card(card_filename)
shrunk_card = noteshrink_card_from_file(blurred)
# keypoints_card(shrunk_card)
# TODO return image in correct format
return
def main():
for unprocessed_card_file in sys.argv[1:]:
unprocessed_card = cv2.imread(unprocessed_card_file, 1)
processed_card = process_card(unprocessed_card_file)
# TODO cannot currently write bc noteshrink does not return a cv2 image
# write_im(processed_card, PROCESSED_CARD_FILENAME, PROCESS_CARD_OUT_DIR)
if __name__ == "__main__":
main()