Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
- Mục tiêu của chúng tôi là chuyển đổi một hình ảnh văn bản nhất định thành một chuỗi văn bản, lưu nó vào một tệp và để nghe những gì được viết trong hình ảnh thông qua âm thanh.It is an optical character recognition [OCR] tool for python sponsored by google.
- Đối với điều này, chúng tôi cần nhập một số thư việnIt is an offline cross-platform Text-to-Speech library
- Pytesseract [Python-Tesseract]: Đây là công cụ nhận dạng ký tự quang học [OCR] cho Python được tài trợ bởi Google.It adds image processing capabilities to your Python interpreter
- PYTTSX3: Nó là một thư viện văn bản-nền tảng đa nền tảng ngoại tuyếnIt is a free python library that implements the Google Translate API.
Examples:
Input : We Have an image with some text Output: THE TEXT FROM THE IMAGE IS EXTRACTED AND A VOICE WILL SPEAK THE TEXT This is the first line of this text example. This is the second line of the same text. Translated[src=en, dest=de, text=Dies ist die erste Zeile von Dieses Textbeispiel. Dies ist die zweite Zeile desselben Textes., pronunciation=None, extra_data="{'translat..."
Thư viện hình ảnh Python [PIL]: Nó thêm khả năng xử lý hình ảnh vào trình thông dịch Python của bạn
Googletrans: Đây là một thư viện Python miễn phí thực hiện API Google Dịch.
Mã: Mã Python để chuyển đổi văn bản thành bài phát biểu
import
pytesseract
from
PIL
import
pip install pytesseract pip install pillow1
import
pip install pytesseract pip install pillow3
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]5
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]6
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]7
pip install pytesseract pip install pillow9
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]9
from
pip install pytesseract pip install pillow5
import
pip install pytesseract pip install pillow7
Sample Text 13
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]1
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]2
Sample Text 16
Sample Text 17
pip install pytesseract pip install pillow9
Sample Text 19
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]0
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]1
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]2
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]3
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]1
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]5
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]3
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]5
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]8
pip install pytesseract pip install pillow8
pip install pytesseract pip install pillow9
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]0
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]1
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]2223
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]4
Sample Text 10
pip install pytesseract pip install pillow9
Sample Text 12
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]5
Sample Text 1 Sample Text 2 Sample Text 39
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]9
pip install pytesseract pip install pillow9
Sample Text 1 Sample Text 2 Sample Text 31
import
3
import
4
Sample Text 1
Sample Text 2
Sample Text 3
2pip install pytesseract
pip install pillow
9 Sample Text 1
Sample Text 2
Sample Text 3
4pip install pytesseract
pip install pillow
9Sample Text 1
Sample Text 2
Sample Text 3
6Sample Text 1
Sample Text 2
Sample Text 3
7 We can convert the text into any desired language. For Example Japanese, Russian, Hindi. But the only condition is that the googletrans should
recognize the destination language. Also, pyttsx3 will speak only the languages which are recognized by it.
Bài viết này được xuất bản lần đầu tiên trên Pyshark, và vui lòng đóng góp cho những người viết blog Python. [Bạn có thể báo cáo vấn đề về nội dung trên trang này tại đây] Bạn muốn chia sẻ nội dung của bạn trên Python-Bloggers? bấm vào đây. PyShark , and kindly contributed to python-bloggers. [You can report issue about the content on this page here]
Want to share your content on python-bloggers? click here.
Trong hướng dẫn này, chúng tôi sẽ khám phá cách trích xuất văn bản từ hình ảnh bằng Python.
Mục lục
- Giới thiệu
- Hình ảnh mẫu
- Trích xuất văn bản từ một hình ảnh duy nhất bằng Python
- Trích xuất văn bản từ nhiều hình ảnh bằng Python
- Sự kết luận
Giới thiệu
Hình ảnh mẫu
Trích xuất văn bản từ một hình ảnh duy nhất bằng Python
Trích xuất văn bản từ nhiều hình ảnh bằng Python
- Trích xuất văn bản từ hình ảnh là một nhiệm vụ rất phổ biến trong các đơn vị hoạt động của doanh nghiệp [trích xuất thông tin từ hóa đơn và biên lai] cũng như trong các lĩnh vực khác.
- OCR [Nhận dạng ký tự quang học] là một cách tiếp cận dựa trên máy tính điện tử để chuyển đổi hình ảnh của văn bản thành văn bản được mã hóa bằng máy, sau đó có thể được trích xuất và sử dụng ở định dạng văn bản.
- Để tiếp tục làm theo hướng dẫn này, chúng tôi sẽ cần:
- Tesseract
Hai thư viện Python:
pytesseract
cái gối
Tesseract là một công cụ OCR [Nhận dạng ký tự quang học] nguồn mở cho phép trích xuất văn bản từ hình ảnh.
Để sử dụng nó trong Python, chúng tôi cũng sẽ cần thư viện PyTesseract, một trình bao bọc cho động cơ Tesseract.
pip install pytesseract pip install pillow
Hình ảnh mẫu
Trích xuất văn bản từ một hình ảnh duy nhất bằng Python
Trích xuất văn bản từ nhiều hình ảnh bằng Python
Trích xuất văn bản từ hình ảnh là một nhiệm vụ rất phổ biến trong các đơn vị hoạt động của doanh nghiệp [trích xuất thông tin từ hóa đơn và biên lai] cũng như trong các lĩnh vực khác.
OCR [Nhận dạng ký tự quang học] là một cách tiếp cận dựa trên máy tính điện tử để chuyển đổi hình ảnh của văn bản thành văn bản được mã hóa bằng máy, sau đó có thể được trích xuất và sử dụng ở định dạng văn bản.
Để tiếp tục làm theo hướng dẫn này, chúng tôi sẽ cần:sampletext1-ocr.png
Tesseract
Hai thư viện Python:images and the code resides in main.py
pytesseract
cái gốitessaract.exe which was created after the installation. On Windows it should reside in: C:\Program Files\Tesseract-OCR\tesseract.exe
Bây giờ chúng ta có mọi thứ chúng ta cần và có thể dễ dàng trích xuất văn bản từ hình ảnh bằng Python:
from PIL import Image from pytesseract import pytesseract #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_image = 'images/sampletext1-ocr.png' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract #Open image with PIL img = Image.open[path_to_image] #Extract text from image text = pytesseract.image_to_string[img] print[text]
Và bạn nên nhận được:
Sample Text 1
Trong phần này, chúng tôi sẽ khám phá cách trích xuất văn bản từ nhiều hình ảnh bằng Python.
Chúng tôi biết rằng tất cả các hình ảnh được đặt trong hình ảnh thư mục và mã nằm trong Main.pyimages and the code resides in main.py
Một cách trích xuất văn bản từ mỗi hình ảnh sẽ là sử dụng tên tệp của mỗi hình ảnh và trích xuất văn bản từ từng hình ảnh.
Nhưng nếu chúng ta có 100 hình ảnh trong thư mục? Sử dụng thư viện OS, chúng tôi có thể truy cập tất cả các tệp trong một thư mục nhất định.
Khi chúng tôi có quyền truy cập vào tất cả các tệp hình ảnh trong thư mục hình ảnh, chúng tôi sẽ lặp lại chúng và trích xuất văn bản từ mỗi hình ảnh bằng Python:images folder, we will iterate over them and extract text from each image using Python:
from PIL import Image from pytesseract import pytesseract import os #Define path to tessaract.exe path_to_tesseract = r'C:\Program Files\Tesseract-OCR\tesseract.exe' #Define path to image path_to_images = r'images/' #Point tessaract_cmd to tessaract.exe pytesseract.tesseract_cmd = path_to_tesseract for root, dirs, file_names in os.walk[path_to_images]: #Iterate over each file_name in the folder for file_name in file_names: #Open image with PIL img = Image.open[path_to_images + file_name] #Extract text from image text = pytesseract.image_to_string[img] print[text]
Và bạn nên nhận được:
Sample Text 1 Sample Text 2 Sample Text 3
Trong phần này, chúng tôi sẽ khám phá cách trích xuất văn bản từ nhiều hình ảnh bằng Python.
Chúng tôi biết rằng tất cả các hình ảnh được đặt trong hình ảnh thư mục và mã nằm trong Main.py
Một cách trích xuất văn bản từ mỗi hình ảnh sẽ là sử dụng tên tệp của mỗi hình ảnh và trích xuất văn bản từ từng hình ảnh.
Nhưng nếu chúng ta có 100 hình ảnh trong thư mục? Sử dụng thư viện OS, chúng tôi có thể truy cập tất cả các tệp trong một thư mục nhất định.
Khi chúng tôi có quyền truy cập vào tất cả các tệp hình ảnh trong thư mục hình ảnh, chúng tôi sẽ lặp lại chúng và trích xuất văn bản từ mỗi hình ảnh bằng Python:
Đó chính xác là văn bản chúng ta có trong các hình ảnh.leave a comment for the author, please follow the link and comment on their blog: PyShark .
Sự kết luận