Skip to content

Commit ffa0305

Browse files
fast api added
1 parent 8c4601a commit ffa0305

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

Backend/NotesToText.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,42 @@ def pdf_to_images(pdf_path, output_folder):
2727

2828
substring_to_remove = "Scanned by CamScanner"
2929

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-
'{}\nFor 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+
'{}\nFor more info on error messages, check: '
64+
'https://cloud.google.com/apis/design/errors'.format(
65+
response.error.message))
6466

6567

6668

0 commit comments

Comments
 (0)