/
server.py
59 lines (47 loc) · 1.88 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from flask import Flask, jsonify, request
import google
from google.cloud import vision
import io
app = Flask(__name__)
@app.route('/', methods=["POST"])
def handle_image_cv():
if request.method == 'POST':
file = request.files['file']
file.save(file.filename)
print("doing " + file.filename)
res = detect_document(file.filename)
return jsonify({"status":"ok",
"response": res,
})
return "hello"
def detect_document(path):
"""Detects document features in an image."""
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.document_text_detection(image=image)
result = ''
for page in response.full_text_annotation.pages:
for block in page.blocks:
# print('\nBlock confidence: {}\n'.format(block.confidence))
for paragraph in block.paragraphs:
# print('Paragraph confidence: {}'.format(
#paragraph.confidence))
for word in paragraph.words:
word_text = ''.join([
symbol.text for symbol in word.symbols
])
result += '\n' if word_text == 'm' else word_text
# print('Word text: {} (confidence: {})'.format(
#word_text, word.confidence))
#for symbol in word.symbols:
# print('\tSymbol: {} (confidence: {})'.format(
#symbol.text, symbol.confidence))
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 result
#print(detect_document('fe'))