From 7bca063e2e0b7bf057f7426e3b7c353a0504e316 Mon Sep 17 00:00:00 2001 From: Albert Cervera i Areny Date: Mon, 12 May 2008 17:55:51 +0200 Subject: [PATCH] Now we work with millimeters instead of pixels. This should allow us work with images scanned at different resolutions. --- barcode.py | 18 +++++++++++++++--- ocr.py | 20 ++++++++++++-------- recognizer.py | 2 ++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/barcode.py b/barcode.py index 48f1a23..8e82651 100644 --- a/barcode.py +++ b/barcode.py @@ -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 ) diff --git a/ocr.py b/ocr.py index 99b13ce..b275b8c 100755 --- a/ocr.py +++ b/ocr.py @@ -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() - diff --git a/recognizer.py b/recognizer.py index 54b3ce5..5bd2bf1 100644 --- a/recognizer.py +++ b/recognizer.py @@ -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 )