Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
11c5268
提交手势识别类,供后续主函数调用,同时添加了requirements.txt
Arrebol01 Nov 20, 2025
4e214f6
Merge branch 'OpenHUTB:main' into main
Arrebol01 Nov 21, 2025
b92bf27
清理移除无关的IDE和网站文件,并提交手势识别类以及require.txt文件
Arrebol01 Nov 21, 2025
e78bacf
Merge branch 'OpenHUTB:main' into main
Arrebol01 Nov 21, 2025
b0550a8
添加无人机控制模块
Arrebol01 Nov 22, 2025
c03d23d
Merge branch 'OpenHUTB:main' into main
Arrebol01 Nov 24, 2025
9ae0608
Merge branch 'OpenHUTB:main' into main
Arrebol01 Nov 25, 2025
dc537c0
Merge branch 'OpenHUTB:main' into main
Arrebol01 Nov 26, 2025
edaf42b
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 1, 2025
2f02eeb
添加了main.py主程序初步实现手势识别的显示窗口创建
Arrebol01 Dec 1, 2025
4ab80f7
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 2, 2025
73f398f
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 2, 2025
66b97f0
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 2, 2025
9e9a2db
更新.gitignore,剔除多余跟踪,添加主程序
Arrebol01 Dec 2, 2025
81af7c9
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 2, 2025
42a04e0
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 2, 2025
b77daa3
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 4, 2025
28887ad
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 4, 2025
931dc36
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 12, 2025
719b663
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 13, 2025
fa2d1bb
清理Python缓存文件
Arrebol01 Dec 13, 2025
7536bcf
完善README.md文件内容,并清除python缓存文件
Arrebol01 Dec 13, 2025
3dba771
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 13, 2025
df4bef4
将显示菜单更改为中文显示
Arrebol01 Dec 13, 2025
1b3075e
更改菜单栏显示为中文显示
Arrebol01 Dec 13, 2025
bca5ab8
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 14, 2025
55451ce
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 14, 2025
e151783
加入打开摄像头功能,实现手势识别
Arrebol01 Dec 14, 2025
de6e99a
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 14, 2025
90cbf47
创建无人机仿真模型
Arrebol01 Dec 14, 2025
02fc7cf
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 15, 2025
c490bff
添加无人机物理引擎
Arrebol01 Dec 15, 2025
6132063
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 15, 2025
e76b785
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 15, 2025
46d4d83
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 15, 2025
da2c0b6
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
3df8629
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
53e9146
整合整个项目内容
Arrebol01 Dec 16, 2025
8fff882
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
9c677e7
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
e8c3adb
添加训练数据的收集程序
Arrebol01 Dec 16, 2025
58e6601
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
dbe34cb
更改README.md当中文件结构,新增依赖
Arrebol01 Dec 16, 2025
7335468
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
3c40474
将requirements.txt转为UTF-8字符编码
Arrebol01 Dec 16, 2025
131176c
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
cbc4463
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 16, 2025
1e65692
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 17, 2025
2001e04
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 17, 2025
9993082
新添了学习分类模块,以及模型训练程序
Arrebol01 Dec 17, 2025
55d8696
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 17, 2025
0f214a0
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 17, 2025
574c38e
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 17, 2025
2ce9f61
Merge branch 'OpenHUTB:main' into main
Arrebol01 Dec 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions src/Drone_hand_gesture_project/gesture_classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
import numpy as np
import pickle
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report, accuracy_score
import joblib


class GestureClassifier:
"""机器学习手势分类器"""

def __init__(self, model_type='svm', model_path=None):
self.model_type = model_type
self.model_path = model_path
self.scaler = StandardScaler()
self.model = None
self.gesture_classes = None

# 如果提供了模型路径,加载模型
if model_path and os.path.exists(model_path):
self.load_model(model_path)

def create_model(self):
"""创建分类模型"""
if self.model_type == 'svm':
self.model = SVC(
kernel='rbf',
C=10.0,
gamma='scale',
probability=True,
random_state=42
)
elif self.model_type == 'random_forest':
self.model = RandomForestClassifier(
n_estimators=100,
max_depth=10,
random_state=42,
n_jobs=-1
)
elif self.model_type == 'mlp':
self.model = MLPClassifier(
hidden_layer_sizes=(128, 64),
activation='relu',
solver='adam',
max_iter=1000,
random_state=42
)
else:
raise ValueError(f"未知的模型类型: {self.model_type}")

def load_dataset(self, dataset_path):
"""加载数据集"""
with open(dataset_path, 'rb') as f:
dataset = pickle.load(f)

X = dataset['features']
y = dataset['labels']
self.gesture_classes = dataset['gesture_classes']

return X, y

def preprocess_data(self, X_train, X_test):
"""数据预处理"""
# 标准化特征
X_train_scaled = self.scaler.fit_transform(X_train)
X_test_scaled = self.scaler.transform(X_test)

