Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import easyocr | |
| import re | |
| from PIL import Image | |
| # Load the EasyOCR reader (Hindi + English) | |
| reader = easyocr.Reader(['hi', 'en'], gpu=False) | |
| def extract_info_from_image(image): | |
| result = reader.readtext(image, detail=0) | |
| text = '\n'.join(result) | |
| def extract_data(text): | |
| data = {} | |
| data['क्रमांक'] = re.search(r'क्रमांक\s*[:\-]?\s*JS\s*(\d+)', text) | |
| data['पूरा नाम'] = re.search(r'पूरा नाम\s*[:\-]?\s*(.+)', text) | |
| data['लिंग'] = 'पुरुष' if 'पुरुष' in text and '[x]' in text.split('पुरुष')[0][-3:] else 'महिला' if 'महिला' in text and '[x]' in text.split('महिला')[0][-3:] else 'अन्य' | |
| data['उम्र'] = re.search(r'उम्र\s*[:\-]?\s*(\d+)', text) | |
| data['गांव'] = re.search(r'गांव\s*[:\-]?\s*(.+)', text) | |
| data['पंचायत'] = re.search(r'पंचायत\s*[:\-]?\s*(.+)', text) | |
| data['ब्लॉक'] = re.search(r'ब्लॉक\s*[:\-]?\s*(.+)', text) | |
| data['जिला'] = re.search(r'जिला\s*[:\-]?\s*(.+)', text) | |
| data['पिन कोड'] = re.search(r'पिन कोड\s*[:\-]?\s*(\d+)', text) | |
| data['मोबाइल नंबर'] = re.search(r'मोबाइल नंबर\s*[:\-]?\s*([\d०१२३४५६७८९]+)', text) | |
| preferences = { | |
| "चुनाव के माध्यम से": "चुनाव के माध्यम से", | |
| "PK ऐप चलव खोलकर": "PK ऐप चलव खोलकर", | |
| "संगठन में जिम्मेदारी लेकर": "संगठन में जिम्मेदारी लेकर", | |
| "डिजिटल वालंटियर बनकर": "डिजिटल वालंटियर बनकर" | |
| } | |
| support_methods = [] | |
| for phrase in preferences: | |
| if '[x]' in text.split(phrase)[0][-3:]: | |
| support_methods.append(phrase) | |
| data['सहयोग के प्रकार'] = support_methods | |
| checklist = { | |
| "उद्देश्य बताया": "संस्थापक सदस्य को विस्तार से जन सुराज का उद्देश्य बताया", | |
| "कार्य और अधिकार बताए": "सदस्य को बताया गया कि उसके कार्य और अधिकारों से अवगत कराया", | |
| "कॉल बताया": "सदस्य को सहयोग के लिए संपर्क नंबर" | |
| } | |
| checklist_results = {} | |
| for key, phrase in checklist.items(): | |
| match = re.search(rf'{re.escape(phrase)}\?\s*\[([x ])\]', text) | |
| checklist_results[key] = 'हाँ' if match and match.group(1).strip() == 'x' else 'नहीं' | |
| data['चेकलिस्ट'] = checklist_results | |
| for k, v in data.items(): | |
| if isinstance(v, re.Match): | |
| data[k] = v.group(1).strip() | |
| return data | |
| return extract_data(text) | |
| iface = gr.Interface( | |
| fn=extract_info_from_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="json", | |
| title="जन सुराज सदस्यता OCR Extractor", | |
| description="Upload a scanned जन सुराज form and extract structured information." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |