1.安装python 版本最好大于3.6
官网:https://www.python.org/downloads/windows/

随意什么版本,大于3.6就行
一般win10都会默认安装python 可以通过python -V 查看
如果没有 就去官网下载即可
运行下载好的安装程序(.exe文件)。
在安装向导的第一个界面,勾选“Add Python to PATH”选项,这一步很重要,这样可以在命令行中直接使用Python命令。如果忘记勾选,后续可能需要手动配置环境变量
2.安装EasyOCR https://www.python.org/downloads/
pip install easyocr
3.python脚本
import easyocr
import sys
sys.stdout.reconfigure(encoding='utf-8')
# 创建阅读器,指定语言,这里以英文和中文为例
reader = easyocr.Reader(['en', 'ch_sim'])
image_path = sys.argv[1]
try:
# 进行OCR识别
result = reader.readtext(image_path, detail=0)
text = ' '.join(result)
print(text)
except Exception as e:
print(f"Error: {e}")
4.php执行脚本
//输出乱码问题处理
header('Content-Type: text/html; charset=utf-8');
//本地图片路径
$imagePath = 'D:\phpstudy_pro\WWW\tp6\public\image\625638924bfd1.jpeg';
//python安装目录
$pythonScript = 'C:\Users\Administrator\AppData\Local\Programs\Python\Python313\python.exe';
//脚本位置
$scriptPath = 'D:\phpstudy_pro\WWW\tp6\ocr.py';
if (!file_exists($imagePath)) {
echo "图片文件不存在: $imagePath";
return;
}
if (!file_exists($scriptPath)) {
echo "Python脚本文件不存在: $scriptPath";
return;
}
$command = escapeshellcmd("$pythonScript \"$scriptPath\" \"$imagePath\"");
exec($command, $output, $returnCode);
if ($returnCode === 0) {
$text = implode(' ', $output);
echo "识别结果: $text";
} else {
echo "执行Python脚本时出错,返回码: $returnCode";
}