J'ai essayé d'extraire du texte d'images avec l'API GCP Cloud Vision
Reportez-vous à Détecter le texte dans l'image.
$ conda create -n py36 python=3.6
$ conda activate py36
$ pip install numpy
$ pip install pillow
$ pip install opencv-python
$ pip install --upgrade google-cloud-vision
from google.cloud import vision
import io
import os
import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
def detect_text(image):
"""Detects text in the file."""
client = vision.ImageAnnotatorClient()
content = cv2.imencode(".png ", image)[1].tostring()
tmp = vision.Image(content=content)
response = client.text_detection(image=tmp)
texts = response.text_annotations
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
return texts
filename = "338px-Atomist_quote_from_Democritus.png "
root, ext = os.path.splitext(filename)
image = cv2.imread(filename, cv2.IMREAD_COLOR)
texts = detect_text(image)
fontpath ='C:\Windows\Fonts\meiryo.ttc'
font = ImageFont.truetype(fontpath, 10)
image_pil = Image.fromarray(image)
for text in texts:
print(text.description)
vertices = [(vertex.x, vertex.y) for vertex in text.bounding_poly.vertices]
# cv2.putText(image, text.description, vertices[0], cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1, cv2.LINE_AA)
# cv2.rectangle(image, vertices[0], vertices[2], (0, 255, 0))
draw = ImageDraw.Draw(image_pil)
w, h = draw.textsize(text.description, font = font)
draw.text((vertices[0][0], vertices[0][1]-h), text.description, font=font, fill=(255, 255, 255, 0))
# draw.text(vertices[0], text.description, font=font, fill=(255, 255, 255, 0))
draw.rectangle((vertices[0], vertices[2]), outline=(0, 255, 0))
image = np.array(image_pil)
cv2.imshow("image", image)
cv2.imwrite(root+"_ocr"+ext, image)
cv2.waitKey(0)
input | PIL | PIL(Style OpenCV) | OpenCV |
---|---|---|---|
Avec OpenCV, l'anglais pleine largeur (japonais) est brouillé, j'ai donc utilisé PIL pour afficher le texte. Je vous remercie pour votre travail acharné.
Recommended Posts