Now we work with millimeters instead of pixels. This should

allow us work with images scanned at different resolutions.
This commit is contained in:
Albert Cervera i Areny 2008-05-12 17:55:51 +02:00
parent 32bb3f1aa8
commit 7bca063e2e
3 changed files with 29 additions and 11 deletions

View File

@ -17,6 +17,9 @@
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 tempfile
@ -50,10 +53,13 @@ class Barcode:
for line in content.splitlines():
pieces = line.split( ' ' )
box = Box()
box.text = pieces[0]
box.text = lower(pieces[0])
box.type = pieces[2]
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 )
def printBoxes(self):
@ -64,12 +70,18 @@ class Barcode:
for x in self.boxes:
if region.contains(x.position):
return unicode(x.text)
# We always return unicode strings
# Always return unicode strings
return u''
def scan(self, file):
# Clean boxes so scan() can be called more than once
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'
content = self.spawn( command, file )
self.parseBardecodeOutput( content )

20
ocr.py
View File

@ -17,6 +17,7 @@
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import os
# Do not import everything as Template is defined in string too
from string import lower
import codecs
import tempfile
@ -48,6 +49,7 @@ class Ocr:
output = []
# Output example line: "w 116 1724 133 1736"
# 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'):
if not x:
continue
@ -61,6 +63,11 @@ class Ocr:
c = Character()
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 )
output.append( c )
return output
@ -89,13 +96,11 @@ class Ocr:
onebit.save_tiff(output)
def scan(self, file):
# Loading
image = load_image(file)
self.width = image.data.ncols
self.height = image.data.nrows
info = image_info(file)
self.xResolution = info.x_resolution
self.yResolution = info.y_resolution
image = QImage( file )
self.width = image.width()
self.height = image.height()
self.dotsPerMillimeterX = float( image.dotsPerMeterX() ) / 1000.0
self.dotsPerMillimeterY = float( image.dotsPerMeterY() ) / 1000.0
self.convertToBinary(file, '/tmp/tmp.tif')
#self.convertIntoValidInput(file, '/tmp/tmp.tif')
@ -108,4 +113,3 @@ class Ocr:
def initOcrSystem():
init_gamera()

View File

@ -77,11 +77,13 @@ class Recognizer(QObject):
# Synchronous: Returns the types of analyzers available
def scan(self, file):
self.fileName = file
self.barcode.scan( file )
self.ocr.scan( file )
# Asynchronous: Starts analyzers in background threads. Emits finished() at the end
def startScan(self, file):
self.fileName = file
self.ocrThread = Analyze( self.ocr, file, self )
self.barcodeThread = Analyze( self.barcode, file, self )
self.connect( self.ocrThread, SIGNAL('finished()'), self.scanFinished )