ocr-orderid3 / process.py
ethanrom's picture
Upload 17 files
9ec9085
import cv2
import numpy as np
def preprocess_image_simple(image_file):
img = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
return gray
def preprocess_image(image_file):
img = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
blur = cv2.GaussianBlur(thresh, (3,3), 0)
thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
coords = np.column_stack(np.where(thresh > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = thresh.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(thresh, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated
def preprocess_image_high(image_file):
img = cv2.imread(image_file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Adaptive thresholding
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
opened = cv2.morphologyEx(closed, cv2.MORPH_OPEN, kernel)
# Connected Component Analysis (CCA)
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(opened)
filtered_labels = []
for i in range(1, num_labels):
# Filter out components based on their size, aspect ratio, and position
x, y, w, h, area = stats[i]
aspect_ratio = float(w) / h
if area > 100 and aspect_ratio < 5 and aspect_ratio > 0.2 and x > 10 and y > 10:
filtered_labels.append(i)
filtered = np.zeros_like(labels)
for i, label in enumerate(filtered_labels):
filtered[labels == label] = i + 1
# Skew correction
coords = np.column_stack(np.where(filtered > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(90 + angle)
else:
angle = -angle
(h, w) = filtered.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated = cv2.warpAffine(filtered, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)
return rotated