Unit4-GAIA / tools /extract_text_from_image.py
Romain Lembo
Add extract_text_from_image tool and update requirements
f6453f6
raw
history blame
590 Bytes
import pytesseract
from PIL import Image
from langchain_core.tools import tool
@tool
def extract_text_from_image(image_path: str) -> str:
"""
Extract text from an image in using the pytesseract library.
Args:
image_path (str): the path to the image file.
"""
try:
# Open the image
image = Image.open(image_path)
# Extract text from the image
text = pytesseract.image_to_string(image)
return f"Extracted text from image:\n\n{text}"
except Exception as e:
return f"Error extracting text from image: {str(e)}"