This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
scan_card.py
224 lines (184 loc) · 6.05 KB
/
scan_card.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
copyright 2013-2014 Talin Salway
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import math
import cv
import os
import sqlite3
import numpy
import cv2
from detect_card import detect_card
from cv_utils import float_version, show_scaled, sum_squared, ccoeff_normed
import config
def get_card(color_capture, corners):
target = [(0,0), (223,0), (223,310), (0,310)]
mat = cv.CreateMat(3,3, cv.CV_32FC1)
cv.GetPerspectiveTransform(corners, target, mat)
warped = cv.CloneImage(color_capture)
cv.WarpPerspective(color_capture, warped, mat)
cv.SetImageROI(warped, (0,0,223,310) )
return warped
#*****************
#this is the watch-for-card bit
captures = []
def card_window_clicked(event, x, y, flags, param):
if event == 6:
#delete capture array indexed at param, update windows
global captures
del captures[param]
update_windows()
def update_windows(n=3):
#print "update windows!"
l = len(captures)
for i in xrange(1,min(n,l)+1):
#print "setting ",i
tmp = cv.CloneImage(captures[-i])
cv.PutText(tmp, "%s" % (l-i+1), (1,24), font, (255,255,255))
cv.ShowImage("card_%d" % i, tmp)
cv.SetMouseCallback("card_%d" % i, card_window_clicked, l - i)
def watch_for_card(camera):
has_moved = False
been_to_base = False
global captures
global font
captures = []
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0)
img = cv.QueryFrame(camera)
size = cv.GetSize(img)
n_pixels = size[0]*size[1]
grey = cv.CreateImage(size, 8,1)
recent_frames = [cv.CloneImage(grey)]
base = cv.CloneImage(grey)
cv.CvtColor(img, base, cv.CV_RGB2GRAY)
#cv.ShowImage('card', base)
tmp = cv.CloneImage(grey)
while True:
img = cv.QueryFrame(camera)
cv.CvtColor(img, grey, cv.CV_RGB2GRAY)
biggest_diff = max(sum_squared(grey, frame) / n_pixels for frame in recent_frames)
#display the cam view
cv.PutText(img, "%s" % biggest_diff, (1,24), font, (255,255,255))
cv.ShowImage('win',img)
recent_frames.append(cv.CloneImage(grey))
if len(recent_frames) > 3:
del recent_frames[0]
#check for keystroke
c = cv.WaitKey(10)
#if there was a keystroke, reset the last capture
if c == 27:
return captures
elif c == 32:
has_moved = True
been_to_base = True
elif c == 114:
base = cv.CloneImage(grey)
#if we're stable-ish
if biggest_diff < 10:
#if we're similar to base, update base
#else, check for card
#base_diff = max(sum_squared(base, frame) / n_pixels for frame in recent_frames)
base_corr = min(ccoeff_normed(base, frame) for frame in recent_frames)
#cv.ShowImage('debug', base)
"""for i, frame in enumerate(recent_frames):
tmp = cv.CloneImage(base)
cv.Sub(base, frame, tmp)
cv.Pow(tmp, tmp, 2.0)
cv.PutText(tmp, "%s" % (i+1), (1,24), font, (255, 255, 255))
#my_diff = sum_squared(base, frame) / n_pixels
my_diff = ccoeff_normed(base, frame) #score(base, frame, cv.CV_TM_CCOEFF_NORMED)
cv.PutText(tmp, "%s" % my_diff, (40, 24), font, (255, 255, 255))
cv.ShowImage('dbg%s' % (i+1), tmp)"""
#print "stable. corr = %s. moved = %s. been_to_base = %s" % (base_corr, has_moved, been_to_base)
if base_corr > 0.75 and not been_to_base:
base = cv.CloneImage(grey)
# cv.ShowImage('debug', base)
has_moved = False
been_to_base = True
print "STATE: been to base. waiting for move"
elif has_moved and been_to_base:
corners = detect_card(grey, base)
if corners is not None:
card = get_card(grey, corners)
cv.Flip(card,card,-1)
captures.append(card)
update_windows()
#cv.ShowImage('card', card)
has_moved = False
been_to_base = False
print "STATE: detected. waiting for go to base"
else:
if not has_moved:
print "STATE: has moved. waiting for stable"
has_moved = True
def setup_windows():
cv.NamedWindow('card_1')
cv.NamedWindow('card_2')
cv.NamedWindow('card_3')
#cv.NamedWindow('base')
cv.NamedWindow('win')
#cv.StartWindowThread()
'''
import cv
import scan_card
base = cv.LoadImage("base.png", 0)
known = cv.LoadImage("known/swamp_m12_03.jpg")
capture = cv.LoadImage("swamp_02.png", 0)
corners = scan_card.detect_card(capture, base)
card = scan_card.get_card(cv.LoadImage("swamp_02.png"), corners)
cv.NamedWindow("win")
cv.StartWindowThread()
cv.ShowImage("win", card)
'''
'''
test 1
base = cv.LoadImage("base.png", 0)
capture = cv.LoadImage("swamp_02.png", 0)
corners = scan_card.detect_card(capture, base)
corners should not be None
corners should be close to [(253, 44), (503, 44), (530, 400), (244, 402)]
test 2
base = cv.LoadImage("base_03.png", 0)
capture = cv.LoadImage("swamp_03.png", 0)
corners = scan_card.detect_card(capture, base)
corners should not be none
corners should be close to [(167, 126), (384, 69), (460, 366), (235, 423)]
'''
'''
for dirname, dirnames, filenames in os.walk('known'):
for filename in filenames:
path = os.path.join(dirname, filename)
img = cv.LoadImage(path,0)
cv.SetImageROI(img, (0,0,223,310))
known.append( (path, img) )
r = cv.CreateMat(1, 1, cv.CV_32FC1)
'''
'''
import cv
import scan_card
cv.NamedWindow('win')
cv.NamedWindow('base')
cv.NamedWindow('card')
cv.StartWindowThread()
cam = cv.CreateCameraCapture(0)
scan_card.watch_for_card(cam)
'''
'''
cards = scan_card.load_sets(base_dir, ['ISD', 'DKA'])
c2 = [(name, scan_card.gradient(the_card)[1]) for name, the_card in cards]
for i in xrange(9):
card = cv.LoadImage('captures/card_%04d.png' % i,0)
cv.ShowImage('card',card); g = scan_card.gradient(card)[1]
f = sorted([(score(g, the_card_g, cv.CV_TM_CCOEFF), name) for name,the_card_g in c2], reverse=True)[0:5]
print f
raw_input()
'''