Spaces:
Sleeping
Sleeping
File size: 1,895 Bytes
3c1af4b ee00213 3c1af4b ee00213 ff90c28 ee00213 3c1af4b ff90c28 ee00213 3c1af4b ff90c28 3c1af4b ff90c28 3c1af4b ff90c28 3c1af4b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import cv2
import numpy as np
from PIL import Image
import easyocr
# Skew Correction
def deskew(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(gray)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated
# Lighting Correction
def correct_lighting(image):
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
cl = clahe.apply(l)
limg = cv2.merge((cl,a,b))
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
return final
# OCR Extraction using EasyOCR
def extract_text(image, langs=['en']):
reader = easyocr.Reader(langs, gpu=False)
results = reader.readtext(image, detail=0, paragraph=True)
text = "\n".join(results)
return text
def enhance_for_ocr(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Adaptive Threshold
th = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 15, 10
)
return th
# Full pipeline
def process_image(file, langs=['en']):
img = Image.open(file).convert('RGB')
img_cv = np.array(img)
# 1. تصحيح الانحراف
img_cv = deskew(img_cv)
# 2. تصحيح الإضاءة
img_cv = correct_lighting(img_cv)
# 3. تحسين للنصوص
img_cv = enhance_for_ocr(img_cv)
# 4. استخراج النصوص
text = extract_text(img_cv, langs)
return text
|