Finetuned-MobilVIT / table_deskew.py
Nekshay's picture
Update table_deskew.py
ea67a8d verified
import cv2
import numpy as np
import pytesseract
import platform
from PIL import Image
from deskew import determine_skew
from shutil import which
import os
def _configure_tesseract_path():
current_os = platform.system()
if current_os == "Windows":
win_path = r"C:\Users\Wissen\AppData\Local\Programs\Tesseract-OCR\tesseract.exe"
if os.path.exists(win_path):
pytesseract.pytesseract.tesseract_cmd = win_path
else:
raise FileNotFoundError("Tesseract not found at expected Windows path.")
else:
if which("tesseract") is None:
raise EnvironmentError("Tesseract not found in PATH. Install using: sudo apt install tesseract-ocr")
# Configure Tesseract path once
_configure_tesseract_path()
def _get_rotation_angle(image: np.ndarray) -> int:
try:
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(rgb_image)
osd = pytesseract.image_to_osd(pil_image)
for line in osd.split('\n'):
if 'Rotate:' in line:
return int(line.split(":")[1].strip())
except Exception as e:
print("Tesseract OSD failed:", e)
return 0
def _rotate_image(image: np.ndarray, angle: int) -> np.ndarray:
if angle == 0:
return image
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
matrix = cv2.getRotationMatrix2D(center, -angle, 1.0)
cos = np.abs(matrix[0, 0])
sin = np.abs(matrix[0, 1])
new_w = int((h * sin) + (w * cos))
new_h = int((h * cos) + (w * sin))
matrix[0, 2] += (new_w / 2) - center[0]
matrix[1, 2] += (new_h / 2) - center[1]
return cv2.warpAffine(image, matrix, (new_w, new_h),
flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
def _deskew_image(image: np.ndarray, max_skew: float = 90.0) -> np.ndarray:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
binary = cv2.bitwise_not(binary)
angle = determine_skew(binary)
if abs(angle) > max_skew:
raise ValueError(f"Detected skew angle {angle:.2f}° exceeds max_skew limit of {max_skew}°.")
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
cos = np.abs(matrix[0, 0])
sin = np.abs(matrix[0, 1])
new_w = int((h * sin) + (w * cos))
new_h = int((h * cos) + (w * sin))
matrix[0, 2] += (new_w / 2) - center[0]
matrix[1, 2] += (new_h / 2) - center[1]
return cv2.warpAffine(image, matrix, (new_w, new_h),
flags=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_REPLICATE)
def fix_orientation_and_deskew(image: np.ndarray, max_skew: float = 90.0) -> np.ndarray:
angle = _get_rotation_angle(image)
rotated = _rotate_image(image, angle)
deskewed = _deskew_image(rotated, max_skew=max_skew)
return deskewed
if __name__ == "__main__":
img_path = r"C:\Users\Wissen\Downloads\image (3).png"
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError(f"Image not found at path: {img_path}")
out_img = fix_orientation_and_deskew(img)
cv2.imwrite("out_img_fixed.png", out_img)
print("Image processing complete. Saved as 'out_img_fixed.png'.")
import subprocess
cert_file = "certis3.crt"
try:
result = subprocess.run(
["openssl", "x509", "-in", cert_file, "-text", "-noout"],
capture_output=True,
text=True,
check=True
)
print("Certificate Details:\n")
print(result.stdout)
except subprocess.CalledProcessError as e:
print("Error running openssl command:")
print(e.stderr)
except FileNotFoundError:
print("OpenSSL executable not found. Ensure it's installed and in PATH.")