{"id":198,"date":"2024-08-09T16:49:22","date_gmt":"2024-08-09T08:49:22","guid":{"rendered":"http:\/\/znl.pub\/?p=198"},"modified":"2024-08-22T19:56:05","modified_gmt":"2024-08-22T11:56:05","slug":"onnxocr-%e5%a5%bd%e7%94%a8%e7%9a%84ocr%e8%af%86%e5%88%ab%e5%b7%a5%e5%85%b7%e3%80%90-paddleocr-%e8%bd%ac-onnx%e3%80%91","status":"publish","type":"post","link":"https:\/\/znl.pub\/?p=198","title":{"rendered":"onnxOCR-\u597d\u7528\u7684ocr\u8bc6\u522b\u5de5\u5177\u3010 paddleOCR \u8f6c ONNX\u3011"},"content":{"rendered":"<h1>\u5206\u4eab\u9879\u76ee paddleOCR \u4e4b onnxOCR<\/h1>\n<h4>\u7248\u672c\u66f4\u65b0<\/h4>\n<p>PPOCRv4\u6a21\u578b\u8f6connx\u6a21\u578b\u63a8\u7406\uff0c\u7cbe\u5ea6\u548c\u6027\u80fd\u66f4\u9ad8\uff0c\u63a8\u7406\u901f\u5ea6\u6bd4\u4f7f\u7528paddlepaddle\u6846\u67b6\u5feb5\u500d<\/p>\n<h4>\u4e00.\u4f18\u52bf\uff1a<\/h4>\n<p>1.\u8131\u79bb\u6df1\u5ea6\u5b66\u4e60\u8bad\u7ec3\u6846\u67b6\uff0c\u53ef\u76f4\u63a5\u7528\u4e8e\u90e8\u7f72\u7684\u901a\u7528OCR\u3002<br \/>\n2.\u5728\u7b97\u529b\u6709\u9650\uff0c\u7cbe\u5ea6\u4e0d\u53d8\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528paddleOCR\u8f6c\u6210ONNX\u6a21\u578b\uff0c\u8fdb\u884c\u91cd\u65b0\u6784\u5efa\u7684\u4e00\u6b3e\u53ef\u90e8\u7f72\u5728arm\u67b6\u6784\u548cx86\u67b6\u6784\u8ba1\u7b97\u673a\u4e0a\u7684OCR\u6a21\u578b\u3002<br \/>\n3.\u5728\u540c\u6837\u6027\u80fd\u7684\u8ba1\u7b97\u673a\u4e0a\u63a8\u7406\u901f\u5ea6\u52a0\u901f\u4e864-5\u500d\u3002<\/p>\n<h4>\u4e8c.\u73af\u5883\u5b89\u88c5<\/h4>\n<pre><code>python>=3.6\n\npip install -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple -r requirements.txt<\/code><\/pre>\n<h4>\u4e09.\u4e00\u952e\u8fd0\u884c<\/h4>\n<pre><code>python test_ocr.py<\/code><\/pre>\n<h4>\u56db\u3001 \u9644\u4e0a\u6211\u6539\u5199\u7684\u6d4b\u8bd5\u4ee3\u7801 api server \uff082024.08.09\uff09<\/h4>\n<p>\u4e0b\u9762\u4ee3\u7801\u6267\u884c\u524d\uff0c\u9700\u8981\u989d\u5916\u5b89\u88c5 pyzbar\u3002<br \/>\n<code>pip3 install pyzbar -i https:\/\/pypi.mirrors.ustc.edu.cn\/simple\/ <\/code><br \/>\n\u6211\u628a\u5199\u4e86\u4e00\u4e2ahttp api\uff0c\u540c\u65f6ocr\u548cqrcode\u8bc6\u522b\u63a2\u6d4b\u3002\u5176\u4e2dqrcode\u63a2\u6d4b\u548c\u8bc6\u522b\u6211\u91c7\u7528\u4e862\u79cd\u65b9\u6cd5\u3002\u4e00\u662fopencv\u81ea\u5e26\u7684QRCodeDetector(),\u53e6\u4e00\u4e2a\u662fpyzbar\u3002\u5b9e\u6d4b\u7ed3\u679c\u662f\u81ea\u5e26\u7684\u8fd9\u4e2aQRCodeDetector\u5bf9\u4e00\u9875\u5f53\u4e2d\u591a\u4e2a\u4e8c\u7ef4\u7801\u7684\u60c5\u51b5\u5b58\u4e0d\u80fd\u5168\u90e8\u8bc6\u522b\uff0c\u800cpyzbar\u5c31\u884c\u3002\u540c\u65f6\u81ea\u5e26\u7684\u8fd8\u4e0d\u80fd\u8bc6\u522b\u4e00\u4e9b\u53d8\u4f53\u4e8c\u7ef4\u7801\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u7684\u5c0f\u6050\u9f99\u4e8c\u7ef4\u7801\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/znl.pub\/wp-content\/uploads\/2024\/08\/image-1723192928243.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-python3\">import cv2\nimport time\nfrom onnxocr.onnx_paddleocr import ONNXPaddleOcr\nimport sys\nfrom flask import Flask,request, Response, jsonify\nimport json\nimport os\nfrom pyzbar.pyzbar import decode\n\nmodel = ONNXPaddleOcr(use_angle_cls=True, use_gpu=False)\n\napp= Flask(__name__)\n\ndef decode_qr_code(img):\n    decoded_objects=decode(img)\n    results=[]\n    for obj in decoded_objects:\n        result_dict={\n                \"type\":obj.type,\n                \"data\": obj.data.decode(\"utf-8\")\n        }\n        results.append(result_dict)\n    return results\n\ndef qrcodeDetect(img):\n    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n    qrcoder = cv2.QRCodeDetector()\n    codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(img)\n    # \u521b\u5efa\u7ed3\u679c\u5bf9\u8c61\n    result = {\n        'has_qrcode': int(bool(codeinfo)),\n        'result': codeinfo\n    }\n    return result\n\n@app.route('\/api\/ocr',methods=['GET'])\ndef ocr():\n    # \u8fd9\u91cc\u5b9e\u9645\u4e0a\u751f\u4ea7\u73af\u5883\u8981\u505a\u8def\u5f84\u9650\u5236\u548c\u68c0\u6d4b\uff0c\u751a\u81f3\u662f\u6539\u7528\u4e0a\u4f20\u56fe\u7247\u7684\u65b9\u5f0f\n    file_path=request.args.get('path')\n    if file_path and os.path.exists(file_path):\n        try:\n            s = time.time()\n            img = cv2.imread(file_path)\n            result = model.ocr(img)\n            results=[ item[1][0] for item in result[0] ]\n            result_str = ' '.join(results)\n            e = time.time()\n            has_qrcode=qrcodeDetect(img)\n            decode_qr=decode_qr_code(img)\n            d=e-s json_content=json.dumps({\"time\":d,\"result\":result_str,\"has_qrcode\":has_qrcode,\"decode_qr\":decode_qr},ensure_ascii=False)\n            return Response(json_content,mimetype='application\/json'),200\n        except Exception as e:\n            return jsonify({'error':f'Error readig file:{str(e)}'}),500\n    else:\n        return jsonify({'error': 'Invalid or missing file path'}), 400\n\nif __name__ == '__main__':\n    app.run(debug=True)<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4fdd\u5b58\u4e3a test_ocr_server.py<br \/>\n<code>python3 test_ocr_server.py<\/code><br \/>\n<img decoding=\"async\" src=\"http:\/\/znl.pub\/wp-content\/uploads\/2024\/08\/image-1723193126600.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-bash\">curl -s  http:\/\/127.0.0.1:5000\/api\/ocr?path=.\/test\/13.png<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/znl.pub\/wp-content\/uploads\/2024\/08\/image-1723193179266.png\" alt=\"file\" \/><br \/>\n13.png :<br \/>\n<img decoding=\"async\" src=\"http:\/\/znl.pub\/wp-content\/uploads\/2024\/08\/image-1723193238631.png\" alt=\"file\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u9879\u76ee paddleOCR \u4e4b onnxOCR \u7248\u672c\u66f4\u65b0 PPOCRv4\u6a21\u578b\u8f6connx\u6a21\u578b\u63a8\u7406\uff0c\u7cbe\u5ea6\u548c\u6027\u80fd\u66f4&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":3,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions\/205"}],"wp:attachment":[{"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}