diff --git a/src/Drone_gesture_operation/main.py b/src/Drone_gesture_operation/main.py index 22c3f190a9..6616097b9b 100644 --- a/src/Drone_gesture_operation/main.py +++ b/src/Drone_gesture_operation/main.py @@ -18,6 +18,11 @@ def __init__(self, target_fps=30): self.skin_upper = np.array([30, 255, 180], np.uint8) self.kernel = np.ones((3, 3), np.uint8) + # 新增:手指检测参数 + self.defect_depth_threshold = 20 # 凸包缺陷深度阈值 + self.min_defect_distance = 10 # 缺陷点最小距离 + self.palm_solidity_threshold = 0.6 # 手掌的密实度阈值 + # 3. 手势缓存(仅2帧,快速响应+稳定) self.gesture_buffer = [] self.stable_gesture = "None" @@ -26,6 +31,42 @@ def __init__(self, target_fps=30): self.frame_queue = [] self.queue_lock = threading.Lock() + def count_fingers(self, cnt, frame_small): + """通过凸包缺陷计算手指数量""" + try: + # 计算凸包和凸包缺陷 + hull = cv.convexHull(cnt, returnPoints=False) + defects = cv.convexityDefects(cnt, hull) + + if defects is None: + return 0 + + finger_count = 0 + defect_points = [] + + # 遍历所有凸包缺陷 + for i in range(defects.shape[0]): + s, e, f, d = defects[i, 0] + start = tuple(cnt[s][0]) + end = tuple(cnt[e][0]) + far = tuple(cnt[f][0]) + + # 计算缺陷深度(转换为实际像素值) + depth = d / 256.0 + + # 只考虑深度足够的缺陷(手指间的凹陷) + if depth > self.defect_depth_threshold: + # 计算两点间距离,避免重复计数 + if all(np.linalg.norm(np.array(far) - np.array(p)) > self.min_defect_distance for p in + defect_points): + defect_points.append(far) + finger_count += 1 + + # 缺陷数+1 = 手指数量(例如:4个缺陷=5根手指) + return min(finger_count + 1, 5) # 最多5根手指 + except: + return 0 + def capture_frames(self, cap): """独立线程采集帧,避免主线程阻塞""" while cap.isOpened(): @@ -48,6 +89,34 @@ def process_frame(self, frame): mask = cv.morphologyEx(mask, cv.MORPH_OPEN, self.kernel) # 2. 快速找轮廓 + contours, _ = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) + current_gesture = "None" + + if contours: + cnt = max(contours, key=cv.contourArea) + area = cv.contourArea(cnt) + + if area > 1000: + # 3. 手势分类(新增五指识别) + hull = cv.convexHull(cnt) + solidity = cv.contourArea(cnt) / cv.contourArea(hull) + + # 计算手指数量 + finger_count = self.count_fingers(cnt, frame_small) + + # 手势判断逻辑 + if solidity > 0.85: + # 密实度高 = 握拳 + current_gesture = "Fist" + elif finger_count == 1: + # 1根手指 = 单指 + current_gesture = "Point" + elif finger_count >= 4: + # 4-5根手指 = 手掌张开 + current_gesture = "Palm" + elif 2 <= finger_count <= 3: + # 2-3根手指 = 部分张开(归类为Point) + current_gesture = "Point" # 极简手势识别(仅保留拳头/点手势,极致流畅) @@ -177,6 +246,7 @@ def main(): if len(set(self.gesture_buffer)) == 1: self.stable_gesture = self.gesture_buffer[0] + # 5. 绘制极简UI(仅保留手势和FPS显示,移除手指数量) # 5. 绘制极简UI(控制绘制耗时) cv.putText(frame, f"Gesture: {self.stable_gesture}", (10, 40), cv.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3) @@ -202,6 +272,7 @@ def run(self): capture_thread.start() print(f"✅ 帧率锁定 {self.target_fps} 帧 | ESC退出") + print("💡 把手放在画面中间,握拳=Fist,伸食指=Point,五指张开=Palm") print("💡 把手放在画面中间,握拳=Fist,伸食指=Point") # 3. 主线程处理+显示(严格控时) @@ -239,6 +310,7 @@ def run(self): if __name__ == '__main__': # 实例化并运行,锁定30帧(可改20/15帧,更低更稳) recognizer = StableFPSHandRecognizer(target_fps=30) + recognizer.run() recognizer.run() # 7. 稳定输出(连续2帧相同) if current_gesture == last_gesture: