hytos / minorTools / gcloud_test.py @ 65eb78a7
이력 | 보기 | 이력해설 | 다운로드 (1.44 KB)
1 |
def detect_text(path): |
---|---|
2 |
"""Detects text in the file."""
|
3 |
from google.cloud import vision |
4 |
import cv2 |
5 |
import numpy as np |
6 |
|
7 |
client = vision.ImageAnnotatorClient() |
8 |
|
9 |
with open(path, "rb") as image_file: |
10 |
content = image_file.read() |
11 |
|
12 |
_image = cv2.imread(path, 1)
|
13 |
|
14 |
image = vision.Image(content=content) |
15 |
|
16 |
response = client.text_detection(image=image) |
17 |
#response = client.document_text_detection(image=image)
|
18 |
texts = response.text_annotations |
19 |
#print("Texts:")
|
20 |
|
21 |
for text in texts[1:]: |
22 |
#print(f'\n"{text.description}"')
|
23 |
|
24 |
#vertices = [ f"({vertex.x},{vertex.y})" for vertex in text.bounding_poly.vertices ]
|
25 |
#print("bounds: {}".format(",".join(vertices)))
|
26 |
|
27 |
_vertices = [(vertex.x,vertex.y) for vertex in text.bounding_poly.vertices] |
28 |
|
29 |
cv2.rectangle(_image, _vertices[0], _vertices[2], (100, 100, 100), 2) |
30 |
cv2.putText(_image, text.description, (_vertices[0][0], (_vertices[0][1] - 10)), cv2.FONT_HERSHEY_PLAIN, 1, (100, 100, 100), 1) |
31 |
|
32 |
cv2.imwrite(path.replace('test', 'test_res'), _image) |
33 |
|
34 |
if response.error.message:
|
35 |
raise Exception( |
36 |
"{}\nFor more info on error messages, check: "
|
37 |
"https://cloud.google.com/apis/design/errors".format(response.error.message)
|
38 |
) |
39 |
|
40 |
|
41 |
if __name__ == "__main__": |
42 |
import os |
43 |
path = 'E:\Projects\DTIPID\minorTools\gcloud'
|
44 |
path = os.path.join(path, 'test.png')
|
45 |
detect_text(path) |
46 |
|