-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit_Img.py
More file actions
60 lines (44 loc) · 2.12 KB
/
Split_Img.py
File metadata and controls
60 lines (44 loc) · 2.12 KB
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
import cv2
import pytesseract
import os
# Read the input image
def perform_ocr(image_path: str) -> None:
image = cv2.imread(image_path)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Perform OCR to extract text and bounding boxes
data = pytesseract.image_to_data(gray, output_type=pytesseract.Output.DICT)
# Set the output folder name
output_folder = 'images_after_splitting'
# Check if the output folder already exists
if not os.path.exists(output_folder):
# If the folder doesn't exist, create it
os.makedirs(output_folder)
# Initialize a counter for words
word_counter = 0
# Define the expansion factor for the bounding box (in pixels)
expansion_factor = 12 # Adjust this value as needed
# Iterate over each word bounding box
for i, word_text in reversed(list(enumerate(data['text']))):
# Filter out non-word regions (ignore empty strings)
if word_text.strip():
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
# Expand the bounding box coordinates
x -= expansion_factor
y -= expansion_factor
w += 2 * expansion_factor
h += 2 * expansion_factor
# Ensure the coordinates are within the image boundaries
x = max(0, x)
y = max(0, y)
w = min(w, image.shape[1] - x)
h = min(h, image.shape[0] - y)
# Crop the region corresponding to the expanded bounding box
word_image = image[y:y+h, x:x+w]
resized_word_image = cv2.resize(word_image, (128, 32))
# Save the word image into the folder
cv2.imwrite(os.path.join(output_folder, f'word_{word_counter}.jpg'), resized_word_image)
# Print the text of each word along with its expanded bounding box coordinates
print(f"Word {word_counter}: {word_text}, Expanded Bounding Box: (x={x}, y={y}, w={w}, h={h})")
# Increment the word counter
word_counter += 1