-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
executable file
·164 lines (128 loc) · 4.92 KB
/
process.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
#!/usr/bin/python
#
# 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/>.
"""
batch processing script, unused from server, but can be used manually.
"""
import sys,os, subprocess
from pydmtx import DataMatrix, Image
from pdb import set_trace as st
import findcode
import time
import cv
findcode.low_res = False
EXPECTED_LEN = 10
if findcode.low_res:
SHRINK = 1
else:
SHRINK = 2
myDir='/tmp/'
pref='split'
ext='.png'
if __name__ == '__main__':
do_display = False
verbose = False
totalTime = time.time()
failed = []
files = None
if len(sys.argv) > 1:
do_display = bool(int(sys.argv[1]))
if len(sys.argv) > 2:
# if only one arg and it has no number in it, assume it's the prefix and suffix only
if len(sys.argv) == 3 and not reduce(lambda x,y:x or y,map(lambda x: x.isdigit(),sys.argv[2]),False):
splt = sys.argv[2].split('.')
pref = splt[0]
if pref.find('/') != -1:
myDir,pref = os.path.split(pref)
ext = '.'+splt[1]
else:
files = sys.argv[2:]
myDir += '/'
if files == None:
files = [i for i in os.listdir(myDir) if i.find(pref) != -1 and ''.join(i.split(pref)).rstrip(ext).isdigit()]
files.sort()
n=0
m=0
lastCVTime = 0
timeForCV = 0
for filename in files:
is_found = False
if filename.find('/') != -1:
myDir,filename = os.path.split(filename)
myDir += '/'
lastCVTime = time.time()
cv_orig,cv_smoo,cv_final = findcode.findAndOrient(myDir,filename,do_display, verbose)
timeForCV += (time.time() - lastCVTime)
for img,name in [#[cv_smoo,"smooth"],
[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
padding = 0.2
ncols,nrows = dmtx_im.size
padw = (ncols)*padding
padh = (nrows)*padding
isize = (round(ncols+2*padw),round(nrows+2*padh))#cols*photow+padw,nrows*photoh+padho)
# Create the new image. The background doesn't have to be white
dmtx_image = Image.new('RGB',isize,0)
bbox = (round(padw),round(padh),ncols+round(padw),nrows+round(padh))
dmtx_image.paste(dmtx_im,bbox)
# dmtx_im.save("/tmp/t1.png")
# dmtx_image.save("/tmp/t2.png")
# import pdb;pdb.set_trace()
(width, height) = dmtx_image.size
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, min_edge = 20, max_edge = 32, threshold = 5, deviation = 10, shrink = SHRINK)
#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
else:
dmtx_im.save("/tmp/t1.png")
dmtx_image.save("/tmp/t2.png")
#import pdb;pdb.set_trace()
out = dmtx_code
# if not is_found:
# dm_read = DataMatrix(max_count = 1, timeout = 300, 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(2): "+str(name)
# is_found = True
# out = dmtx_code
if is_found:
n+=1
print filename, out, how
else:
print filename, None
failed.append(filename)
m+=1
print "\nFound %d of %d in "%(n,m),time.time()-totalTime," seconds.",
print "(OpenCV:",timeForCV," sec)\n"
if not(len(failed) == 0):# and verbose:
dirr = ' '+myDir
print dirr+dirr.join(failed)