1.准备工作
1.1 软件和开发工具版本
pycharm2019.3.5及以上版本
安装python3.6及以上版本
PaddleOCR-2.6.0
1.2安装依赖
pip install -r requirements.txt
1.3安装paddle
python -m pip install paddlepaddle==2.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
2.使用测试
准备一张图片,这张图片已经很模糊了,字体倾斜,识别准确率还是挺高的。而且识别耗时不超过1秒
img_path = "test/aaa.png"
print("开始")
start_time = datetime.datetime.now()
result = engine.ocr(img_path, det=args.det, rec=args.rec, cls=args.use_angle_cls)
if result is not None:
for line in result:
paddleocr.logger.info(line)
end_time = datetime.datetime.now()
total_time = end_time - start_time
print("程序运行总耗时:" + str(total_time))
logging.info("程序运行总耗时:" + str(total_time))
识别效果
3.开发web api服务为其他模块调用提供API接口
@app.route('/ocr/general_basic', methods=['POST'])
def api():
timeout = 5
# 来捕捉客户端传来的数据
data = request.get_data().decode('utf-8')
# json.loads将string转换为dict
data = json.loads(data)
# 获取dict中'img'标签的数据
image_b64 = data["img"]
# 进行base64解码工作 base64->数组
image_decode = base64.b64decode(image_b64)
# fromstring实现了字符串到Ascii码的转换
nparr = np.fromstring(image_decode, np.uint8)
# 从nparr中读取数据,并把数据转换(解码)成图像格式
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
now = datetime.datetime.now()
formatted_date = now.strftime("%Y-%m-%d")
timestamp_uuid = uuid.uuid1()
image_path = "{0}/{1}".format(root_image_path, formatted_date)
if(os.path.exists(image_path) == False):
os.mkdir(image_path)
file = os.path.join(image_path, str(timestamp_uuid) + ".jpg")
cv2.imwrite(file, img_np)
print("开始")
start_time = datetime.datetime.now()
result = engine.ocr(file, cls=args.use_angle_cls)
content = {}
if result is not None:
i = 0
for line in result:
content[i] = line[1][0]
i = i + 1
print(content)
end_time = datetime.datetime.now()
total_time = end_time - start_time
print("程序运行总耗时:" + str(total_time))
logger.info("耗时:{0}, 内容:{1}", str(total_time), content)
data = {
"total_time": str(total_time),
"content": content
}
response = make_response(json.dumps(data))
response.headers["Content-Type"] = "application/json;charset=UTF-8"
return response
if __name__ == '__main__':
server = make_server('0.0.0.0', 5000, app)
print('Serving HTTP on port 5000...')
server.serve_forever()
4.其他模块调用(Java)
try {
//转换为base64
byte[] bytes = IOUtils.toByteArray(stream);
String encoded = Base64.getEncoder().encodeToString(bytes);
Map<String, String> req = new HashMap<>();
req.put("img", encoded);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Map> responseEntity = restTemplate.postForEntity(paddleUrl, req, Map.class);
HashMap<String, String> content = (HashMap) responseEntity.getBody().get("content");
int i = 0;
for (Map.Entry<String, String> entry : content.entrySet()) {
parseContent += entry.getValue();
if (i < content.size() - 1) {
parseContent += "\n";
}
i++;
}
} catch (IOException ex) {
log.error("异常" + ex.toString());
ex.printStackTrace();
}