{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":43873,"databundleVersionId":5585780,"sourceType":"competition"},{"sourceId":5214849,"sourceType":"datasetVersion","datasetId":3033230},{"sourceId":5663450,"sourceType":"datasetVersion","datasetId":3253605}],"dockerImageVersionId":30476,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!git lfs install\n!git clone https://huggingface.co/pszemraj/neuspell-subwordbert-probwordnoise\n!pip install -U neuspell -q","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:30:30.116425Z","iopub.execute_input":"2024-03-06T17:30:30.116812Z","iopub.status.idle":"2024-03-06T17:31:17.253651Z","shell.execute_reply.started":"2024-03-06T17:30:30.11678Z","shell.execute_reply":"2024-03-06T17:31:17.252409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport cv2\nimport json\nimport easyocr\nimport random\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom neuspell import BertChecker","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:45:30.053797Z","iopub.execute_input":"2024-03-06T17:45:30.054189Z","iopub.status.idle":"2024-03-06T17:45:30.059433Z","shell.execute_reply.started":"2024-03-06T17:45:30.054155Z","shell.execute_reply":"2024-03-06T17:45:30.058508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /root/.EasyOCR/user_network","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:31:50.317083Z","iopub.execute_input":"2024-03-06T17:31:50.317667Z","iopub.status.idle":"2024-03-06T17:31:51.304981Z","shell.execute_reply.started":"2024-03-06T17:31:50.317638Z","shell.execute_reply":"2024-03-06T17:31:51.303742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/feasyocr-fintuned-model/best_accuracy.yaml /root/.EasyOCR/user_network/best_accuracy.yaml\n!cp /kaggle/input/feasyocr-fintuned-model/best_accuracy.py /root/.EasyOCR/user_network/best_accuracy.py\n!cp /kaggle/input/feasyocr-fintuned-model/best_accuracy.pth /root/.EasyOCR/model/best_accuracy.pth","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:31:51.30656Z","iopub.execute_input":"2024-03-06T17:31:51.306908Z","iopub.status.idle":"2024-03-06T17:31:54.500154Z","shell.execute_reply.started":"2024-03-06T17:31:51.306863Z","shell.execute_reply":"2024-03-06T17:31:54.498888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"public_model_reader = easyocr.Reader(['en'],gpu=True)\nfinetuned_model_reader = easyocr.Reader(['en'], recog_network='best_accuracy',gpu=True)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:31:54.502653Z","iopub.execute_input":"2024-03-06T17:31:54.502967Z","iopub.status.idle":"2024-03-06T17:32:01.554053Z","shell.execute_reply.started":"2024-03-06T17:31:54.502936Z","shell.execute_reply":"2024-03-06T17:32:01.553258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nBASE_DIR = '/kaggle/input/benetech-making-graphs-accessible'\nfiles = os.listdir(f\"{BASE_DIR}/train/images\")\nf = open(f\"{BASE_DIR}/train/annotations/0000ae6cbdb1.json\")\nannotated_data = json.load(f)\n\ndef hwlt2ltrb(coor):\n    left = coor[2]\n    top = coor[3]\n    right = left + coor[1]\n    bottom = top + coor[0]\n    \n    return (left,top,right,bottom)\n\n\ndef get_ltrb(source,idx):\n    l,t,r,b = float(\"inf\"),float(\"inf\"),float(\"-inf\"),float(\"-inf\")\n    data = source[idx][\"polygon\"]\n    x_values = (data['x0'],data['x1'],data['x2'],data['x3'])\n    y_values = (data['y0'],data['y1'],data['y2'],data['y3'])\n    l = min(l,min(x_values))\n    t = min(t,min(y_values))\n    r = max(r,max(x_values))\n    b = max(b,max(y_values))\n    text = source[idx]['text']\n    return (l,t,r,b),text\n\n\npublic_model_correct_count = 0\nfinetuned_model_correct_count = 0\n\nfor i,file in enumerate(files):\n    if i > 50000:\n        img = cv2.imread(f\"{BASE_DIR}/train/images/{file}\")\n        image = img.copy()\n        h,w,_ = img.shape\n        f = open(f\"{BASE_DIR}/train/annotations/{file.replace('jpg','json')}\")\n        annotated_data = json.load(f)\n\n        indicies = list(map(lambda x: x[\"id\"], annotated_data[\"axes\"][\"x-axis\"][\"ticks\"]))\n\n        indicies.extend(list(map(lambda x: x[\"id\"], annotated_data[\"axes\"][\"y-axis\"][\"ticks\"])))\n\n        for idx in indicies:\n            try:\n                coor,text = get_ltrb(annotated_data['text'],idx)\n                l,t,r,b = coor\n                canvas = cv2.rectangle(image,(l,t),(r,b),(0,255,0),1)\n                text_crop = img[t:b,l:r]\n                img = text_crop\n                try:\n                    public_model_res = public_model_reader.readtext(img)\n                    if public_model_res[0][1] == text:\n                        public_model_correct_count += 1\n                except:\n                    pass\n\n                try:\n                    finetuned_model_res = finetuned_model_reader.readtext(img)\n\n                    if finetuned_model_res[0][1] == text:\n                        finetuned_model_correct_count += 1\n                except:\n                    pass\n                    \n            except:\n                pass\n            \n        if i % 1000 == 0:\n            print(f\"Exsist Model Per: {public_model_correct_count}\")\n            print(f\"Finetunned Model Per: {finetuned_model_correct_count}\")\n            print(\"#\" * 100)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:32:01.555171Z","iopub.execute_input":"2024-03-06T17:32:01.555456Z","iopub.status.idle":"2024-03-06T17:39:37.289888Z","shell.execute_reply.started":"2024-03-06T17:32:01.555431Z","shell.execute_reply":"2024-03-06T17:39:37.288931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def text_recognition(image_path):\n    finetuned_model_res = easyocr.Reader(['en'])\n    text = finetuned_model_res.readtext(image_path)\n    text = ' '.join([word[1] for word in text])\n    return text","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:50:25.313493Z","iopub.execute_input":"2024-03-06T17:50:25.314351Z","iopub.status.idle":"2024-03-06T17:50:25.319082Z","shell.execute_reply.started":"2024-03-06T17:50:25.314314Z","shell.execute_reply":"2024-03-06T17:50:25.318191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checker = BertChecker()\nchecker.from_pretrained(\"./neuspell-subwordbert-probwordnoise/\")","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:52:46.085994Z","iopub.execute_input":"2024-03-06T17:52:46.086361Z","iopub.status.idle":"2024-03-06T17:52:49.691029Z","shell.execute_reply.started":"2024-03-06T17:52:46.086331Z","shell.execute_reply":"2024-03-06T17:52:49.690169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def spell_correction(text):\n    words = text.split()\n    corrected_words = checker.correct_strings(words)\n    corrected_text = ' '.join(corrected_words)\n    return corrected_text","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:52:51.009779Z","iopub.execute_input":"2024-03-06T17:52:51.010636Z","iopub.status.idle":"2024-03-06T17:52:51.015305Z","shell.execute_reply.started":"2024-03-06T17:52:51.0106Z","shell.execute_reply":"2024-03-06T17:52:51.014415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_train_path = '/kaggle/input/benetech-making-graphs-accessible/train/images'\nimages_paths = [images_train_path + '/' + f for f in listdir(images_train_path) if isfile(join(images_train_path, f))]","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:41:26.438334Z","iopub.execute_input":"2024-03-06T17:41:26.438945Z","iopub.status.idle":"2024-03-06T17:43:35.979256Z","shell.execute_reply.started":"2024-03-06T17:41:26.438912Z","shell.execute_reply":"2024-03-06T17:43:35.978399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_images_paths = random.sample(images_paths, 10)\nrandom_images_paths","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:45:06.608618Z","iopub.execute_input":"2024-03-06T17:45:06.609298Z","iopub.status.idle":"2024-03-06T17:45:06.615435Z","shell.execute_reply.started":"2024-03-06T17:45:06.609262Z","shell.execute_reply":"2024-03-06T17:45:06.614527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, image_path in enumerate(random_images_paths):\n    image = Image.open(image_path)\n    before_spell_check = text_recognition(image_path)\n    after_spell_check = spell_correction(before_spell_check)\n\n    plt.imshow(image)\n    plt.title(f'Image {i + 1}')\n    plt.show()\n    \n    # Вывод результатов\n    print(f\"Before spell check: {before_spell_check}\")\n    print(f\"After spell check: {after_spell_check}\")\n    print(\"-\" * 100)","metadata":{"execution":{"iopub.status.busy":"2024-03-06T17:55:17.774005Z","iopub.execute_input":"2024-03-06T17:55:17.77473Z","iopub.status.idle":"2024-03-06T17:55:58.496871Z","shell.execute_reply.started":"2024-03-06T17:55:17.774684Z","shell.execute_reply":"2024-03-06T17:55:58.495815Z"},"trusted":true},"execution_count":null,"outputs":[]}]}