|
4 | 4 | import cv2 as cv |
5 | 5 | import time |
6 | 6 | import threading |
7 | | -import pynput.mouse as pm |
8 | 7 |
|
9 | 8 |
|
10 | 9 | class Emoticon: |
11 | | - def __init__(self, file_path, window_size: list = [], play_time=5) -> None: |
| 10 | + def __init__(self, file_path: list = [], window_size: list = [], play_time=5) -> None: |
12 | 11 | """API for playing emoticons |
13 | 12 |
|
14 | 13 | Args: |
15 | | - file_path: Absolute path of facial expression video. |
16 | | - window_size: `[Length, width] `Size of the playback window (default is full screen). |
| 14 | + file_path(list): Absolute path of facial expression video. |
| 15 | + window_size(list): `[Length, width] `Size of the playback window (default is full screen). |
17 | 16 | play_time: Playback duration. 5 seconds by default |
18 | 17 |
|
19 | 18 | """ |
20 | | - self.file_path = file_path |
21 | | - self.window_size = window_size |
22 | | - self.play_time = play_time |
| 19 | + self.__file_path = file_path |
| 20 | + self.__window_size = window_size |
| 21 | + self.__play_time = play_time |
23 | 22 | self.quit = False |
| 23 | + self.stop_play = False |
| 24 | + |
| 25 | + @property |
| 26 | + def file_path(self): |
| 27 | + return self.__file_path |
| 28 | + |
| 29 | + def add_file_path(self, path): |
| 30 | + self.__file_path.append(path) |
24 | 31 |
|
25 | 32 | @property |
26 | 33 | def window_size(self): |
27 | | - return self.window_size |
| 34 | + return self.__window_size |
| 35 | + |
| 36 | + @window_size.setter |
| 37 | + def window_size(self, data): |
| 38 | + self.__window_size = data |
28 | 39 |
|
29 | 40 | @property |
30 | 41 | def play_time(self): |
31 | | - return self.play_time |
32 | | - |
33 | | - def on_click(self, x, y, button, pressed): |
34 | | - if pressed: |
| 42 | + return self.__play_time |
| 43 | + |
| 44 | + @play_time.setter |
| 45 | + def play_time(self, data): |
| 46 | + self.__play_time = data |
| 47 | + |
| 48 | + def mouse_callback(self, event, x, y, flags, param): |
| 49 | + if event == cv.EVENT_LBUTTONDOWN: |
35 | 50 | self.quit = True |
36 | | - return True |
37 | | - else: |
38 | | - return False |
39 | | - |
40 | | - def ls_k_thread(self): |
41 | | - while 1: |
42 | | - with pm.Listener(on_click=self.on_click) as pmlistener: |
43 | | - pmlistener.join() |
44 | | - |
45 | | - def analyse_pic_thread(self): |
46 | | - r = threading.Thread(target=self.ls_k_thread) |
47 | | - r.setDaemon(True) |
48 | | - r.start() |
| 51 | + |
| 52 | + def stop(self): |
| 53 | + self.stop_play = True |
| 54 | + |
| 55 | + def start(self): |
| 56 | + self.stop_play = False |
| 57 | + |
| 58 | + def run(self): |
| 59 | + t = threading.Thread(target=self.play, daemon=True) |
| 60 | + t.start() |
49 | 61 |
|
50 | 62 | def play(self): |
51 | | - cap = cv.VideoCapture(self.file_path) |
| 63 | + index = 0 |
| 64 | + cap = cv.VideoCapture(self.__file_path[index]) |
52 | 65 | out_win = "frame" |
53 | 66 | cv.namedWindow(out_win, cv.WINDOW_NORMAL) |
54 | 67 | if not self.window_size: |
55 | 68 | cv.setWindowProperty(out_win, cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN) |
56 | 69 | else: |
57 | 70 | cv.resizeWindow(out_win, self.window_size[0], self.window_size[1]) |
58 | 71 | t = time.time() |
| 72 | + cv.setMouseCallback(out_win, self.mouse_callback) |
59 | 73 | while time.time() - t < self.play_time and self.quit == False: |
| 74 | + while self.stop_play: |
| 75 | + pass |
60 | 76 | ret, frame = cap.read() |
61 | 77 | # print(frame) |
62 | 78 | if frame is not None: |
63 | 79 | cv.imshow(out_win, frame) |
64 | | - if ret == False: |
65 | | - cap = cv.VideoCapture(self.file_path) |
| 80 | + if cv.waitKey(1) & 0xFF == ord('q') or ret == False: |
| 81 | + index += 1 |
| 82 | + if index >= len(self.__file_path): |
| 83 | + index = 0 |
| 84 | + cap = cv.VideoCapture(self.__file_path[index]) |
66 | 85 | cap.release() |
67 | 86 | cv.destroyAllWindows() |
0 commit comments