Skip to content

Commit ce6f28c

Browse files
committed
add class MyBuddyEmoticon
1 parent d02132a commit ce6f28c

File tree

4 files changed

+144
-14
lines changed

4 files changed

+144
-14
lines changed

demo/mybuddy_emo.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from pymycobot import MyBuddyEmoticon
2+
import time
3+
4+
file_path = [
5+
['/home/er/emo/look_happy.mp4', 10]
6+
]
7+
8+
em = MyBuddyEmoticon(file_path, loop=True)
9+
10+
em.start()
11+
time.sleep(5)
12+
13+
# 暂停
14+
em.pause()
15+
16+
time.sleep(2)
17+
18+
# 继续播放
19+
em.run()
20+
21+
# 等待播放结束
22+
em.join()

docs/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ We support Python2, Python3.5 or later.
216216
- [set_gpio_mode(pin_no, mode)](#set_gpio_modepin_no-mode)
217217
- [set_gpio_output(pin, v)](#set_gpio_outputpin-v)
218218
- [set_gpio_pwm(pin, baud, dc)](#set_gpio_pwmpin-baud-dc)
219+
- [MyBuddyEmoticon](#mybuddyemoticon)
220+
- [MyBuddyEmoticon(file_path: list = [], window_size: tuple = [], loop=False)](#mybuddyemoticonfile_path-list---window_size-tuple---loopfalse)
221+
- [add_file_path(path_time: list)](#add_file_pathpath_time-list)
222+
- [del_file_path(index: int)](#del_file_pathindex-int)
223+
- [file_path](#file_path)
224+
- [join()](#join)
225+
- [pause()](#pause-1)
226+
- [run()](#run)
227+
- [start()](#start)
219228

220229
<!-- vim-markdown-toc -->
221230
</details>
@@ -2406,5 +2415,87 @@ Set GPIO PWM value.
24062415

24072416
* **dc** – (int) 0 - 100
24082417

2418+
2419+
# MyBuddyEmoticon
2420+
2421+
## MyBuddyEmoticon(file_path: list = [], window_size: tuple = [], loop=False)
2422+
2423+
API for playing emoticons
2424+
2425+
* **Parameters**
2426+
* **file_path** - `[[path, time],...]` The absolute path of facial expression video and the length of time to play.Time in seconds.
2427+
2428+
* **window_size** - `(Length, width) `Size of the playback window (default is full screen).
2429+
2430+
* **loop** - Loop playback or not (only once by default).
2431+
2432+
```python
2433+
from pymycobot import MyBuddyEmoticon
2434+
import time
2435+
2436+
# playlist
2437+
file_path = [
2438+
['/home/er/emo/look_happy.mp4', 10],
2439+
]
2440+
# Initialize the object and set it to loop playback
2441+
em = MyBuddyEmoticon(file_path, loop = True)
2442+
# Start playing
2443+
em.start()
2444+
2445+
# Pause playback after 3 seconds
2446+
# time.sleep(3)
2447+
# em.pause()
2448+
2449+
# Continue playing
2450+
# em.run()
2451+
2452+
# The main thread waits for the completion of playback
2453+
em.join()
2454+
```
2455+
2456+
### add_file_path(path_time: list)
2457+
Add Playback File
2458+
2459+
2460+
* **Parameters**
2461+
2462+
**path_time**[path, time] The video address to be added and the running time
2463+
2464+
2465+
2466+
### del_file_path(index: int)
2467+
Delete the element with the specified subscript in the playlist list
2468+
2469+
2470+
* **Parameters**
2471+
2472+
**index** – The subscript of the element in the playlist to be deleted
2473+
2474+
2475+
2476+
### file_path
2477+
Get Playfile List
2478+
2479+
2480+
* **Returns**
2481+
2482+
list
2483+
2484+
2485+
2486+
### join()
2487+
Wait for the thread playing the video to finish
2488+
2489+
2490+
### pause()
2491+
Pause playback
2492+
2493+
### run()
2494+
Continue playing
2495+
2496+
2497+
### start()
2498+
Start playing
2499+
24092500
---
24102501
More demo can go to [here](../demo).

pymycobot/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pymycobot.mybuddysocket import MyBuddySocket
1515
from pymycobot.mira import Mira
1616
from pymycobot.mybuddybluetooth import MyBuddyBlueTooth
17-
from pymycobot.mybuddyemoticon import Emoticon
17+
from pymycobot.mybuddyemoticon import MyBuddyEmoticon
1818

1919
__all__ = [
2020
"MyCobot",
@@ -29,10 +29,10 @@
2929
"MyBuddySocket",
3030
"MyBuddyBlueTooth",
3131
"Mira",
32-
"Emoticon",
32+
"MyBuddyEmoticon",
3333
]
3434

35-
__version__ = "2.9.7b1"
35+
__version__ = "2.9.7b2"
3636
__author__ = "Elephantrobotics"
3737
__email__ = "[email protected]"
3838
__git_url__ = "https://github.com/elephantrobotics/pymycobot"

pymycobot/mybuddyemoticon.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
import threading
77

88

9-
class Emoticon:
10-
def __init__(self, file_path: list = [], window_size: list = [], loop=False) -> None:
9+
class MyBuddyEmoticon:
10+
def __init__(self, file_path: list = [], window_size: tuple = (), loop=False) -> None:
1111
"""API for playing emoticons
1212
1313
Args:
1414
file_path(list): `[[path, time]]`The absolute path of facial expression video and the length of time to play.Time in seconds.
15-
window_size(list): `[Length, width] `Size of the playback window (default is full screen).
15+
window_size(tuple): `(Length, width) `Size of the playback window (default is full screen).
16+
loop: Loop playback or not (only once by default).
1617
1718
"""
1819
self.__file_path = file_path
@@ -24,15 +25,27 @@ def __init__(self, file_path: list = [], window_size: list = [], loop=False) ->
2425

2526
@property
2627
def file_path(self):
27-
"""Get Playfile List"""
28+
"""Get Playfile List
29+
30+
Return:
31+
list
32+
"""
2833
return self.__file_path
2934

3035
def add_file_path(self, path_time: list):
31-
"""Add Playback File"""
36+
"""Add Playback File
37+
38+
Args:
39+
path_time: `[path, time]` The video address to be added and the running time
40+
"""
3241
self.__file_path.append(path_time)
3342

3443
def del_file_path(self, index: int):
35-
"""Delete the element with the specified subscript in the playlist list"""
44+
"""Delete the element with the specified subscript in the playlist list
45+
46+
Args:
47+
index: The subscript of the element in the playlist to be deleted
48+
"""
3649
if index >= len(self.__file_path):
3750
raise IndexError("list index out of range")
3851
self.__file_path.pop(index)
@@ -42,7 +55,7 @@ def window_size(self):
4255
return self.__window_size
4356

4457
@window_size.setter
45-
def window_size(self, data):
58+
def window_size(self, data: tuple):
4659
"""Set playback window size"""
4760
self.__window_size = data
4861

@@ -70,6 +83,7 @@ def play(self):
7083
cv.resizeWindow(out_win, self.window_size[0], self.window_size[1])
7184
t = time.time()
7285
cv.setMouseCallback(out_win, self.mouse_callback)
86+
_exit = False
7387
while True:
7488
while time.time() - t < self.__file_path[index][1]:
7589
if self.quit:
@@ -86,11 +100,16 @@ def play(self):
86100
if time.time() - t >= self.__file_path[index][1]:
87101
index += 1
88102
if index >= len(self.__file_path):
89-
index = 0
103+
if self.loop:
104+
index = 0
105+
else:
106+
_exit = True
107+
break
90108
t = time.time()
91109
if cv.waitKey(1) & 0xFF == ord('q') or ret == False:
92110
cap = cv.VideoCapture(self.__file_path[index][0])
93-
111+
if _exit or self.quit:
112+
break
94113
if time.time() - t >= self.__file_path[index][1]:
95114
index += 1
96115
if index >= len(self.__file_path):
@@ -100,8 +119,6 @@ def play(self):
100119
break
101120
t = time.time()
102121
cap = cv.VideoCapture(self.__file_path[index][0])
103-
if self.quit:
104-
break
105122
cap.release()
106123
cv.destroyAllWindows()
107124

0 commit comments

Comments
 (0)