Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Chinese language, Indian companies hit as US provides 32 entities to commerce blacklist

    September 13, 2025

    High 5 fish present in Karachi’s waters

    September 13, 2025

    Solana Enters High 5 Cryptos With $126B Market Cap, Galaxy Digital Fuels Rally

    September 13, 2025
    Facebook X (Twitter) Instagram
    Saturday, September 13
    Trending
    • Chinese language, Indian companies hit as US provides 32 entities to commerce blacklist
    • High 5 fish present in Karachi’s waters
    • Solana Enters High 5 Cryptos With $126B Market Cap, Galaxy Digital Fuels Rally
    • Each Gears of Warfare Recreation Ever Launched, Ranked
    • Cadet School Pano Aqil Jobs 2025 For Principal Newest Commercial
    • The Alpha Change
    • 12 troopers martyred, 13 terrorists killed throughout hearth trade in KP’s South Waziristan: ISPR – Pakistan
    • Feelings flare as Pakistan, India face off after four-day navy clashes in Could
    • AI tech identifies Central Okanagan properties and unsafe materials of their bins – Okanagan
    • Petrol costs in Pakistan might rise from September 16
    Facebook X (Twitter) Instagram Pinterest Vimeo
    The News92The News92
    • Home
    • World
    • National
    • Sports
    • Crypto
    • Travel
    • Lifestyle
    • Jobs
    • Insurance
    • Gaming
    • AI & Tech
    • Health & Fitness
    The News92The News92
    Home»AI & Tech»Easy methods to Construct a Multilingual OCR AI Agent in Python with EasyOCR and OpenCV
    AI & Tech

    Easy methods to Construct a Multilingual OCR AI Agent in Python with EasyOCR and OpenCV

    Naveed AhmadBy Naveed AhmadSeptember 12, 2025No Comments4 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    class AdvancedOCRAgent:
       """
       Superior OCR AI Agent with preprocessing, multi-language help,
       and clever textual content extraction capabilities.
       """
      
       def __init__(self, languages: Listing[str] = ['en'], gpu: bool = True):
           """Initialize OCR agent with specified languages."""
           print("🤖 Initializing Superior OCR Agent...")
           self.languages = languages
           self.reader = easyocr.Reader(languages, gpu=gpu)
           self.confidence_threshold = 0.5
           print(f"✅ OCR Agent prepared! Languages: {languages}")
      
       def upload_image(self) -> Non-compulsory[str]:
           """Add picture file by Colab interface."""
           print("📁 Add your picture file:")
           uploaded = information.add()
           if uploaded:
               filename = listing(uploaded.keys())[0]
               print(f"✅ Uploaded: {filename}")
               return filename
           return None
      
       def preprocess_image(self, picture: np.ndarray, improve: bool = True) -> np.ndarray:
           """Superior picture preprocessing for higher OCR accuracy."""
           if len(picture.form) == 3:
               grey = cv2.cvtColor(picture, cv2.COLOR_BGR2GRAY)
           else:
               grey = picture.copy()
          
           if improve:
               clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
               grey = clahe.apply(grey)
              
               grey = cv2.fastNlMeansDenoising(grey)
              
               kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
               grey = cv2.filter2D(grey, -1, kernel)
          
           binary = cv2.adaptiveThreshold(
               grey, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2
           )
          
           return binary
      
       def extract_text(self, image_path: str, preprocess: bool = True) -> Dict:
           """Extract textual content from picture with superior processing."""
           print(f"🔍 Processing picture: {image_path}")
          
           picture = cv2.imread(image_path)
           if picture is None:
               increase ValueError(f"Couldn't load picture: {image_path}")
          
           if preprocess:
               processed_image = self.preprocess_image(picture)
           else:
               processed_image = picture
          
           outcomes = self.reader.readtext(processed_image)
          
           extracted_data = {
               'raw_results': outcomes,
               'filtered_results': [],
               'full_text': '',
               'confidence_stats': {},
               'word_count': 0,
               'line_count': 0
           }
          
           high_confidence_text = []
           confidences = []
          
           for (bbox, textual content, confidence) in outcomes:
               if confidence >= self.confidence_threshold:
                   extracted_data['filtered_results'].append({
                       'textual content': textual content,
                       'confidence': confidence,
                       'bbox': bbox
                   })
                   high_confidence_text.append(textual content)
                   confidences.append(confidence)
          
           extracted_data['full_text'] = ' '.be part of(high_confidence_text)
           extracted_data['word_count'] = len(extracted_data['full_text'].cut up())
           extracted_data['line_count'] = len(high_confidence_text)
          
           if confidences:
               extracted_data['confidence_stats'] = {
                   'imply': np.imply(confidences),
                   'min': np.min(confidences),
                   'max': np.max(confidences),
                   'std': np.std(confidences)
               }
          
           return extracted_data
      
       def visualize_results(self, image_path: str, outcomes: Dict, show_bbox: bool = True):
           """Visualize OCR outcomes with bounding containers."""
           picture = cv2.imread(image_path)
           image_rgb = cv2.cvtColor(picture, cv2.COLOR_BGR2RGB)
          
           plt.determine(figsize=(15, 10))
          
           if show_bbox:
               plt.subplot(2, 2, 1)
               img_with_boxes = image_rgb.copy()
              
               for merchandise in outcomes['filtered_results']:
                   bbox = np.array(merchandise['bbox']).astype(int)
                   cv2.polylines(img_with_boxes, [bbox], True, (255, 0, 0), 2)
                  
                   x, y = bbox[0]
                   cv2.putText(img_with_boxes, f"{merchandise['confidence']:.2f}",
                              (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
              
               plt.imshow(img_with_boxes)
               plt.title("OCR Outcomes with Bounding Packing containers")
               plt.axis('off')
          
           plt.subplot(2, 2, 2)
           processed = self.preprocess_image(picture)
           plt.imshow(processed, cmap='grey')
           plt.title("Preprocessed Picture")
           plt.axis('off')
          
           plt.subplot(2, 2, 3)
           confidences = [item['confidence'] for merchandise in outcomes['filtered_results']]
           if confidences:
               plt.hist(confidences, bins=20, alpha=0.7, colour="blue")
               plt.xlabel('Confidence Rating')
               plt.ylabel('Frequency')
               plt.title('Confidence Rating Distribution')
               plt.axvline(self.confidence_threshold, colour="purple", linestyle="--",
                          label=f'Threshold: {self.confidence_threshold}')
               plt.legend()
          
           plt.subplot(2, 2, 4)
           stats = outcomes['confidence_stats']
           if stats:
               labels = ['Mean', 'Min', 'Max']
               values = [stats['mean'], stats['min'], stats['max']]
               plt.bar(labels, values, colour=['green', 'red', 'blue'])
               plt.ylabel('Confidence Rating')
               plt.title('Confidence Statistics')
               plt.ylim(0, 1)
          
           plt.tight_layout()
           plt.present()
      
       def smart_text_analysis(self, textual content: str) -> Dict:
           """Carry out clever evaluation of extracted textual content."""
           evaluation = {
               'language_detection': 'unknown',
               'text_type': 'unknown',
               'key_info': {},
               'patterns': []
           }
          
           email_pattern = r'b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}b'
           phone_pattern = r'(+d{1,3}[-.s]?)?(?d{3})?[-.s]?d{3}[-.s]?d{4}'
           url_pattern = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
           date_pattern = r'bd{1,2}[/-]d{1,2}[/-]d{2,4}b'
          
           patterns = {
               'emails': re.findall(email_pattern, textual content, re.IGNORECASE),
               'telephones': re.findall(phone_pattern, textual content),
               'urls': re.findall(url_pattern, textual content, re.IGNORECASE),
               'dates': re.findall(date_pattern, textual content)
           }
          
           evaluation['patterns'] = {okay: v for okay, v in patterns.gadgets() if v}
          
           if any(patterns.values()):
               if patterns.get('emails') or patterns.get('telephones'):
                   evaluation['text_type'] = 'contact_info'
               elif patterns.get('urls'):
                   evaluation['text_type'] = 'web_content'
               elif patterns.get('dates'):
                   evaluation['text_type'] = 'document_with_dates'
          
           if re.search(r'[а-яё]', textual content.decrease()):
               evaluation['language_detection'] = 'russian'
           elif re.search(r'[àáâãäåæçèéêëìíîïñòóôõöøùúûüý]', textual content.decrease()):
               evaluation['language_detection'] = 'romance_language'
           elif re.search(r'[一-龯]', textual content):
               evaluation['language_detection'] = 'chinese language'
           elif re.search(r'[ひらがなカタカナ]', textual content):
               evaluation['language_detection'] = 'japanese'
           elif re.search(r'[a-zA-Z]', textual content):
               evaluation['language_detection'] = 'latin_based'
          
           return evaluation
      
       def process_batch(self, image_folder: str) -> Listing[Dict]:
           """Course of a number of pictures in batch."""
           outcomes = []
           supported_formats = ('.png', '.jpg', '.jpeg', '.bmp', '.tiff')
          
           for filename in os.listdir(image_folder):
               if filename.decrease().endswith(supported_formats):
                   image_path = os.path.be part of(image_folder, filename)
                   attempt:
                       outcome = self.extract_text(image_path)
                       outcome['filename'] = filename
                       outcomes.append(outcome)
                       print(f"✅ Processed: {filename}")
                   besides Exception as e:
                       print(f"❌ Error processing {filename}: {str(e)}")
          
           return outcomes
      
       def export_results(self, outcomes: Dict, format: str="json") -> str:
           """Export leads to specified format."""
           if format.decrease() == 'json':
               output = json.dumps(outcomes, indent=2, ensure_ascii=False)
               filename="ocr_results.json"
           elif format.decrease() == 'txt':
               output = outcomes['full_text']
               filename="extracted_text.txt"
           else:
               increase ValueError("Supported codecs: 'json', 'txt'")
          
           with open(filename, 'w', encoding='utf-8') as f:
               f.write(output)
          
           print(f"📄 Outcomes exported to: {filename}")
           return filename



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleAbu Dhabi launches Golden Meals Product Listing to fast-track imports and minimize prices
    Next Article Maritimes left off Ottawa’s ‘nation-building’ initiatives record for now
    Naveed Ahmad
    • Website

    Related Posts

    AI & Tech

    Google AI Releases VaultGemma: The Largest and Most Succesful Open Mannequin (1B-parameters) Skilled from Scratch with Differential Privateness

    September 13, 2025
    AI & Tech

    We’re coming into a golden age of robotics startups — and never simply due to AI

    September 13, 2025
    AI & Tech

    How an over-the-air replace made Quilt’s warmth pumps extra highly effective

    September 13, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Demo
    Top Posts

    Women cricketers send unity and hope on August 14

    August 14, 20256 Views

    Particular Training Division Punjab Jobs 2025 Present Openings

    August 17, 20253 Views

    Lawyer ‘very assured’ a overseas adversary attacked Canadian diplomats in Cuba – Nationwide

    August 17, 20253 Views
    Stay In Touch
    • Facebook
    • YouTube
    • TikTok
    • WhatsApp
    • Twitter
    • Instagram
    Latest Reviews

    Subscribe to Updates

    Get the latest tech news from FooBar about tech, design and biz.

    Demo
    Most Popular

    Women cricketers send unity and hope on August 14

    August 14, 20256 Views

    Particular Training Division Punjab Jobs 2025 Present Openings

    August 17, 20253 Views

    Lawyer ‘very assured’ a overseas adversary attacked Canadian diplomats in Cuba – Nationwide

    August 17, 20253 Views
    Our Picks

    Chinese language, Indian companies hit as US provides 32 entities to commerce blacklist

    September 13, 2025

    High 5 fish present in Karachi’s waters

    September 13, 2025

    Solana Enters High 5 Cryptos With $126B Market Cap, Galaxy Digital Fuels Rally

    September 13, 2025

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    Facebook X (Twitter) Instagram Pinterest
    • About Us
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    • Advertise
    • Disclaimer
    © 2025 TheNews92.com. All Rights Reserved. Unauthorized reproduction or redistribution of content is strictly prohibited.

    Type above and press Enter to search. Press Esc to cancel.