[Pycodeocr-main] SF.net SVN: pycodeocr:[69] branches/redesign
Status: Beta
Brought to you by:
drtrigon
|
From: <la...@us...> - 2014-08-28 14:37:11
|
Revision: 69
http://sourceforge.net/p/pycodeocr/code/69
Author: laserb
Date: 2014-08-28 14:37:06 +0000 (Thu, 28 Aug 2014)
Log Message:
-----------
Add possibility to have more than one output.
Modified Paths:
--------------
branches/redesign/Recognition/ESR.py
branches/redesign/Recognition/ImageRecognition.py
branches/redesign/gtkGUI/ImagePreview.py
Modified: branches/redesign/Recognition/ESR.py
===================================================================
--- branches/redesign/Recognition/ESR.py 2014-08-27 06:55:49 UTC (rev 68)
+++ branches/redesign/Recognition/ESR.py 2014-08-28 14:37:06 UTC (rev 69)
@@ -33,25 +33,22 @@
## @overwrite verification
def doVerification(self):
- # correct for common char recognition errors
- self.recognitionData.setContent(self.char_correction(self.recognitionData.getContent()))
-
# get data
data = self.recognitionData.getContent()
+ code = data[0][1]
+ # correct for common char recognition errors
+ code = self.char_correction(code)
+
+
# check for invalid characters
- check = (not "?" in data) or (not "!" in data) # any unrecognized char in code?
- check = check and ( len(data) in self.valid_code_len ) # correct code len?
+ check = (not "?" in code) or (not "!" in code) # any unrecognized char in code?
+ check = check and ( len(code) in self.valid_code_len ) # correct code len?
if not check:
return (False, "Invalid length or invalid characters")
- # extract amount, reference and account from codeline
- tmp = data[:-1].split(">")
- amount = tmp[0]
- tmp = tmp[1].split("+ ")
- reference = tmp[0]
- account = tmp[1]
-
+ (amount, reference, account) = self.split(code)
+
# do checksum for amount, reference and account
if not self.checksum(amount):
return (False, "Checksum for amount failed.")
@@ -60,9 +57,27 @@
if not self.checksum(account):
return (False, "Checksum for account failed.")
+ # save data in split form
+ account = account[0:2]+"-"+account[2:-1]+"-"+account[-1]
+ amount = str(float(amount[2:-1])/100)
+ self.setContent(code, account, amount, reference)
+
# return true if all checks are positive
return (True, "")
+ def split(self, data):
+ try:
+ # extract amount, reference and account from codeline
+ tmp = data[:-1].split(">")
+ amount = tmp[0]
+ tmp = tmp[1].split("+ ")
+ reference = tmp[0]
+ account = tmp[1]
+ return (amount, reference, account)
+ except:
+ return ("","","")
+
+
# thanks to http://www.hosang.ch/modulo10.aspx
# much more details http://www.sic.ch/de/dl_tkicch_dta.pdf ( page 51 )
# @see http://www.bundesbank.de/download/zahlungsverkehr/zv_pz201012.pdf
@@ -112,8 +127,10 @@
os.remove(self.temp_name + "tesseract.txt") # clean-up
os.remove(self.temp_name + ".png") # clean-up
+
# set content
- self.recognitionData.setContent(data)
+ (amount, reference, account) = self.split(data)
+ self.setContent(data, account, amount, reference)
# check if data is valid
(check, msg) = self.verify()
@@ -123,6 +140,11 @@
# return the data
return self.recognitionData.getContent()
+ def setContent(self, all, account, amount, reference):
+ data = [["All",all],["Account",account],["Amount",amount],["Reference",reference]]
+ self.recognitionData.setContent(data)
+
+
## Character correction after recogition (on basis that there should be numbers and few special chars).
#
# @test Add unittest to check correct operation of this important module/function.
Modified: branches/redesign/Recognition/ImageRecognition.py
===================================================================
--- branches/redesign/Recognition/ImageRecognition.py 2014-08-27 06:55:49 UTC (rev 68)
+++ branches/redesign/Recognition/ImageRecognition.py 2014-08-28 14:37:06 UTC (rev 69)
@@ -95,7 +95,7 @@
def __init__(self, image):
self.image = image
self.temp_name = "image_object_temp_file"
- self.content = ""
+ self.content = []
self.valid = None
## get image
@@ -135,6 +135,15 @@
def getContent(self):
return self.content
+ def getContentString(self):
+ if isinstance(self.content, list):
+ content = ""
+ for i in len(self.content):
+ content += " "+str(self.content[i][1])
+ return content
+ return self.content
+
+
def setValid(self, valid):
self.valid = valid
@@ -144,11 +153,18 @@
## save content to clipboard
# @pre information set
# @post information saved in clipboard
- def saveClipboard(self):
- # set the clipboard text data
- pyperclip.setcb(self.content)
- # make our data available to other applications
- pyperclip.getcb()
+ def saveClipboard(self):
+ if isinstance(self.content, list):
+ for content in self.content:
+ # set the clipboard text data
+ pyperclip.setcb(content[1])
+ # make our data available to other applications
+ pyperclip.getcb()
+ else:
+ # set the clipboard text data
+ pyperclip.setcb(self.content)
+ # make our data available to other applications
+ pyperclip.getcb()
## Run the recognition and verify the information
class RunRecognition(gobject.GObject):
@@ -183,7 +199,7 @@
# if data is valid, save to clipboard and return data
self.emit("recognized")
if check:
- self.emit("updateRecognition","Data is valid. Save to clipboard.")
+ self.emit("updateRecognition","Data is valid. Click to save to clipboard.")
self.imageRecognition.getRecognitionData().saveClipboard()
return (True, self.imageRecognition.getRecognitionData().getContent())
Modified: branches/redesign/gtkGUI/ImagePreview.py
===================================================================
--- branches/redesign/gtkGUI/ImagePreview.py 2014-08-27 06:55:49 UTC (rev 68)
+++ branches/redesign/gtkGUI/ImagePreview.py 2014-08-28 14:37:06 UTC (rev 69)
@@ -1,4 +1,4 @@
-import gtk, os
+import gtk, os, pyperclip, pango
import StringIO
# PIL/Pillow image library and it's SANE bindings
@@ -9,8 +9,10 @@
def __init__(self):
#Initialize the VBox
gtk.VBox.__init__(self,False,0)
- self.res_color = { True: gtk.gdk.color_parse("#00FF00"),
- False: gtk.gdk.color_parse("#FF0000"),
+ self.recognitionData = None
+
+ self.res_color = { True: gtk.gdk.color_parse("#006600"),
+ False: gtk.gdk.color_parse("#660000"),
None: gtk.gdk.color_parse("#FFFFFF"), }
# # retrieve widgets
@@ -38,12 +40,12 @@
self.img_popup = self.builder.get_object('img_popup')
## output line
- self.output = self.builder.get_object('output')
+ self.output = Output()
# connect eventbox to close image window
(self.new_width, self.new_height ) = (0,0)
self.main_image.set_tooltip_text("Click for original image")
- #self.main_image.connect('expose-event', self.on_image_resize, "") # high cpu usage
+ #self.main_image.connect('motion-notify-event', self.on_image_resize, "") # high cpu usage
self.popup_image.set_tooltip_text("Click to close")
self.pack_start(self.scanned_code_label,False,False,0)
@@ -113,11 +115,13 @@
self.setOutputHelper()
def setOutputHelper(self):
- self.output.set_text(self.recognitionData.getContent())
+ content = self.recognitionData.getContent()
mode = self.recognitionData.isValid()
-
- # widget color
- self.output.modify_base(gtk.STATE_NORMAL, self.res_color[mode])
+ if isinstance(content, list):
+ for text in content:
+ self.output.addText(text, self.res_color[mode])
+ else:
+ self.output.addText(content, self.res_color[mode])
def setRecognitionData(self, recognitionData):
self.recognitionData = recognitionData
@@ -138,6 +142,8 @@
def on_image_resize(self, widget, event, window):
allocation = widget.get_allocation()
+ if not self.recognitionData:
+ return
image = self.recognitionData.getImage()
pixbuf = self.image_to_pixbuf(image)
image = gtk.Image()
@@ -164,4 +170,39 @@
# set new pixbuf
widget.set_from_pixbuf(pixbuf)
+
+class Output(gtk.HBox):
+ def __init__(self):
+ #Initialize the VBox
+ gtk.HBox.__init__(self,False,0)
+ self.button = {}
+
+ def addText(self, text, color):
+ labelText = text[0] + "\n" + text[1]
+ button = gtk.Button(labelText)
+ label = button.get_child()
+ label.set_ellipsize(pango.ELLIPSIZE_MIDDLE)
+ label.modify_fg(gtk.STATE_NORMAL, color)
+ button.connect("clicked",self.copyClipboard)
+ self.button[text[0]] = [button, text]
+ self.add(button)
+ self.show_all()
+
+ def removeText(self, text):
+ self.remove(self.button[text])
+
+ def removeAll(self):
+ for button in self.button:
+ self.remove(button[0])
+
+ def copyClipboard(self, widget):
+ label = widget.get_label()
+ label = label.split("\n")[0]
+ text = self.button[label][1][1]
+ # set the clipboard text data
+ pyperclip.setcb(text)
+ # make our data available to other applications
+ pyperclip.getcb()
+
+
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|