@@ -27,40 +27,42 @@ def pdf_to_images(pdf_path, output_folder):
27
27
28
28
substring_to_remove = "Scanned by CamScanner"
29
29
30
- for i in range (1 ):
31
- pdf_path = f'Local_Storage/notes_pdf/module_{ i + 1 } .pdf'
32
- output_folder = f'images/Notes_images'
33
-
34
- # Convert the PDF to images and save them in the output folder
35
- image_paths , noImg = pdf_to_images (pdf_path , output_folder )
36
- os .environ ['GOOGLE_APPLICATION_CREDENTIALS' ] = 'Files\Client_file_vision.json'
37
- client = vision .ImageAnnotatorClient ()
38
-
39
- # [START vision_python_migration_text_detection]
40
- image_contents = " "
41
-
42
- for j in range (noImg ):
43
- image_path = f'images/Notes_images/Module_{ i + 1 } /page_{ j + 1 } .jpeg'
44
- with open (image_path , 'rb' ) as image_file :
45
- content = image_file .read ()
46
- image = vision .Image (content = content )
47
- response = client .text_detection (image = image )
48
- texts = response .text_annotations [0 ]
49
- text = str (texts .description )
50
- image_contents += text .replace (substring_to_remove , "" )
51
-
52
-
53
- output_file = f"Local_Storage/notes_txt/module{ i + 1 } .txt"
54
-
55
- # Write the text content to the output file
56
- with open (output_file , "w" ) as file :
57
- file .write (image_contents )
58
-
59
- if response .error .message :
60
- raise Exception (
61
- '{}\n For more info on error messages, check: '
62
- 'https://cloud.google.com/apis/design/errors' .format (
63
- response .error .message ))
30
+ @router .get ("/NotesToText" )
31
+ def NotesToText_handler ():
32
+ for i in range (1 ):
33
+ print (f"converting module-{ i + 1 } ...." )
34
+ pdf_path = f'Local_Storage/notes_pdf/module_{ i + 1 } .pdf'
35
+ output_folder = f'images/Notes_images'
36
+
37
+ # Convert the PDF to images and save them in the output folder
38
+ image_paths , noImg = pdf_to_images (pdf_path , output_folder )
39
+ os .environ ['GOOGLE_APPLICATION_CREDENTIALS' ] = 'Files\Client_file_vision.json'
40
+ client = vision .ImageAnnotatorClient ()
41
+
42
+ # [START vision_python_migration_text_detection]
43
+ image_contents = " "
44
+
45
+ for j in range (noImg ):
46
+ image_path = f'images/Notes_images/Module_{ i + 1 } /page_{ j + 1 } .jpeg'
47
+ with open (image_path , 'rb' ) as image_file :
48
+ content = image_file .read ()
49
+ image = vision .Image (content = content )
50
+ response = client .text_detection (image = image )
51
+ texts = response .text_annotations [0 ]
52
+ text = str (texts .description )
53
+ image_contents += text .replace (substring_to_remove , "" )
54
+
55
+
56
+ output_file = f"Local_Storage/notes_txt/module{ i + 1 } .txt"
57
+ # Write the text content to the output file
58
+ with open (output_file , "w" ) as file :
59
+ file .write (image_contents )
60
+
61
+ if response .error .message :
62
+ raise Exception (
63
+ '{}\n For more info on error messages, check: '
64
+ 'https://cloud.google.com/apis/design/errors' .format (
65
+ response .error .message ))
64
66
65
67
66
68
0 commit comments