- Resources
Cookbooks
Language Support OCR: Persian Handwriting to English
Hands-on notebooks and guides for building with the Mistral API.
Language Support OCR: Persian Handwriting to English
Displays samples/persian-writing.jpeg, extracts Persian handwriting with Mistral OCR-4 and renders the English result as Markdown.
# Import required libraries
import os
from pathlib import Path
from dotenv import load_dotenv
from IPython.display import Image, display
# 2. Load environment variables and paths
load_dotenv()
ENDPOINT = os.getenv('AZURE_MISTRAL_DOCUMENT_AI_ENDPOINT')
API_KEY = os.getenv('AZURE_MISTRAL_DOCUMENT_AI_KEY')
MODEL_NAME = os.getenv('AZURE_AI_DEPLOYMENT_NAME')
IMAGE_PATH = Path.cwd() / 'samples' / 'persian-writing.jpeg'
HEADERS = {'Content-Type': 'application/json', 'Authorization': f'Bearer {API_KEY}'}
from ocr4_utils import call_ocr4, extract_text as extract_lang_text
print('Shared OCR helpers imported.')
if not IMAGE_PATH.exists():
raise FileNotFoundError(f'Could not find {IMAGE_PATH}')
display(Image(filename=str(IMAGE_PATH)))
ocr_response = call_ocr4(IMAGE_PATH)
persian_text = extract_lang_text(ocr_response)
display('Persian OCR text:', persian_text)
# 8. Optional local fallback OCR (requires Tesseract and fas.traineddata)
# from PIL import Image as PILImage
# import pytesseract
# persian_text = pytesseract.image_to_string(PILImage.open(IMAGE_PATH), lang='fas')
IMAGE_PATH = Path.cwd() / 'samples' / 'persian_script.jpeg'
display(Image(filename=str(IMAGE_PATH)))
ocr_response = call_ocr4(IMAGE_PATH)
persian_text = extract_lang_text(ocr_response)
display('Persian OCR text:', persian_text)
Hindi Sample
IMAGE_PATH = Path.cwd() / 'samples' / 'hindi_scanned_story.png'
display(Image(filename=str(IMAGE_PATH)))
ocr_response = call_ocr4(IMAGE_PATH)
hindi_text = extract_lang_text(ocr_response)
display('Hindi OCR text:', hindi_text)