Coverage for lib/lib_ocr.py: 86%

192 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2026-01-26 23:58 +0100

1__author__ = 'moilerat' 

2 

3import sys 

4 

5 

6def ocr_google_vision(in_file = '/Users/moilerat/Dropbox/Chargements appareil photo/2023-05-03 17.23.31.jpg', 

7 verbose = False): 

8 # AIzaSyCGW-rDNT3fFxK9bqEIlkYGixMJ7_0FeYg 

9 # Imports the Google Cloud client library 

10 from google.cloud import vision 

11 import os, io 

12 

13 from auth.lib_conf_system import lcs_global_singleton 

14 lcs_global_singleton.set_conf_google_environ() 

15 

16# if 'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ and "GITSAFIA" in os.environ: 

17# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(os.environ["GITSAFIA"], "prompt/python", "google_credential_cracra.json") 

18# elif 'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ: # Else for Fotonower environment while prompt is in Doc (to be moved around 20/6/23 => moved in Safia around indeed) 

19# os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.join(os.environ["HOME"], "workarea/git/Safia/prompt/python", "google_credential_cracra.json") 

20# else : 

21# print("Still bad management") 

22 

23 # Instantiates a client 

24 client = vision.ImageAnnotatorClient() 

25 

26 # The name of the image file to annotate 

27 file_name = os.path.abspath(in_file) 

28 

29 if verbose: 

30 print(" file_name : " + str(file_name)) 

31 

32 # Loads the image into memory 

33 with io.open(file_name, 'rb') as image_file: 

34 content = image_file.read() 

35 

36 image = vision.Image(content=content) 

37 

38 response = client.text_detection(image=image) 

39 texts = response.text_annotations 

40 if verbose : 

41 print('Texts:') 

42 

43 maxx = 0 

44 maxy = 0 

45 

46 return_text = "" 

47 list_boxes = [] 

48 for text in texts: 

49 one_box = {} 

50 if verbose : 

51 print('\n"{}"'.format(text.description)) 

52 if return_text == "": # On ne prend que le premier 

53 return_text = '\n"{}"'.format(text.description) 

54 one_box["text"] = text.description 

55 

56 vertices = (['({},{})'.format(vertex.x, vertex.y) 

57 for vertex in text.bounding_poly.vertices]) 

58 one_box["vertices"] = [{"x" : vertex.x, "y" : vertex.y} for vertex in text.bounding_poly.vertices] 

59 for vertex in text.bounding_poly.vertices: 

60 if vertex.x > maxx: 

61 maxx = vertex.x 

62 if vertex.y > maxy: 

63 maxy = vertex.y 

64# one_box["h"] = maxy 

65 

66 list_boxes.append(one_box) 

67 

68 if verbose : 

69 print('bounds: {}\n'.format(','.join(vertices))) 

70 

71 list_boxes_ordered = sorted(list_boxes, key=lambda k: (2000 * k['vertices'][0]["y"] + k['vertices'][0]["x"])) 

72 

73 print("list_boxes_ordered : ") 

74 value_ordered = 0 

75 for x in list_boxes_ordered: 

76 previous_value_ordered = value_ordered 

77 value_ordered = 2000 * x['vertices'][0]["y"] + x['vertices'][0]["x"] 

78 if value_ordered - previous_value_ordered < 0: 

79 print("Decreasing !") 

80 if str(type(sys)) == "<class 'module'>": 

81 sys.stdout.write(x["text"]) 

82 sys.stdout.write(" ") 

83 else : 

84 print("ERROR treated as warning : sys is not a module, how come !") 

85 

86 if response.error.message: 

87 print( 

88 '{}\nFor more info on error messages, check: ' 

89 'https://cloud.google.com/apis/design/errors'.format( 

90 response.error.message)) 

91 return "", [], 0, 0, [] 

92 

93 if verbose: 

94 print(return_text) 

95 

96 return return_text, list_boxes, maxx, maxy, list_boxes_ordered 

97 

98 

99# https://keras-ocr.readthedocs.io/en/latest/ 

100 

101import cv2 

102 

103def img_to_texte(in_file, verbose = False): # pragma no cover 

104 import pytesseract 

105 image = cv2.imread(in_file) 

106 

107 list_blocks = {"lines" : [], "paragraphs" : [], "tokens" : []} 

108 

109 # Convertissez l'image en niveaux de gris (optionnel, mais peut améliorer les résultats) 

