This repository was archived by the owner on Dec 18, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
138 lines (115 loc) · 4.34 KB
/
Copy pathcli.py
File metadata and controls
138 lines (115 loc) · 4.34 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import argparse
import cv2 as cv
from circle_detection import detect_pizzas
from file_handling import get_pizza_centre, load_pkl, write_pkl
from fingerprinting import calc_fingerprint, similarity
PRINTS_FILE = 'prints.pkl'
def display_circles(circles, image):
if len(
circles): # Check if circles have been found and only then iterate over these and add them to the image
for circle in circles:
circle = tuple(int(x) for x in circle)
cv.circle(image, (circle[0], circle[1]), circle[2], (0, 0, 255), 3,
cv.LINE_AA)
cv.circle(image, (circle[0], circle[1]), 2, (0, 255, 0), 3,
cv.LINE_AA) # draw center of circle
cv.imshow("detected circles", image)
cv.waitKey(0)
def guess_pizza_circle(img):
radius = max(img.shape) / 3
return \
int(img.shape[1]/2), int(img.shape[0]/2), radius
def collect_data(
file_names,
detect_circles=False,
show_edges=False,
show_fingers=False,
show_circles=False,
prints_file=PRINTS_FILE,
):
saved_prints = load_pkl(prints_file)
# if we're displaying, go through all of them
if any([show_edges, show_fingers, show_circles]):
to_process = file_names
else:
# otherwise only process the ones we need to process
to_process = set(file_names) - set(saved_prints.keys())
if not to_process:
print("Nothing to process!")
for file_name in to_process:
print('Calculating', file_name)
img = cv.imread(file_name, 1)
if detect_circles:
circles = detect_pizzas(img)
else:
circles = [guess_pizza_circle(img)]
if show_circles:
display_circles(circles, img.copy())
if len(circles):
fingerprint = calc_fingerprint(
img,
circles[0],
show_edges=show_edges,
show_fingers=show_fingers,
)
if not detect_circles: # (only save if we've manually done the circles)
saved_prints[file_name] = fingerprint
else:
print(f"No circles in {file_name}")
write_pkl(prints_file, saved_prints)
# display_circles(circles, img.copy())
def find_nearest(file_names, prints_file=PRINTS_FILE, show_similars=False):
print("finding nearest...")
saved_prints = load_pkl(prints_file)
# print("prints", saved_prints)
for file_name in file_names:
samenesses = []
candidate = saved_prints[file_name]
for compare_file_name, comparison in saved_prints.items():
if file_name == compare_file_name:
continue
sameness = similarity(candidate, comparison)
samenesses.append((sameness, compare_file_name))
samenesses.sort()
print([(0, file_name)] + [(s,f) for s,f in samenesses if s < 18000])
if show_similars:
samest, samest_file_name = samenesses[0]
img = cv.imread(file_name)
calc_fingerprint(
img,
guess_pizza_circle(img),
show_fingers='Candidate'
)
img = cv.imread(samest_file_name)
calc_fingerprint(
img,
guess_pizza_circle(img),
show_fingers='Closest'
)
cv.waitKey(0)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='+')
parser.add_argument('--detect_circles', action='store_true', default=False)
parser.add_argument('--show_circles', action='store_true', default=False)
parser.add_argument('--show_edges', action='store_true', default=False)
parser.add_argument('--show_fingers', action='store_true', default=False)
parser.add_argument('--show_similars', action='store_true', default=False)
parser.add_argument('--prints', default=PRINTS_FILE)
args = parser.parse_args()
file_names = args.files
collect_data(
file_names,
detect_circles=args.detect_circles,
show_edges=args.show_edges,
show_fingers=args.show_fingers,
show_circles=args.show_circles,
prints_file=args.prints,
)
find_nearest(
file_names,
prints_file=args.prints,
show_similars=args.show_similars,
)
if __name__ == '__main__':
main()