Text extraction with GCP Cloud Vision API (Python3.6)

Introduction

I tried extracting text from images with GCP Cloud Vision API

Development environment

Introduction

Refer to Detect text in images (https://cloud.google.com/vision/docs/ocr).

    1. Create a project from the Cloud Console.
  1. Make sure billing is enabled.
    1. Enable the Vision API. Four. Set the authentication and the JSON file will be downloaded to your PC. Five. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the JSON file path.
  2. Open the anaconda prompt and create a Python 3.6 environment.
$ conda create -n py36 python=3.6
$ conda activate py36
    1. Install the library
$ pip install numpy
$ pip install pillow
$ pip install opencv-python
$ pip install --upgrade google-cloud-vision
    1. Let's run the code below
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(OpenCV style) OpenCV
338px-Atomist_quote_from_Democritus.png 338px-Atomist_quote_from_Democritus_ocr.png 338px-Atomist_quote_from_Democritus_ocr_2.png 338px-Atomist_quote_from_Democritus_ocr_3.png

With OpenCV, double-byte English (Japanese) is garbled, so I used PIL to display the text. Thank you for your hard work.

Recommended Posts

Text extraction with GCP Cloud Vision API (Python3.6)
Text extraction (Read API) with Azure Computer Vision API (Python3.6)
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Text extraction with AWS Textract (Python3.6)
Flow of extracting text in PDF with Cloud Vision API
Google Cloud Vision API sample for python
Use Google Cloud Vision API from Python
[GCP] Operate Google Cloud Storage with Python
Text mining with Python ② Visualization with Word Cloud
Problems with output results with Google's Cloud Vision API
[Blender] Complement Blender's Python API with a text editor
Use Trello API with python
Use Twitter API with Python
Web API with Python + Falcon
Play RocketChat with API / Python
Call the API with python3.
Use subsonic API with python3
Easy to use Nifty Cloud API with botocore and python
[Package cloud] Manage python packages with package cloud
Create Awaitable with Python / C API
GOTO in Python with Sublime Text 3
Get reviews with python googlemap api
Run Rotrics DexArm with python API
Quine Post with Qiita API (Python)
Text mining with Python ① Morphological analysis
Enable Python raw_input with Sublime Text 3
Run XGBoost with Cloud Dataflow (Python)
Hit the Etherpad-lite API with Python
[python] Read information with Redmine API
Speak Japanese text with OpenJTalk + python
"AttributeError: module'google.cloud.vision' has no attribute'types'" in Cloud Vision API (GCP vision AI)
Play with GCP free frame ① ~ Cloud Run, Datastore & LINE Messaging API ~
Cloud DevOps Cookbook Part 4-Explore DevOps DirectMail in Python with REST API
Translate Coursera's WebVTT format subtitles with the GCP Cloud Translation API
Collecting information from Twitter with Python (Twitter API)
Easy keyword extraction with TermExtract for Python
English speech recognition with python [speech to text]
Automatically create Python API documentation with Sphinx
Streaming speech recognition with Google Cloud Speech API
Python calling Google Cloud Vision API from LINE BOT via AWS Lambda
Simple Slack API client made with Python
Retrieving food data with Amazon API (Python)
Clogged with python update of GCP console ①
Try using Python with Google Cloud Functions
[AWS] [GCP] I tried to make cloud services easy to use with Python
Transcription of images with GCP's Vision API
Machine Learning x Web App Diagnosis: Recognize CAPTCHA with Cloud Vision API
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
[Azure] Hit Custom Vision Service with Python
[Python] Quickly create an API with Flask
Detect Japanese characters from images using Google's Cloud Vision API in Python
Serverless face recognition API made with Python
[Python] Get Python package information with PyPI API
How to use GCP's Cloud Vision API
Read text in images with python OCR
Transcribe WAV files with Cloud Speech API
Handle structured logs with GCP Cloud Logging
[Cloud102] # 1 Let's get started with Python (Part 3 Jupyter Notebook Construction GCP Cloud Shell Edition)
Extract text from PowerPoint with Python! (Compatible with tables)
Try it with Word Cloud Japanese Python JupyterLab.
I tried "License OCR" with Google Vision API