77
88
99class Emoticon :
10- def __init__ (self , file_path : list = [], window_size : list = [], play_time = 5 ) -> None :
10+ def __init__ (self , file_path : list = [], window_size : list = []) -> None :
1111 """API for playing emoticons
1212
1313 Args:
14- file_path(list): Absolute path of facial expression video.
14+ file_path(list): `[[path, time]]`The absolute path of facial expression video and the length of time to play.Time in seconds .
1515 window_size(list): `[Length, width] `Size of the playback window (default is full screen).
16- play_time: Playback duration. 5 seconds by default
1716
1817 """
1918 self .__file_path = file_path
2019 self .__window_size = window_size
21- self .__play_time = play_time
2220 self .quit = False
2321 self .stop_play = False
2422
2523 @property
2624 def file_path (self ):
25+ """Get Playfile List"""
2726 return self .__file_path
2827
29- def add_file_path (self , path ):
30- self .__file_path .append (path )
28+ def add_file_path (self , path_time : list ):
29+ """Add Playback File"""
30+ self .__file_path .append (path_time )
31+
32+ def del_file_path (self , index : int ):
33+ """Delete the element with the specified subscript in the playlist list"""
34+ if index >= len (self .__file_path ):
35+ raise IndexError ("list index out of range" )
36+ self .__file_path .pop (index )
3137
3238 @property
3339 def window_size (self ):
3440 return self .__window_size
3541
3642 @window_size .setter
3743 def window_size (self , data ):
44+ """Set playback window size"""
3845 self .__window_size = data
39-
40- @property
41- def play_time (self ):
42- return self .__play_time
43-
44- @play_time .setter
45- def play_time (self , data ):
46- self .__play_time = data
4746
4847 def mouse_callback (self , event , x , y , flags , param ):
48+ """Mouse click callback function"""
4949 if event == cv .EVENT_LBUTTONDOWN :
5050 self .quit = True
5151
52- def stop (self ):
52+ def pause (self ):
53+ """Pause playback"""
5354 self .stop_play = True
5455
55- def start (self ):
56- self .stop_play = False
57-
5856 def run (self ):
59- t = threading . Thread ( target = self . play , daemon = True )
60- t . start ()
57+ """Continue playing"""
58+ self . stop_play = False
6159
6260 def play (self ):
6361 index = 0
64- cap = cv .VideoCapture (self .__file_path [index ])
62+ cap = cv .VideoCapture (self .__file_path [index ][ 0 ] )
6563 out_win = "frame"
6664 cv .namedWindow (out_win , cv .WINDOW_NORMAL )
6765 if not self .window_size :
@@ -70,17 +68,32 @@ def play(self):
7068 cv .resizeWindow (out_win , self .window_size [0 ], self .window_size [1 ])
7169 t = time .time ()
7270 cv .setMouseCallback (out_win , self .mouse_callback )
73- while time .time () - t < self .play_time and self .quit == False :
74- while self .stop_play :
75- pass
76- ret , frame = cap .read ()
77- # print(frame)
78- if frame is not None :
79- cv .imshow (out_win , frame )
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 ])
71+ while True :
72+ while time .time () - t < self .__file_path [index ][1 ]:
73+ if self .quit :
74+ break
75+ while self .stop_play and self .quit == False :
76+ pass
77+ ret , frame = cap .read ()
78+ # print(frame)
79+ if frame is not None :
80+ cv .imshow (out_win , frame )
81+ if cv .waitKey (1 ) & 0xFF == ord ('q' ) or ret == False :
82+ if time .time () - t >= self .__file_path [index ][1 ]:
83+ index += 1
84+ if index >= len (self .__file_path ):
85+ index = 0
86+ cap = cv .VideoCapture (self .__file_path [index ][0 ])
87+ if self .quit :
88+ break
8589 cap .release ()
8690 cv .destroyAllWindows ()
91+
92+ def start (self ):
93+ """Start playing"""
94+ self .t = threading .Thread (target = self .play , daemon = True )
95+ self .t .start ()
96+
97+ def joint (self ):
98+ """Wait for the thread playing the video to finish"""
99+ self .t .joint ()
0 commit comments