110 gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 

111 

112 # Effectuez la reconnaissance OCR 

113 text = pytesseract.image_to_string(gray_image) # , lang='eng' 

114 all_data = pytesseract.image_to_data(gray_image, output_type=pytesseract.Output.DICT) 

115# image_to_boxes 

116 

117 maxx = 0 

118 maxy = 0 

119 

120 list_boxes = [] 

121 for d in range(len(all_data["text"])): 

122 one_box = {} 

123 if verbose : 

124 print('\n"{}"'.format(all_data["text"][d])) 

125# if return_text == "": # On ne prend que le premier 

126# return_text = '\n"{}"'.format(text.description) 

127 one_box["text"] = all_data["text"][d] 

128 one_box["conf"] = all_data["conf"][d] 

129 one_box["line"] = all_data["line_num"][d] 

130 

131# vertices = (['({},{})'.format(vertex.x, vertex.y) 

132# for vertex in text.bounding_poly.vertices]) 

133 

134 # if verbose: 

135 # print('bounds: {}'.format(','.join(vertices))) 

136 

137 x1 = all_data["left"][d] 

138 y1 = all_data["top"][d] 

139 x2 = all_data["left"][d] + all_data["width"][d] 

140 y2 = all_data["top"][d] + all_data["height"][d] 

141 x3 = all_data["left"][d] + all_data["width"][d] 

142 y3 = all_data["top"][d] 

143 x4 = all_data["left"][d] 

144 y4 = all_data["top"][d] + all_data["height"][d] 

145 

146 one_box["vertices"] = [{"x" : x1, "y" : y1}, {"x" : x2, "y" : y2}, {"x" : x3, "y" : y3}, {"x" : x4, "y" : y4}] 

147 for vertex in one_box["vertices"]: 

148 if vertex["x"] > maxx: 

149 maxx = vertex["x"] 

150 if vertex["y"] > maxy: 

151 maxy = vertex["y"] 

152 

153 list_boxes.append(one_box) 

154 

155# xmin = min(list(map(lambda x: x["x"], box["vertices"]))) 

156# xmax = max(list(map(lambda x: x["x"], box["vertices"]))) 

157# ymin = min(list(map(lambda x: x["y"], box["vertices"]))) 

158# ymax = max(list(map(lambda x: x["y"], box["vertices"]))) 

159 one_block_paragraph = {"text" : all_data["text"][d], "x" : x1, "y" : y1, "w" : x2 - x1, "h" : y2 - y1, "conf" : all_data["conf"][d]} 

160 list_blocks["paragraphs"].append(one_block_paragraph) 

161 

162 if True: 

163 list_blocks_to_check_a_mon_Avis_doublon = pytesseract.image_to_boxes(gray_image, lang="eng", 

164 config="--psm 8", output_type=pytesseract.Output.DICT) 

165 

166# bbox_list = list() 

167# for i, _ in enumerate(list_boxes['bottom']): 

168# tl = (list_boxes['left'][i], list_boxes['bottom'][i]) 

169# br = (list_boxes['right'][i], list_boxes['top'][i]) 

170# bbox_list.append((tl, br)) 

171# from lib.lib_util import show_frame 

172# show_frame(gray_image, bbox_list=bbox_list, wait_flag=True) 

173 

174 # https://snyk.io/advisor/python/pytesseract/functions/pytesseract.image_to_data 

175 

176 # Affichez le texte reconnu 

177 if verbose: 

178 print(text) 

179 

180 return text, list_boxes, maxx, maxy, list_blocks 

181 

182def img_to_texte_keras(in_file, verbose = False): # pragma no cover 

183 

184 import keras_ocr 

185 import matplotlib.pyplot as plt 

186 

187 # Instancier le détecteur et le reconnaissant 

188 pipeline = keras_ocr.pipeline.Pipeline() 

189 

190 # Charger l'image 

191 image_path = in_file 

192 image = keras_ocr.tools.read(image_path) 

193 

194 # Effectuer la reconnaissance OCR 

195 predictions = pipeline.recognize([image]) 

196 

197 # Afficher les résultats 

198 for text, box in predictions[0]: 

199 print(text) 

200 

201 # Afficher l'image avec les boîtes de texte 

202 fig, ax = plt.subplots() 

203 keras_ocr.tools.drawAnnotations(image=image, predictions=predictions[0], ax=ax) 

204 plt.show() 

205 

