Skip to content

Commit 880e400

Browse files
committed
Refactor and add screen recorder
Refactor and add screen recorder
1 parent 61aecbc commit 880e400

15 files changed

+183
-114
lines changed

.idea/workspace.xml

+52-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/source/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# -- Project information -----------------------------------------------------
1919

2020
project = 'AutoControl'
21-
copyright = '2020 ~ 2023, JE-Chen'
21+
copyright = '2020 ~ Now, JE-Chen'
2222
author = 'JE-Chen'
2323

2424
# -- General configuration ---------------------------------------------------

je_auto_control/__init__.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
callback_executor
88
# Critical
99
from je_auto_control.utils.critical_exit.critcal_exit import CriticalExit
10+
from je_auto_control.utils.cv2_utils.screen_record import ScreenRecorder
11+
# utils cv2_utils
12+
from je_auto_control.utils.cv2_utils.screenshot import pil_screenshot
1013
from je_auto_control.utils.exception.exceptions import \
1114
AutoControlActionException
1215
from je_auto_control.utils.exception.exceptions import \
@@ -32,8 +35,6 @@
3235
from je_auto_control.utils.executor.action_executor import execute_action
3336
from je_auto_control.utils.executor.action_executor import execute_files
3437
from je_auto_control.utils.executor.action_executor import executor
35-
from je_auto_control.utils.project.create_project_structure import \
36-
create_project_dir
3738
# file process
3839
from je_auto_control.utils.file_process.get_dir_file_list import \
3940
get_dir_files_as_list
@@ -51,17 +52,24 @@
5152
generate_xml
5253
from je_auto_control.utils.generate_report.generate_xml_report import \
5354
generate_xml_report
54-
# utils image
55-
from je_auto_control.utils.image.screenshot import pil_screenshot
5655
# json
5756
from je_auto_control.utils.json.json_file import read_action_json
5857
from je_auto_control.utils.json.json_file import write_action_json
5958
# package manager
6059
from je_auto_control.utils.package_manager.package_manager_class import \
6160
package_manager
61+
from je_auto_control.utils.project.create_project_structure import \
62+
create_project_dir
63+
# Scheduler
64+
from je_auto_control.utils.scheduler.extend_apscheduler import SchedulerManager
65+
# Shell command
66+
from je_auto_control.utils.shell_process.shell_exec import ShellManager
67+
from je_auto_control.utils.shell_process.shell_exec import default_shell_manager
6268
# socket server
6369
from je_auto_control.utils.socket_server.auto_control_socket_server import \
6470
start_autocontrol_socket_server
71+
# Start exe
72+
from je_auto_control.utils.start_exe.start_another_process import start_exe
6573
# test record
6674
from je_auto_control.utils.test_record.record_test_class import \
6775
test_record_instance
@@ -83,26 +91,19 @@
8391
from je_auto_control.wrapper.auto_control_keyboard import write
8492
# import mouse
8593
from je_auto_control.wrapper.auto_control_mouse import click_mouse
86-
from je_auto_control.wrapper.auto_control_mouse import mouse_keys_table
8794
from je_auto_control.wrapper.auto_control_mouse import get_mouse_position
95+
from je_auto_control.wrapper.auto_control_mouse import mouse_keys_table
96+
from je_auto_control.wrapper.auto_control_mouse import mouse_scroll
8897
from je_auto_control.wrapper.auto_control_mouse import press_mouse
8998
from je_auto_control.wrapper.auto_control_mouse import release_mouse
90-
from je_auto_control.wrapper.auto_control_mouse import mouse_scroll
9199
from je_auto_control.wrapper.auto_control_mouse import set_mouse_position
92100
from je_auto_control.wrapper.auto_control_mouse import special_mouse_keys_table
93101
# test_record
94102
from je_auto_control.wrapper.auto_control_record import record
95103
from je_auto_control.wrapper.auto_control_record import stop_record
96-
from je_auto_control.wrapper.auto_control_screen import screenshot
97104
# import screen
98105
from je_auto_control.wrapper.auto_control_screen import screen_size
99-
# Shell command
100-
from je_auto_control.utils.shell_process.shell_exec import ShellManager
101-
from je_auto_control.utils.shell_process.shell_exec import default_shell_manager
102-
# Start exe
103-
from je_auto_control.utils.start_exe.start_another_process import start_exe
104-
# Scheduler
105-
from je_auto_control.utils.scheduler.extend_apscheduler import SchedulerManager
106+
from je_auto_control.wrapper.auto_control_screen import screenshot
106107

