赞
踩
语言环境:Python
数据库:Mysql
开发工具:VScode
再分割 ISBN 所在行:通过 split_col()函数将二值图像中的字符进行分割,并识别出每个字符。
- def split_col(bin_img2):
- the_col = [0] * bin_img2.shape[1]
- for i in range(bin_img2.shape[0]):
- for j in range(bin_img2.shape[1]):
- if bin_img2[i, j] != 0:
- the_col[j] += 1
- a = [] num = 0
- for j in range(bin_img2.shape[1]):
- if the_col[j] >= 3 and num % 2 == 0:
- num += 1
- left_bound = j
- j += 2
- elif the_col[j] == 0 and num % 2 != 0:
- num += 1
- right_bound = j
- a.append((left_bound, right_bound))
- j += 2
- return a
- def recognition(input_img):
- sample_img_path = "D:\Desktop\sample\*"
- sample_img_FN = glob.glob(sample_img_path)
- sample_img_nums = len(sample_img_FN)
- min_diff = float('inf')
- recognized_char = ''
- for img_path in sample_img_FN:
- num_img = cv2.imread(img_path, 0)
- num_img = cv2.resize(num_img, (40, 60))
- input_img = cv2.resize(input_img, (40, 60))
- diff = np.sum(cv2.absdiff(input_img, num_img))
- if diff < min_diff:
- min_diff = diff
- recognized_char = img_path[len(sample_img_path) - 1]
- return recognized_char
负责与MySQL数据库交互,并处理存储和管理书籍信息、借阅记录等数据。当图像识别模块识别出图书信息后,该模块将相关信息存储到数据库中,并能根据用户的查询需求从数据库中检索信息,如书籍详情或借阅记录等。
- import mysql.connector
- db = mysql.connector.connect(
- host="localhost", user="root", password="030303", database="book" )
- cursor = db.cursor()
- create_table_query = """
- CREATE TABLE IF NOT EXISTS book (
- id INT AUTO_INCREMENT PRIMARY KEY, isbn VARCHAR(20) UNIQUE NOT NULL
- )"""
- cursor.execute(create_table_query)
- cursor.close()
- db.close()
- def add_isbn_to_database(isbn):
- db = mysql.connector.connect(
- host="localhost", user="root", password="******", database="book")
- cursor = db.cursor()
- insert_query = "INSERT INTO book (isbn) VALUES (%s)"
- cursor.execute(insert_query, (isbn,))
- db.commit()
- cursor.close()
- db.close()
- def borrow_book(isbn_number):
- cursor = db.cursor()
- check_book_query = "SELECT * FROM book WHERE isbn = %s"
- cursor.execute(check_book_query, (isbn_number,))
- book_exists = cursor.fetchone()
- if book_exists:
- print("书籍已找到!")
- borrow_choice = input("是否需要借阅该书籍?(y/n): ")
- if borrow_choice.lower() == 'y':
- borrower_name = input("请输入借阅者姓名: ")
- print('请输入借阅者姓名:',borrower_name)
- # 获取当前日期时间
- current_date_time = datetime.datetime.now()
- borrow_date = current_date_time.strftime('%Y-%m-%d %H:%M:%S')
- insert_query = "INSERT INTO borrow_records (isbn, borrower_name, borrow_date)VALUES (%s, %s, %s)" borrow_data = (isbn_number, borrower_name, borrow_date)
- cursor.execute(insert_query, borrow_data)
- db.commit()
- print("借阅记录已更新!")
- else:
- print("未找到该书籍,请检查 ISBN 码是否正确。")
- cursor.close()
- db.close()
- borrow_book()
总而言之,以上期末项目虽还无法实际应用,但还是很好地训练和巩固了本人对于OpenCV库和搭建MySQL数据库的知识,以上代码非完整代码但所展示的模块代码经测试都可直接运行。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。