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
-
Hello Guys,
i have en other Idee for my app to do some inputs easier.
My Picture look's like this:
https://imgur.com/a/QKuykE7is it possible to read the Number on it ?
7989476
the beginning!
is not on every Number..Maybe anyone know's whether it's possible to do this.
Thanks guys!
-
-
This did i found on an other topic:
https://forum.omz-software.com/topic/6016/recognize-text-from-picture/41I can't Post Code because it's Spam ??....
I get this pic as result:
https://imgur.com/a/hykpTgIBut i have no idea How it is possible to select only the Numbers with 7 digits
-
@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