स्कैन किए गए दस्तावेज़ों के लिए OCR
pytesseract के ज़रिए Tesseract, इमेज प्रीप्रोसेसिंग और सटीकता में सुधार।
स्कैन किए गए दस्तावेज़ों के लिए OCR, CoddyKit पर AI एजेंट का एक निःशुल्क पाठ है। यह 4 में से 2वाँ पाठ है। आप नीचे पूरा पाठ निःशुल्क पढ़ सकते हैं—फिर अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर के साथ ब्राउज़र में इसका व्यावहारिक अभ्यास कर सकते हैं। यह AI एजेंट सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। AI एजेंट पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
OCR कब आवश्यक है
सभी दस्तावेज़ मूल रूप से डिजिटल PDF नहीं होते। स्कैन किए गए दस्तावेज़, पाठ वाली तस्वीरें, हस्तलिखित टिप्पणियाँ और छवि-आधारित PDF से पाठ निकालने के लिए ऑप्टिकल अक्षर पहचान (OCR) आवश्यक है।
OCR पाठ की पिक्सेल छवियों को मशीन-पठनीय अक्षरों में बदलता है। दो प्रमुख पाइथन लाइब्रेरी हैं: पाइटेसेरैक्ट (Google टेसेरैक्ट) और EasyOCR (डीप लर्निंग, बहुभाषी)।
पाइटेसेरैक्ट की मूल बातें
पाइटेसेरैक्ट, Google के टेसेरैक्ट OCR इंजन के चारों ओर बना पाइथन रैपर है। पहले टेसेरैक्ट इंस्टॉल करें (OS-स्तर पर), फिर pip install pytesseract Pillow चलाएँ।
import pytesseract
from PIL import Image
# Simple text extraction
image = Image.open('scanned_document.png')
text = pytesseract.image_to_string(image)
print(text)
# Specify language (default: English)
text_fr = pytesseract.image_to_string(
Image.open('french_doc.png'),
lang='fra'
)
# Get detailed output with bounding boxes
data = pytesseract.image_to_data(
image,
output_type=pytesseract.Output.DICT
)
for i, word in enumerate(data['text']):
if word.strip():
conf = data['conf'][i]
print(f'Word: {word!r:20} Confidence: {conf}')छवि पूर्व-संसाधन: ग्रेस्केल
OCR की सटीकता छवि की गुणवत्ता पर बहुत अधिक निर्भर करती है। पहला पूर्व-संसाधन चरण छवि को ग्रेस्केल में बदलना है—रंग शोर बढ़ाते हैं, अक्षर पहचान में मदद नहीं करते।
पूर्व-संसाधन के लिए पिलो या OpenCV का उपयोग करें। ग्रेस्केल शोर कम करता है और टेसेरैक्ट की अक्षर-सीमा पहचान को बेहतर बनाता है।
from PIL import Image, ImageOps
import numpy as np
def preprocess_grayscale(image_path):
img = Image.open(image_path)
# Convert to grayscale
img = img.convert('L') # 'L' = 8-bit grayscale
# Optionally resize for better OCR (Tesseract works best at ~300 DPI)
# Scale up small images
width, height = img.size
if width < 800:
scale = 800 / width
new_size = (int(width * scale), int(height * scale))
img = img.resize(new_size, Image.LANCZOS)
return img
img = preprocess_grayscale('scan.jpg')
text = pytesseract.image_to_string(img)
print(text[:300])छवि पूर्व-संसाधन: थ्रेशहोल्डिंग
थ्रेशहोल्डिंग ग्रेस्केल छवि को पूरी तरह काले-सफ़ेद में बदलती है। इससे धूसर छायाएँ, असमान रोशनी और पृष्ठभूमि का शोर हट जाता है, जिससे OCR के लिए पाठ स्पष्ट रूप से उभर आता है।
from PIL import Image, ImageFilter
import numpy as np
def apply_threshold(img):
# Method 1: Simple fixed threshold
img_array = np.array(img)
threshold = 128
binary = np.where(img_array > threshold, 255, 0).astype(np.uint8)
return Image.fromarray(binary)
def adaptive_threshold(img):
# Method 2: Otsu's method via OpenCV (better for uneven lighting)
try:
import cv2
img_array = np.array(img)
_, binary = cv2.threshold(
img_array, 0, 255,
cv2.THRESH_BINARY + cv2.THRESH_OTSU
)
return Image.fromarray(binary)
except ImportError:
return apply_threshold(img)
img = preprocess_grayscale('uneven_scan.png')
img_clean = adaptive_threshold(img)
text = pytesseract.image_to_string(img_clean)
print(text[:300])छवि पूर्व-संसाधन: तिरछापन सुधारना
स्कैन किए गए दस्तावेज़ अक्सर थोड़े घुमे हुए होते हैं। केवल 2 डिग्री का झुकाव भी OCR की सटीकता को काफ़ी कम कर देता है। तिरछापन सुधारने की प्रक्रिया छवि को OCR इंजन में भेजने से पहले घुमाव के कोण का पता लगाकर उसे ठीक करती है।
import numpy as np
from PIL import Image
def deskew(img):
try:
import cv2
img_array = np.array(img)
# Find rotation angle using Hough line transform
edges = cv2.Canny(img_array, 50, 150, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi/180, threshold=100)
if lines is None:
return img # no lines detected, return unchanged
angles = []
for line in lines:
rho, theta = line[0]
angle = np.degrees(theta) - 90
if -45 < angle < 45:
angles.append(angle)
if not angles:
return img
median_angle = np.median(angles)
if abs(median_angle) > 0.5: # only deskew if significant tilt
print(f'Deskewing by {median_angle:.2f} degrees')
return img.rotate(-median_angle, expand=True, fillcolor=255)
return img
except ImportError:
return img # OpenCV not available — skip deskewटेसेरैक्ट के भाषा विकल्प
टेसेरैक्ट 100 से अधिक भाषाओं का समर्थन करता है। अपने OS के पैकेज प्रबंधक से भाषा पैक डाउनलोड करें। बहुभाषी दस्तावेज़ों के लिए + विभाजक का उपयोग करके कई भाषाएँ निर्दिष्ट करें।
import pytesseract
from PIL import Image
# List available languages
import subprocess
result = subprocess.run(['tesseract', '--list-langs'], capture_output=True, text=True)
print('Available languages:')
print(result.stdout)
# Single language
text_en = pytesseract.image_to_string(Image.open('doc.png'), lang='eng')
# Multiple languages (auto-detect best match)
text_multi = pytesseract.image_to_string(
Image.open('doc.png'),
lang='eng+fra+deu' # English + French + German
)
# Tesseract config options for better accuracy
custom_config = r'--oem 3 --psm 6' # OEM 3=LSTM, PSM 6=assume uniform block of text
text_tuned = pytesseract.image_to_string(
Image.open('doc.png'),
config=custom_config
)EasyOCR: डीप लर्निंग आधारित
EasyOCR डीप लर्निंग मॉडल का उपयोग करता है और खराब गुणवत्ता वाली छवियों, घुमावदार पाठ तथा गैर-लैटिन लिपियों पर टेसेरैक्ट से बेहतर प्रदर्शन करता है। इसके लिए OS-स्तरीय इंस्टॉलेशन की आवश्यकता नहीं होती।
pip install easyocr से इंस्टॉल करें। पहली बार चलाने पर मॉडल वेट (लगभग 200 एमबी) डाउनलोड होते हैं।
import easyocr
# Initialize reader (downloads model on first run)
reader = easyocr.Reader(
['en', 'tr'], # list of languages
gpu=False # set True if CUDA available
)
# Read text from image
results = reader.readtext('scanned_page.png')
for (bbox, text, confidence) in results:
print(f'Text: {text!r:30} Confidence: {confidence:.2f}')
# bbox = [[x1,y1],[x2,y2],[x3,y3],[x4,y4]] (four corners)
# Get plain text only
text_only = ' '.join(result[1] for result in results)
print('Full text:', text_only[:300])बहुभाषी दस्तावेज़ों के लिए EasyOCR
EasyOCR मिश्रित-भाषा वाले दस्तावेज़ों को अच्छी तरह संभालता है—यह अंतरराष्ट्रीय अनुबंधों, कई भाषाओं में शीर्षकों वाले शोध-पत्रों या किसी दूसरी लिपि में लिखे तकनीकी शब्दों वाले दस्तावेज़ों के लिए उपयोगी है।
import easyocr
# For Arabic-English mixed document
reader_ar = easyocr.Reader(['ar', 'en'], gpu=False)
results = reader_ar.readtext('arabic_contract.png')
# For CJK languages
reader_cjk = easyocr.Reader(['ch_sim', 'en'], gpu=False) # Simplified Chinese + English
results_cjk = reader_cjk.readtext('chinese_report.png')
# Filter by confidence threshold
HIGH_CONFIDENCE = 0.7
def filter_confident_results(results, threshold=HIGH_CONFIDENCE):
return [
(bbox, text, conf)
for bbox, text, conf in results
if conf >= threshold
]पूर्ण पूर्व-संसाधन कार्यप्रवाह
सभी पूर्व-संसाधन चरणों—ग्रेस्केल, थ्रेशहोल्ड और तिरछापन सुधार—को एकल कार्यप्रवाह फ़ंक्शन में जोड़ें। साफ़ की गई छवि पर OCR चलाएँ और पाठ तथा विश्वसनीयता मापदंड दोनों लौटाएँ।
import pytesseract
from PIL import Image
def ocr_pipeline(image_path, lang='eng', engine='tesseract'):
print(f'Processing: {image_path}')
# 1. Load
img = Image.open(image_path)
# 2. Preprocess
img = img.convert('L') # grayscale
img = adaptive_threshold(img) # binarize
img = deskew(img) # correct rotation
# 3. OCR
if engine == 'easyocr':
reader = easyocr.Reader([lang], gpu=False)
results = reader.readtext(image_path)
text = ' '.join(r[1] for r in results if r[2] > 0.5)
else:
config = f'--oem 3 --psm 6 -l {lang}'
text = pytesseract.image_to_string(img, config=config)
# 4. Clean
text = '\n'.join(
line.strip() for line in text.splitlines()
if line.strip()
)
return text
text = ocr_pipeline('invoice_scan.jpg')
print(text[:500])छवि-आधारित PDF को संभालना
कुछ PDF में PDF कंटेनर के भीतर छवियों के रूप में संग्रहीत स्कैन किए गए पृष्ठ होते हैं। इन 'छवि PDF' की पहचान करें और पहले PyMuPDF का उपयोग करके छवियाँ निकालते हुए, पृष्ठ-दर-पृष्ठ OCR लागू करें।
import fitz
from PIL import Image
import io
import pytesseract
def is_image_pdf(doc):
# Check if first page has very little extractable text
text = doc[0].get_text().strip()
return len(text) < 50
def ocr_pdf(filepath):
doc = fitz.open(filepath)
if not is_image_pdf(doc):
# Native text — no OCR needed
return '\n'.join(doc[i].get_text() for i in range(len(doc)))
print('Image PDF detected — running OCR')
all_text = []
for page_num in range(len(doc)):
page = doc[page_num]
# Render page as image at 300 DPI
mat = fitz.Matrix(300/72, 300/72) # scale to 300 DPI
pix = page.get_pixmap(matrix=mat)
img = Image.open(io.BytesIO(pix.tobytes('png')))
text = pytesseract.image_to_string(img)
all_text.append(f'--- Page {page_num+1} ---\n{text}')
doc.close()
return '\n'.join(all_text)विश्वसनीयता फ़िल्टरिंग और पश्च-संसाधन
OCR परिणामों में त्रुटियाँ होती हैं, विशेषकर खराब गुणवत्ता वाली छवियों पर। कम विश्वसनीयता वाले शब्दों को फ़िल्टर करके, सामान्य OCR गलतियों (0 बनाम O, 1 बनाम l) को सुधारकर और रिक्त स्थानों को सामान्य रूप देकर पाठ का पश्च-संसाधन करें।
import re
COMMON_OCR_ERRORS = {
r'\b0([a-z])\b': r'O\1',
r'\bl([0-9])\b': r'1\1',
r'\|': 'I',
r' +': ' ',
}
def post_process_ocr(text):
for pattern, replacement in COMMON_OCR_ERRORS.items():
text = re.sub(pattern, replacement, text)
lines = [line.strip() for line in text.splitlines()]
return '\n'.join(line for line in lines if line)
def high_confidence_ocr(words_with_conf, min_confidence=60):
words = [w for w, conf in words_with_conf if w.strip() and conf >= min_confidence]
return post_process_ocr(' '.join(words))
fake_ocr_output = [('0range', 90), ('|s', 40), ('l5', 85), ('good', 95)]
print(high_confidence_ocr(fake_ocr_output))ज्ञान जाँच
OCR से पहले छवि पर सीमा (द्विआधारीकरण) लागू करने का उद्देश्य क्या है?
पुनरावलोकन: स्कैन किए गए दस्तावेज़ों के लिए OCR
OCR स्कैन की गई छवियों को मशीन-पठनीय पाठ में बदलता है। पाइटेसेरैक्ट Google टेसेरैक्ट का रैपर है—100 से अधिक भाषा पैक के साथ तेज़ और परिपक्व। EasyOCR डीप लर्निंग का उपयोग करता है और खराब गुणवत्ता वाली छवियों तथा बहुभाषी पाठ को बेहतर ढंग से संभालता है।
हमेशा पूर्व-संसाधन करें: ग्रेस्केल में बदलें, द्विआधारीकरण के लिए ओत्सु थ्रेशहोल्डिंग लागू करें और घुमाव ठीक करने के लिए तिरछापन सुधारें। छवि-आधारित PDF के लिए PyMuPDF से पृष्ठों को उच्च-DPI छवियों के रूप में रेंडर करें, फिर OCR चलाएँ। शोर को हटाने के लिए परिणामों को विश्वसनीयता सीमा के आधार पर फ़िल्टर करें।
एआई शिक्षक के साथ AI एजेंट सीखें — निःशुल्क
अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।
- पाठ्यक्रम
- 60
- पाठ
- 239
अक्सर पूछे जाने वाले प्रश्न
क्या “स्कैन किए गए दस्तावेज़ों के लिए OCR” पाठ निःशुल्क है?
हाँ—“स्कैन किए गए दस्तावेज़ों के लिए OCR” का पूरा पाठ यहाँ वेब पर निःशुल्क पढ़ा जा सकता है। इंटरैक्टिव अभ्यास (अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर) करने और AI एजेंट पाठ्यक्रम का बाकी हिस्सा अनलॉक करने के लिए CoddyKit PRO लें। AI एजेंट पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
“स्कैन किए गए दस्तावेज़ों के लिए OCR” में मैं क्या सीखूँगा?
pytesseract के ज़रिए Tesseract, इमेज प्रीप्रोसेसिंग और सटीकता में सुधार। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ AI एजेंट का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।
क्या AI एजेंट शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?
पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर AI एजेंट शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 2वाँ पाठ है।
“स्कैन किए गए दस्तावेज़ों के लिए OCR” पाठ पूरा करने में कितना समय लगता है?
CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।
क्या मैं इस AI एजेंट पाठ में कोड लिख और चला सकता हूँ?
हाँ। हर AI एजेंट पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।
इस पाठ्यक्रम के सभी पाठ
- PyMuPDF और pdfplumber से PDF पार्स करना
- स्कैन किए गए दस्तावेज़ों के लिए OCR
- बहु-दस्तावेज़ प्रश्नोत्तर एजेंट
- दस्तावेज़ वर्गीकरण और रूटिंग