===============
== Techniche ==
===============
Unveiling the Nexus of Tech and Self-Reflection

OCR is cool

OCR

I have worked on a project to develop an OCR (Optical Character Recognition) solution using Swift and the Vision framework. The main.swift file contains the core code for performing text recognition in images, utilizing the VNRecognizeTextRequest class provided by Apple’s Vision framework. This project aims to provide a straightforward and efficient OCR solution for Swift developers, leveraging the power of machine learning and computer vision technologies.

I have this executable called SwiftOCR-Vision which can be used to extract texts from images and get it’s box data.

Github: https://github.com/adiaholic/SwiftOCR-Vision

To build this project I researched a lot of other libraries and framework and below is an overview of some of the stuff I tried.

Text Recognition with Tesseract, Hugging Face’s Candle, and Apple Vision Kit

In this blog, we will explore text recognition using Tesseract in Python, Hugging Face’s Candle in Rust, and Apple’s Vision Kit in Swift. We’ll cover the basics of each approach and provide code examples for implementing text recognition in these languages.

Text Recognition with Tesseract in Python

Tesseract is a popular open-source OCR (Optical Character Recognition) engine that supports over 100 languages. In Python, you can use the pytesseract library to interface with Tesseract and perform text recognition on images.

Here’s a simple example of using Tesseract in Python:

python
import pytesseract
from PIL import Image
Load an image using PIL
image = Image.open('image.jpg')
Perform text recognition using Tesseract
text = pytesseract.image_to_string(image)
Print the recognized text
print(text)

Text Recognition with Hugging Face’s Candle in Rust

Hugging Face’s Candle is a Rust library for natural language processing tasks, including text recognition. It provides a high-level interface for using pre-trained models and performing text recognition tasks in Rust.

Here’s a basic example of using Candle for text recognition in Rust:

rust
use candle::text_recognition;

fn main() {
let image_path = "image.jpg";
let recognized_text = text_recognition::recognize_text(image_path);
println!("{}", recognized_text);
}

Text Recognition with Apple Vision Kit in Swift

Apple’s Vision Kit provides classes and APIs for performing image analysis and computer vision tasks, including text recognition. In Swift, you can use Vision Kit to recognize text in images captured using the device’s camera or loaded from the device’s storage.

Here’s a simple example of using Vision Kit for text recognition in Swift:

swift
import Vision
import UIKit

func recognizeText(in image: UIImage) {
guard let cgImage = image.cgImage else { return }

let request = VNRecognizeTextRequest { request, error in
guard let observations = request.results as? [VNRecognizedTextObservation] else { return }
for observation in observations {
let recognizedText = observation.topCandidates(1).first?.string ?? ""
print(recognizedText)
}
}

let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do {
try handler.perform([request])
} catch {
print(error)
}
}

// Usage
let image = UIImage(named: "image.jpg")!
recognizeText(in: image)

By following the examples provided in this blog, you can start performing text recognition using Tesseract in Python, Hugging Face’s Candle in Rust, and Apple’s Vision Kit in Swift. Each approach has its own strengths and use cases, so choose the one that best fits your requirements.