draw-bounding-box.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from __future__ import annotations
  2. import sys
  3. from datetime import datetime
  4. from pathlib import Path
  5. import cv2
  6. _REPOSITORY_ROOT_DIRECTORY = Path(__file__).resolve().parent.parent
  7. _FULL_SCREEN_SCREENSHOT_PNG_FILE_PATH = _REPOSITORY_ROOT_DIRECTORY / "output" / "screenshot.png"
  8. _BOUNDING_BOX_DRAWING_OUTPUT_PARENT_DIRECTORY_PATH = _REPOSITORY_ROOT_DIRECTORY / "output"
  9. def draw_bounding_box_xywh_on_current_screenshot_png_and_save_timestamped_png_file(
  10. bounding_box_left_top_width_height_integer_tuple: tuple[int, int, int, int],
  11. ) -> Path:
  12. bounding_box_left_integer = int(bounding_box_left_top_width_height_integer_tuple[0])
  13. bounding_box_top_integer = int(bounding_box_left_top_width_height_integer_tuple[1])
  14. bounding_box_width_integer = int(bounding_box_left_top_width_height_integer_tuple[2])
  15. bounding_box_height_integer = int(bounding_box_left_top_width_height_integer_tuple[3])
  16. screenshot_bgr_numpy_array = cv2.imread(
  17. str(_FULL_SCREEN_SCREENSHOT_PNG_FILE_PATH),
  18. cv2.IMREAD_COLOR,
  19. )
  20. output_image_bgr_numpy_array = screenshot_bgr_numpy_array.copy()
  21. rectangle_top_left_corner_x_integer = bounding_box_left_integer
  22. rectangle_top_left_corner_y_integer = bounding_box_top_integer
  23. rectangle_bottom_right_corner_x_integer = (
  24. bounding_box_left_integer + bounding_box_width_integer
  25. )
  26. rectangle_bottom_right_corner_y_integer = (
  27. bounding_box_top_integer + bounding_box_height_integer
  28. )
  29. green_outline_color_bgr_tuple = (0, 255, 0)
  30. rectangle_outline_thickness_pixels_integer = 2
  31. cv2.rectangle(
  32. output_image_bgr_numpy_array,
  33. (rectangle_top_left_corner_x_integer, rectangle_top_left_corner_y_integer),
  34. (rectangle_bottom_right_corner_x_integer, rectangle_bottom_right_corner_y_integer),
  35. green_outline_color_bgr_tuple,
  36. rectangle_outline_thickness_pixels_integer,
  37. )
  38. timestamp_string_for_output_filename = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
  39. bounding_box_preview_png_file_path = (
  40. _BOUNDING_BOX_DRAWING_OUTPUT_PARENT_DIRECTORY_PATH
  41. / f"boundingbox-{timestamp_string_for_output_filename}.png"
  42. )
  43. bounding_box_preview_png_file_path.parent.mkdir(parents=True, exist_ok=True)
  44. cv2.imwrite(str(bounding_box_preview_png_file_path), output_image_bgr_numpy_array)
  45. return bounding_box_preview_png_file_path
  46. def start(command_line_argument_string_list: list[str] | None = None) -> int:
  47. command_line_arguments = (
  48. command_line_argument_string_list
  49. if command_line_argument_string_list is not None
  50. else sys.argv[1:]
  51. )
  52. bounding_box_left_integer = int(command_line_arguments[0].strip())
  53. bounding_box_top_integer = int(command_line_arguments[1].strip())
  54. bounding_box_width_integer = int(command_line_arguments[2].strip())
  55. bounding_box_height_integer = int(command_line_arguments[3].strip())
  56. draw_bounding_box_xywh_on_current_screenshot_png_and_save_timestamped_png_file(
  57. (
  58. bounding_box_left_integer,
  59. bounding_box_top_integer,
  60. bounding_box_width_integer,
  61. bounding_box_height_integer,
  62. ),
  63. )
  64. return 0
  65. def main() -> int:
  66. return start()
  67. __all__ = [
  68. "draw_bounding_box_xywh_on_current_screenshot_png_and_save_timestamped_png_file",
  69. "start",
  70. "main",
  71. ]
  72. if __name__ == "__main__":
  73. raise SystemExit(main())