{"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":"# Extracting Text from Images using OpenCV and Pytesseract in Python\n\n\n********************","metadata":{}},{"cell_type":"markdown","source":"# Importing Libraries and Loading Data\n\n\n","metadata":{}},{"cell_type":"code","source":"#First, we'll import the necessary libraries and load the training data:\nimport cv2\nimport pytesseract\nimport pandas as pd\nimport PIL.Image as image\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom PIL import Image\n","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:23.238042Z","iopub.execute_input":"2023-04-08T23:24:23.238527Z","iopub.status.idle":"2023-04-08T23:24:23.247669Z","shell.execute_reply.started":"2023-04-08T23:24:23.238489Z","shell.execute_reply":"2023-04-08T23:24:23.246309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This code provides a simple example of how to use the OpenCV and Pytesseract libraries in Python to extract text from images. The image is read using the PIL library and displayed using Matplotlib. Then, the OpenCV library is used to process and enhance the image before passing it to Pytesseract for extracting the text present in the image. This technique can be used in various applications such as optical character recognition (OCR), image-to-text conversion, data extraction from scanned documents, and more.**","metadata":{}},{"cell_type":"markdown","source":"# Configuring Data for Vesuvius Challenge Ink Detection","metadata":{}},{"cell_type":"code","source":"# Read CSV file and create DataFrame\ndf = pd.read_csv('/kaggle/input/vesuvius-challenge-ink-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:23.259228Z","iopub.execute_input":"2023-04-08T23:24:23.260504Z","iopub.status.idle":"2023-04-08T23:24:23.271852Z","shell.execute_reply.started":"2023-04-08T23:24:23.260423Z","shell.execute_reply":"2023-04-08T23:24:23.270597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:23.285963Z","iopub.execute_input":"2023-04-08T23:24:23.287128Z","iopub.status.idle":"2023-04-08T23:24:23.296373Z","shell.execute_reply.started":"2023-04-08T23:24:23.287078Z","shell.execute_reply":"2023-04-08T23:24:23.294758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Configure data\ndf = '/kaggle/input/vesuvius-challenge-ink-detection/'\n","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:23.302006Z","iopub.execute_input":"2023-04-08T23:24:23.302428Z","iopub.status.idle":"2023-04-08T23:24:23.307791Z","shell.execute_reply.started":"2023-04-08T23:24:23.302392Z","shell.execute_reply":"2023-04-08T23:24:23.306381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**  **This code snippet appears to configure data for the Vesuvius Challenge ink detection, using a file path located in the Kaggle input directory. It is likely that this data is being prepared for further processing or analysis in the Vesuvius Challenge, which may involve detecting ink in images or performing other related tasks. Properly configuring and preparing data is an essential step in any machine learning or data analysis task, as it ensures that the data is organized and formatted correctly for subsequent processing.****","metadata":{}},{"cell_type":"markdown","source":"# Displaying an Infrared Image from the Train Dataset using Matplotlib","metadata":{}},{"cell_type":"code","source":"plt.imshow(image.open(df + \"/train/1/ir.png\"), cmap=\"gray\")\n","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:23.322938Z","iopub.execute_input":"2023-04-08T23:24:23.323691Z","iopub.status.idle":"2023-04-08T23:24:27.069106Z","shell.execute_reply.started":"2023-04-08T23:24:23.323649Z","shell.execute_reply":"2023-04-08T23:24:27.067596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This code snippet uses Matplotlib to display an infrared image from the train dataset. The image.open() function is used to open the image file, which is located in the directory specified by the df variable, concatenated with \"/train/1/ir.png\". The cmap=\"gray\" argument specifies that the image should be displayed in grayscale colormap. Visualization of the image can be helpful for inspecting the content and quality of the infrared image, which may be useful in the context of the Vesuvius Challenge ink detection task.******","metadata":{}},{"cell_type":"markdown","source":"# \"Extracting Text from Infrared Image using OpenCV and Pytesseract in Python\"","metadata":{}},{"cell_type":"code","source":"# Move to appointments\nimg = cv2.imread('/kaggle/input/vesuvius-challenge-ink-detection/train/1/ir.png')\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n# Clean up the image\ngray = cv2.medianBlur(gray, 3)\ngray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]\n# Extract text\ntext = pytesseract.image_to_string(Image.fromarray(gray), lang='eng', config='--psm 6')\nprint(text)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:27.071652Z","iopub.execute_input":"2023-04-08T23:24:27.072004Z","iopub.status.idle":"2023-04-08T23:24:36.255709Z","shell.execute_reply.started":"2023-04-08T23:24:27.071969Z","shell.execute_reply":"2023-04-08T23:24:36.254040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This code snippet demonstrates the process of extracting text from an infrared image using OpenCV and Pytesseract libraries in Python. The image is loaded using the cv2.imread() function from the specified file path. Then, the image is converted to grayscale using cv2.cvtColor() function. Next, some image preprocessing is applied, including median blur using cv2.medianBlur() function and thresholding using cv2.threshold() function to create a binary image.\n\nFinally, the pytesseract.image_to_string() function is used to extract text from the preprocessed image. The extracted text is stored in the text variable and printed using the print() function. The lang parameter specifies the language of the text to be extracted (in this case, 'eng' for English), and the config parameter allows specifying additional configurations for the Pytesseract OCR engine (in this case, '--psm 6' which sets the page segmentation mode to block of vertically aligned text). This code can be useful for applications that require text extraction from infrared images, such as document scanning, image-based data extraction, or text recognition tasks.**","metadata":{}},{"cell_type":"markdown","source":"# Displaying an Infrared Image from the Train Dataset using Matplotlib","metadata":{}},{"cell_type":"code","source":"plt.imshow(image.open(df + \"/train/2/ir.png\"), cmap=\"gray\")\n","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:36.257488Z","iopub.execute_input":"2023-04-08T23:24:36.257887Z","iopub.status.idle":"2023-04-08T23:24:44.843372Z","shell.execute_reply.started":"2023-04-08T23:24:36.257845Z","shell.execute_reply":"2023-04-08T23:24:44.842020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This code snippet uses Matplotlib to display an infrared image from the train dataset. The image.open() function is used to open the image file, which is located in the directory specified by the df variable, concatenated with \"/train/2/ir.png\". The cmap=\"gray\" argument specifies that the image should be displayed in grayscale colormap. Visualization of the image can be helpful for inspecting the content and quality of the infrared image, which may be useful in the context of the Vesuvius Challenge ink detection task or any other relevant application.**","metadata":{}},{"cell_type":"markdown","source":"#  \"Extracting Text from Infrared Image using OpenCV and Pytesseract in Python\"","metadata":{}},{"cell_type":"code","source":"# Move to appointments\nimg = cv2.imread('/kaggle/input/vesuvius-challenge-ink-detection/train/2/ir.png')\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n# Clean up the image\ngray = cv2.medianBlur(gray, 3)\ngray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]\n# Extract text\ntext = pytesseract.image_to_string(Image.fromarray(gray), lang='eng', config='--psm 6')\nprint(text)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:24:44.846589Z","iopub.execute_input":"2023-04-08T23:24:44.846977Z","iopub.status.idle":"2023-04-08T23:26:32.335740Z","shell.execute_reply.started":"2023-04-08T23:24:44.846940Z","shell.execute_reply":"2023-04-08T23:26:32.333922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This code snippet demonstrates the process of extracting text from an infrared image using OpenCV and Pytesseract libraries in Python. The image is loaded using the cv2.imread() function from the specified file path. Then, the image is converted to grayscale using cv2.cvtColor() function. Next, some image preprocessing is applied, including median blur using cv2.medianBlur() function and thresholding using cv2.threshold() function to create a binary image.\n\nFinally, the pytesseract.image_to_string() function is used to extract text from the preprocessed image. The extracted text is stored in the text variable and printed using the print() function. The lang parameter specifies the language of the text to be extracted (in this case, 'eng' for English), and the config parameter allows specifying additional configurations for the Pytesseract OCR engine (in this case, '--psm 6' which sets the page segmentation mode to block of vertically aligned text). This code can be useful for applications that require text extraction from infrared images, such as document scanning, image-based data extraction, or text recognition tasks.******","metadata":{}},{"cell_type":"markdown","source":"# \"Displaying an Infrared Image from the Train Dataset using Matplotlib\"","metadata":{}},{"cell_type":"code","source":"plt.imshow(image.open(df + \"/train/3/ir.png\"), cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:26:32.337786Z","iopub.execute_input":"2023-04-08T23:26:32.338345Z","iopub.status.idle":"2023-04-08T23:26:35.478132Z","shell.execute_reply.started":"2023-04-08T23:26:32.338298Z","shell.execute_reply":"2023-04-08T23:26:35.477150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This code snippet uses Matplotlib to display an infrared image from the train dataset. The image.open() function is used to open the image file, which is located in the directory specified by the df variable, concatenated with \"/train/3/ir.png\". The cmap=\"gray\" argument specifies that the image should be displayed in grayscale colormap. Visualization of the image can be helpful for inspecting the content and quality of the infrared image, which may be useful in the context of the Vesuvius Challenge ink detection task or any other relevant application.**","metadata":{}},{"cell_type":"markdown","source":"# \"Extracting Text from Infrared Image using OpenCV and Pytesseract in Python\"","metadata":{}},{"cell_type":"code","source":"# Move to appointments\nimg = cv2.imread('/kaggle/input/vesuvius-challenge-ink-detection/train/3/ir.png')\ngray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n# Clean up the image\ngray = cv2.medianBlur(gray, 3)\ngray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]\n# Extract text\ntext = pytesseract.image_to_string(Image.fromarray(gray), lang='eng', config='--psm 6')\nprint(text)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:26:35.479654Z","iopub.execute_input":"2023-04-08T23:26:35.480565Z","iopub.status.idle":"2023-04-08T23:26:41.476131Z","shell.execute_reply.started":"2023-04-08T23:26:35.480526Z","shell.execute_reply":"2023-04-08T23:26:41.473662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**This code snippet demonstrates the process of extracting text from an infrared image using OpenCV and Pytesseract libraries in Python. The image is loaded using the cv2.imread() function from the specified file path. Then, the image is converted to grayscale using cv2.cvtColor() function. Next, some image preprocessing is applied, including median blur using cv2.medianBlur() function and thresholding using cv2.threshold() function to create a binary image.\n\nFinally, the pytesseract.image_to_string() function is used to extract text from the preprocessed image. The extracted text is stored in the text variable and printed using the print() function. The lang parameter specifies the language of the text to be extracted (in this case, 'eng' for English), and the config parameter allows specifying additional configurations for the Pytesseract OCR engine (in this case, '--psm 6' which sets the page segmentation mode to block of vertically aligned text). This code can be useful for applications that require text extraction from infrared images, such as document scanning, image-based data extraction, or text recognition tasks.**","metadata":{}},{"cell_type":"markdown","source":"# Summary Report on the Previous Analysis","metadata":{}},{"cell_type":"markdown","source":"**Python programming language and specialized libraries such as OpenCV and Pytesseract were used to perform analysis on infrared X-ray images as part of the Vesuvius Challenge for detecting hidden ink on paper.\n\nThe \"cv2.imread()\" function was used to open the image from the specified path and convert it to grayscale using \"cv2.cvtColor()\". Image cleaning was performed using the Median Blur operation with \"cv2.medianBlur()\" and applying the Binary Thresholding operation using \"cv2.threshold()\" to create a binary image.\n\nThen, the \"pytesseract.image_to_string()\" function was used to extract text from the preprocessed image. The extracted text was stored in the \"text\" variable and printed using the \"print()\" function. The \"lang\" parameter was used to specify the language of the extracted text (in this case, English), and the \"config\" parameter was used to specify additional configurations for the Pytesseract optical character recognition engine (in this case, \"--psm 6\" which sets the page segmentation mode to treat the image as a single block of vertically aligned text).\n\nThis analysis can be used in applications that require text extraction from infrared X-ray images, such as document scanning, data extraction from images, or text recognition tasks.**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\n# Provide the correct file path to 'input.csv'\nfile_path = '/kaggle/input/vesuvius-challenge-ink-detection/sample_submission.csv'\n\n# Read data from the CSV file and create a DataFrame\ndf = pd.read_csv(file_path)\n\n# Perform operations on the DataFrame, if needed\n\n# Write the DataFrame to a new CSV file\ndf.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-08T23:26:41.478410Z","iopub.execute_input":"2023-04-08T23:26:41.479604Z","iopub.status.idle":"2023-04-08T23:26:41.496796Z","shell.execute_reply.started":"2023-04-08T23:26:41.479535Z","shell.execute_reply":"2023-04-08T23:26:41.494904Z"},"trusted":true},"execution_count":null,"outputs":[]}]}