1
+ import os
1
2
from fastapi import APIRouter
2
-
3
+ from pdf2image import convert_from_path
4
+ from google .cloud import vision
3
5
4
6
# Create an instance of APIRouter
5
7
router = APIRouter ()
@@ -22,21 +24,17 @@ def pdf_to_images(pdf_path, output_folder):
22
24
23
25
return image_paths ,noImg
24
26
25
-
26
-
27
-
28
- substring_to_remove = "Scanned by CamScanner"
29
-
30
27
@router .get ("/NotesToText" )
31
28
def NotesToText_handler ():
32
- for i in range (1 ):
29
+ substring_to_remove = "Scanned by CamScanner"
30
+ for i in range (4 ):
33
31
print (f"converting module-{ i + 1 } ...." )
34
32
pdf_path = f'Local_Storage/notes_pdf/module_{ i + 1 } .pdf'
35
- output_folder = f'images/Notes_images'
33
+ output_folder = f'images/Notes_images/module_ { i + 1 } '
36
34
37
35
# Convert the PDF to images and save them in the output folder
38
36
image_paths , noImg = pdf_to_images (pdf_path , output_folder )
39
- os .environ ['GOOGLE_APPLICATION_CREDENTIALS' ] = 'Files\Client_file_vision .json'
37
+ os .environ ['GOOGLE_APPLICATION_CREDENTIALS' ] = 'Files/client_file_vision .json'
40
38
client = vision .ImageAnnotatorClient ()
41
39
42
40
# [START vision_python_migration_text_detection]
@@ -53,13 +51,14 @@ def NotesToText_handler():
53
51
image_contents += text .replace (substring_to_remove , "" )
54
52
55
53
56
- output_file = f"Local_Storage/notes_txt/module { i + 1 } .txt"
54
+ output_file = f"Local_Storage/notes_txt/module_ { i + 1 } .txt"
57
55
# Write the text content to the output file
58
56
with open (output_file , "w" ) as file :
59
57
file .write (image_contents )
60
-
58
+ print (f"module-{ i + 1 } completed" )
59
+
61
60
if response .error .message :
62
- raise Exception (
61
+ raise Exception (
63
62
'{}\n For more info on error messages, check: '
64
63
'https://cloud.google.com/apis/design/errors' .format (
65
64
response .error .message ))
0 commit comments