Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
FaceMask Detection using TensorFlow in Python
Next article icon

Optical Character Recognition (OCR) is a technology used to extract text from images which is used in applications like document digitization, license plate recognition and automated data entry. In this article, we explore how to detect and extract text from images usingOpenCVfor image processing andTesseract OCRfor text recognition.

Before we start we need to install required libraries using following commands:

  • !pip install opencv-python
  • !pip install pytesseract
  • !sudo apt-get install tesseract-ocr

Step 1: Importing Required Packages

Import the required Python libraries likeOpenCV,pytesseractandmatplotlib.

Python
importcv2importpytesseractfrommatplotlibimportpyplotasplt

Step 2: Loading Image

Now we will load the image usingcv2.imread()function. OpenCV loads images in BGR format but matplotlib expects RGB format so we convert it usingcv2.cvtColor().

Python
image_path="/mnt/data/08d702e9-fb88-4a5b-98ce-9ad0bc19afd9.png"image=cv2.imread(image_path)image_rgb=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

Step 3: Converting Image to Grayscale

Convert the input image to grayscale to simplify the image and remove color information.

Python
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)print("Grayscale Image:")cv2_imshow(gray)

Step 4:Displaying Original Image

Let's see original image with the help of matplotlib.

Python
plt.figure(figsize=(10,6))plt.imshow(image_rgb)plt.title("Original Image")plt.axis("off")plt.show()

Output:

Original-Image
Sample Image

Step 5: Extracting Text from Image

In this step we extract text from the image with the help ofpytesseract.image_to_string().

Python
extracted_text=pytesseract.image_to_string(image_rgb)print(" Extracted Text:\n")print(extracted_text)

Output:

extracted_text
extracted text

Step 6: Drawing Bounding Boxes Around Detected Text

Now we loop through each detected word and draw ablue box around it usingcv2.reactangle.

Python
data=pytesseract.image_to_data(image_rgb,output_type=pytesseract.Output.DICT)n_boxes=len(data['level'])foriinrange(n_boxes):(x,y,w,h)=(data['left'][i],data['top'][i],data['width'][i],data['height'][i])cv2.rectangle(image_rgb,(x,y),(x+w,y+h),(255,0,0),2)

Step 7: Showing Image with Bounding Boxes

Finally this shows the processed image with bounding boxes around the detected text.

Python
plt.figure(figsize=(10,6))plt.imshow(image_rgb)plt.title("Image with Text Bounding Boxes")plt.axis("off")plt.show()

Output:

Output-image
Output Image

Get complete notebook link here :

Notebook:click here.
Sample Image:click here.


Text Detection and Extraction using OpenCV and OCR
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp