import gradio as gr import os # from pdf2image import convert_from_path from pypdf import PdfReader import cv2 import pytesseract import numpy as np import easyocr import re import json import shutil def fix_text(data): data1 = data.replace('?', ':') data1 = data1.replace('!', ':') data1 = data1.replace('#', ':') data1 = data1.replace('‘', '') data1 = data1.replace('Q', '9') return data1 def extract_pdf_to_image(pdf_path, split_pdf_dir): sub_dir = os.path.join( split_pdf_dir, os.path.basename(pdf_path).split('.')[0]) if not os.path.exists(sub_dir): os.mkdir(sub_dir) # print(sub_dir, pdf_path) reader = PdfReader(pdf_path) i = 0 for page in reader.pages: for image in page.images: print("loading ...") # print(image.name) # Use f-string for filename formatting filename = f"image_{i:02d}.png" file_path = os.path.join(sub_dir, filename) with open(file_path, "wb") as fp: fp.write(image.data) i += 1 def extract_info_from_image(root_dir): tmp_root = os.listdir(root_dir) for tmp_dir in tmp_root: sub_dir = os.path.join(root_dir, tmp_dir) if os.path.isdir(sub_dir): datalist = [] serial = 1 output_file = os.path.join( root_dir, os.path.basename(sub_dir) + '.json') tmp_images = os.listdir(sub_dir) num_images = len(tmp_images) title = [] for num in range(num_images): tmp_image = tmp_images[num] # for num, tmp_image in enumerate(tmp_images) if num == 0: image_path = os.path.join(sub_dir, tmp_image) # print(image_path) image = cv2.imread(image_path, 0) image = cv2.resize(image, (1241, 1753)) def easy(img, allowlist): reader = easyocr.Reader(["en"], gpu=True) results = reader.readtext( img, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist=allowlist, ) return results allowlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789-()" allowlistnum = "0123456789" allowlet = "ABCDEFGHIJKLMNOPQUSTUVWXYZ" sub = image[21:73, 100:1100] title = easy(sub, allowlist) if not title: sub1 = image[150:188, 590:862] subexpand1 = np.ones((38 * 3, 272 * 3), dtype=np.uint8) * 255 subexpand1[38:38*2, 272:272*2] = sub1 sub2 = image[152:184, 1133:1171] subexpand2 = np.ones((32 * 3, 38 * 3), dtype=np.uint8) * 255 subexpand2[32:32*2, 38:38*2] = sub2 sub3 = image[124:130, 1089:1131] subexpand3 = np.ones((6 * 3, 42 * 3), dtype=np.uint8) * 255 subexpand3[6:6*2, 42:42*2] = sub3 sub4 = image[228:251, 30:182] sub5 = image[326:358, 225:492] sub6 = image[1052:1089, 31:312] sub7 = image[1358:1386, 83:110] sub8 = image[1345:1386, 181:286] subexpand8 = np.ones((41 * 3, 105 * 3), dtype=np.uint8) * 255 subexpand8[41:41*2, 105:105*2] = sub8 sub9 = image[1345:1386, 355:485] sub10 = image[1350:1386, 588:693] sub11 = image[1349:1388, 827:923] sub12 = image[708:731, 859:1003] sub13 = image[733:756, 859:1003] sub14 = image[658:682, 859:1113] sub15 = image[757:783, 859:1113] sub16 = image[785:809, 860:953] else: sub1 = image[88:125, 750:939] subexpand1 = np.ones((37 * 3, 189 * 3), dtype=np.uint8) * 255 subexpand1[37:37*2, 189:189*2] = sub1 sub2 = image[107:130, 1022:1063] subexpand2 = np.ones((23 * 3, 41 * 3), dtype=np.uint8) * 255 subexpand2[23:23*2, 41:41*2] = sub2 sub3 = image[182:202, 352:553] subexpand3 = np.ones((20 * 3, 201 * 3), dtype=np.uint8) * 255 subexpand3[20:20*2, 201:201*2] = sub3 sub5 = image[270:304, 304:510] sub6 = image[1040:1070, 62:243] sub7 = image[1278:1302, 86:173] sub8 = image[1278:1302, 266:375] subexpand8 = np.ones((24 * 3, 109 * 3), dtype=np.uint8) * 255 subexpand8[24:24*2, 109:109*2] = sub8 sub9 = image[1276:1298, 486:570] sub10 = image[1276:1298, 679:745] sub11 = image[1277:1299, 860:915] sub12 = image[593:612, 825:983] sub13 = image[632:650, 825:983] sub14 = image[572:592, 825:983] sub15 = image[651:669, 825:983] sub16 = image[669:690, 825:983] name = easy(subexpand1, allowlist) if not name: # Check if the list is empty name = ["xx"] part = easy(subexpand2, allowlistnum) if not part: # Check if the list is empty part = ["1"] name1 = easy(subexpand3, allowlist) if not name1: # Check if the list is empty name1 = ["1"] year = easy(sub5, allowlistnum) if not year: # Check if the list is empty year = ["xx"] station_name = easy(sub6, allowlist) if not station_name: # Check if the list is empty station_name = ["xx"] station_type = "GENERAL" start = easy(sub7, allowlistnum) end = easy(sub8, allowlistnum) if not end: # Check if the list is empty end = ["xx"] male = easy(sub9, allowlistnum) if not male: # Check if the list is empty male = ["xx"] female = easy(sub10, allowlistnum) if not female: # Check if the list is empty female = ["xx"] third = easy(sub11, allowlistnum) police = easy(sub12, allowlet) if not police: # Check if the list is empty police = ["xx"] revenue = easy(sub13, allowlet) if not revenue: # Check if the list is empty revenue = ["xx"] main = easy(sub14, allowlet) if not main: # Check if the list is empty main = ["xx"] district = easy(sub15, allowlet) if not district: # Check if the list is empty district = ["xx"] pin = easy(sub16, allowlistnum) if not pin: # Check if the list is empty pin = ["xx"] # Define a list of variables data = { "name_assembly_c": "1 -"+name[0], "part_no": part[0], "name_parliamentary_c": name1[0], "year_revision": year[0], "polling_station_name": station_name[0], "polling_station_type": station_type, "starting_no": "1", "ending_no": end[0], "male": male[0], "female": female[0], "third": "0", "police_station": police[0], "revenue_division": revenue[0], "main_town": main[0], "district": district[0], "pin_code": pin[0] } datalist.append(data) with open(output_file, 'w') as f: json.dump(datalist, f, indent=2) os.remove(image_path) if num == 2: if not title: image_path = os.path.join(sub_dir, tmp_image) img = cv2.imread(image_path, 0) image = cv2.resize(img, (1239, 1754)) rectangles = [ (31, 61, 382, 150), (427, 61, 382, 150), (823, 61, 382, 150), (31, 226, 382, 150), (427, 226, 382, 150), (823, 226, 382, 150), (31, 391, 382, 150), (427, 391, 382, 150), (823, 391, 382, 150), (31, 556, 382, 150), (427, 556, 382, 150), (823, 556, 382, 150), (31, 721, 382, 150), (427, 721, 382, 150), (823, 721, 382, 150), (31, 886, 382, 150), (427, 886, 382, 150), (823, 886, 382, 150), (31, 1051, 382, 150), (427, 1051, 382, 150), (823, 1051, 382, 150), (31, 1216, 382, 150), (427, 1216, 382, 150), (823, 1216, 382, 150), (31, 1381, 382, 150), (427, 1381, 382, 150), (823, 1381, 382, 150), (31, 1546, 382, 150), (427, 1546, 382, 150), (823, 1546, 382, 150), ] else: image_path = os.path.join(sub_dir, tmp_image) img = cv2.imread(image_path, 0) image = cv2.resize(img, (1239, 1754)) rectangles = [ (54, 105, 371, 140), (431, 105, 371, 140), (808, 105, 371, 140), (54, 255, 371, 140), (431, 255, 371, 140), (808, 255, 371, 140), (54, 404, 371, 140), (431, 404, 371, 140), (808, 404, 371, 140), (54, 554, 371, 140), (431, 554, 371, 140), (808, 554, 371, 140), (54, 703, 371, 140), (431, 703, 371, 140), (808, 703, 371, 140), (54, 853, 371, 140), (431, 853, 371, 140), (808, 853, 371, 140), (54, 1002, 371, 140), (431, 1002, 371, 140), (808, 1002, 371, 140), (54, 1151, 371, 140), (431, 1151, 371, 140), (808, 1151, 371, 140), (54, 1301, 371, 140), (431, 1301, 371, 140), (808, 1301, 371, 140), (54, 1451, 371, 140), (431, 1451, 371, 140), (808, 1451, 371, 140), ] crop_img = [] for subnum, (x, y, wid, hei) in enumerate(rectangles): crop = image[y:y+hei, x:x+wid] crop_img.append(crop) # cv2.imshow("ss", crop) subcrop_1 = crop[2:20, 265:368] subcrop_2 = crop[22:37, 52:256] subcrop_3 = crop[38:56, 2:261] subcrop_4 = crop[56:71, 2:263] subcrop_5 = crop[73:89, 2:263] # subcrop_6 = crop[5:25, 5:128] wid1 = subcrop_1.shape[1] hei1 = subcrop_1.shape[0] # channel1 = subcrop_1.shape[2] wid2 = subcrop_2.shape[1] hei2 = subcrop_2.shape[0] # channel2 = subcrop_2.shape[2] wid3 = subcrop_3.shape[1] hei3 = subcrop_3.shape[0] # channel3 = subcrop_3.shape[2] wid4 = subcrop_4.shape[1] hei4 = subcrop_4.shape[0] # channel4 = subcrop_4.shape[2] wid5 = subcrop_5.shape[1] hei5 = subcrop_5.shape[0] # channel5 = subcrop_5.shape[2] expand1 = np.ones((hei1 * 3, wid1 * 3), dtype=np.uint8) * 255 expand1[hei1:hei1*2, wid1:wid1*2] = subcrop_1 expand2 = np.ones((hei2 * 3, wid2 * 3), dtype=np.uint8) * 255 expand2[hei2:hei2*2, wid2:wid2*2] = subcrop_2 expand3 = np.ones((hei3 * 3, wid3 * 3), dtype=np.uint8) * 255 expand3[hei3:hei3*2, wid3:wid3*2] = subcrop_3 expand4 = np.ones((hei4 * 3, wid4 * 3), dtype=np.uint8) * 255 expand4[hei4:hei4*2, wid4:wid4*2] = subcrop_4 expand5 = np.ones((hei5 * 3, wid5 * 3), dtype=np.uint8) * 255 expand5[hei5:hei5*2, wid5:wid5*2] = subcrop_5 reader = easyocr.Reader(["en"], gpu=True) results_id = reader.readtext( expand1, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890", # rotation_info=[90], ) results = reader.readtext( expand2, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqustuvwxyz ", # rotation_info=[90], ) results1 = reader.readtext( expand3, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz: ", # rotation_info=[90], ) if not results1: results1 = ["xx"] results2 = reader.readtext( expand4, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="House Number1234567890:-/ABH.NOhno ", # rotation_info=[90], ) if not results2: results2 = ["xx"] results3 = reader.readtext( expand5, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="AgeGenderFMalm1234567890: ", # rotation_info=[90], ) if not results3: results3 = ["xx"] fixname = fix_text(results1[0]) parts = fixname.split(':') if len(parts) >= 2: key = parts[0].strip() values = parts[1].strip() else: key = fixname[:12] values = fixname[14:] if key[0] == "H": key = "Husbands name" elif key[0] == "F": key = "Fathers name" elif key[0] == "O": key = "Others name" if "Fe" or "fe" in results3[0]: sex = "Female" else: sex = "Male" for i in range(1, len(results2[0]) + 1): if results2[0][-i] == ' ': break houseno = results2[0][-i:] match = re.search(r'\b\d+\b', results3[0]) if match is not None: age = match.group() else: age = "24" # Iterate through the lists and replace the first element with "xx" if it's None if len(results) != 0 and len(results_id) != 0: id = fix_text(results_id[0]) print(num+1) print(serial) print(id) print(results[0]) print(key) print(values) print(houseno) print(age) print(sex) data = { "page": num+1, "SN": serial, "id": id, "name": results[0], "name2": values, "name2 type": key, "house_no": houseno, "age": age, "sex": sex } datalist.append(data) serial = serial+1 with open(output_file, 'w') as f: json.dump(datalist, f, indent=2) os.remove(image_path) if num > 2 and num < (num_images - 2): if not title: image_path = os.path.join(sub_dir, tmp_image) img = cv2.imread(image_path, 0) image = cv2.resize(img, (1239, 1754)) rectangles = [ (31, 61, 382, 150), (427, 61, 382, 150), (823, 61, 382, 150), (31, 226, 382, 150), (427, 226, 382, 150), (823, 226, 382, 150), (31, 391, 382, 150), (427, 391, 382, 150), (823, 391, 382, 150), (31, 556, 382, 150), (427, 556, 382, 150), (823, 556, 382, 150), (31, 721, 382, 150), (427, 721, 382, 150), (823, 721, 382, 150), (31, 886, 382, 150), (427, 886, 382, 150), (823, 886, 382, 150), (31, 1051, 382, 150), (427, 1051, 382, 150), (823, 1051, 382, 150), (31, 1216, 382, 150), (427, 1216, 382, 150), (823, 1216, 382, 150), (31, 1381, 382, 150), (427, 1381, 382, 150), (823, 1381, 382, 150), (31, 1546, 382, 150), (427, 1546, 382, 150), (823, 1546, 382, 150), ] else: print(title) image_path = os.path.join(sub_dir, tmp_image) img = cv2.imread(image_path, 0) image = cv2.resize(img, (1241, 1753)) rectangles = [ (54, 87, 371, 140), (431, 87, 371, 140), (808, 87, 371, 140), (54, 236, 371, 140), (431, 236, 371, 140), (808, 236, 371, 140), (54, 386, 371, 140), (431, 386, 371, 140), (808, 386, 371, 140), (54, 536, 371, 140), (431, 536, 371, 140), (808, 536, 371, 140), (54, 685, 371, 140), (431, 685, 371, 140), (808, 685, 371, 140), (54, 835, 371, 140), (431, 835, 371, 140), (808, 835, 371, 140), (54, 984, 371, 140), (431, 984, 371, 140), (808, 984, 371, 140), (54, 1133, 371, 140), (431, 1133, 371, 140), (808, 1133, 371, 140), (54, 1283, 371, 140), (431, 1283, 371, 140), (808, 1283, 371, 140), (54, 1433, 371, 140), (431, 1433, 371, 140), (808, 1433, 371, 140), ] crop_img = [] for subnum, (x, y, wid, hei) in enumerate(rectangles): crop = image[y:y+hei, x:x+wid] crop_img.append(crop) # cv2.imshow("ss", crop) subcrop_1 = crop[2:20, 265:368] subcrop_2 = crop[22:37, 52:256] subcrop_3 = crop[38:56, 2:261] subcrop_4 = crop[56:71, 2:263] subcrop_5 = crop[73:89, 2:263] # subcrop_6 = crop[5:25, 5:128] wid1 = subcrop_1.shape[1] hei1 = subcrop_1.shape[0] # channel1 = subcrop_1.shape[2] wid2 = subcrop_2.shape[1] hei2 = subcrop_2.shape[0] # channel2 = subcrop_2.shape[2] wid3 = subcrop_3.shape[1] hei3 = subcrop_3.shape[0] # channel3 = subcrop_3.shape[2] wid4 = subcrop_4.shape[1] hei4 = subcrop_4.shape[0] # channel4 = subcrop_4.shape[2] wid5 = subcrop_5.shape[1] hei5 = subcrop_5.shape[0] # channel5 = subcrop_5.shape[2] expand1 = np.ones((hei1 * 3, wid1 * 3), dtype=np.uint8) * 255 expand1[hei1:hei1*2, wid1:wid1*2] = subcrop_1 expand2 = np.ones((hei2 * 3, wid2 * 3), dtype=np.uint8) * 255 expand2[hei2:hei2*2, wid2:wid2*2] = subcrop_2 expand3 = np.ones((hei3 * 3, wid3 * 3), dtype=np.uint8) * 255 expand3[hei3:hei3*2, wid3:wid3*2] = subcrop_3 expand4 = np.ones((hei4 * 3, wid4 * 3), dtype=np.uint8) * 255 expand4[hei4:hei4*2, wid4:wid4*2] = subcrop_4 expand5 = np.ones((hei5 * 3, wid5 * 3), dtype=np.uint8) * 255 expand5[hei5:hei5*2, wid5:wid5*2] = subcrop_5 reader = easyocr.Reader(["en"], gpu=True) results_id = reader.readtext( expand1, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890", # rotation_info=[90], ) results = reader.readtext( expand2, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqustuvwxyz ", # rotation_info=[90], ) results1 = reader.readtext( expand3, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz: ", # rotation_info=[90], ) if not results1: results1 = ["xx"] results2 = reader.readtext( expand4, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="House Number1234567890:-/ABH.NOhno ", # rotation_info=[90], ) if not results2: results2 = ["xx"] results3 = reader.readtext( expand5, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="AgeGenderFMalm1234567890: ", # rotation_info=[90], ) if not results3: results3 = ["xx"] fixname = fix_text(results1[0]) parts = fixname.split(':') if len(parts) >= 2: key = parts[0].strip() values = parts[1].strip() else: key = fixname[:12] values = fixname[14:] if key[0] == "H": key = "Husbands name" elif key[0] == "F": key = "Fathers name" elif key[0] == "O": key = "Others name" if "Fe" in results3[0]: sex = "Female" else: sex = "Male" for i in range(1, len(results2[0]) + 1): if results2[0][-i] == ' ': break houseno = results2[0][-i:] match = re.search(r'\b\d+\b', results3[0]) if match is not None: age = match.group() else: age = "24" # Iterate through the lists and replace the first element with "xx" if it's None if len(results) != 0 and len(results_id) != 0: id = fix_text(results_id[0]) print(num+1) print(serial) print(id) print(results[0]) print(key) print(values) print(houseno) print(age) print(sex) data = { "page": num+1, "SN": serial, "id": id, "name": results[0], "name2": values, "name2 type": key, "house_no": houseno, "age": age, "sex": sex } datalist.append(data) serial = serial+1 with open(output_file, 'w') as f: json.dump(datalist, f, indent=2) else: break os.remove(image_path) if num == num_images - 2: if not title: image_path = os.path.join(sub_dir, tmp_image) img = cv2.imread(image_path, 0) image = cv2.resize(img, (1239, 1754)) rectangles = [ (31, 61, 382, 150), (427, 61, 382, 150), (823, 61, 382, 150), (31, 226, 382, 150), (427, 226, 382, 150), (823, 226, 382, 150), (31, 391, 382, 150), (427, 391, 382, 150), (823, 391, 382, 150), (31, 556, 382, 150), (427, 556, 382, 150), (823, 556, 382, 150), (31, 721, 382, 150), (427, 721, 382, 150), (823, 721, 382, 150), (31, 886, 382, 150), (427, 886, 382, 150), (823, 886, 382, 150), (31, 1051, 382, 150), (427, 1051, 382, 150), (823, 1051, 382, 150), (31, 1216, 382, 150), (427, 1216, 382, 150), (823, 1216, 382, 150), (31, 1381, 382, 150), (427, 1381, 382, 150), (823, 1381, 382, 150), (31, 1546, 382, 150), (427, 1546, 382, 150), (823, 1546, 382, 150), ] else: print(title) image_path = os.path.join(sub_dir, tmp_image) img = cv2.imread(image_path, 0) image = cv2.resize(img, (1241, 1753)) rectangles = [ (51, 155, 371, 150), (428, 155, 371, 150), (806, 155, 371, 150), (51, 312, 371, 150), (428, 312, 371, 150), (806, 312, 371, 150), (51, 469, 371, 150), (428, 469, 371, 150), (806, 469, 371, 150), (51, 625, 371, 150), (428, 625, 371, 150), (806, 625, 371, 150), (54, 781, 371, 150), (428, 781, 371, 150), (806, 781, 371, 150), (51, 937, 371, 150), (428, 937, 371, 150), (806, 937, 371, 150), (51, 1093, 371, 150), (428, 1093, 371, 150), (806, 1093, 371, 150), (51, 1249, 371, 150), (428, 1249, 371, 150), (806, 1249, 371, 150), (51, 1405, 371, 150), (428, 1405, 371, 150), (806, 1405, 371, 150), (51, 1561, 371, 150), (428, 1561, 371, 150), (806, 1561, 371, 150), ] crop_img = [] for subnum, (x, y, wid, hei) in enumerate(rectangles): crop = image[y:y+hei, x:x+wid] crop_img.append(crop) # cv2.imshow("ss", crop) subcrop_1 = crop[2:20, 265:368] subcrop_2 = crop[29:47, 52:256] subcrop_3 = crop[47:64, 2:261] subcrop_4 = crop[64:83, 2:263] subcrop_5 = crop[83:95, 2:263] # subcrop_6 = crop[5:25, 5:128] wid1 = subcrop_1.shape[1] hei1 = subcrop_1.shape[0] # channel1 = subcrop_1.shape[2] wid2 = subcrop_2.shape[1] hei2 = subcrop_2.shape[0] # channel2 = subcrop_2.shape[2] wid3 = subcrop_3.shape[1] hei3 = subcrop_3.shape[0] # channel3 = subcrop_3.shape[2] wid4 = subcrop_4.shape[1] hei4 = subcrop_4.shape[0] # channel4 = subcrop_4.shape[2] wid5 = subcrop_5.shape[1] hei5 = subcrop_5.shape[0] # channel5 = subcrop_5.shape[2] expand1 = np.ones((hei1 * 3, wid1 * 3), dtype=np.uint8) * 255 expand1[hei1:hei1*2, wid1:wid1*2] = subcrop_1 expand2 = np.ones((hei2 * 3, wid2 * 3), dtype=np.uint8) * 255 expand2[hei2:hei2*2, wid2:wid2*2] = subcrop_2 expand3 = np.ones((hei3 * 3, wid3 * 3), dtype=np.uint8) * 255 expand3[hei3:hei3*2, wid3:wid3*2] = subcrop_3 expand4 = np.ones((hei4 * 3, wid4 * 3), dtype=np.uint8) * 255 expand4[hei4:hei4*2, wid4:wid4*2] = subcrop_4 expand5 = np.ones((hei5 * 3, wid5 * 3), dtype=np.uint8) * 255 expand5[hei5:hei5*2, wid5:wid5*2] = subcrop_5 reader = easyocr.Reader(["en"], gpu=True) results_id = reader.readtext( expand1, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890", # rotation_info=[90], ) results = reader.readtext( expand2, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqustuvwxyz ", # rotation_info=[90], ) results1 = reader.readtext( expand3, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz: ", # rotation_info=[90], ) if not results1: results1 = ["xx"] results2 = reader.readtext( expand4, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="House Number1234567890:-/ABH.NOhno ", # rotation_info=[90], ) if not results2: results2 = ["xx"] results3 = reader.readtext( expand5, decoder="greedy", detail=0, paragraph=True, y_ths=0.1, allowlist="AgeGenderFMalm1234567890: ", # rotation_info=[90], ) if not results3: results3 = ["xx"] fixname = fix_text(results1[0]) parts = fixname.split(':') if len(parts) >= 2: key = parts[0].strip() values = parts[1].strip() else: key = fixname[:12] values = fixname[14:] if key[0] == "H": key = "Husbands name" elif key[0] == "F": key = "Fathers name" elif key[0] == "O": key = "Others name" if "Fe" in results3[0]: sex = "Female" else: sex = "Male" for i in range(1, len(results2[0]) + 1): if results2[0][-i] == ' ': break houseno = results2[0][-i:] match = re.search(r'\b\d+\b', results3[0]) if match is not None: age = match.group() else: age = "24" # Iterate through the lists and replace the first element with "xx" if it's None if len(results) != 0 and len(results_id) != 0: id = fix_text(results_id[0]) print(num+1) print(serial) print(id) print(results[0]) print(key) print(values) print(houseno) print(age) print(sex) data = { "page": num+1, "SN": serial, "id": id, "name": results[0], "name2": values, "name2 type": key, "house_no": houseno, "age": age, "sex": sex } datalist.append(data) serial = serial+1 with open(output_file, 'w') as f: json.dump(datalist, f, indent=2) else: break os.remove(image_path) elif num == 1 or num == num_images-1: image_path = os.path.join(sub_dir, tmp_image) os.remove(image_path) os.rmdir(sub_dir) def _main(pdf_path): split_pdf_dir = 'split' result_dir = '' if not os.path.exists(split_pdf_dir): os.mkdir(split_pdf_dir) # pdf_file = get_single_pdf_path(pdf_path) # for pdf_path in pdf_files: extract_pdf_to_image(pdf_path, split_pdf_dir) extract_info_from_image(split_pdf_dir) def process_pdf_files(file_dir): input_pdf = file_dir # Replace with actual PDF file path _main(input_pdf) output_dir = 'split/' + os.path.basename(input_pdf).split('.')[0] + '.json' return output_dir # re = process_pdf_files('input_folder/example(2).pdf') # re iface = gr.Interface( fn=process_pdf_files, inputs="file", outputs="file", title="PDF to JSON", description="Upload a PDF file for processing." ) iface.launch()