Skip to content

Commit b8ef937

Browse files
committed
fix(ScreenshotScraper): impose dynamic imports
1 parent 08afc92 commit b8ef937

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

scrapegraphai/utils/screenshot_scraping/screenshot_preparation.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from io import BytesIO
66
from PIL import Image, ImageGrab
77
from playwright.async_api import async_playwright
8-
import cv2 as cv
98
import numpy as np
109
from io import BytesIO
1110

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

44+
try:
45+
import cv2 as cv
46+
except ImportError:
47+
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")
48+
49+
4550
fullscreen_screenshot = ImageGrab.grab()
4651
dw, dh = fullscreen_screenshot.size
4752

@@ -116,8 +121,12 @@ def select_area_with_ipywidget(image):
116121

117122
import matplotlib.pyplot as plt
118123
import numpy as np
119-
from ipywidgets import interact, IntSlider
120-
import ipywidgets as widgets
124+
try:
125+
from ipywidgets import interact, IntSlider
126+
import ipywidgets as widgets
127+
except:
128+
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")
129+
121130
from PIL import Image
122131

123132
img_array = np.array(image)

scrapegraphai/utils/screenshot_scraping/text_detection.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
"""
22
text_detection_module
33
"""
4-
from surya.ocr import run_ocr
5-
from surya.model.detection.model import (load_model as load_det_model,
6-
load_processor as load_det_processor)
7-
from surya.model.recognition.model import load_model as load_rec_model
8-
from surya.model.recognition.processor import load_processor as load_rec_processor
94

105

116
def detect_text(image, languages: list = ["en"]):
127
"""
13-
Detects and extracts text from a given image.
14-
Parameters:
15-
image (PIL Image): The input image to extract text from.
16-
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
17-
Returns:
18-
str: The extracted text from the image.
19-
Notes:
20-
Model weights will automatically download the first time you run this function.
21-
"""
8+
Detects and extracts text from a given image.
9+
Parameters:
10+
image (PIL Image): The input image to extract text from.
11+
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
12+
Returns:
13+
str: The extracted text from the image.
14+
Notes:
15+
Model weights will automatically download the first time you run this function.
16+
"""
17+
18+
try:
19+
from surya.ocr import run_ocr
20+
from surya.model.detection.model import (load_model as load_det_model,
21+
load_processor as load_det_processor)
22+
from surya.model.recognition.model import load_model as load_rec_model
23+
from surya.model.recognition.processor import load_processor as load_rec_processor
24+
except:
25+
raise ImportError("The dependencies for screenshot scraping are not installed. Please install them using `pip install scrapegraphai[screenshot_scraper]`.")
26+
2227

2328
langs = languages
2429
det_processor, det_model = load_det_processor(), load_det_model()

0 commit comments

Comments
 (0)