Untitled

 avatar
unknown
plain_text
2 years ago
1.6 kB
4
Indexable
import pytesseract
from PIL import Image
import numpy as np
import queue

# Load the image
img = Image.open('image.png')

# Convert the image to a numpy array
img_array = np.array(img)

# Define the connectivity of the pixels
connectivity = [(0, 1), (1, 0), (0, -1), (-1, 0)]

# Define the BFS function
def bfs(img_array, start):
    q = queue.Queue()
    q.put(start)
    visited = set()
    text = ''
    while not q.empty():
        node = q.get()
        if node in visited:
            continue
        visited.add(node)
        x, y = node
        if img_array[x][y] == 0:
            text += pytesseract.image_to_string(Image.fromarray(img_array))
        for dx, dy in connectivity:
            nx, ny = x + dx, y + dy
            if 0 <= nx < img_array.shape and 0 <= ny < img_array.shape[1]:
                q.put((nx, ny))
    return text

# Define the DFS function
def dfs(img_array, start):
    stack = [start]
    visited = set()
    text = ''
    while stack:
        node = stack.pop()
        if node in visited:
            continue
        visited.add(node)
        x, y = node
        if img_array[x][y] == 0:
            text += pytesseract.image_to_string(Image.fromarray(img_array))
        for dx, dy in connectivity:
            nx, ny = x + dx, y + dy
            if 0 <= nx < img_array.shape and 0 <= ny < img_array.shape[1]:
                stack.append((nx, ny))
    return text

# Perform BFS and DFS on the image
start = (0, 0)
text_bfs = bfs(img_array, start)
text_dfs = dfs(img_array, start)

# Print the recognized text
print('BFS:', text_bfs)
print('DFS:', text_dfs)