安装pytesseract
pip install pytesseract
pip install Flask
安装语音包
sudo apt install tesseract-ocr-chi-sim
sudo apt install tesseract-ocr-eng
创建一个python文件ocr_server.py
from flask import Flask, request, jsonify
import pytesseract
from PIL import Image
import io
app = Flask(__name__)
# 设置 Tesseract 的路径
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
@app.route('/ocr', methods=['POST'])
def ocr():
try:
# 获取上传的图片文件
file = request.files['image']
if not file:
return jsonify({'error': 'No image provided'}), 400
# 读取图片文件
image = Image.open(io.BytesIO(file.read()))
# 进行 OCR 识别
text = pytesseract.image_to_string(image, lang='chi_sim+eng') # 同时使用简体中文和英文语言包
# 返回识别结果
return jsonify({'text': text})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5003, debug=True)
运行服务
nohup python3 ocr_server.py &