206def test_mindee(): # pragma no cover 

207 from mindee import Client 

208 

209 my_client = Client().config_receipt("api-key") 

210 

211 receipt_doc = my_client.doc_from_path("/path/to/receipt.jpg") 

212 

213 parsed_receipt = receipt_doc.parse("receipt") 

214 

215 

216def test_parble(): # pragma no cover 

217 # install the package using "pip install parble" 

218 from parble import ParbleSDK 

219 

220 sdk = ParbleSDK("https://api.parble.com/v1/o95d3t", "f5af505e35c289ef54c10e7464e06e3e98cf8aff352a24c3e58568d3e64e54cb") 

221 file = sdk.upload_path("/Users/moilerat/Dropbox/Chargements appareil photo/2023-05-03 17.23.31.jpg") 

222 print(file) 

223 

224from typing import Optional, Sequence 

225 

226# TODO(developer): Uncomment these variables before running the sample. 

227# project_id = "YOUR_PROJECT_ID" 

228# location = "YOUR_PROCESSOR_LOCATION" # Format is "us" or "eu" 

229# processor_id = "YOUR_PROCESSOR_ID" # Create processor before running sample 

230# processor_version = "rc" # Refer to https://cloud.google.com/document-ai/docs/manage-processor-versions for more information 

231# file_path = "/path/to/local/pdf" 

232mime_type = "application/pdf" # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types 

233 

234location="eu" 

235processor_id="b2b2b2b2b2b2b2b2b" 

236processor_version="0000" 

237project_id="fpa0523" # "testdriveintegrationarticle " # "safia-310713" 

238endpoint = "https://eu-documentai.googleapis.com/v1/projects/541585891601/locations/eu/processors/a0bc421b279d53a3:process" 

239 

240endpoint = "https://eu-documentai.googleapis.com/v1/projects/300602618264/locations/eu/processors/1f997913b0bd28fc:process" 

241 

242def detect_table(file_path, verbose = False): # pragma no cover test 

243# document = process_document( 

244# project_id, location, processor_id, processor_version, file_path, mime_type 

245# ) 

246 

247 from google.api_core.client_options import ClientOptions 

248 from google.cloud import documentai 

249#from google.cloud.documentai_v1beta3 import DocumentProcessorServiceClient 

250 

251 process_options = documentai.ProcessOptions( 

252 ocr_config=documentai.OcrConfig( 

253 enable_native_pdf_parsing=True, 

254 enable_image_quality_scores=True, 

255 enable_symbol=True, 

256 # OCR Add Ons https://cloud.google.com/document-ai/docs/ocr-add-ons 

257 premium_features=documentai.OcrConfig.PremiumFeatures( 

258 compute_style_info=True, 

259 enable_math_ocr=False, # Enable to use Math OCR Model 

260 enable_selection_mark_detection=True, 

261 ), 

262 ) 

263 ) 

264 

265 with open(file_path, "rb") as image: 

266 image_content = image.read() 

267 

268 

269 client = documentai.DocumentProcessorServiceClient( 

270 client_options=ClientOptions( 

271 api_endpoint=f"{location}-documentai.googleapis.com" 

272 ) 

273) 

274 

275 # The full resource name of the processor version, e.g.: 

276 # `projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}` 

277 # You must create a processor before running this sample. 

278 name = client.processor_version_path( 

279 project_id, location, processor_id, processor_version 

280 ) 

281 name = "projects/300602618264/locations/eu/processors/1f997913b0bd28fc:process" 

282 name = "projects/85385769829/locations/eu/processors/8fab5acaf743eccf:process" 

283 

284 # Configure the process request 

285 request = documentai.ProcessRequest( 

286 name=name, 

287 raw_document=documentai.RawDocument(content=image_content, mime_type=mime_type), 

288 # Only supported for Document OCR processor 

289 process_options=process_options, 

290 ) 

291 

292 

293 

294 result = client.process_document(request=request) 

295 

296 # For a full list of `Document` object attributes, reference this page: 

297 # https://cloud.google.com/document-ai/docs/reference/rest/v1/Document 

298# return result.document 

299 

300 

301 text = result.document.text 

302 print(f"Full document text: {text}\n") 

303 print(f"There are {len(result.document.pages)} page(s) in this document.\n") 

304 

305 return text, [], 0, 0 

306 

307 

308 

309 

310# TODO(developer): Uncomment these variables before running the sample. 

