質問編集履歴

1

コードの記載を修正

2022/03/19 05:59

投稿

Shimizukei
Shimizukei

スコア6

test CHANGED
File without changes
test CHANGED
@@ -5,29 +5,54 @@
5
5
 
6
6
  [https://hituji-ws.com/code/python/python-gcp-ocr/](https://hituji-ws.com/code/python/python-gcp-ocr/)
7
7
  ```
8
+ import requests
9
+ import base64
8
- from pathlib import Path
10
+ import json
9
- from google.cloud import vision
11
+ API_KEY = ''
12
+ GOOGLE_CLOUD_VISION_API_URL = 'https://vision.googleapis.com/v1/images:annotate?key='
10
13
 
14
+ # TEXT_DETECTION:比較的短い文字
11
- def render_doc_text(filein):
15
+ # DOCUMENT_TEXT_DETECTION:文章
16
+ DETECTION_TYPE = "DOCUMENT_TEXT_DETECTION"
12
17
 
18
+ def request_cloud_vison_api(image_base64, type="DOCUMENT_TEXT_DETECTION"):
19
+ """ http のリクエストでVisionAPIにアクセス """
20
+ api_url = GOOGLE_CLOUD_VISION_API_URL + API_KEY
21
+ req_body = json.dumps({
22
+ 'requests': [{
23
+ 'image': {
24
+ 'content': image_base64.decode('utf-8')
25
+ },
26
+ 'features': [{
27
+ 'type': type,
28
+ 'maxResults': 10,
29
+ }]
30
+ }]
31
+ })
13
- client = vision.ImageAnnotatorClient()
32
+ res = requests.post(api_url, data=req_body)
33
+ return res.json()
14
34
 
15
- p = Path(__file__).parent / filein
16
- with p.open('rb') as image_file:
17
- content = image_file.read()
18
35
 
36
+ def img_to_base64(filepath):
37
+ """ 画像データをエンコード """
38
+ with open(filepath, 'rb') as img:
39
+ img_byte = img.read()
19
- image = vision.Image(content=content)
40
+ return base64.b64encode(img_byte)
41
+
42
+
43
+ def render_doc_text(file_path):
44
+
45
+ result = request_cloud_vison_api(image_base64=img_to_base64(file_path),
46
+ type=DETECTION_TYPE)
20
47
 
21
48
  data_list = []
22
- response = client.document_text_detection(image=image)
23
- document = response.full_text_annotation
49
+ # データの取得 textAnnotationsに座標とテキスト fullTextAnnotationにテキスト
24
- for page in document.pages:
50
+ result_list = result["responses"][0]["textAnnotations"]
25
- for block in page.blocks:
51
+ for d in result_list:
26
- for paragraph in block.paragraphs:
27
- for word in paragraph.words:
28
- box = [{'x':v.x, 'y':v.y} for v in word.bounding_box.vertices]
52
+ data_list.append([d['boundingPoly']['vertices'], d['description']])
53
+
29
- text = [symbol.text for symbol in word.symbols]
54
+ # 1つ目除外
30
- data_list.append([box, ''.join(text)])
55
+ data_list = data_list[1:len(data_list)]
31
56
  return data_list
32
57
  ```
33
58