| import easyocr
|
| import cv2
|
| import os
|
|
|
|
|
| def extract_text_from_image(image_path, language='en'):
|
| """
|
| Extracts text from an image using EasyOCR.
|
|
|
| Args:
|
| image_path (str): Path to the image file.
|
| language (str, optional): Language(s) to be recognized. Defaults to 'en' (English).
|
|
|
| Returns:
|
| list: List of recognized text strings.
|
| """
|
|
|
| reader = easyocr.Reader([language])
|
| reader.detector = reader.initDetector('best\BEST.pth')
|
|
|
| image = cv2.imread(image_path)
|
| result = reader.readtext(image, detail=0)
|
|
|
| return result
|
|
|
|
|
| if __name__ == '__main__':
|
|
|
| folder_path = "inference_results\Anil Maheshwari - Data analytics-McGraw-Hill Education (2017)"
|
|
|
|
|
| all_extracted_text = ""
|
|
|
|
|
| for filename in os.listdir(folder_path):
|
| if filename.endswith(".jpg") or filename.endswith(".png"):
|
| image_path = os.path.join(folder_path, filename)
|
|
|
|
|
| extracted_text = extract_text_from_image(image_path)
|
|
|
|
|
| all_extracted_text += "\n".join(extracted_text) + "\n\n"
|
|
|
|
|