mirror of https://github.com/NaN-tic/nanscan.git
Now we work with millimeters instead of pixels. This should
allow us work with images scanned at different resolutions.
This commit is contained in:
parent
32bb3f1aa8
commit
7bca063e2e
18
barcode.py
18
barcode.py
|
@ -17,6 +17,9 @@
|
||||||
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
|
from PyQt4.QtGui import *
|
||||||
|
# Do not import everything as Template is defined in string too
|
||||||
|
from string import lower
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
@ -50,10 +53,13 @@ class Barcode:
|
||||||
for line in content.splitlines():
|
for line in content.splitlines():
|
||||||
pieces = line.split( ' ' )
|
pieces = line.split( ' ' )
|
||||||
box = Box()
|
box = Box()
|
||||||
box.text = pieces[0]
|
box.text = lower(pieces[0])
|
||||||
box.type = pieces[2]
|
box.type = pieces[2]
|
||||||
pos = pieces[4].strip( '()]' ).split(',')
|
pos = pieces[4].strip( '()]' ).split(',')
|
||||||
box.position = QPointF( float(pos[0]), float(pos[1]))
|
|
||||||
|
x = float(pos[0]) / self.dotsPerMillimeterX
|
||||||
|
y = float(pos[1]) / self.dotsPerMillimeterY
|
||||||
|
box.position = QPointF( x, y )
|
||||||
self.boxes.append( box )
|
self.boxes.append( box )
|
||||||
|
|
||||||
def printBoxes(self):
|
def printBoxes(self):
|
||||||
|
@ -64,12 +70,18 @@ class Barcode:
|
||||||
for x in self.boxes:
|
for x in self.boxes:
|
||||||
if region.contains(x.position):
|
if region.contains(x.position):
|
||||||
return unicode(x.text)
|
return unicode(x.text)
|
||||||
# We always return unicode strings
|
# Always return unicode strings
|
||||||
return u''
|
return u''
|
||||||
|
|
||||||
def scan(self, file):
|
def scan(self, file):
|
||||||
# Clean boxes so scan() can be called more than once
|
# Clean boxes so scan() can be called more than once
|
||||||
self.boxes = []
|
self.boxes = []
|
||||||
|
|
||||||
|
# Obtain image resolution
|
||||||
|
image = QImage( file )
|
||||||
|
self.dotsPerMillimeterX = float( image.dotsPerMeterX() ) / 1000.0
|
||||||
|
self.dotsPerMillimeterY = float( image.dotsPerMeterY() ) / 1000.0
|
||||||
|
|
||||||
command = '/home/albert/d/git/exact-image-0.5.0/objdir/frontends/bardecode'
|
command = '/home/albert/d/git/exact-image-0.5.0/objdir/frontends/bardecode'
|
||||||
content = self.spawn( command, file )
|
content = self.spawn( command, file )
|
||||||
self.parseBardecodeOutput( content )
|
self.parseBardecodeOutput( content )
|
||||||
|
|
20
ocr.py
20
ocr.py
|
@ -17,6 +17,7 @@
|
||||||
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
# Do not import everything as Template is defined in string too
|
||||||
from string import lower
|
from string import lower
|
||||||
import codecs
|
import codecs
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -48,6 +49,7 @@ class Ocr:
|
||||||
output = []
|
output = []
|
||||||
# Output example line: "w 116 1724 133 1736"
|
# Output example line: "w 116 1724 133 1736"
|
||||||
# Coordinates start at bottom left corner but we convert this into top left.
|
# Coordinates start at bottom left corner but we convert this into top left.
|
||||||
|
# Convert pixel coordinates into millimeters too.
|
||||||
for x in input.split('\n'):
|
for x in input.split('\n'):
|
||||||
if not x:
|
if not x:
|
||||||
continue
|
continue
|
||||||
|
@ -61,6 +63,11 @@ class Ocr:
|
||||||
|
|
||||||
c = Character()
|
c = Character()
|
||||||
c.character = line[0]
|
c.character = line[0]
|
||||||
|
|
||||||
|
x1 = float(x1) / self.dotsPerMillimeterX
|
||||||
|
width = float(width) / self.dotsPerMillimeterX
|
||||||
|
y2 = float(y2) / self.dotsPerMillimeterY
|
||||||
|
height = float(height) / self.dotsPerMillimeterY
|
||||||
c.box = QRectF( x1, y2, width, height )
|
c.box = QRectF( x1, y2, width, height )
|
||||||
output.append( c )
|
output.append( c )
|
||||||
return output
|
return output
|
||||||
|
@ -89,13 +96,11 @@ class Ocr:
|
||||||
onebit.save_tiff(output)
|
onebit.save_tiff(output)
|
||||||
|
|
||||||
def scan(self, file):
|
def scan(self, file):
|
||||||
# Loading
|
image = QImage( file )
|
||||||
image = load_image(file)
|
self.width = image.width()
|
||||||
self.width = image.data.ncols
|
self.height = image.height()
|
||||||
self.height = image.data.nrows
|
self.dotsPerMillimeterX = float( image.dotsPerMeterX() ) / 1000.0
|
||||||
info = image_info(file)
|
self.dotsPerMillimeterY = float( image.dotsPerMeterY() ) / 1000.0
|
||||||
self.xResolution = info.x_resolution
|
|
||||||
self.yResolution = info.y_resolution
|
|
||||||
|
|
||||||
self.convertToBinary(file, '/tmp/tmp.tif')
|
self.convertToBinary(file, '/tmp/tmp.tif')
|
||||||
#self.convertIntoValidInput(file, '/tmp/tmp.tif')
|
#self.convertIntoValidInput(file, '/tmp/tmp.tif')
|
||||||
|
@ -108,4 +113,3 @@ class Ocr:
|
||||||
def initOcrSystem():
|
def initOcrSystem():
|
||||||
init_gamera()
|
init_gamera()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -77,11 +77,13 @@ class Recognizer(QObject):
|
||||||
|
|
||||||
# Synchronous: Returns the types of analyzers available
|
# Synchronous: Returns the types of analyzers available
|
||||||
def scan(self, file):
|
def scan(self, file):
|
||||||
|
self.fileName = file
|
||||||
self.barcode.scan( file )
|
self.barcode.scan( file )
|
||||||
self.ocr.scan( file )
|
self.ocr.scan( file )
|
||||||
|
|
||||||
# Asynchronous: Starts analyzers in background threads. Emits finished() at the end
|
# Asynchronous: Starts analyzers in background threads. Emits finished() at the end
|
||||||
def startScan(self, file):
|
def startScan(self, file):
|
||||||
|
self.fileName = file
|
||||||
self.ocrThread = Analyze( self.ocr, file, self )
|
self.ocrThread = Analyze( self.ocr, file, self )
|
||||||
self.barcodeThread = Analyze( self.barcode, file, self )
|
self.barcodeThread = Analyze( self.barcode, file, self )
|
||||||
self.connect( self.ocrThread, SIGNAL('finished()'), self.scanFinished )
|
self.connect( self.ocrThread, SIGNAL('finished()'), self.scanFinished )
|
||||||
|
|
Loading…
Reference in New Issue