diff options
author | FreeArtMan <dos21h@gmail.com> | 2021-04-01 08:46:27 +0100 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2021-04-01 08:46:27 +0100 |
commit | cfa037e92da0a34f7919ce4ca9db2bd546995c93 (patch) | |
tree | 6e4ab4fb3ae1323b8e1d7ed677ed6dbc76a9bd61 | |
parent | 7561f774958ea1d495036d9aadf59366bd3320b8 (diff) | |
download | md-content-cfa037e92da0a34f7919ce4ca9db2bd546995c93.tar.gz md-content-cfa037e92da0a34f7919ce4ca9db2bd546995c93.zip |
Swift OCR post
-rw-r--r-- | md/writeup.md | 1 | ||||
-rw-r--r-- | md/writeup/swift_ocr_example.md | 77 |
2 files changed, 78 insertions, 0 deletions
diff --git a/md/writeup.md b/md/writeup.md index 2d79481..6c4dd05 100644 --- a/md/writeup.md +++ b/md/writeup.md @@ -29,6 +29,7 @@ title: Writeup page [Linux hello world in Swift](writeup/linux_hello_world_in_swift.md) [Running disk images in QEMU](writeup/running_disk_images_in_qemu.md) [Mqueue IPC example](writeup/mqueue_ipc_example.md) +[Swift OCR example](writeup/swift_ocr_example.md) ## Projects diff --git a/md/writeup/swift_ocr_example.md b/md/writeup/swift_ocr_example.md new file mode 100644 index 0000000..fd0e169 --- /dev/null +++ b/md/writeup/swift_ocr_example.md @@ -0,0 +1,77 @@ +title:Swift OCR example +keywords:swift,ocr,macos + +# Swift OCR example + +## Intro + +After getting new M1 whanted to try some fancy machine learning stuff that avaliable. +Got inspired when seen new post on reddit about OCRImage tool realse. As +it was written in Object-C https://www.turbozen.com/sourceCode/ocrImage/ ,so I +decided that is good learning task to read the code and reimplement all in Swift. + +## OCR + +The whole working prototype can fit just in few lines of code. All you need to create +is text recognition requester and handle that collect results. There is few example on +apple doc page. + +```siwft +func recognizeImageUrl(_ url:URL, _ error: Error?) { + var pieces:[TextPiece] = [] + var err:NSError? + + var textRequest = VNRecognizeTextRequest(completionHandler: recognizeTextHandler(request:error:)) + + var handler = VNImageRequestHandler(url: url) + do { + try handler.perform([textRequest]) + } catch { + print("Cannot perform request error:\(error)") + } + } + + func recognizeTextHandler(request: VNRequest, error: Error?) { + print("Start recognize handler") + guard let observations = + request.results as? [VNRecognizedTextObservation] else { + return + } + + let recognizedStrings = observations.compactMap { observation in + // Return the string of the top VNRecognizedText instance. + return observation.topCandidates(1).first?.string + } + print("\(recognizedStrings)") + } +``` + +## Source code + +Example XCode project is located at http://git.main.lv/cgit.cgi/OCRImage.git/ +clone it and launch with XCode +```bash +git clone http://git.main.lv/cgit.cgi/OCRImage.git/ +``` + +## Future developments + +Could be nice to add sorting text, and some option to import location of text to json file with coordinates. + +Drawing boxes around original image with detected text is next thing to add. + +And there is alot of things that could be added to make it full featured OCR software. + + + +## Links + +http://git.main.lv/cgit.cgi/OCRImage.git/ +https://www.turbozen.com/sourceCode/ocrImage/ +https://turbozen.com/sourceCode/ocrImage/ocrImage_src/ +https://developer.apple.com/documentation/vision/recognizing_text_in_images +http://git.main.lv/cgit.cgi/OCRImage.git/tree/OCRImage/main.swift?h=main + + + + |