107108
__all__ = [
108109
"click_mouse", "mouse_keys_table", "get_mouse_position", "press_mouse", "release_mouse",

je_auto_control/utils/callback/callback_function_executor.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from typing import Callable, Any
21
from sys import stderr
2+
from typing import Callable, Any
33

4+
# utils cv2_utils
5+
from je_auto_control.utils.cv2_utils.screenshot import pil_screenshot
46
from je_auto_control.utils.exception.exception_tags import get_bad_trigger_method, get_bad_trigger_function
57
from je_auto_control.utils.exception.exceptions import CallbackExecutorException
68
# executor
@@ -16,8 +18,6 @@
1618
# xml
1719
from je_auto_control.utils.generate_report.generate_xml_report import generate_xml
1820
from je_auto_control.utils.generate_report.generate_xml_report import generate_xml_report
19-
# utils image
20-
from je_auto_control.utils.image.screenshot import pil_screenshot
2121
# json
2222
from je_auto_control.utils.json.json_file import read_action_json
2323
from je_auto_control.utils.json.json_file import write_action_json
@@ -30,7 +30,7 @@
3030
from je_auto_control.utils.start_exe.start_another_process import start_exe
3131
# test record
3232
from je_auto_control.utils.test_record.record_test_class import test_record_instance
33-
# import image
33+
# import cv2_utils
3434
from je_auto_control.wrapper.auto_control_image import locate_all_image
3535
from je_auto_control.wrapper.auto_control_image import locate_and_click
3636
from je_auto_control.wrapper.auto_control_image import locate_image_center
@@ -80,7 +80,7 @@ def __init__(self):
8080
"AC_check_key_is_press": check_key_is_press,
8181
"AC_write": write,
8282
"AC_hotkey": hotkey,
83-
# image
83+
# cv2_utils
8484
"AC_locate_all_image": locate_all_image,
8585
"AC_locate_image_center": locate_image_center,
8686
"AC_locate_and_click": locate_and_click,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import threading
2+
from typing import Dict, Tuple
3+
4+
from cv2 import VideoWriter
5+
6+
from je_auto_control.wrapper.auto_control_screen import screenshot
7+
8+
9+
class ScreenRecorder(object):
10+
11+
def __init__(self):
12+
self.running_recorder: Dict[str, ScreenRecordThread] = {}
13+
14+
def start_new_recode(self, recoder_name: str, path_and_filename: str = "output.avi", codec: str = "XVID",
15+
frame_per_sec: int = 30, resolution: Tuple[int, int] = (1920, 1080)):
16+
record_thread = ScreenRecordThread(path_and_filename, codec, frame_per_sec, resolution)
17+
old_record = self.running_recorder.get(recoder_name, None)
18+
if old_record is not None:
19+
old_record.record_flag = False
20+
record_thread.daemon = True
21+
record_thread.start()
22+
self.running_recorder.update({recoder_name: record_thread})
23+
24+
25+
class ScreenRecordThread(threading.Thread):
26+
27+
def __init__(self, path_and_filename, codec, frame_per_sec, resolution: Tuple[int, int]):
28+
super().__init__()
29+
self.fourcc = VideoWriter.fourcc(*codec)
30+
self.video_writer = VideoWriter(path_and_filename, self.fourcc, frame_per_sec, resolution)
31+
self.record_flag = False
32+
33+
def run(self) -> None:
34+
self.record_flag = True
35+
while self.record_flag:
36+
# Get raw pixels from the screen, save it to a Numpy array
37+
image = screenshot()
38+
self.video_writer.write(image)
39+
else:
40+
self.video_writer.release()

je_auto_control/utils/image/template_detection.py je_auto_control/utils/cv2_utils/template_detection.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
def find_image(image, detect_threshold: float = 1, draw_image: bool = False) -> List[int]:
88
"""
9-
:param image which image we want to find on screen
9+
:param image which cv2_utils we want to find on screen
1010
:param detect_threshold detect precision 0.0 ~ 1.0; 1 is absolute equal
11-
:param draw_image draw detect tag on return image
11+
:param draw_image draw detect tag on return cv2_utils
1212
"""
1313
grab_image = ImageGrab.grab()
1414
return template_detection.find_object(image=grab_image, template=image,
@@ -17,9 +17,9 @@ def find_image(image, detect_threshold: float = 1, draw_image: bool = False) ->
1717

1818
def find_image_multi(image, detect_threshold: float = 1, draw_image: bool = False) -> List[List[int]]:
1919
"""
20-
:param image which image we want to find on screen
20+
:param image which cv2_utils we want to find on screen
2121
:param detect_threshold detect precision 0.0 ~ 1.0; 1 is absolute equal
22-
:param draw_image draw detect tag on return image
22+
:param draw_image draw detect tag on return cv2_utils
2323
"""
2424
grab_image = ImageGrab.grab()
2525
return template_detection.find_multi_object(image=grab_image, template=image,

je_auto_control/utils/exception/exception_tags.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
screen_screenshot: str = "screen screenshot error"
3030
# table tags
3131
table_cant_find_key: str = "cant find key error"
32-
# image tags
33-
cant_find_image: str = "cant find image"
32+
# cv2_utils tags
33+
cant_find_image: str = "cant find cv2_utils"
3434
find_image_error_variable: str = "variable error"
3535
# listener tags
3636
listener_error: str = "Auto control listener error"

je_auto_control/utils/executor/action_executor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __init__(self):
5959
"AC_check_key_is_press": check_key_is_press,
6060
"AC_write": write,
6161
"AC_hotkey": hotkey,
62-
# image
62+
# cv2_utils
6363
"AC_locate_all_image": locate_all_image,
6464
"AC_locate_image_center": locate_image_center,
6565
"AC_locate_and_click": locate_and_click,

0 commit comments

Comments
 (0)