-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
59 lines (47 loc) · 1.69 KB
/
Copy pathdatabase.py
File metadata and controls
59 lines (47 loc) · 1.69 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
# database.py
import mysql.connector
import os
from dotenv import load_dotenv
import base64
import io
import cv2 as cv # Import OpenCV
load_dotenv()
def connect_to_database():
conn = mysql.connector.connect(
host=os.getenv('DB_HOST'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
database=os.getenv('DB_NAME')
)
return conn
def create_plate_number_table(cursor):
cursor.execute('''
CREATE TABLE IF NOT EXISTS plate_number (
id INT AUTO_INCREMENT PRIMARY KEY,
plate_number TEXT,
image_path VARCHAR(255)
)
''')
def save_to_database(plate_number, img_show_plate):
try:
conn = connect_to_database()
cursor = conn.cursor()
create_plate_number_table(cursor)
# Generate a sanitized filename using the plate number
sanitized_plate_number = "".join(c if c.isalnum() else "_" for c in plate_number)
filename = f"result/{sanitized_plate_number}_output.jpg"
filepath = os.path.join(os.getcwd(), filename)
# Create the result directory if it doesn't exist
os.makedirs(os.path.dirname(filepath), exist_ok=True)
# Save the image to the file with original color space
cv.imwrite(filepath, img_show_plate)
# Save the result to the database
cursor.execute('INSERT INTO plate_number (plate_number, image_path) VALUES (%s, %s)', (plate_number, filename))
conn.commit()
print("Data saved to the database and file written successfully")
except Exception as e:
print(f"Error: {e}")
finally:
if conn.is_connected():
cursor.close()
conn.close()