return X_train_scaled, X_test_scaled

def train(self, dataset_path, test_size=0.2, save_path=None):
"""训练模型"""
# 加载数据
X, y = self.load_dataset(dataset_path)

# 检查数据
print(f"数据集信息:")
print(f" 总样本数: {len(X)}")
print(f" 特征维度: {X.shape[1]}")

unique_labels, counts = np.unique(y, return_counts=True)
print(" 各类样本数:")
for label, count in zip(unique_labels, counts):
class_name = self.gesture_classes[label]
print(f" {class_name}: {count}")

if len(unique_labels) < 2:
raise ValueError(f"需要至少2个类别才能训练,当前只有 {len(unique_labels)} 个类别")

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=42, stratify=y
)

# 数据预处理
X_train_scaled, X_test_scaled = self.preprocess_data(X_train, X_test)

# 创建并训练模型
self.create_model()
print(f"训练 {self.model_type} 模型...")
self.model.fit(X_train_scaled, y_train)

# 评估模型
train_acc = self.model.score(X_train_scaled, y_train)
test_acc = self.model.score(X_test_scaled, y_test)

print(f"训练准确率: {train_acc:.4f}")
print(f"测试准确率: {test_acc:.4f}")

# 详细分类报告
y_pred = self.model.predict(X_test_scaled)
print("\n分类报告:")
print(classification_report(y_test, y_pred,
target_names=self.gesture_classes))

# 保存模型
if save_path:
self.save_model(save_path)

return test_acc

def predict(self, landmarks):
"""预测手势类别"""
if self.model is None:
raise ValueError("模型未训练或未加载")

# 检查landmarks是否有效
if len(landmarks) != 63: # 21个关键点 * 3坐标
return "none", 0.0

# 预处理
landmarks_scaled = self.scaler.transform([landmarks])

# 预测
probabilities = self.model.predict_proba(landmarks_scaled)[0]
predicted_class_idx = np.argmax(probabilities)
confidence = probabilities[predicted_class_idx]

predicted_class = self.gesture_classes[predicted_class_idx]

return predicted_class, confidence

def predict_batch(self, landmarks_list):
"""批量预测"""
if not landmarks_list:
return [], []

# 预处理
landmarks_array = np.array(landmarks_list)
landmarks_scaled = self.scaler.transform(landmarks_array)

# 预测
probabilities = self.model.predict_proba(landmarks_scaled)
predicted_indices = np.argmax(probabilities, axis=1)
confidences = np.max(probabilities, axis=1)

predicted_classes = [self.gesture_classes[idx] for idx in predicted_indices]

return predicted_classes, confidences

def save_model(self, save_path):
"""保存模型"""
model_data = {
'model_type': self.model_type,
'model': self.model,
'scaler': self.scaler,
'gesture_classes': self.gesture_classes
}

joblib.dump(model_data, save_path)
print(f"模型已保存到: {save_path}")

def load_model(self, model_path):
"""加载模型"""
model_data = joblib.load(model_path)

self.model_type = model_data['model_type']
self.model = model_data['model']
self.scaler = model_data['scaler']
self.gesture_classes = model_data['gesture_classes']

print(f"模型已从 {model_path} 加载")
print(f"模型类型: {self.model_type}")
print(f"手势类别: {self.gesture_classes}")


class EnsembleGestureClassifier:
"""集成学习手势分类器"""

def __init__(self, model_paths=None):
self.models = []
self.gesture_classes = None

if model_paths:
self.load_models(model_paths)

def load_models(self, model_paths):
"""加载多个模型"""
for path in model_paths:
classifier = GestureClassifier(model_path=path)
self.models.append(classifier)

# 使用第一个模型的类别
if self.models:
self.gesture_classes = self.models[0].gesture_classes

def predict(self, landmarks, voting='soft'):
"""集成预测"""
if not self.models:
raise ValueError("没有加载任何模型")

if len(landmarks) != 63:
return "none", 0.0

predictions = []
confidences = []

for model in self.models:
pred_class, confidence = model.predict(landmarks)
predictions.append(pred_class)
confidences.append(confidence)

if voting == 'hard':
# 硬投票
from collections import Counter
most_common = Counter(predictions).most_common(1)[0]
final_prediction = most_common[0]
final_confidence = most_common[1] / len(predictions)
else:
# 软投票(平均置信度)
class_confidences = {}
for model, pred, conf in zip(self.models, predictions, confidences):
if pred not in class_confidences:
class_confidences[pred] = []
class_confidences[pred].append(conf)

# 平均每个类别的置信度
avg_confidences = {
cls: np.mean(confs) for cls, confs in class_confidences.items()
}

# 选择平均置信度最高的类别
final_prediction = max(avg_confidences.items(), key=lambda x: x[1])[0]
final_confidence = avg_confidences[final_prediction]

return final_prediction, final_confidence
Loading
Loading