311# project_id = "YOUR_PROJECT_ID" 

312location = "eu" # "YOUR_PROCESSOR_LOCATION" # Format is "us" or "eu" 

313# file_path = "/path/to/local/pdf" 

314# processor_display_name = "YOUR_PROCESSOR_DISPLAY_NAME" # Must be unique per project, e.g.: "My Processor" 

315 

316def gcp_doc_ai( 

317 file_path: str, 

318 project_id: str = "85385769829", #"fpa0523" 

319 location: str = "eu", 

320 processor_display_name: str = "doc_ocr_safia", # "fpa0523" # "doc_ocr_safia" 

321 verbose : bool = False 

322): 

323 from google.api_core.client_options import ClientOptions 

324 from google.cloud import documentai # type: ignore 

325 

326 from auth.lib_conf_system import lcs_global_singleton 

327 lcs_global_singleton.set_conf_google_environ() 

328 

329 # You must set the `api_endpoint`if you use a location other than "us". 

330 opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") 

331 

332 client = documentai.DocumentProcessorServiceClient(client_options=opts) 

333 

334 # The full resource name of the location, e.g.: 

335 # `projects/{project_id}/locations/{location}` 

336 parent = client.common_location_path(project_id, location) 

337 

338 # Create a Processor 

339# processor = client.create_processor( 

340# parent=parent, 

341# processor=documentai.Processor( 

342# type_="OCR_PROCESSOR", # Refer to https://cloud.google.com/document-ai/docs/create-processor for how to get available processor types 

343# display_name=processor_display_name, 

344# ), 

345# ) 

346 

347 # Print the processor information 

348# print(f"Processor Name: {processor.name}") 

349 

350 # Read the file into memory 

351 with open(file_path, "rb") as image: 

352 image_content = image.read() 

353 

354 # Load binary data 

355 raw_document = documentai.RawDocument( 

356 content=image_content, 

357# mime_type="application/pdf", # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types 

358 mime_type="image/png", 

359 # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types 

360 ) 

361 

362 process_options = documentai.ProcessOptions( 

363 ocr_config=documentai.OcrConfig( 

364 enable_native_pdf_parsing=True, 

365 enable_image_quality_scores=True, 

366 enable_symbol=True, 

367 # OCR Add Ons https://cloud.google.com/document-ai/docs/ocr-add-ons 

368 premium_features=documentai.OcrConfig.PremiumFeatures( 

369 compute_style_info=True, 

370 enable_math_ocr=False, # Enable to use Math OCR Model 

371 enable_selection_mark_detection=True, 

372 ), 

373 ) 

374 ) 

375 

376 # Configure the process request 

377 # `processor.name` is the full resource name of the processor, e.g.: 

378 # `projects/{project_id}/locations/{location}/processors/{processor_id}` 

379 # https://eu-documentai.googleapis.com/v1/projects/85385769829/locations/eu/processors/8fab5acaf743eccf:process 

380 processor_display_name = "projects/85385769829/locations/eu/processors/8fab5acaf743eccf" 

381 request = documentai.ProcessRequest(name=processor_display_name, raw_document=raw_document, process_options=process_options) 

382 

383 result = client.process_document(request=request) 

384 

385 # For a full list of `Document` object attributes, reference this page: 

386 # https://cloud.google.com/document-ai/docs/reference/rest/v1/Document 

387 document = result.document 

388 

389 # Read the text recognition output from the processor 

390 print("The document contains the following text: " + str(len(document.text))) 

391 if verbose : 

392 print(document.text) 

393 

394 # Read the entity recognition output from the processor 

395 list_blocks = {} 

396 if len(document.pages) != 1: 

397 print(" We expected one page, we got ", len(document.pages)) 

398 return None, list_blocks, 0, 0 

399 list_lines = [] 

400 list_paragraphs = [] 

401 list_tokens = [] 

402 id_line = 0 

403 id_paragraph = 0 

404 id_token = 0 

405 

406 

407 

408 for page in document.pages: 

409 

410 if page.visual_elements != [] \ 

411 or page.form_fields != [] \ 

412 or page.tables != [] \ 

413 or page.transforms != [] \ 

414 or page.detected_barcodes != [] : 

