ezgo 0.0.2__py3-none-any.whl → 0.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ezgo/__init__.py +87 -0
- ezgo/camera.py +71 -0
- ezgo/go2.py +795 -0
- ezgo/go2_camera.py +290 -0
- ezgo/go2_vui.py +355 -0
- ezgo/ui.py +77 -0
- {ezgo-0.0.2.dist-info → ezgo-0.0.4.dist-info}/METADATA +12 -5
- ezgo-0.0.4.dist-info/RECORD +11 -0
- ezgo-0.0.4.dist-info/top_level.txt +1 -0
- ezgo-0.0.2.dist-info/RECORD +0 -5
- ezgo-0.0.2.dist-info/top_level.txt +0 -1
- {ezgo-0.0.2.dist-info → ezgo-0.0.4.dist-info}/LICENSE +0 -0
- {ezgo-0.0.2.dist-info → ezgo-0.0.4.dist-info}/WHEEL +0 -0
ezgo/__init__.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
ezgo - 宇树Go2机器狗Python控制库
|
|
6
|
+
|
|
7
|
+
这是一个用于控制宇树Go2机器狗的Python库,提供了简单易用的API接口。
|
|
8
|
+
支持运动控制、视频流获取、UI界面等功能。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
__version__ = "0.0.4"
|
|
12
|
+
__author__ = "ezgo"
|
|
13
|
+
__email__ = ""
|
|
14
|
+
__license__ = "MIT"
|
|
15
|
+
|
|
16
|
+
# 导入主要类
|
|
17
|
+
try:
|
|
18
|
+
from .go2 import Go2
|
|
19
|
+
from .camera import Camera
|
|
20
|
+
from .ui import APP
|
|
21
|
+
from .go2_camera import Go2Camera
|
|
22
|
+
from .go2_vui import Go2VUI
|
|
23
|
+
except ImportError as e:
|
|
24
|
+
print(f"警告: 无法导入部分模块: {e}")
|
|
25
|
+
print("请确保已安装所有依赖包")
|
|
26
|
+
Go2 = None
|
|
27
|
+
Camera = None
|
|
28
|
+
APP = None
|
|
29
|
+
Go2Camera = None
|
|
30
|
+
Go2VUI = None
|
|
31
|
+
|
|
32
|
+
# 定义公开的API
|
|
33
|
+
__all__ = [
|
|
34
|
+
"Go2",
|
|
35
|
+
"Camera",
|
|
36
|
+
"APP",
|
|
37
|
+
"Go2Camera",
|
|
38
|
+
"Go2VUI",
|
|
39
|
+
"__version__"
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
# 依赖检查和提示
|
|
43
|
+
def _check_dependencies():
|
|
44
|
+
"""检查关键依赖是否安装,并给出安装提示"""
|
|
45
|
+
missing_deps = []
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
import cv2
|
|
49
|
+
except ImportError:
|
|
50
|
+
missing_deps.append("opencv-python")
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
import numpy
|
|
54
|
+
except ImportError:
|
|
55
|
+
missing_deps.append("numpy")
|
|
56
|
+
|
|
57
|
+
try:
|
|
58
|
+
from PIL import Image
|
|
59
|
+
except ImportError:
|
|
60
|
+
missing_deps.append("Pillow")
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
import netifaces
|
|
64
|
+
except ImportError:
|
|
65
|
+
missing_deps.append("netifaces")
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
import unitree_sdk2py
|
|
69
|
+
except ImportError:
|
|
70
|
+
missing_deps.append("unitree-sdk2py")
|
|
71
|
+
|
|
72
|
+
if missing_deps:
|
|
73
|
+
print("=" * 60)
|
|
74
|
+
print("警告: ezgo 缺少以下依赖包:")
|
|
75
|
+
for dep in missing_deps:
|
|
76
|
+
print(f" - {dep}")
|
|
77
|
+
print()
|
|
78
|
+
print("请安装缺少的依赖包:")
|
|
79
|
+
print(" pip install opencv-python numpy Pillow netifaces")
|
|
80
|
+
print(" # unitree-sdk2py 需要从官方源安装")
|
|
81
|
+
print("=" * 60)
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
return True
|
|
85
|
+
|
|
86
|
+
# 在导入时进行依赖检查
|
|
87
|
+
_check_dependencies()
|
ezgo/camera.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import cv2
|
|
2
|
+
from PIL import Image, ImageTk
|
|
3
|
+
import platform
|
|
4
|
+
|
|
5
|
+
class Camera:
|
|
6
|
+
def __init__(self, index=0, width=640, height=480, fps=30,):
|
|
7
|
+
self.index = index
|
|
8
|
+
self.width = width
|
|
9
|
+
self.height = height
|
|
10
|
+
self.fps = fps
|
|
11
|
+
|
|
12
|
+
self.cap = None
|
|
13
|
+
# self.open_camera()
|
|
14
|
+
|
|
15
|
+
def open_camera(self):
|
|
16
|
+
|
|
17
|
+
if platform.system() == "Linux":
|
|
18
|
+
self.cap = cv2.VideoCapture(self.index, cv2.CAP_V4L2) # 启用 V4L2 硬件加速
|
|
19
|
+
fourcc = cv2.VideoWriter_fourcc(*'MJPG') # 强制硬件压缩的 MJPG 格式(利用 FFMPEG 加速)
|
|
20
|
+
self.cap.set(cv2.CAP_PROP_FOURCC, fourcc)
|
|
21
|
+
else:
|
|
22
|
+
self.cap = cv2.VideoCapture(self.index)
|
|
23
|
+
|
|
24
|
+
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
|
|
25
|
+
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
|
|
26
|
+
self.cap.set(cv2.CAP_PROP_FPS, self.fps)
|
|
27
|
+
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) # 清空缓存
|
|
28
|
+
self.cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
|
|
29
|
+
|
|
30
|
+
if not self.cap.isOpened():
|
|
31
|
+
raise Exception("无法打开摄像头")
|
|
32
|
+
actual_width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
33
|
+
actual_height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
34
|
+
print(f"打开摄像头 {self.index},分辨率 {actual_width}x{actual_height},帧率 {self.fps}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def read_cv2_image(self):
|
|
38
|
+
ret, frame = self.cap.read()
|
|
39
|
+
if not ret:
|
|
40
|
+
raise Exception("无法读取摄像头帧")
|
|
41
|
+
return frame
|
|
42
|
+
|
|
43
|
+
def read_pil_image(self):
|
|
44
|
+
"""将BGR帧转换为PIL"""
|
|
45
|
+
frame = self.read_cv2_image()
|
|
46
|
+
return self.cv2_to_pil(frame)
|
|
47
|
+
|
|
48
|
+
def cv2_to_tk(self, frame):
|
|
49
|
+
"""将BGR帧转换为Tkinter PhotoImage"""
|
|
50
|
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
51
|
+
frame_pil = Image.fromarray(frame_rgb)
|
|
52
|
+
frame_tk = ImageTk.PhotoImage(image=frame_pil)
|
|
53
|
+
return frame_tk
|
|
54
|
+
|
|
55
|
+
def resize(self, frame, size=(224, 224)):
|
|
56
|
+
"""将图像调整为指定尺寸"""
|
|
57
|
+
return cv2.resize(frame, size, interpolation=cv2.INTER_LINEAR)
|
|
58
|
+
|
|
59
|
+
def crop_to_square(self, frame):
|
|
60
|
+
"""将图像裁剪为正方形"""
|
|
61
|
+
h, w = frame.shape[:2]
|
|
62
|
+
min_dim = min(h, w)
|
|
63
|
+
y = (h - min_dim) // 2
|
|
64
|
+
x = (w - min_dim) // 2
|
|
65
|
+
return frame[y:y+min_dim, x:x+min_dim]
|
|
66
|
+
|
|
67
|
+
def __del__(self):
|
|
68
|
+
"""释放摄像头资源"""
|
|
69
|
+
if self.cap is not None:
|
|
70
|
+
self.cap.release()
|
|
71
|
+
|