02、利用paddleocr识别监控图像的时间
PaddleOCR
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
项目地址:https://gitcode.com/gh_mirrors/pa/PaddleOCR
免费下载资源
·
1 需求
根据摄像头抓拍图像中的时间,判断设备是否出现时差
2 安装paddle依赖
这里使用paddleocr进行识别,首先使用pip安装paddleocr依赖库
python -m pip install paddlepaddle paddleocr -i https://mirror.baidu.com/pypi/simple
3 写个测试类进行验证
from paddleocr import PaddleOCR
from PIL import Image
def get_result_list(img_path):
# 模型路径下必须含有model和params文件,如果没有,现在可以自动下载了,不过是最简单的模型
# use_gpu 如果paddle是GPU版本请设置为 True
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
result_list = ocr.ocr(img_path, cls=True)
return result_list
if __name__ == '__main__':
img_path = r'./2.jpg' # 这个是自己的图片,自行放置在代码目录下修改名称
result_list = get_result_list(img_path)
print(result_list)
# save_img(img_path, result_list)
4 根据上面的返回值结果,进行接口开发
4.1 引入Flask,实现web接口
from logging.handlers import RotatingFileHandler
from flask import Flask, request, jsonify,make_response
from gevent import pywsgi
app = Flask(__name__)
# 全局变量
PROGRAM_NAME = 'ocr'
PROGRAM_VERSION = '1.0.0'
LOG_LEVEL = logging.INFO
@app.route('/ocr/hello',methods=['GET'])
def test():
#获取参数name
name = request.args.get('name')
print(name)
#给用户响应,hello
return 'hello ' + name
if __name__ == '__main__':
here = os.path.dirname(os.path.abspath(__file__))
os.chdir(here)
if not os.path.isdir('logs'):
os.mkdir('logs')
# 日志初始化
logfile = os.path.join(here, 'logs', '%s.log' % PROGRAM_NAME)
log_handler = RotatingFileHandler(logfile, maxBytes=1*1024*1024*1024, backupCount=2)
log_formatter = logging.Formatter('%(asctime)s - %(name)s [%(levelname)s] %(message)s')
log_handler.setFormatter(log_formatter)
log = logging.getLogger()
log.setLevel(LOG_LEVEL)
log.addHandler(log_handler)
#--------------同步输出到控制台---------
console_handler = logging.StreamHandler()
console_fmt = "%(name)s-%(levelname)s-%(asctime)s- %(message)s"
fmt1 = logging.Formatter(fmt=console_fmt)
console_handler.setFormatter(fmt1)
log.addHandler(console_handler)
#--------------同步输出到控制台---------
server = pywsgi.WSGIServer(('0.0.0.0', 8985), app)
log.info(f"服务启动成功: {PROGRAM_NAME}")
server.serve_forever()
启动脚本,访问 8985:/ocr/hello 测试
4.2 引入ocr,进行图片识别
from paddleocr import PaddleOCR
from PIL import Image
def get_result_list(img):
# 模型路径下必须含有model和params文件,如果没有,现在可以自动下载了,不过是最简单的模型
# use_gpu 如果paddle是GPU版本请设置为 True
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False)
cropped_img_array = np.array(img)
result_list = ocr.ocr(cropped_img_array, cls=True)
return result_list
# 根据ocr获取图片中的文字
@app.route('/ocr/getTime',methods=['POST'])
def GetTimeByOcr():
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log.info("接收到请求时间是: " + str(current_time))
# 检查请求是否包含文件
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
# 检查文件是否为空
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# 检查文件类型是否是图像
if not file.filename.endswith(('.jpg', '.jpeg', '.png', '.gif')):
return jsonify({'error': 'Unsupported file format'}), 400
try:
# 使用Pillow库打开图像
img = Image.open(file)
# 这里可以添加处理图像的代码,比如保存、修改等
# ...
result_list = get_result_list(img)
timestamps = convert_to_timestamp(result_list)
return jsonify({'message': 'Image uploaded and processed successfully','result':timestamps}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
启动脚本,访问接口测试:
5 编辑Dockerfile,打包成镜像,上线部署
完整代码包下载:https://download.csdn.net/download/qq_16089135/88418315
GitHub 加速计划 / pa / PaddleOCR
41.52 K
7.59 K
下载
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
最近提交(Master分支:3 个月前 )
d3d7e858
7 天前
d1bc4166
8 天前
更多推荐
已为社区贡献1条内容
所有评论(0)