415 print("ERROR : DATA TO BE ANALYZED : page.visual_elements : " + str(len(page.visual_elements)) + \ 

416 " page.form_fields : " + str(len(page.form_fields)) + \ 

417 " page.tables : " + str(len(page.tables)) + \ 

418 " page.transforms : " + str(len(page.transforms)) + \ 

419 " page.detected_barcodes : " + str(len(page.detected_barcodes)) ) 

420 

421 # Et encore : page.blocks page.symbols 

422 

423 print(str(page.dimension)) 

424 print(str(page.dimension)) 

425 

426 prev_end_index = -1 

427 # page.paragraphs, page.lines, page.tokens. 

428 for lin in page.lines: 

429 if verbose: 

430 print(f"Line confidence: {lin.layout.confidence}") 

431# print(f"Line text: {lin.text.content}") 

432 print(f"Line bounding box: {lin.layout.bounding_poly}") 

433 

434 one_block = {} 

435 if len(lin.layout.text_anchor.text_segments) != 1: 

436 print(" We expected one text segment, we got ", len(lin.layout.text_anchor.text_segments)) 

437 return None, list_blocks, 0, 0 

438 if hasattr(lin.layout.text_anchor.text_segments[0], "start_index"): 

439 start_index = lin.layout.text_anchor.text_segments[0].start_index 

440 else: 

441 start_index = 0 

442 end_index = lin.layout.text_anchor.text_segments[0].end_index 

443 

444 # Check juste pour vérification 

445 if prev_end_index >= 0: 

446 if start_index != prev_end_index: 

447 sys.stdout.write("!") 

448 

449 one_block["text"] = document.text[start_index:end_index] 

450 

451 one_block["x"] = lin.layout.bounding_poly.vertices[0].x 

452 one_block["y"] = lin.layout.bounding_poly.vertices[0].y 

453 one_block["w"] = lin.layout.bounding_poly.vertices[2].x - lin.layout.bounding_poly.vertices[0].x 

454 one_block["h"] = lin.layout.bounding_poly.vertices[2].y - lin.layout.bounding_poly.vertices[0].y 

455 

456 one_block["type"] = 'line' 

457 one_block["id"] = id_line 

458 one_block["start_index"] = start_index 

459 one_block["end_index"] = end_index 

460 id_line += 1 

461 prev_end_index = end_index 

462 

463 list_lines.append(one_block) 

464 

465 prev_end_index = -1 

466 for paragraph in page.paragraphs: 

467 if verbose: 

468 print(f"Paragraph confidence: {paragraph.layout.confidence}") 

469# print(f"Paragraph text: {paragraph.text.content}") 

470 print(f"Paragraph bounding box: {paragraph.layout.bounding_poly}") 

471 

472 one_block = {} 

473 start_index = paragraph.layout.text_anchor.text_segments[0].start_index 

474 end_index = paragraph.layout.text_anchor.text_segments[0].end_index 

475 if prev_end_index >= 0: 

476 if start_index != prev_end_index: 

477 sys.stdout.write("!") 

478 one_block["text"] = document.text[start_index:end_index] 

479 # paragraph.text.content 

480 one_block["x"] = paragraph.layout.bounding_poly.vertices[0].x 

481 one_block["y"] = paragraph.layout.bounding_poly.vertices[0].y 

482 one_block["w"] = paragraph.layout.bounding_poly.vertices[2].x - paragraph.layout.bounding_poly.vertices[0].x 

483 one_block["h"] = paragraph.layout.bounding_poly.vertices[2].y - paragraph.layout.bounding_poly.vertices[0].y 

484 

485 one_block["type"] = 'paragraph' 

486 one_block["id"] = id_paragraph 

487 one_block["start_index"] = start_index 

488 one_block["end_index"] = end_index 

489 id_paragraph += 1 

490 prev_end_index = end_index 

491 

492 list_paragraphs.append(one_block) 

493 

494 prev_end_index = -1 

495 for token in page.tokens: 

496 # print(f"Token text: {token.text.content}") 

497# print(f"Token start index: {token.text.start_index}") 

498 

499 one_block = {} 

500 if hasattr(token.layout.text_anchor.text_segments[0], "start_index"): 

501 start_index = token.layout.text_anchor.text_segments[0].start_index 

502 else: 

503 start_index = 0 

504 end_index = token.layout.text_anchor.text_segments[0].end_index 

505 if prev_end_index >= 0: 

506 if start_index != prev_end_index: 

507 sys.stdout.write("!") 

508 one_block["text"] = document.text[start_index:end_index] 

509# one_block["text"] = token.text.content 

510 one_block["x"] = token.layout.bounding_poly.vertices[0].x 

511 one_block["y"] = token.layout.bounding_poly.vertices[0].y 

512 one_block["w"] = token.layout.bounding_poly.vertices[2].x - token.layout.bounding_poly.vertices[0].x 

513 one_block["h"] = token.layout.bounding_poly.vertices[2].y - token.layout.bounding_poly.vertices[0].y 

514 

515 one_block["type"] = 'token' 

516 one_block["id"] = id_token 

517 one_block["start_index"] = start_index 

518 one_block["end_index"] = end_index 

519 id_token += 1 

520 prev_end_index = end_index 

521 

522 list_tokens.append(one_block) 

523 

524# for block in page.blocks: 

525# print(str(block)) 

526# # je vais rajouter le block dans la liste avec le texte et sa localisation, le point en haut à droite et la largeur et hauteur 

527# one_block = {} 

528# text = "" 

529# for t in block.layout.text_anchor.text_segments: 

530# text += t.content 

531# one_block["text"] = block.text 

532# # one_block["vertices"] = [{"x" : vertex.x, "y" : vertex.y} for vertex in block.bounding_box.vertices] 

533# # Non je veux le point en haut à gauche et la largeur et hauteur 

534# x1 = block.bounding_box.vertices[0].x 

535# y1 = block.bounding_box.vertices[0].y 

536# x2 = block.bounding_box.vertices[2].x 

537# y2 = block.bounding_box.vertices[2].y 

538# one_block["x"] = x1 

539# one_block["y"] = y1 

540# one_block["w"] = x2 - x1 

541# one_block["h"] = y2 - y1 

542# list_blocks.append(one_block) 

543 

544 # Maintenant on associe les tokens aux paragraphs 

545 id_token = 0 

546 id_paragraph = 0 

547 while id_paragraph < len(list_paragraphs) and id_token < len(list_tokens): 

548# start_index = list_paragraphs[id_paragraph]["start_index"] 

549 end_index = list_paragraphs[id_paragraph]["end_index"] 

550 

551 start_index_token = list_tokens[id_token]["start_index"] 

552# end_index_token = list_tokens[id_token]["end_index"] 

553 if start_index_token < end_index: 

554 list_tokens[id_token]["id_paragraph"] = id_paragraph 

555 id_token += 1 

556 continue 

557 

558 id_paragraph += 1 

559 

560 list_blocks["lines"] = list_lines 

561 list_blocks["paragraphs"] = list_paragraphs 

562 list_blocks["tokens"] = list_tokens 

563 

564 return document.text, "", int(document.pages[0].dimension.width), int(document.pages[0].dimension.height), list_blocks 

565 

566 

567 

568def detect_document(path, verbose = False): # pragma no cover test 

569 """Detects document features in an image.""" 

570 from google.cloud import vision 

571 

572 client = vision.ImageAnnotatorClient() 

573 

574 with open(path, "rb") as image_file: 

575 content = image_file.read() 

576 

577 image = vision.Image(content=content) 

578 

579 response = client.document_text_detection(image=image) 

580 

581 concat_word = "" 

582 

583 for page in response.full_text_annotation.pages: 

584 for block in page.blocks: 

585 print(f"\nBlock confidence: {block.confidence}\n") 

586 

587 for paragraph in block.paragraphs: 

588 print("Paragraph confidence: {}".format(paragraph.confidence)) 

589 

590 for word in paragraph.words: 

591 word_text = "".join([symbol.text for symbol in word.symbols]) 

592 print( 

593 "Word text: {} (confidence: {})".format( 

594 word_text, word.confidence 

595 ) 

596 ) 

597 

598 concat_word += " " + word_text 

599 

600 for symbol in word.symbols: 

601 print( 

602 "\tSymbol: {} (confidence: {})".format( 

603 symbol.text, symbol.confidence 

604 ) 

605 ) 

606 

607 print(concat_word) 

608 

609 if response.error.message: 

610 raise Exception( 

611 "{}\nFor more info on error messages, check: " 

612 "https://cloud.google.com/apis/design/errors".format(response.error.message) 

613 ) 

614 

615 return concat_word 

616 

617 

618if __name__ == "__main__": 

619 filepath = "/Users/moilerat/Downloads/lettre_wien_1941.jpg" 

620 

621 detect_document(filepath) 

622 

623 print(" Hello ! ") 

624 

625