{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"This notebook compares TesseractOCR and EasyOCR.\n\nThe comparison shows that EasyOCR is more accurate at reading text, but TesseractOCR is faster.","metadata":{}},{"cell_type":"code","source":"import pytesseract\nfrom PIL import Image\nfrom PIL.JpegImagePlugin import JpegImageFile\nimport easyocr","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:13:18.813107Z","iopub.execute_input":"2023-04-08T02:13:18.813542Z","iopub.status.idle":"2023-04-08T02:13:18.820849Z","shell.execute_reply.started":"2023-04-08T02:13:18.813503Z","shell.execute_reply":"2023-04-08T02:13:18.819108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def open_image(path: str) -> JpegImageFile:\n    return Image.open(path)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:18:04.821241Z","iopub.execute_input":"2023-04-08T02:18:04.821842Z","iopub.status.idle":"2023-04-08T02:18:04.828761Z","shell.execute_reply.started":"2023-04-08T02:18:04.821796Z","shell.execute_reply":"2023-04-08T02:18:04.827036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def TesseractOCR(image: JpegImageFile) -> str:\n    return pytesseract.image_to_string(image, lang=\"eng\")","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:18:05.371006Z","iopub.execute_input":"2023-04-08T02:18:05.371440Z","iopub.status.idle":"2023-04-08T02:18:05.377466Z","shell.execute_reply.started":"2023-04-08T02:18:05.371400Z","shell.execute_reply":"2023-04-08T02:18:05.376167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def EasyOCR(image: JpegImageFile) -> str:\n    reader = easyocr.Reader(['en']) # initialize OCR\n    result = reader.readtext(image) # input image\n    return \"\\n\".join([res[1] for res in result])","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:18:05.627575Z","iopub.execute_input":"2023-04-08T02:18:05.628018Z","iopub.status.idle":"2023-04-08T02:18:05.634387Z","shell.execute_reply.started":"2023-04-08T02:18:05.627977Z","shell.execute_reply":"2023-04-08T02:18:05.633063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample 1","metadata":{}},{"cell_type":"code","source":"image = open_image(\"/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg\")\nimage","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:36:30.572035Z","iopub.execute_input":"2023-04-08T02:36:30.572451Z","iopub.status.idle":"2023-04-08T02:36:30.609975Z","shell.execute_reply.started":"2023-04-08T02:36:30.572412Z","shell.execute_reply":"2023-04-08T02:36:30.608640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprint(TesseractOCR(image))","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:26:03.255142Z","iopub.execute_input":"2023-04-08T02:26:03.255577Z","iopub.status.idle":"2023-04-08T02:26:03.631179Z","shell.execute_reply.started":"2023-04-08T02:26:03.255515Z","shell.execute_reply":"2023-04-08T02:26:03.629170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprint(EasyOCR(image))","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:26:15.622398Z","iopub.execute_input":"2023-04-08T02:26:15.622872Z","iopub.status.idle":"2023-04-08T02:26:21.146020Z","shell.execute_reply.started":"2023-04-08T02:26:15.622826Z","shell.execute_reply":"2023-04-08T02:26:21.144339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample2","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:35:20.775164Z","iopub.execute_input":"2023-04-08T02:35:20.775616Z","iopub.status.idle":"2023-04-08T02:35:20.781213Z","shell.execute_reply.started":"2023-04-08T02:35:20.775573Z","shell.execute_reply":"2023-04-08T02:35:20.779765Z"}}},{"cell_type":"code","source":"image = open_image(\"/kaggle/input/benetech-making-graphs-accessible/train/images/0208cb944192.jpg\")\nimage","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:38:34.357451Z","iopub.execute_input":"2023-04-08T02:38:34.358730Z","iopub.status.idle":"2023-04-08T02:38:34.387987Z","shell.execute_reply.started":"2023-04-08T02:38:34.358671Z","shell.execute_reply":"2023-04-08T02:38:34.386887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprint(TesseractOCR(image))","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:38:50.751679Z","iopub.execute_input":"2023-04-08T02:38:50.753063Z","iopub.status.idle":"2023-04-08T02:38:51.137736Z","shell.execute_reply.started":"2023-04-08T02:38:50.752999Z","shell.execute_reply":"2023-04-08T02:38:51.136038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprint(EasyOCR(image))","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:38:58.637276Z","iopub.execute_input":"2023-04-08T02:38:58.637732Z","iopub.status.idle":"2023-04-08T02:39:03.555898Z","shell.execute_reply.started":"2023-04-08T02:38:58.637687Z","shell.execute_reply":"2023-04-08T02:39:03.554936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample 3","metadata":{}},{"cell_type":"code","source":"image = open_image(\"/kaggle/input/benetech-making-graphs-accessible/train/images/023130533cbe.jpg\")\nimage","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:44:16.128808Z","iopub.execute_input":"2023-04-08T02:44:16.129832Z","iopub.status.idle":"2023-04-08T02:44:16.177603Z","shell.execute_reply.started":"2023-04-08T02:44:16.129687Z","shell.execute_reply":"2023-04-08T02:44:16.175922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprint(TesseractOCR(image))","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:44:24.830835Z","iopub.execute_input":"2023-04-08T02:44:24.831672Z","iopub.status.idle":"2023-04-08T02:44:25.804897Z","shell.execute_reply.started":"2023-04-08T02:44:24.831613Z","shell.execute_reply":"2023-04-08T02:44:25.802690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nprint(EasyOCR(image))","metadata":{"execution":{"iopub.status.busy":"2023-04-08T02:44:28.856992Z","iopub.execute_input":"2023-04-08T02:44:28.857452Z","iopub.status.idle":"2023-04-08T02:44:33.653468Z","shell.execute_reply.started":"2023-04-08T02:44:28.857406Z","shell.execute_reply":"2023-04-08T02:44:33.652626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}