Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
recognize Number from Picture
-
@DavinE I guess that you recognize function has bugs because with mine, and
img = ObjCInstance(info)['UIImagePickerControllerOriginalImage'] picker.allowsEditing = False
I get all infos from your image with
def recognize(image_data): req = VNRecognizeTextRequest.alloc().init().autorelease() handler = VNImageRequestHandler.alloc().initWithData_options_( image_data, None ).autorelease() success = handler.performRequests_error_([req], None) if success: recognized_text = [ str(result.text()) for result in req.results() ] for txt in recognized_text: if 1==1:#len(txt) == 7 and txt.isnumeric(): print(txt) else: dialogs.hud_alert('Failed to recognize anything')
With OriginalImage, Your code hangs if I do not skip these lines
print('here') continue if console.alert('Objektnummer Fehler ?',f'Die Angabe welcher in dem Objekt hinterlegt ist unterscheiden sich:\nAktive Nr.: {orderConfirmation}\nNeue Nr.: {[int(temp)for temp in txt.split() if temp.isdigit()][0]}',hide_cancel_button=True) is 2: sys.exit(0)
-
@cvp said:
@DavinE I guess that you recognize function has bugs because with mine, and
img = ObjCInstance(info)['UIImagePickerControllerOriginalImage'] picker.allowsEditing = False
I get all infos from your image with
def recognize(image_data): req = VNRecognizeTextRequest.alloc().init().autorelease() handler = VNImageRequestHandler.alloc().initWithData_options_( image_data, None ).autorelease() success = handler.performRequests_error_([req], None) if success: recognized_text = [ str(result.text()) for result in req.results() ] for txt in recognized_text: if 1==1:#len(txt) == 7 and txt.isnumeric(): print(txt) else: dialogs.hud_alert('Failed to recognize anything')
With OriginalImage, Your code hangs if I do not skip these lines
print('here') continue if console.alert('Objektnummer Fehler ?',f'Die Angabe welcher in dem Objekt hinterlegt ist unterscheiden sich:\nAktive Nr.: {orderConfirmation}\nNeue Nr.: {[int(temp)for temp in txt.split() if temp.isdigit()][0]}',hide_cancel_button=True) is 2: sys.exit(0)
That was my mistake.....
you need to add:
@ui.in_background
like that:@ui.in_background def recognize(self, image_data):
then it works and you see i get nothing as a result in my array only with the Edited Mode
-
@DavinE I have the same result with both edited or original BUT if you change the mode, please restart Pythonista before each new run, because you change a delegate of a class you created.
orderConfirmation -- [['some text', '1179629', '+1', '74.20', 92.75, '0001', ''], ['some text', '3500670', '+1', '74.50', 93.12, '0001', ''], ['some text', '3500156', '+1', '32.44', 40.55, '0001', ''], ['some text', '3532239', '+1', '55.10', 68.88, '0001', ''], ['some text', '1177739', '+1', '0.84', 1.05, '0001', '']] 5
-
@cvp said:
@DavinE I have the same result with both edited or original BUT if you change the mode, please restart Pythonista before each new run, because you change a delegate of a class you created.
orderConfirmation -- [['some text', '1179629', '+1', '74.20', 92.75, '0001', ''], ['some text', '3500670', '+1', '74.50', 93.12, '0001', ''], ['some text', '3500156', '+1', '32.44', 40.55, '0001', ''], ['some text', '3532239', '+1', '55.10', 68.88, '0001', ''], ['some text', '1177739', '+1', '0.84', 1.05, '0001', '']] 5
It doesn't work for me ...
This is all of my code, maybe you will find a bug:# https://raw.githubusercontent.com/mikaelho/pythonista-misc/master/pic2text.py import ui, dialogs, re, console from ctypes import c_void_p from objc_util import ObjCClass, UIColor, ObjCInstance, on_main_thread, c, create_objc_class, NSBundle, load_framework class main(): def __init__(self): def imagePickerController_didFinishPickingMediaWithInfo_(self, cmd, picker, info): pick = ObjCInstance(picker) pick.setDelegate_(None) ObjCInstance(self).release() pick.dismissViewControllerAnimated_completion_(True, None) img = ObjCInstance(info)['UIImagePickerControllerOriginalImage'] png_data = ObjCInstance(UIImagePNGRepresentation(img.ptr)) recognize(png_data) load_framework('Vision') self.VNRecognizeTextRequest = ObjCClass('VNRecognizeTextRequest') self.VNImageRequestHandler = ObjCClass('VNImageRequestHandler') (picker_photos, picker_camera) = (0, 1) UIImagePNGRepresentation = c.UIImagePNGRepresentation UIImagePNGRepresentation.argtypes = [c_void_p] UIImagePNGRepresentation.restype = c_void_p recognize = self.recognize self.root = ui.View() self.root.background_color = 'white' self.SUIViewController = ObjCClass('SUIViewController') self.MyPickerDelegate = create_objc_class('MyPickerDelegate', methods=[imagePickerController_didFinishPickingMediaWithInfo_], protocols=['UIImagePickerControllerDelegate']) b1 = ui.ButtonItem() b1.tint_color='black' b1.image = ui.Image('iob:ios7_photos_32') b1.action = partial(self.get_photo_action,0) b2 = ui.ButtonItem() b2.tint_color='black' b2.image = ui.Image('iob:camera_32') b2.action = partial(self.get_photo_action,1) b3 = ui.ButtonItem() b3.tint_color='black' b3.image = ui.Image('iob:ios7_folder_outline_32') b3.action = self.get_file_action self.root.right_button_items = (b1,b2,b3) self.root.present() @on_main_thread def get_photo_action(self, picker_type,sender): picker = ObjCClass('UIImagePickerController').alloc().init() delegate = self.MyPickerDelegate.alloc().init() picker.setDelegate_(delegate) picker.allowsEditing = False picker.sourceType = picker_type vc = self.SUIViewController.viewControllerForView_( self.root.objc_instance) vc.presentModalViewController_animated_(picker, True) @ui.in_background def recognize(self, image_data): req = self.VNRecognizeTextRequest.alloc().init().autorelease() handler = self.VNImageRequestHandler.alloc().initWithData_options_( image_data, None ).autorelease() success = handler.performRequests_error_([req], None) if success: recognized_text = [str(result.text()) for result in req.results()] # print(recognized_text) price = None orderConfirmation = [] for txt in recognized_text: if 'Objekt' in txt: txt = txt.replace(',', '') # Einlesung der Ersten Seite der Auftragsbestätiigung if len(str([int(temp)for temp in txt.split() if temp.isdigit()][0])) is 8: if orderConfirmation is None: orderConfirmation = [int(temp)for temp in txt.split() if temp.isdigit()][0] else: if orderConfirmation != [int(temp)for temp in txt.split() if temp.isdigit()][0]: if console.alert( 'Objektnummer Fehler ?', f'Die Angabe welcher in dem Objekt hinterlegt ist unterscheiden sich:\nAktive Nr.: {orderConfirmation}\nNeue Nr.: {[int(temp)for temp in txt.split() if temp.isdigit()][0]}', 'weiter', 'abbrechen', hide_cancel_button=True ) is 2: sys.exit(0) else: if orderConfirmation is None: console.alert('Objektnummer Fehler!!', 'An der Einzulesenden Auftragsbestätigung wurde keine Projektnummer gefunden!!', hide_cancel_button=False) else: console.alert('Objektnummer ??', 'An der Einzulesenden Auftragsbestätigung wurde keine Projektnummer gefunden ??\nGehört dem gelichen kunden an ?', 'weiter', hide_cancel_button=False) if len(txt) == 7 and txt.isnumeric(): orderNumber = txt if 'St' in txt and not len(txt) >= 8: price = True piece = '+%s' % (re.findall('([0-9]+)', txt)[0]) if price: if ',' in txt and not len(txt) >= 7 or '.' in txt and not len(txt) >= 7: txt = txt.replace(',', '.') orderConfirmationPrice = float(txt) orderConfirmationPrice = round((orderConfirmationPrice + ((orderConfirmationPrice * 25) / 100)), 2) price = False # designation__short = SQL # QRCode = SQL designation__short = 'some text' QRCode = '0001' temp_orderNumber = [] for temp_requestOrderConfirmation in orderConfirmation: temp_orderNumber.append(temp_requestOrderConfirmation[1]) if not orderNumber in temp_orderNumber: orderConfirmation.append([designation__short, orderNumber, piece, txt, orderConfirmationPrice, QRCode, '']) else: console.alert('Lieferantennummer existiert', f'Die Gescannte Lieferantennummer existiert bereits in der Liste:\n{orderNumber}', 'überspringen', hide_cancel_button=True) del orderNumber, piece print(f'orderConfirmation -- {orderConfirmation}') print(len(orderConfirmation)) else: console.hud_alert('Failed to recognize anything') def get_file_action(self, sender): f = console.pick_document() with open(f, mode='rb') as fil: img_data = fil.read() self.recognize(img_data) if __name__ == '__main__': main()
-
@DavinE if thé wanted answer is not
orderConfirmation -- [['some text', '1179629', '+1', '74.20', 92.75, '0001', ''], ['some text', '3500670', '+1', '74.50', 93.12, '0001', ''], ['some text', '3500156', '+1', '32.44', 40.55, '0001', ''], ['some text', '3532239', '+1', '55.10', 68.88, '0001', ''], ['some text', '1177739', '+1', '0.84', 1.05, '0001', '']] 5
Could you give me what you hope as answer
-
@cvp said:
@DavinE if thé wanted answer is not
orderConfirmation -- [['some text', '1179629', '+1', '74.20', 92.75, '0001', ''], ['some text', '3500670', '+1', '74.50', 93.12, '0001', ''], ['some text', '3500156', '+1', '32.44', 40.55, '0001', ''], ['some text', '3532239', '+1', '55.10', 68.88, '0001', ''], ['some text', '1177739', '+1', '0.84', 1.05, '0001', '']] 5
Could you give me what you hope as answer
that is correct.
But i don‘t get it....My Output is empty...
-
@DavinE did you remove Pythonista from memory and restart it?
-
-
@DavinE weird because I have the same answer with both modes if, an only if, I restart entirely Pythonista, and I have been obliged to add to your code...
from functools import partial
-
-
@DavinE I used your code you just posted. And it does not include the from functools import partial, thus I have some doubts....how do you remove Pythonista from memory?
-
@cvp said:
@DavinE I used your code you just posted. And it does not include the from functools import partial, thus I have some doubts....how do you remove Pythonista from memory?
Here is it included:
# https://raw.githubusercontent.com/mikaelho/pythonista-misc/master/pic2text.py import ui, dialogs, re, console from ctypes import c_void_p from functools import partial from objc_util import ObjCClass, UIColor, ObjCInstance, on_main_thread, c, create_objc_class, NSBundle, load_framework class main(): def __init__(self): def imagePickerController_didFinishPickingMediaWithInfo_(self, cmd, picker, info): pick = ObjCInstance(picker) pick.setDelegate_(None) ObjCInstance(self).release() pick.dismissViewControllerAnimated_completion_(True, None) img = ObjCInstance(info)['UIImagePickerControllerOriginalImage'] png_data = ObjCInstance(UIImagePNGRepresentation(img.ptr)) recognize(png_data) load_framework('Vision') self.VNRecognizeTextRequest = ObjCClass('VNRecognizeTextRequest') self.VNImageRequestHandler = ObjCClass('VNImageRequestHandler') (picker_photos, picker_camera) = (0, 1) UIImagePNGRepresentation = c.UIImagePNGRepresentation UIImagePNGRepresentation.argtypes = [c_void_p] UIImagePNGRepresentation.restype = c_void_p recognize = self.recognize self.root = ui.View() self.root.background_color = 'white' self.SUIViewController = ObjCClass('SUIViewController') self.MyPickerDelegate = create_objc_class('MyPickerDelegate', methods=[imagePickerController_didFinishPickingMediaWithInfo_], protocols=['UIImagePickerControllerDelegate']) b1 = ui.ButtonItem() b1.tint_color='black' b1.image = ui.Image('iob:ios7_photos_32') b1.action = partial(self.get_photo_action,0) b2 = ui.ButtonItem() b2.tint_color='black' b2.image = ui.Image('iob:camera_32') b2.action = partial(self.get_photo_action,1) b3 = ui.ButtonItem() b3.tint_color='black' b3.image = ui.Image('iob:ios7_folder_outline_32') b3.action = self.get_file_action self.root.right_button_items = (b1,b2,b3) self.root.present() @on_main_thread def get_photo_action(self, picker_type,sender): picker = ObjCClass('UIImagePickerController').alloc().init() delegate = self.MyPickerDelegate.alloc().init() picker.setDelegate_(delegate) picker.allowsEditing = False picker.sourceType = picker_type vc = self.SUIViewController.viewControllerForView_( self.root.objc_instance) vc.presentModalViewController_animated_(picker, True) @ui.in_background def recognize(self, image_data): req = self.VNRecognizeTextRequest.alloc().init().autorelease() handler = self.VNImageRequestHandler.alloc().initWithData_options_( image_data, None ).autorelease() success = handler.performRequests_error_([req], None) if success: recognized_text = [str(result.text()) for result in req.results()] # print(recognized_text) price = None orderConfirmation = [] for txt in recognized_text: if 'Objekt' in txt: txt = txt.replace(',', '') # Einlesung der Ersten Seite der Auftragsbestätiigung if len(str([int(temp)for temp in txt.split() if temp.isdigit()][0])) is 8: if orderConfirmation is None: orderConfirmation = [int(temp)for temp in txt.split() if temp.isdigit()][0] else: if orderConfirmation != [int(temp)for temp in txt.split() if temp.isdigit()][0]: if console.alert( 'Objektnummer Fehler ?', f'Die Angabe welcher in dem Objekt hinterlegt ist unterscheiden sich:\nAktive Nr.: {orderConfirmation}\nNeue Nr.: {[int(temp)for temp in txt.split() if temp.isdigit()][0]}', 'weiter', 'abbrechen', hide_cancel_button=True ) is 2: sys.exit(0) else: if orderConfirmation is None: console.alert('Objektnummer Fehler!!', 'An der Einzulesenden Auftragsbestätigung wurde keine Projektnummer gefunden!!', hide_cancel_button=False) else: console.alert('Objektnummer ??', 'An der Einzulesenden Auftragsbestätigung wurde keine Projektnummer gefunden ??\nGehört dem gelichen kunden an ?', 'weiter', hide_cancel_button=False) if len(txt) == 7 and txt.isnumeric(): orderNumber = txt if 'St' in txt and not len(txt) >= 8: price = True piece = '+%s' % (re.findall('([0-9]+)', txt)[0]) if price: if ',' in txt and not len(txt) >= 7 or '.' in txt and not len(txt) >= 7: txt = txt.replace(',', '.') orderConfirmationPrice = float(txt) orderConfirmationPrice = round((orderConfirmationPrice + ((orderConfirmationPrice * 25) / 100)), 2) price = False # designation__short = SQL # QRCode = SQL designation__short = 'some text' QRCode = '0001' temp_orderNumber = [] for temp_requestOrderConfirmation in orderConfirmation: temp_orderNumber.append(temp_requestOrderConfirmation[1]) if not orderNumber in temp_orderNumber: orderConfirmation.append([designation__short, orderNumber, piece, txt, orderConfirmationPrice, QRCode, '']) else: console.alert('Lieferantennummer existiert', f'Die Gescannte Lieferantennummer existiert bereits in der Liste:\n{orderNumber}', 'überspringen', hide_cancel_button=True) del orderNumber, piece print(f'orderConfirmation -- {orderConfirmation}') print(len(orderConfirmation)) else: console.hud_alert('Failed to recognize anything') def get_file_action(self, sender): f = console.pick_document() with open(f, mode='rb') as fil: img_data = fil.read() self.recognize(img_data) if __name__ == '__main__': main()
From Multitasking
-
@DavinE I have reinstalled this last code, restarted Pythonista, run and result is
orderConfirmation -- [['some text', '1179629', '+1', '74.20', 92.75, '0001', ''], ['some text', '3500670', '+1', '74.50', 93.12, '0001', ''], ['some text', '3500156', '+1', '32.44', 40.55, '0001', ''], ['some text', '3532239', '+1', '55.10', 68.88, '0001', ''], ['some text', '1177739', '+1', '0.84', 1.05, '0001', '']] 5
-
-
-
@DavinE I had downloaded your image in my photos and Thus I use the photos mode. And you?
-
@cvp, the same as you 😊
-
@DavinE Nothing special in your pythonista_startup in site-packages?
-
@cvp, i look only Stash
-
@DavinE Could you power off/on your device to be sure Pythonista memory is cleared (I agree that it is not very professional but who knows)