| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- from __future__ import annotations
- import sys
- from datetime import datetime
- from pathlib import Path
- import cv2
- _REPOSITORY_ROOT_DIRECTORY = Path(__file__).resolve().parent.parent
- _FULL_SCREEN_SCREENSHOT_PNG_FILE_PATH = _REPOSITORY_ROOT_DIRECTORY / "output" / "screenshot.png"
- _BOUNDING_BOX_DRAWING_OUTPUT_PARENT_DIRECTORY_PATH = _REPOSITORY_ROOT_DIRECTORY / "output"
- def draw_bounding_box_xywh_on_current_screenshot_png_and_save_timestamped_png_file(
- bounding_box_left_top_width_height_integer_tuple: tuple[int, int, int, int],
- ) -> Path:
- bounding_box_left_integer = int(bounding_box_left_top_width_height_integer_tuple[0])
- bounding_box_top_integer = int(bounding_box_left_top_width_height_integer_tuple[1])
- bounding_box_width_integer = int(bounding_box_left_top_width_height_integer_tuple[2])
- bounding_box_height_integer = int(bounding_box_left_top_width_height_integer_tuple[3])
- screenshot_bgr_numpy_array = cv2.imread(
- str(_FULL_SCREEN_SCREENSHOT_PNG_FILE_PATH),
- cv2.IMREAD_COLOR,
- )
- output_image_bgr_numpy_array = screenshot_bgr_numpy_array.copy()
- rectangle_top_left_corner_x_integer = bounding_box_left_integer
- rectangle_top_left_corner_y_integer = bounding_box_top_integer
- rectangle_bottom_right_corner_x_integer = (
- bounding_box_left_integer + bounding_box_width_integer
- )
- rectangle_bottom_right_corner_y_integer = (
- bounding_box_top_integer + bounding_box_height_integer
- )
- green_outline_color_bgr_tuple = (0, 255, 0)
- rectangle_outline_thickness_pixels_integer = 2
- cv2.rectangle(
- output_image_bgr_numpy_array,
- (rectangle_top_left_corner_x_integer, rectangle_top_left_corner_y_integer),
- (rectangle_bottom_right_corner_x_integer, rectangle_bottom_right_corner_y_integer),
- green_outline_color_bgr_tuple,
- rectangle_outline_thickness_pixels_integer,
- )
- timestamp_string_for_output_filename = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
- bounding_box_preview_png_file_path = (
- _BOUNDING_BOX_DRAWING_OUTPUT_PARENT_DIRECTORY_PATH
- / f"boundingbox-{timestamp_string_for_output_filename}.png"
- )
- bounding_box_preview_png_file_path.parent.mkdir(parents=True, exist_ok=True)
- cv2.imwrite(str(bounding_box_preview_png_file_path), output_image_bgr_numpy_array)
- return bounding_box_preview_png_file_path
- def start(command_line_argument_string_list: list[str] | None = None) -> int:
- command_line_arguments = (
- command_line_argument_string_list
- if command_line_argument_string_list is not None
- else sys.argv[1:]
- )
- bounding_box_left_integer = int(command_line_arguments[0].strip())
- bounding_box_top_integer = int(command_line_arguments[1].strip())
- bounding_box_width_integer = int(command_line_arguments[2].strip())
- bounding_box_height_integer = int(command_line_arguments[3].strip())
- draw_bounding_box_xywh_on_current_screenshot_png_and_save_timestamped_png_file(
- (
- bounding_box_left_integer,
- bounding_box_top_integer,
- bounding_box_width_integer,
- bounding_box_height_integer,
- ),
- )
- return 0
- def main() -> int:
- return start()
- __all__ = [
- "draw_bounding_box_xywh_on_current_screenshot_png_and_save_timestamped_png_file",
- "start",
- "main",
- ]
- if __name__ == "__main__":
- raise SystemExit(main())
|