如何为 OCR 制作 REST API
How to make a REST API for OCR
我正在执行一项任务,其中我有一张图像并且必须从中提取日期。使用 Google Cloud Platform Vision API 提取日期。如何使用接受图像和 returns 日期的烧瓶制作 API?格式如下所示:
Request:POST /extract_date
Payload: {“base_64_image_content”: }
Response: If date is present: {“date”: “YYYY-MM-DD”} If date is not
present: {“date”: null}
你能帮帮我吗?
Flask is among the most popular web frameworks for Python. It's relatively easy to learn, while its extension Flask-RESTful 使您能够快速构建 REST API。
最小示例:
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class MyApi(Resource):
def get(self, date):
return {'date': 'if present'}
api.add_resource(MyApi, '/')
if __name__ == '__main__':
app.run()
测试 curl
:
curl http://localhost:5000/ -d "data=base_64_image_content" -X PUT
经过评论中的讨论,以下是如何使用 GCP Functions 构建 OCR REST API:
import re
import json
from google.protobuf.json_format import MessageToJson
from google.cloud import vision
from flask import Response
def detect_text(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>`.
"""
client = vision.ImageAnnotatorClient()
image = vision.types.Image(content=request.data)
response = client.text_detection(image=image)
serialized = MessageToJson(response)
annotations = json.loads(serialized)
full_text = annotations['textAnnotations'][0]['description']
annotations = json.dumps(annotations)
r = Response(response=annotations, status=200, mimetype="application/json")
return r
这是您可以用来发出请求的一段代码:
def post_image(path, URL):
headers = {'content-type': 'image/jpeg'}
img = open(path, 'rb').read()
response = requests.post(URL, data=img, headers=headers)
return response
我正在执行一项任务,其中我有一张图像并且必须从中提取日期。使用 Google Cloud Platform Vision API 提取日期。如何使用接受图像和 returns 日期的烧瓶制作 API?格式如下所示:
Request:POST /extract_date
Payload: {“base_64_image_content”: }
Response: If date is present: {“date”: “YYYY-MM-DD”} If date is not present: {“date”: null}
你能帮帮我吗?
Flask is among the most popular web frameworks for Python. It's relatively easy to learn, while its extension Flask-RESTful 使您能够快速构建 REST API。
最小示例:
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class MyApi(Resource):
def get(self, date):
return {'date': 'if present'}
api.add_resource(MyApi, '/')
if __name__ == '__main__':
app.run()
测试 curl
:
curl http://localhost:5000/ -d "data=base_64_image_content" -X PUT
经过评论中的讨论,以下是如何使用 GCP Functions 构建 OCR REST API:
import re
import json
from google.protobuf.json_format import MessageToJson
from google.cloud import vision
from flask import Response
def detect_text(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/0.12/api/#flask.Flask.make_response>`.
"""
client = vision.ImageAnnotatorClient()
image = vision.types.Image(content=request.data)
response = client.text_detection(image=image)
serialized = MessageToJson(response)
annotations = json.loads(serialized)
full_text = annotations['textAnnotations'][0]['description']
annotations = json.dumps(annotations)
r = Response(response=annotations, status=200, mimetype="application/json")
return r
这是您可以用来发出请求的一段代码:
def post_image(path, URL):
headers = {'content-type': 'image/jpeg'}
img = open(path, 'rb').read()
response = requests.post(URL, data=img, headers=headers)
return response