{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "f02a8ab8-2e27-493a-91d9-e0dffae99a62", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "0d209bb5-6977-4c7c-b6d9-414e5ce7e9f1", "metadata": {}, "outputs": [], "source": [ "from aip import AipOcr\n", "import glob\n", "from opencc import OpenCC\n", "import time\n", "\"\"\" 你的 APPID AK SK \"\"\"\n", "APP_ID = '17553946'\n", "API_KEY = 'IMC1ss3Tyo3vEAdVH6jgcdv2'\n", "SECRET_KEY = 'Iua6KdhuDtz159rjzGeFGpqgjhnoU0zZ'\n", "\n", "client = AipOcr(APP_ID, API_KEY, SECRET_KEY)\n", "def get_file_content(filePath):\n", " with open(filePath, 'rb') as fp:\n", " return fp.read()\n", "\n", "\n", "fi_path = './pic'\n", "fls = glob.glob(f'{fi_path}/*.jpg')\n", "fls.sort()\n", "\"\"\" 调用通用文字识别, 图片参数为本地图片 \"\"\"\n", "#client.basicGeneral(image);\n", "\n", "\"\"\" 如果有可选参数 \"\"\"\n", "options = {}\n", "options[\"language_type\"] = \"CHN_ENG\"\n", "options[\"detect_direction\"] = \"true\"\n", "options[\"detect_language\"] = \"true\"\n", "options[\"probability\"] = \"true\"\n", "#options[\"detect_direction\"] = \"true\"\n", "cc = OpenCC('t2s')\n", "for fi in fls:\n", " image = get_file_content(fi)\n", " result= client.basicAccurate(image, options)\n", " print(fi)\n", " list1 = []\n", " if 'words_result' in result:\n", " for w in result['words_result']:\n", " list1.append(cc.convert(w['words']))\n", " print(''.join(list1))\n", " time.sleep(5)" ] }, { "cell_type": "code", "execution_count": 29, "id": "75e8d016-deba-4177-9036-01bfd77002a9", "metadata": { "execution": { "iopub.execute_input": "2024-07-02T15:29:41.546649Z", "iopub.status.busy": "2024-07-02T15:29:41.545827Z", "iopub.status.idle": "2024-07-02T15:29:41.553053Z", "shell.execute_reply": "2024-07-02T15:29:41.551700Z", "shell.execute_reply.started": "2024-07-02T15:29:41.546573Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "第二图一尖白二点黑三做眼固易做括然黑一倘改尖一莅则白一位点经黑甲白乙黑丙白丁黑竟死可\n" ] } ], "source": [ "s = '第二圖一尖白二點黑三做眼固易做括然黑一倘改尖一莅則白一位點經黑甲白乙黑丙白丁黑竟死可'\n", "print(cc.convert(s))" ] }, { "cell_type": "code", "execution_count": null, "id": "d4df9553-0ded-4c40-bf89-38ee90feecbd", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }