-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (69 loc) · 2.11 KB
/
Copy pathmain.py
File metadata and controls
84 lines (69 loc) · 2.11 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import cv2
import numpy as np
import pytesseract
import psycopg2
# Load an image from file
image = cv2.imread('path_to_image.jpg')
# Display the image in a window
cv2.imshow('Image', image)
# Wait for a key press and close the window
cv2.waitKey(0)
cv2.destroyAllWindows()
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Use pytesseract to do OCR on the image
text = pytesseract.image_to_string(gray_image)
# Print the recognized text
print(text)
# Connect to your postgres DB
conn = psycopg2.connect(
dbname="your_dbname",
user="your_username",
password="your_password",
host="your_host",
port="your_port"
)
# Open a cursor to perform database operations
cur = conn.cursor()
# Create a table to store OCR results if it doesn't exist
cur.execute('''
CREATE TABLE IF NOT EXISTS ocr_results (
id SERIAL PRIMARY KEY,
text TEXT NOT NULL
)
''')
# Insert the OCR result into the table
cur.execute('''
INSERT INTO ocr_results (text) VALUES (%s)
''', (text,))
# Commit the transaction
conn.commit()
# Close the cursor and connection
cur.close()
conn.close()
# Function to split the image into cells for scoreboard OCR
def split_image_into_cells(image, rows, cols):
# Get the dimensions of the image
height, width = image.shape[:2]
cell_height = height // rows
cell_width = width // cols
cells = []
for row in range(rows):
for col in range(cols):
# Calculate the coordinates of the cell
start_row = row * cell_height
start_col = col * cell_width
end_row = start_row + cell_height
end_col = start_col + cell_width
# Extract the cell from the image
cell = image[start_row:end_row, start_col:end_col]
cells.append(cell)
return cells
# Example usage
rows = 5 # Number of rows in the scoreboard
cols = 5 # Number of columns in the scoreboard
cells = split_image_into_cells(image, rows, cols)
# Perform OCR on each cell
for i, cell in enumerate(cells):
cell_text = pytesseract.image_to_string(cell)
print(f"Cell {i} text: {cell_text}")