Skip to content

Commit 369ee2a

Browse files
committed
Initial Scheenshot tool
1 parent 1c171ac commit 369ee2a

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

dependencies.hspd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ package: SpeechRecognition, version: 3.10.4, mode: ge
5252

5353
/* Video */
5454
package: opencv-python, version: 4.10.0.84, mode: ge
55+
package: pyautogui, version: 0.9.54, mode: ge
5556

5657
/* Pre-Trained and AI */
5758
package: torch, version: 2.2.0, mode: ge

src/demo/others/screenshot_demo.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
import pyautogui
2-
3-
4-
def take_screenshot(file_path):
5-
# Take a screenshot using pyautogui
6-
screenshot = pyautogui.screenshot()
7-
8-
# Check if saving as JPEG and convert to RGB if necessary
9-
if file_path.lower().endswith(".jpg") or file_path.lower().endswith(".jpeg"):
10-
screenshot = screenshot.convert("RGB")
11-
12-
# Save the screenshot to the specified file path
13-
screenshot.save(file_path)
14-
print(f"Screenshot saved at {file_path}")
1+
from askai.core.features.router.tools.vision import take_screenshot
2+
from hspylib.core.tools.commons import sysout
153

4+
from utils import init_context
165

176
if __name__ == "__main__":
18-
take_screenshot("gabiroba.jpeg")
7+
init_context(log_name="camera-demo")
8+
sysout(take_screenshot("gabiroba.jpeg"))

src/demo/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def init_context(
3434
KeyboardInput.preload_history(cache.load_input_history(commands()))
3535
shared.create_engine(engine_name=engine_name, model_name=model_name)
3636
shared.create_context(context_size)
37-
events.reply.subscribe(cb_event_handler=lambda ev: display_text(ev.args.message))
37+
events.reply.subscribe(cb_event_handler=lambda ev: display_text(ev.args.reply))
3838
atexit.register(cache.save_input_history)
3939

4040

src/main/askai/core/askai_messages.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,17 @@ def press_esc_enter(self) -> str:
117117
def device_switch(self, device_info: str) -> str:
118118
return f"\nSwitching to Audio Input device: `{device_info}`\n"
119119

120+
# Debug messages
121+
120122
def photo_captured(self, photo: str) -> str:
121123
return f"~~[DEBUG]~~ WebCam photo captured: `{photo}`"
122124

125+
def screenshot_saved(self, screenshot: str) -> str:
126+
return f"~~[DEBUG]~~ Screenshot saved: `{screenshot}`"
127+
123128
def executing(self, command_line: str) -> str:
124129
return f"~~[DEBUG]~~ Executing: `{command_line}`…"
125130

126-
# Debug messages
127-
128131
def analysis(self, result: str) -> str:
129132
return f"~~[DEBUG]~~ Analysis result => {result}"
130133

src/main/askai/core/features/router/tools/vision.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import os
2+
from textwrap import indent
3+
4+
import pyautogui
5+
import torch
6+
from PIL import Image
17
from askai.core.askai_events import events
28
from askai.core.askai_messages import msg
39
from askai.core.component.cache_service import PICTURE_DIR
@@ -9,13 +15,9 @@
915
from hspylib.core.config.path_object import PathObject
1016
from hspylib.core.enums.enumeration import Enumeration
1117
from hspylib.core.metaclass.classpath import AnyPath
12-
from PIL import Image
13-
from textwrap import indent
18+
from hspylib.core.preconditions import check_argument
1419
from transformers import BlipForConditionalGeneration, BlipProcessor
1520

16-
import os
17-
import torch
18-
1921

2022
class HFModel(Enumeration):
2123
"""Available Hugging Face models"""
@@ -111,3 +113,24 @@ def parse_caption(image_caption: str) -> str:
111113
) # fmt: on
112114

113115
return msg.no_caption()
116+
117+
118+
def take_screenshot(path_name: AnyPath, load_dir: AnyPath | None = None) -> str:
119+
"""Takes a screenshot and saves it to the specified path.
120+
:param path_name: The path where the screenshot will be saved.
121+
:param load_dir: Optional directory to save the screenshot.
122+
:return: The path to the saved screenshot.
123+
"""
124+
125+
posix_path: PathObject = PathObject.of(path_name)
126+
check_argument(os.path.exists(posix_path.abs_dir))
127+
screenshot = pyautogui.screenshot()
128+
_, ext = os.path.splitext(posix_path.filename)
129+
if ext.casefold().endswith((".jpg", ".jpeg")):
130+
screenshot = screenshot.convert("RGB")
131+
final_path: str = os.path.join(load_dir or posix_path.abs_dir or PICTURE_DIR, posix_path.filename)
132+
screenshot.save(final_path)
133+
events.reply.emit(reply=AIReply.full(msg.screenshot_saved(final_path)))
134+
desktop_caption = image_captioner(final_path, load_dir)
135+
136+
return desktop_caption

0 commit comments

Comments
 (0)