-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.py
154 lines (122 loc) · 5.04 KB
/
decode.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
#
# Copyright (c) 2011 Ginkgo Bioworks Inc.
# Copyright (c) 2011 Daniel Taub
#
# This file is part of Scantelope.
#
# Scantelope 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/>.
"""
Data matrix decoding module for Scantelope.
"""
from os.path import exists as fexist
#curdir, sep, path
EXPECTED_LEN = 10
from time import time
from pydmtx import DataMatrix, Image
import findcode
import cv
class DMDecoder():
do_display = False
verbose = False
totalTime = time()
def resettime(self):
self.totalTime = time()
def __init__(self,myDir = None,files = None):
if myDir != None:
self.myDir = myDir
if files != None:
self.files = [f for f in files if fexist(self.myDir+f)]
self.output = {}
self.failed = []
self.status = ''
self.resettime()
def parseImages(self, files = None):
if files != None:
self.files = files
n=0
m=0
failNum=0
lastCVTime = 0
timeForCV = 0
print "\nFiles to decode: ",len(self.files)
stop = False
# import pdb;pdb.set_trace()
for filename in self.files:
is_found = False
# if filename.find('/') != -1:
# self.myDir,filename = path.split(filename)
# self.myDir += '/'
lastCVTime = time()
cv_orig,cv_smoo,cv_final = findcode.findAndOrient(self.myDir,
filename,
self.do_display,
self.verbose)
timeForCV += (time() - lastCVTime)
cv.SaveImage(self.myDir+filename.replace('tif','jpg'),cv_final)
test = cv.Avg(cv_final)
if stop == True:
pdb.set_trace()
if test[0] < 130 and test[0] > 40: # hard threshold works for avision
for img,name in [#[cv_smoo,"smooth"], #seems to introduce many errors
[cv_final,"clipped"],
[cv_orig,"original"]]:
if is_found:
break
dmtx_im = Image.fromstring("L", cv.GetSize(img), img.tostring())
if name != "original":
padding = 0.1
else:
padding = 0
ncols,nrows = dmtx_im.size
padw = (ncols)*padding
padh = (nrows)*padding
isize = (int(round(ncols+2*padw)),int(round(nrows+2*padh)))
# Create a new color image onto which we can paste findcode output
dmtx_image = Image.new('RGB',isize,0)
bbox = (round(padw),round(padh),ncols+round(padw),nrows+round(padh))
dmtx_image.paste(dmtx_im,map(int,bbox))
(width, height) = dmtx_image.size
# Send to datamatrix library
if findcode.low_res:
dm_read = DataMatrix(max_count = 1, timeout = 300, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10)
else:
dm_read = DataMatrix(max_count = 1, timeout = 300, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10, shrink = 2)
#dm_read = DataMatrix(max_count = 1, timeout = 300, shape = DataMatrix.DmtxSymbol12x12, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10)
dmtx_code = dm_read.decode (width, height, buffer(dmtx_image.tostring()))
if dmtx_code is not None and len(dmtx_code) == EXPECTED_LEN:
how = "Quick Search: "+str(name)
is_found = True
out = dmtx_code
if not is_found:
self.failed.append(filename)
else:
failNum+=1
if is_found:
n+=1
self.output[filename] = out
#print filename, out, test[0]
else:
#print filename, None, test[0]
pass
#self.failed.append(filename)
m+=1
print failNum, "failed to produce images worth decoding"
print n,"of the",m-failNum,"remaining were successfully decoded."
self.status += "\nFound %d of %d in "%(n,m)
self.status += str(time()-self.totalTime)+" seconds.\n"
self.status += "(OpenCV: "+str(timeForCV)+" sec)\n"
if not(len(self.failed) == 0):# and verbose:
dirr = ' '+self.myDir
self.status+= "\nmissing: "+str(self.failed)+'\n'
return self.output,self.failed,self.status