Delete OCR word from Image (OpenCV,Python)

2024/10/2 18:31:33

So, from what I can begin..

I am working with OCR. The script works pretty well for what I need. It detects the words with an accuracy which for me is ok.

This is the result: 100% accuracy with attached image.

syd

from PIL import Image
import pyocr.builders
import osos.putenv("TESSDATA_PREFIX", "C:\\Program Files (x86)\\Tesseract-OCR")tools = pyocr.get_available_tools()
tool = tools[0]
langs = tool.get_available_languages()
lang = langs[0] #engfile = "test.png"txt = tool.image_to_string(Image.open(file), lang=lang, builder=pyocr.builders.TextBuilder())
print(txt + '\n')'''
word = ['SHINE','ON','YOU','CRAZY','DIAMOND','SYD']if word[2] in txt:print("## WORD IN LIST ##")
else:print("## NOT IN LIST ##")'''

Now the question: how can I remove from image a word which exist in the output OCR-list (in the code named txt) ? I mean, if the word SHINE exist as output in console (and in list), how can I delete it in image ? Or, if not remove, create a mask so I can hide it...

I think the ocr work by selecting areas of text and creating a bounding box around the text. In this case, how to delete (or even show) this ROI/bounding box ? In the pyocr documentation there are some hints about this function (show bounding box) but I don't know how to use it.

Any help/hint is appreciated.

Thanks

EDIT: this code show me the bounding box for each character

import csv
import cv2
from pytesseract import pytesseract as ptpt.run_tesseract('test.png', 'output', lang=None, boxes=True, config="hocr")# To read the coordinates
boxes = []
with open('output.box', 'rt') as f:reader = csv.reader(f, delimiter = ' ')for row in reader:if len(row) == 6:boxes.append(row)# Draw the bounding box
img = cv2.imread('test.png')
h, w, _ = img.shape
for b in boxes:img = cv2.rectangle(img,(int(b[1]),h-int(b[2])),(int(b[3]),h-int(b[4])),(255,0,0),2)cv2.imshow('output', img)
cv2.waitKey(0)

sdsdf

How can I tell it to show me only the first (whole) word ?

Answer

Here's a simple approach

  • Convert image to grayscale
  • Otsu's threshold
  • Dilate to connect contours
  • Find contours and extract ROI for each word
  • Perform OCR and remove word

After converting to grayscale, we Otsu's threshold to obtain a binary image

enter image description here

Next we invert the image and dilate to form a single contour for each word

enter image description here

From here we find contours and extract the ROI for each word. Here's the detected ROIs

enter image description here

We throw each ROI into Pytesseract OCR. If the OCR result is a word we want to remove, we simply "delete" the word by filling in the ROI with white and replace it in the original image


With

words_to_remove = ['on', 'you', 'crazy']

The result is

enter image description here

Similarly with

words_to_remove = ['on', 'you', 'shine', 'diamond']

The result is

enter image description here

Finally with

words_to_remove = ['on', 'you', 'crazy', 'diamond']

enter image description here

import cv2
import pytesseractwords_to_remove = ['on', 'you', 'crazy', 'diamond']
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"image = cv2.imread("1.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
inverted_thresh = 255 - thresh
dilate = cv2.dilate(inverted_thresh, kernel, iterations=4)cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:x,y,w,h = cv2.boundingRect(c)ROI = thresh[y:y+h, x:x+w]data = pytesseract.image_to_string(ROI, lang='eng',config='--psm 6').lower()if data in words_to_remove:image[y:y+h, x:x+w] = [255,255,255]cv2.imshow("thresh", thresh)
cv2.imshow("dilate", dilate)
cv2.imshow("image", image)
cv2.waitKey(0)
https://en.xdnf.cn/q/70818.html

Related Q&A

pandas.to_datetime inconsistent time string format

I am attempting to convert the index of a pandas.DataFrame from string format to a datetime index, using pandas.to_datetime().Import pandas:In [1]: import pandas as pdIn [2]: pd.__version__ Out[2]: 0.1…

Python NLTK WUP Similarity Score not unity for exact same word

Simple code like follows gives out similarity score of 0.75 for both cases. As you can see both the words are the exact same. To avoid any confusion I also compared a word with itself. The score refuse…

SystemExit: 2 error when calling parse_args() in iPython Notebook

Im learning to use Python and scikit-learn and executed the following block of codes (originally from http://scikit-learn.org/stable/auto_examples/document_classification_20newsgroups.html#example-docu…

How to convert numpy datetime64 [ns] to python datetime?

I need to convert dates from pandas frame values in the separate function:def myfunc(lat, lon, when):ts = (when - np.datetime64(1970-01-01T00:00:00Z,s)) / np.timedelta64(1, s)date = datetime.datetime.u…

how to remove a back slash from a JSON file

I want to create a json file like this:{"946705035":4,"946706692":4 ...}I am taking a column that only contains Unix Timestamp and group them.result = data[Last_Modified_Date_unixti…

Can sockets be used to connect multiple computers on different networks in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

python logging close and application exit

I am using the logging module in an application and it occurred to me that it would be neat if the logging module supported a method which would gracefully close file handles etc and then close the app…

LookupError: unknown encoding: cp0

I am on window 7, python 2.7.2, pandas 0.11.0, django 1.4, wsgi and apache 2.2. I have a pandas script that works fine if I run it directly with python and also works in ipython with %run. However, w…

Cant activate Python venv in Windows 10

I created a virtual environment with python -m venv myenv from the command prompt, but I dont know how to activate it. I tried executing activate.bat from the command prompt but it does not activate.In…

Opening file path not working in python [duplicate]

This question already has answers here:open() gives FileNotFoundError / IOError: [Errno 2] No such file or directory(11 answers)Closed last year.I am writing a database program and personica is my test…