建筑公司网站运营方案,手机网站 制作,鞍山网站建设联系方式,博远手机销售管理系统1 cgg带你建个工程
如图 不然你的pip baidu-aip 用不了
先对图片进行一点处理
$ 灰度处理 $ 滤波处理 参考
import cv2
import os
def preprocess_images(input_folder, output_folder):# 确保输出文件夹存在if not os.path.exists(output_folder):os.makedirs(output_fol…1 cgg带你建个工程
如图 不然你的pip baidu-aip 用不了
先对图片进行一点处理
$ 灰度处理 $ 滤波处理 参考
import cv2
import os
def preprocess_images(input_folder, output_folder):# 确保输出文件夹存在if not os.path.exists(output_folder):os.makedirs(output_folder)# 获取输入文件夹中所有图片的文件名image_files [f for f in os.listdir(input_folder) if f.lower().endswith((.png, .jpg, .jpeg))]# 初始化计数器count 1for image_file in image_files:# 构建完整的输入文件路径input_path os.path.join(input_folder, image_file)# 读取图片image cv2.imread(input_path)if image is None:print(fWarning: Unable to read image {input_path})continue# 转换为灰度图gray_image cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 应用高斯滤波去除噪声blurred_image cv2.GaussianBlur(gray_image, (5, 5), 0)# 构建输出文件名如 a01.pngoutput_file fa{count:02d}{os.path.splitext(image_file)[1]}output_path os.path.join(output_folder, output_file)# 保存处理后的图片到文件cv2.imwrite(output_path, blurred_image)# 打印处理结果print(fProcessed {input_path} and saved as {output_path})# 增加计数器count 1print(Processing complete.)# 调用函数处理图片
input_folder C:\\Users\\Administrator\\Downloads\\ora
output_folder C:\\Users\\Administrator\\Downloads\\pred
preprocess_images(input_folder, output_folder)然后再申请百度api
https://console.bce.baidu.com/ai/?_1634647029968fromai1#/ai/ocr/overview/index领 66 看文档改代码 发现报错
pip install baidu-aip
pip install chardet像我一样 setting 中 自己检查 识别代码 import os
from aip import AipOcr 你的 APPID AK SK # 换自己的包子
APP_ID 1179
API_KEY lenZRk17s88888888888 # 换自己的包子
SECRET_KEY E7RjDl0L66666666………………………… # 换自己的包子client AipOcr(APP_ID, API_KEY, SECRET_KEY) 读取文件
# 假设您已经有一个OCR客户端实例这里用伪代码表示
# client YourOCRClientInitializer() # 替换为实际的OCR客户端初始化代码def get_file_content(filePath):with open(filePath, rb) as fp:return fp.read()def char_change(data):# 从字典中提取 words_result 列表words_list data.get(words_result, []) # 使用get方法以防words_result键不存在# 提取单词并组合同时处理可能的连字符words []for result in words_list:word result.get(words, ) # 使用get方法以防words键不存在# 根据需要移除连字符或其他不需要的字符word word.replace(-, ).replace( , ) # 也可以考虑移除空格words.append(word)# 使用空格或其他分隔符如果需要的话连接单词# 在这个例子中我们假设车牌号应该是连续的所以不使用分隔符combined_word .join(words)# 输出结果print(combined_word)def process_images_in_folder(folder_path, ocr_client):# 遍历文件夹中的所有文件for filename in os.listdir(folder_path):# 检查文件扩展名只处理图片文件if filename.lower().endswith((.png, .jpg, .jpeg, .bmp, .tiff)):file_path os.path.join(folder_path, filename)# 读取图片内容image_content get_file_content(file_path)# 调用OCR服务进行文字识别# 注意这里假设ocr_client有一个名为basicGeneral的方法来处理图片# 并且该方法返回一个包含words_result键的字典recognition_result ocr_client.basicGeneral(image_content)# 处理识别结果char_change(recognition_result)# 指定文件夹路径
folder_path C:\\Users\\Administrator\\Downloads\\pred
process_images_in_folder(folder_path,client)玩