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!
-
@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
-
@cvp said:
@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
Have you ever used my complete code above?
maybe there is a mistake?
-
@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
-
last edited by
-
@cvp said:
@DavinE said:
From Multitasking
And you did swipe up Pythonista little window?
Yes, i did ^^
-
@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)
-
@DavinE do you have two versions of your program?
-
@cvp said:
@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)
Yes, i try that but it changed nothing
-
last edited by
-
@DavinE because you made tests with edited and original mode, perhaps you have two versions of your program.
Anyway, could you download the photo from Imgur in your photos only to be sure we work with same material
-
@cvp,
Lol, I'm using the same picture!
but when I use the picture I load from imgur and the "original" doesn't ...
what's going on.... 🙈
-
@DavinE that is because the photo you have in your Photos has internally two objects, the original and the edited. BuT the photo in Imgur has only one internal object. Compare their size
-
last edited by
-
last edited by