parent
694ddec9c7
commit
d78d53ac95
@ -0,0 +1,36 @@
|
||||
import pytesseract
|
||||
import cv2
|
||||
from PIL import Image
|
||||
import os
|
||||
|
||||
def recognize_text(input_path):
|
||||
#调整图片宽高
|
||||
def resize_image(input_path, output_path, scale_factor, interpolation=cv2.INTER_CUBIC):
|
||||
# 读取输入图像
|
||||
img = cv2.imread(input_path)
|
||||
# 如果图像读取失败,则返回错误消息
|
||||
if img is None:
|
||||
print("无法读取输入图像,请检查文件路径!")
|
||||
return
|
||||
# 获取输入图像的高度和宽度
|
||||
height, width = img.shape[:2]
|
||||
|
||||
# 根据缩放因子计算输出图像的大小
|
||||
new_width = int(width * scale_factor)
|
||||
new_height = int(height * scale_factor)
|
||||
|
||||
# 调整图像大小
|
||||
resized_img = cv2.resize(img, (new_width, new_height), interpolation=interpolation)
|
||||
|
||||
#删除原本图像
|
||||
os.remove(input_path)
|
||||
# 保存调整后的图像
|
||||
cv2.imwrite(output_path, resized_img)
|
||||
print("已保存调整大小后的图像:", output_path)
|
||||
#调整图片宽高
|
||||
output_path = input_path
|
||||
# resize_image(input_path,output_path, 6)
|
||||
image=Image.open(input_path)
|
||||
configdigit='--psm 11' #参数配置
|
||||
text=pytesseract.image_to_string(image,lang='num',config=configdigit) #选用自己的训练的数据集num
|
||||
return text #返回识别结果
|
Loading…
Reference in new issue