omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    3
    67
    11247
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DavinE
      DavinE last edited by

      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/QKuykE7

      is it possible to read the Number on it ?
      7989476the beginning ! is not on every Number..

      Maybe anyone know's whether it's possible to do this.

      Thanks guys!

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @DavinE last edited by cvp

        @DavinE we have had a lot of topics with same subject. Search the forum with VNCoreMLModel, example here

        1 Reply Last reply Reply Quote 0
        • DavinE
          DavinE last edited by

          @cvp, @JonB

          This did i found on an other topic:
          https://forum.omz-software.com/topic/6016/recognize-text-from-picture/41

          I can't Post Code because it's Spam ??....

          I get this pic as result:
          https://imgur.com/a/hykpTgI

          But i have no idea How it is possible to select only the Numbers with 7 digits

          cvp 2 Replies Last reply Reply Quote 0
          • cvp
            cvp @DavinE last edited by cvp

            @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
            

            1 Reply Last reply Reply Quote 0
            • cvp
              cvp @DavinE last edited by

              @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()
              
              DavinE 1 Reply Last reply Reply Quote 0
              • DavinE
                DavinE @cvp last edited by

                @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...

                1. The root.present() for what is the view ?
                2. and is it possible to select the photos from Camera or folder ?
                3. maybe more then one Photo by Camera or folder ?
                cvp 4 Replies Last reply Reply Quote 0
                • cvp
                  cvp @DavinE last edited by

                  @DavinE said:

                  and is it possible to select the photos from Camera or folder ?

                  The original script of @mikael includes the camera process

                  From folder, usual file picker

                  1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @DavinE last edited by

                    @DavinE said:

                    maybe more then one Photo by Camera or folder ?

                    Sure it is possible but you have to program it

                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @DavinE last edited by cvp

                      @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.

                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @DavinE last edited by

                        @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()
                        
                        1 Reply Last reply Reply Quote 0
                        • DavinE
                          DavinE last edited by

                          @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']

                          1 Reply Last reply Reply Quote 0
                          • JonB
                            JonB last edited by

                            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.

                            1 Reply Last reply Reply Quote 0
                            • DavinE
                              DavinE last edited by

                              @JonB,

                              i Think you getting me wrong.
                              I wanted to try to set the picker.allowsEditing to False because when it's True on every img is a section for the img but i don't want this.

                              do you know now what i meant ?

                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @DavinE last edited by cvp

                                @DavinE try with

                                    img = ObjCInstance(info)['UIImagePickerControllerOriginalImage']   
                                    #img = ObjCInstance(info)['UIImagePickerControllerEditedImage']   
                                .
                                .
                                .
                                        picker.allowsEditing = False
                                
                                DavinE 1 Reply Last reply Reply Quote 0
                                • DavinE
                                  DavinE @cvp last edited by

                                  @cvp said:

                                  @DavinE try with

                                      img = ObjCInstance(info)['UIImagePickerControllerOriginalImage']   
                                      #img = ObjCInstance(info)['UIImagePickerControllerEditedImage']   
                                  .
                                  .
                                  .
                                          picker.allowsEditing = False
                                  

                                  @cvp,
                                  No, this works unfortunately not

                                  cvp 1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @DavinE last edited by

                                    @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"

                                    1 Reply Last reply Reply Quote 0
                                    • DavinE
                                      DavinE last edited by

                                      @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!

                                      1 Reply Last reply Reply Quote 0
                                      • JonB
                                        JonB last edited by JonB

                                        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 ...

                                        cvp DavinE 3 Replies Last reply Reply Quote 0
                                        • cvp
                                          cvp @JonB last edited by

                                          @JonB said:

                                          maybe you know a better way if printing out all options ...

                                          Unfortunately, no

                                          1 Reply Last reply Reply Quote 0
                                          • cvp
                                            cvp @JonB last edited by cvp

                                            @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

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors