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 Please try this @mikael 's script on your photo and you will see that one of the identified texts is your 7-digits number
Better with this little modification
def tableview_cell_for_row(self, tableview, section, row): cell = ui.TableViewCell() cell.text_label.text = self.recognized_text[row] if len(cell.text_label.text) == 7 and cell.text_label.text.isnumeric(): cell.background_color = 'yellow' return cell
-
@DavinE and without TableView because you only need one number
# https://raw.githubusercontent.com/mikaelho/pythonista-misc/master/pic2text.py import photos, ui, dialogs, clipboard, appex import io, ctypes from functools import partial from objc_util import * load_framework('Vision') VNRecognizeTextRequest = ObjCClass('VNRecognizeTextRequest') VNImageRequestHandler = ObjCClass('VNImageRequestHandler') (picker_photos, picker_camera) = (0, 1) UIImagePNGRepresentation = c.UIImagePNGRepresentation UIImagePNGRepresentation.argtypes = [ctypes.c_void_p] UIImagePNGRepresentation.restype = ctypes.c_void_p root = ui.View() def imagePickerController_didFinishPickingMediaWithInfo_(self,cmd,picker,info): pick = ObjCInstance(picker) pick.setDelegate_(None) ObjCInstance(self).release() pick.dismissViewControllerAnimated_completion_(True, None) img = ObjCInstance(info)['UIImagePickerControllerEditedImage'] png_data = ObjCInstance(UIImagePNGRepresentation(img.ptr)) ret = recognize(png_data) if ret: for txt in ret: if len(txt) == 7 and txt.isnumeric(): print(txt) else: dialogs.hud_alert('Failed to recognize anything') SUIViewController = ObjCClass('SUIViewController') MyPickerDelegate = create_objc_class('MyPickerDelegate', methods=[imagePickerController_didFinishPickingMediaWithInfo_], protocols=['UIImagePickerControllerDelegate']) @on_main_thread def get_photo_action(): picker = ObjCClass('UIImagePickerController').alloc().init() delegate = MyPickerDelegate.alloc().init() picker.setDelegate_(delegate) picker.allowsEditing = True picker.sourceType = 0 vc = SUIViewController.viewControllerForView_( root.objc_instance) vc.presentModalViewController_animated_(picker, True) 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() ] return recognized_text else: return None root.present() get_photo_action()
-
@cvp said:
@DavinE and without TableView because you only need one number
# https://raw.githubusercontent.com/mikaelho/pythonista-misc/master/pic2text.py import photos, ui, dialogs, clipboard, appex import io, ctypes from functools import partial from objc_util import * load_framework('Vision') VNRecognizeTextRequest = ObjCClass('VNRecognizeTextRequest') VNImageRequestHandler = ObjCClass('VNImageRequestHandler') (picker_photos, picker_camera) = (0, 1) UIImagePNGRepresentation = c.UIImagePNGRepresentation UIImagePNGRepresentation.argtypes = [ctypes.c_void_p] UIImagePNGRepresentation.restype = ctypes.c_void_p root = ui.View() def imagePickerController_didFinishPickingMediaWithInfo_(self,cmd,picker,info): pick = ObjCInstance(picker) pick.setDelegate_(None) ObjCInstance(self).release() pick.dismissViewControllerAnimated_completion_(True, None) img = ObjCInstance(info)['UIImagePickerControllerEditedImage'] png_data = ObjCInstance(UIImagePNGRepresentation(img.ptr)) ret = recognize(png_data) if ret: for txt in ret: if len(txt) == 7 and txt.isnumeric(): print(txt) else: dialogs.hud_alert('Failed to recognize anything') SUIViewController = ObjCClass('SUIViewController') MyPickerDelegate = create_objc_class('MyPickerDelegate', methods=[imagePickerController_didFinishPickingMediaWithInfo_], protocols=['UIImagePickerControllerDelegate']) @on_main_thread def get_photo_action(): picker = ObjCClass('UIImagePickerController').alloc().init() delegate = MyPickerDelegate.alloc().init() picker.setDelegate_(delegate) picker.allowsEditing = True picker.sourceType = 0 vc = SUIViewController.viewControllerForView_( root.objc_instance) vc.presentModalViewController_animated_(picker, True) 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() ] return recognized_text else: return None root.present() get_photo_action()
@cvp, Exaclly that What i want, thanks!
Only one or two question...
- The
root.present()
for what is the view ? - and is it possible to select the photos from Camera or folder ?
- maybe more then one Photo by Camera or folder ?
- The
-
-
@DavinE said:
maybe more then one Photo by Camera or folder ?
Sure it is possible but you have to program it
-
@DavinE said:
The root.present() for what is the view ?
You need a root view to present a viewcontroller
I only quickly changed the original script, which had a root view for the buttons, try it.
-
@DavinE camera + photos + file
# https://raw.githubusercontent.com/mikaelho/pythonista-misc/master/pic2text.py import photos, ui, dialogs, clipboard, appex import io, ctypes from functools import partial from objc_util import * load_framework('Vision') VNRecognizeTextRequest = ObjCClass('VNRecognizeTextRequest') VNImageRequestHandler = ObjCClass('VNImageRequestHandler') (picker_photos, picker_camera) = (0, 1) UIImagePNGRepresentation = c.UIImagePNGRepresentation UIImagePNGRepresentation.argtypes = [ctypes.c_void_p] UIImagePNGRepresentation.restype = ctypes.c_void_p root = ui.View() root.background_color = 'white' def imagePickerController_didFinishPickingMediaWithInfo_(self,cmd,picker,info): pick = ObjCInstance(picker) pick.setDelegate_(None) ObjCInstance(self).release() pick.dismissViewControllerAnimated_completion_(True, None) img = ObjCInstance(info)['UIImagePickerControllerEditedImage'] png_data = ObjCInstance(UIImagePNGRepresentation(img.ptr)) recognize(png_data) SUIViewController = ObjCClass('SUIViewController') MyPickerDelegate = create_objc_class('MyPickerDelegate', methods=[imagePickerController_didFinishPickingMediaWithInfo_], protocols=['UIImagePickerControllerDelegate']) @on_main_thread def get_photo_action(picker_type,sender): picker = ObjCClass('UIImagePickerController').alloc().init() delegate = MyPickerDelegate.alloc().init() picker.setDelegate_(delegate) picker.allowsEditing = True picker.sourceType = picker_type vc = SUIViewController.viewControllerForView_( root.objc_instance) vc.presentModalViewController_animated_(picker, True) 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 len(txt) == 7 and txt.isnumeric(): print(txt) else: dialogs.hud_alert('Failed to recognize anything') def get_file_action(sender): f = dialogs.pick_document() with open(f, mode='rb') as fil: img_data = fil.read() recognize(img_data) b1 = ui.ButtonItem() b1.tint_color='black' b1.image = ui.Image('iob:ios7_photos_32') b1.action = partial(get_photo_action,0) b2 = ui.ButtonItem() b2.tint_color='black' b2.image = ui.Image('iob:camera_32') b2.action = partial(get_photo_action,1) b3 = ui.ButtonItem() b3.tint_color='black' b3.image = ui.Image('iob:ios7_folder_outline_32') b3.action = get_file_action root.right_button_items = (b1,b2,b3) root.present()
-
@cvp,
That is exactly what i want!
Thanks again ^^once again:
when i set this from:
picker.allowsEditing = True
to:
picker.allowsEditing = False
because i don't want to select or Editing the Picture i get this message:
Traceback (most recent call last): File "_ctypes/callbacks.c", line 234, in 'calling callback function' File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/UI/test.py", line 28, in imagePickerController_didFinishPickingMediaWithInfo_ png_data = ObjCInstance(UIImagePNGRepresentation(img.ptr)) AttributeError: 'NoneType' object has no attribute 'ptr'
with objc_util i have no idea what i need to do ^^
i think the error comes from here:
img = ObjCInstance(info)['UIImagePickerControllerEditedImage']
-
What happened is that img returned none. If you put a try catch around that line, you can cancel in the case that there is no img.
-
i Think you getting me wrong.
I wanted to try to set thepicker.allowsEditing
toFalse
because when it'sTrue
on every img is a section for the img but i don't want this.do you know now what i meant ?
-
@DavinE try with
img = ObjCInstance(info)['UIImagePickerControllerOriginalImage'] #img = ObjCInstance(info)['UIImagePickerControllerEditedImage'] . . . picker.allowsEditing = False
-
-
@DavinE for me it works, it picks a photo and the analysis is immediate without passing via the window where you have to tap "use"
-
@cvp,
I know why it doesn't work for me, he analyzes the image differently ... Unfortunately, the output is different for whatever reason!
So it would work, but unfortunately not an option for me 😊
But thanks for your help!
-
I'm having trouble following the conversation -- is the problem still that you are getting the NoneType error?
Try this in your delegate handler
infodict=ObjCInstance(info)
print(infodict)
print (infodict.allKeys())(At least, I think that's how one gets the keys from a NSDictionary.. you might need a for loop)
There are a few possible key values that the info object can return. @cvp, maybe you know a better way if printing out all options ...
-
-
@JonB said:
is the problem still that you are getting the NoneType error?
For me the problem has disappeared when I used original photo. in this case you can set the allowsEditing flag to False
-
@JonB said:
I'm having trouble following the conversation -- is the problem still that you are getting the NoneType error?
Try this in your delegate handler
infodict=ObjCInstance(info)
print(infodict)
print (infodict.allKeys())(At least, I think that's how one gets the keys from a NSDictionary.. you might need a for loop)
There are a few possible key values that the info object can return. @cvp, maybe you know a better way if printing out all options ...
my Problem is the Following one.
i get this output with that settings:
img = ObjCInstance(info)['UIImagePickerControllerEditedImage']
picker.allowsEditing = True
['Versand: LKW-Anlieferung', 'Objekt: 47491709, Fornoff', 'Bestellt von Herrn xxx xxx', 'Position', 'Bezeichnung', 'Menge', 'Preis', '1', '1179629', 'Hager eHZ-Anschlusskassette', 'KU73S20', '1 - St', '74,20', '5-pol. mit Spannungsabgriff', 'Produktinformationen im Internet', '2', '3500670', 'Hager SLS-Schalter 35A', 'HTS335E', '1 - St', '74,50', '3polig E für Sammelschiene', 'Produktinformationen m Internet', '3', '3500156', 'Hager Ausschalter 3polig sperrbar SH363S', '1 - St', '32,44', '63A 2,5TE sperr- u. plombierbar', 'Produktinformationen m Internet', '4', '3532239', 'Hager Schütz 63A 4S 230V', 'ESC463S', '1 St', '55,10', 'brummfrei', '•Produktinformationen im Internet', '5', '1177739', 'Hager Plombierkappe für Schütze 3M ESC003', '1 - St', '0,84', '1 St wurde in Rückstand genommen.', 'Produktinformationen m Internet', 'Auftragsverfolgung:', 'Gesamtwert:', 'EUR', 'Mehrwertsteuer:', '19,0', '%', 'Endbetrag:', 'EUR', 'Es gelten die mit Ihnen vereinbarten Zahlungsbedingungen.', 'Liefertermine sind unverbindlich. Durch die Weltweite COVID-19 Situation kann es seitens der Hersteller zu unvorherseht', 'Lieferschwierigkeiten kommen. Metallzuschläge und Versandkosten sind nicht skontofähig. Diesem Auftrag liegen unsere', 'neugefassten Allgemeinen Lieferbedingungen (Stand Mai 2018) zugrunde, die Sie unter www.moster.de abrufen können.', 'Wir sind hier: Ludwigshafen am Rhein', 'Neustadt an der Weinstraße - Offenbach - Karlsruhe - Weinheim - Kaiserslauter', 'Moster Elektrogroßhandelsges. mbH', 'Niederlassung:', 'Ernst-Boehe-Straße 10', 'Hauptstraße 62', 'Handelsregister Amtsgericht', 'Geschäftsführer', 'Rudolf Peter Moster', '67059 Ludwigshafen', 'Ludwigshafen', 'Telefon (06 21) 5 90 04 - 0', '63924 Kleinheubach', 'HRB-Nr. 2149', 'Götz Aumüller', 'E-Mail info@moster.de', 'Telefon: 09371 4098-0', 'USt-IdNr. DE197966312', 'E-Mail', 'www.moster.de', 'kleinheubach@moster.de'] orderConfirmation -- [['47491709'], ['1179629', '1', '74,20'], ['3500670', '1', '74,50'], ['3500156', '1', '32,44'], ['3532239', '1', '55,10'], ['1177739', '1', '0,84']]
that is correct and i can work with it.
when i use the following settings:
img = ObjCInstance(info)['UIImagePickerControllerOriginalImage']
picker.allowsEditing = False
get i this:
['Rudolf Peter Moster', 'Geschäftsführer', 'Jelinwny 2109', '%', 'VI TE', "OL'99", '0,84', 'an3', 'an3', '06 L', "O7'7 L", 'Preis', '09 7L', 'IS L', 'IS L', 'ZI899626130 INPHISN', 'IS L', '†S I', 'IS I', 'Endbetrag:', 'nupuebs}wyJejs|BeJs)epuBH', '6717 IN-AHH', 'Ludwigshafen', "L ZOZ'60 t0 :wnjea", 'Mehrwertsteue', 'pemqweses', 'Menge', '0-8605 LLEE', 'E-Mail', 'OZSE/NY', 'HTS335E', 'SE9tOS]', 'SE9CHS Jequueds Bijodg de}leyossny JeBeH', 'joule}u| w! usuonewojurynpold', 'wu6qesbunuueds Hw "jod-g', 'Hosseyssn|yosuy-ZHeJeBeH', '¡DEISNON - ujeya we uajeysbimpn ulely puis 11M 13 neIsIasjey - WeyujaM - aynsuey - usequeHo - e sensujam Jap ue', 'puis ou! queje!7 youna yo! pulque un D¡eM}eM e!p COVID-19 Situation kann es seitens der yesIeyJoAun nz Je||e sIeH', 'eJesun uebe!l beuyny wesela (ersandkosten sind nicht skontofähig. pun edelyosnzijejen uswwoy us ex bue!MupsiejeIT', 'in Rückstand genommen', 'E00OSE WE ezInYas any oddexue|qwold106eH', '}ewe}u| w! Produktinformationen', 'kleinheubach@moster.d', 'ypegneyuie|x +7689', '79 eyensidneH', ':BunsseuepeIN', 'Telefon:', 'euelyos|ewes uny a biods', 'Bezeichnung', 'Bestellt uOA leuq3 youin uwue', '‹önnen. uenqe unter www.moster.de Sie olp epunJ6nz (8 LOZ ¡ew puels) neugefassten Allgemeinen Lieferbedingungen', '¡eule}ui wi usuonewoyurynpold', '}eulejuj wi usuoneuojurynpold', '63A 2,5TE sperr- u. plombierbar', 'euse}ui w! usuonewojurynpold', 'V98 JO}leyS-S7S JeBeH', 'A0EZ St V$9 ZInyOS JeBEH', '@ wunig', 'ubunbuipeqsbunjuezueuequieuenueuyi HIW alp ue106 sg', 'unMIS', 'Moser Elektrogroßhandelsges Hqu', '0 - 90 04 , ( L7 90) uojejel', 'E-Mail info@moster.de', 'Ernst-E ehe-Straße 10', "ep'Je}so u M", 'uejeys6impn 7 690 49', 'Helqo 47491709, Fornoff', 'Versand: LKW-Anlieferunc', '3500670', '3500156', '3532239', '6EZZLI L', 'nbjouansbeyny', '1179629', 'y', 'V', '+', 'uon!sod', 'C'] orderConfirmation -- []
i think the language is the Problem...
-
@DavinE said:
that is correct and i can work with it.
Thus you don't have anymore the error on img.ptr
-