Skip to content

Fix ScreenshotScraper dynamic import errors #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scrapegraphai/graphs/smart_scraper_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _create_graph(self) -> BaseGraph:
input="doc",
output=["parsed_doc"],
node_config={
"llm_model": self.llm_model,
"chunk_size": self.model_token
}
)
Expand Down
15 changes: 12 additions & 3 deletions scrapegraphai/utils/screenshot_scraping/screenshot_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from io import BytesIO
from PIL import Image, ImageGrab
from playwright.async_api import async_playwright
import cv2 as cv
import numpy as np
from io import BytesIO

Expand Down Expand Up @@ -42,6 +41,12 @@ def select_area_with_opencv(image):
A tuple containing the LEFT, TOP, RIGHT, and BOTTOM coordinates of the selected area.
"""

try:
import cv2 as cv
except ImportError:
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")


fullscreen_screenshot = ImageGrab.grab()
dw, dh = fullscreen_screenshot.size

Expand Down Expand Up @@ -116,8 +121,12 @@ def select_area_with_ipywidget(image):

import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, IntSlider
import ipywidgets as widgets
try:
from ipywidgets import interact, IntSlider
import ipywidgets as widgets
except:
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")

from PIL import Image

img_array = np.array(image)
Expand Down
33 changes: 19 additions & 14 deletions scrapegraphai/utils/screenshot_scraping/text_detection.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
"""
text_detection_module
"""
from surya.ocr import run_ocr
from surya.model.detection.model import (load_model as load_det_model,
load_processor as load_det_processor)
from surya.model.recognition.model import load_model as load_rec_model
from surya.model.recognition.processor import load_processor as load_rec_processor


def detect_text(image, languages: list = ["en"]):
"""
Detects and extracts text from a given image.
Parameters:
image (PIL Image): The input image to extract text from.
lahguages (list): A list of languages to detect text in. Defaults to ["en"]. List of languages can be found here: https://github.com/VikParuchuri/surya/blob/master/surya/languages.py
Returns:
str: The extracted text from the image.
Notes:
Model weights will automatically download the first time you run this function.
"""
Detects and extracts text from a given image.
Parameters:
image (PIL Image): The input image to extract text from.
lahguages (list): A list of languages to detect text in. Defaults to ["en"]. List of languages can be found here: https://github.com/VikParuchuri/surya/blob/master/surya/languages.py
Returns:
str: The extracted text from the image.
Notes:
Model weights will automatically download the first time you run this function.
"""

try:
from surya.ocr import run_ocr
from surya.model.detection.model import (load_model as load_det_model,
load_processor as load_det_processor)
from surya.model.recognition.model import load_model as load_rec_model
from surya.model.recognition.processor import load_processor as load_rec_processor
except:
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")


langs = languages
det_processor, det_model = load_det_processor(), load_det_model()
Expand Down
Loading