Hi, currently this script performs OCR based on the imagepaths which is great , the file…
- User
Hi, currently this script performs OCR based on the image_paths which is great , the file location of all has C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output which only works for 1_output. I want it to cater to all other folders in the location C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output such as 2_output, 3_output , etc. The format of folder name is fixed is like 1_output, 2_output and so on. The file paths are fixed , the only thing i want changed is the 1_output to have all folders in that path C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output. After running the first one, an excel should be generated as expected. Next, the 2_output folder results and entries should add to the same excel and so on. My code now: import os import azure.ai.vision as sdk import openpyxl from openpyxl.styles import Font, PatternFill from openpyxl.utils import get_column_letter import os
Exported to environment variable, safer and more secure for sensitive information like APIs
VISION_ENDPOINT = os.environ.get("VISION_ENDPOINT") VISION_KEY = os.environ.get("VISION_KEY")
#to set: setx VISION_KEY
if VISION_ENDPOINT is None or VISION_KEY is None: raise ValueError("VISION_ENDPOINT and VISION_KEY must be set in the environment")
service_options = sdk.VisionServiceOptions(VISION_ENDPOINT, VISION_KEY)
Specify the paths to your local image files
image_paths = [ r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\01_lead_number.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\02_company.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\03_name.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\07_industry.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\08_interest.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\11_preparedBy.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\06_category_resized.jpg\06_category_resized.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\09_product_resized.jpg\09_product_resized.jpg', r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\1_output\05_nameCards.jpg' ]
Specify corresponding headers
headers = ["Lead Number", "Company", "Name", "Industry", "Interest", "Prepared by","Category","Product", "Etc"]
Initialize the Excel workbook and sheet
excel_file_path = r'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\test.xlsx' wb = openpyxl.Workbook() sheet = wb.active
Write headers to the Excel sheet and apply formatting
sheet.append(headers)
Header formatting
for cell in sheet[1]: cell.font = Font(bold=True) cell.fill = PatternFill(start_color="DDDDDD", end_color="DDDDDD", fill_type="solid")
Initialize a list to store the extracted information
extracted_info_list = []
Iterate through each image path and extract information using OCR
- Assistant
- User
strictly use this formulae to do: # Iterate through each folder for output_folder_number in range(1, num_folders + 1):
code# Specify the paths to your local image files for the current folder image_paths = [ rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\01_lead_number.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\02_company.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\03_name.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\07_industry.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\08_interest.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\11_preparedBy.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\06_category_resized.jpg\06_category_resized.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\09_product_resized.jpg\09_product_resized.jpg', rf'C:\Users\ADAM7\Desktop\Projects_demo\03_ocr\output\{output_folder_number}_output\05_nameCards.jpg' ] - Assistant