Spaces:
Running
Running
ocr remove excess code
Browse files- Layoutlmv3_inference/ocr.py +0 -79
Layoutlmv3_inference/ocr.py
CHANGED
@@ -49,85 +49,6 @@ def preprocess_image(image_path, max_file_size_mb=1, target_file_size_mb=0.5):
|
|
49 |
return None
|
50 |
|
51 |
|
52 |
-
def enhance_txt(img, intensity_increase=20, bilateral_filter_diameter=9, bilateral_filter_sigma_color=75, bilateral_filter_sigma_space=75):
|
53 |
-
# Get the width and height of the image
|
54 |
-
w = img.shape[1]
|
55 |
-
h = img.shape[0]
|
56 |
-
w1 = int(w * 0.05)
|
57 |
-
w2 = int(w * 0.95)
|
58 |
-
h1 = int(h * 0.05)
|
59 |
-
h2 = int(h * 0.95)
|
60 |
-
ROI = img[h1:h2, w1:w2] # 95% of the center of the image
|
61 |
-
threshold = np.mean(ROI) * 0.88 # % of average brightness
|
62 |
-
|
63 |
-
# Convert image to grayscale
|
64 |
-
grayscale_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
65 |
-
|
66 |
-
# Apply Gaussian blur
|
67 |
-
blurred = cv2.GaussianBlur(grayscale_img, (1, 1), 0)
|
68 |
-
|
69 |
-
edged = 255 - cv2.Canny(blurred, 100, 150, apertureSize=7)
|
70 |
-
|
71 |
-
# Increase intensity by adding a constant value
|
72 |
-
img = np.clip(img + intensity_increase, 0, 255).astype(np.uint8)
|
73 |
-
|
74 |
-
# Apply bilateral filter to reduce noise
|
75 |
-
img = cv2.bilateralFilter(img, bilaterimport os
|
76 |
-
import pandas as pd
|
77 |
-
import cv2
|
78 |
-
import numpy as np
|
79 |
-
import json
|
80 |
-
import requests
|
81 |
-
import traceback
|
82 |
-
import tempfile
|
83 |
-
from rembg import remove
|
84 |
-
|
85 |
-
|
86 |
-
from PIL import Image
|
87 |
-
|
88 |
-
|
89 |
-
def preprocess_image(image_path, max_file_size_mb=1, target_file_size_mb=0.5):
|
90 |
-
try:
|
91 |
-
# Read the image
|
92 |
-
image = cv2.imread(image_path)
|
93 |
-
# Enhance text
|
94 |
-
enhanced = enhance_txt(image)
|
95 |
-
|
96 |
-
# Save the enhanced image to a temporary file
|
97 |
-
temp_file_path = tempfile.NamedTemporaryFile(suffix='.jpg').name
|
98 |
-
cv2.imwrite(temp_file_path, enhanced)
|
99 |
-
|
100 |
-
# Check file size of the temporary file
|
101 |
-
file_size_mb = os.path.getsize(
|
102 |
-
temp_file_path) / (1024 * 1024) # Convert to megabytes
|
103 |
-
|
104 |
-
while file_size_mb > max_file_size_mb:
|
105 |
-
print(
|
106 |
-
f"File size ({file_size_mb} MB) exceeds the maximum allowed size ({max_file_size_mb} MB). Resizing the image.")
|
107 |
-
ratio = np.sqrt(target_file_size_mb / file_size_mb)
|
108 |
-
new_width = int(image.shape[1] * ratio)
|
109 |
-
new_height = int(image.shape[0] * ratio)
|
110 |
-
|
111 |
-
# Resize the image
|
112 |
-
enhanced = cv2.resize(enhanced, (new_width, new_height))
|
113 |
-
|
114 |
-
# Save the resized image to a temporary file
|
115 |
-
temp_file_path = tempfile.NamedTemporaryFile(suffix='.jpg').name
|
116 |
-
cv2.imwrite(temp_file_path, enhanced)
|
117 |
-
|
118 |
-
# Update file size
|
119 |
-
file_size_mb = os.path.getsize(temp_file_path) / (1024 * 1024)
|
120 |
-
print(f"New file size: ({file_size_mb} MB)")
|
121 |
-
|
122 |
-
# Return the final resized image
|
123 |
-
image_resized = cv2.imread(temp_file_path)
|
124 |
-
return image_resized
|
125 |
-
|
126 |
-
except Exception as e:
|
127 |
-
print(f"An error occurred in preprocess_image: {str(e)}")
|
128 |
-
return None
|
129 |
-
|
130 |
-
|
131 |
def enhance_txt(img, intensity_increase=20, bilateral_filter_diameter=9, bilateral_filter_sigma_color=75, bilateral_filter_sigma_space=75):
|
132 |
# Get the width and height of the image
|
133 |
w = img.shape[1]
|
|
|
49 |
return None
|
50 |
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
def enhance_txt(img, intensity_increase=20, bilateral_filter_diameter=9, bilateral_filter_sigma_color=75, bilateral_filter_sigma_space=75):
|
53 |
# Get the width and height of the image
|
54 |
w = img.shape[1]
|