当前位置: 首页 > news >正文

Opencv小项目——手势数字刷TIKTOK

写在前面:

很久没更新了,之前的实习的记录也算是烂尾了,但是好在自己的实习记录还是有的,最近也忙碌了很多,终于放假了,今天下午正好没事,闲来无事就随便做个小玩意吧。
思来想去,就做个手势识别,再通过识别的数字来控制电脑端口的抖音,话不多说,让我们直接进入正题。

思路

  1. 导入库:

    • 导入需要的库,包括 cv2(OpenCV),mathmediapipepyautoguitimewebbrowser
  2. 打开抖音网站:

    • 使用 webbrowser.open 打开抖音网站,即函数 open_website()
  3. 定义二维向量的角度计算函数:

    • vector_2d_angle 函数接收两个二维向量,计算它们之间的夹角。这个函数用于后续计算手指的角度。
  4. 获取手部关键点的角度列表:

    • hand_angle 函数接收手部关键点坐标,通过 vector_2d_angle 计算大拇指、食指、中指、无名指和小拇指的角度,并返回一个角度列表。
  5. 根据角度确定手势:

    • h_gesture 函数接收角度列表,根据预定义的阈值判断手势类型,返回相应的手势字符串。
  6. 执行键盘动作:

    • perform_keyboard_action 函数接收手势字符串,根据手势执行相应的键盘动作。您可以根据需要添加更多手势和对应的键盘动作。
  7. 手势检测主循环:

    • detect_and_control_keyboard 函数使用 mediapipe 检测手势。通过循环捕获摄像头图像,检测手部关键点,计算手势角度,执行相应的键盘动作,并在图像上显示手势。为了减缓循环速度,使用 time.sleep(0.1) 增加每次循环的间隔时间。
  8. 主程序入口:

    • __main__ 部分,首先打开抖音网站,然后等待一段时间(time.sleep(0.5)),最后开始手势检测主循环。
 if (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "0"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "1"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "2"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] > thr_angle):gesture_str = "3"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "4"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "5"elif (angle_list[0] < thr_angle_s) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "6"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "8"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Pink Up"elif (angle_list[0] < thr_angle_s) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Thumb Up"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Fuck"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "Princess"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Bye"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Spider-Man"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Rock'n'Roll"

自己去探索吧! 各种手势哦

代码

代码如下:

import cv2
import math
import mediapipe as mp
import pyautogui
import time
import webbrowserdef open_website():url = "https://www.douyin.com/"webbrowser.open(url)def vector_2d_angle(v1, v2):# 求解二维向量的角度v1_x, v1_y = v1[0], v1[1]v2_x, v2_y = v2[0], v2[1]try:angle_ = math.degrees(math.acos((v1_x*v2_x + v1_y*v2_y) / (((v1_x**2 + v1_y**2)**0.5) * ((v2_x**2 + v2_y**2)**0.5))))except:angle_ = 65535.if angle_ > 180.:angle_ = 65535.return angle_def hand_angle(hand_):# 获取对应手相关向量的二维角度,根据角度确定手势angle_list = []# ---------------------------- thumb 大拇指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[2][0])), (int(hand_[0][1]) - int(hand_[2][1]))),((int(hand_[3][0]) - int(hand_[4][0])), (int(hand_[3][1]) - int(hand_[4][1]))))angle_list.append(angle_)# ---------------------------- index 食指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[6][0])), (int(hand_[0][1]) - int(hand_[6][1]))),((int(hand_[7][0]) - int(hand_[8][0])), (int(hand_[7][1]) - int(hand_[8][1]))))angle_list.append(angle_)# ---------------------------- middle 中指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[10][0])), (int(hand_[0][1]) - int(hand_[10][1]))),((int(hand_[11][0]) - int(hand_[12][0])), (int(hand_[11][1]) - int(hand_[12][1]))))angle_list.append(angle_)# ---------------------------- ring 无名指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[14][0])), (int(hand_[0][1]) - int(hand_[14][1]))),((int(hand_[15][0]) - int(hand_[16][0])), (int(hand_[15][1]) - int(hand_[16][1]))))angle_list.append(angle_)# ---------------------------- pink 小拇指角度angle_ = vector_2d_angle(((int(hand_[0][0]) - int(hand_[18][0])), (int(hand_[0][1]) - int(hand_[18][1]))),((int(hand_[19][0]) - int(hand_[20][0])), (int(hand_[19][1]) - int(hand_[20][1]))))angle_list.append(angle_)return angle_listdef h_gesture(angle_list):# 二维约束的方法定义手势thr_angle = 65.  # 手指闭合则大于这个值(大拇指除外)thr_angle_thumb = 53.  # 大拇指闭合则大于这个值thr_angle_s = 49.  # 手指张开则小于这个值gesture_str = "Unknown"if 65535. not in angle_list:if (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "0"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "1"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "2"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] > thr_angle):gesture_str = "3"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "4"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "5"elif (angle_list[0] < thr_angle_s) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "6"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "8"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Pink Up"elif (angle_list[0] < thr_angle_s) and (angle_list[1] > thr_angle) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Thumb Up"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Fuck"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] > thr_angle) and (angle_list[2] < thr_angle_s) and (angle_list[3] < thr_angle_s) and (angle_list[4] < thr_angle_s):gesture_str = "Princess"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] < thr_angle_s) and (angle_list[3] > thr_angle) and (angle_list[4] > thr_angle):gesture_str = "Bye"elif (angle_list[0] < thr_angle_s) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Spider-Man"elif (angle_list[0] > thr_angle_thumb) and (angle_list[1] < thr_angle_s) and (angle_list[2] > thr_angle) and (angle_list[3] > thr_angle) and (angle_list[4] < thr_angle_s):gesture_str = "Rock'n'Roll"return gesture_strdef perform_keyboard_action(gesture_str):if gesture_str == "0":pyautogui.press('Space')if gesture_str == "1":pyautogui.press('Up')elif gesture_str == "2":pyautogui.press('down')elif gesture_str == "3":pyautogui.press("Z")elif gesture_str == "4":pyautogui.press("X")elif gesture_str == "5":pyautogui.hotkey('ctrl', 'w')elif gesture_str == "Fuck":pyautogui.keyDown('esc')# Add more gestures and corresponding keyboard actions if neededdef detect_and_control_keyboard():mp_drawing = mp.solutions.drawing_utilsmp_hands = mp.solutions.handshands = mp_hands.Hands(static_image_mode=False,max_num_hands=1,min_detection_confidence=0.75,min_tracking_confidence=0.75)cap = cv2.VideoCapture(0)while True:ret, frame = cap.read()frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)frame = cv2.flip(frame, 1)results = hands.process(frame)frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)if results.multi_hand_landmarks:for hand_landmarks in results.multi_hand_landmarks:mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)hand_local = []for i in range(21):x = hand_landmarks.landmark[i].x * frame.shape[1]y = hand_landmarks.landmark[i].y * frame.shape[0]hand_local.append((x, y))if hand_local:angle_list = hand_angle(hand_local)gesture_str = h_gesture(angle_list)print(gesture_str)cv2.putText(frame, gesture_str, (50, 100), 0, 1.3, (0, 0, 255), 2)# Perform keyboard action based on gestureperform_keyboard_action(gesture_str)cv2.imshow('MediaPipe Hands', frame)if cv2.waitKey(1) & 0xFF == 27:breaktime.sleep(0.1)cap.release()cv2.destroyAllWindows()if __name__ == '__main__':open_website()  # Open Douyin in Microsoft Edgetime.sleep(0.5)  # Give some time for the browser to open before starting hand gesture detectiondetect_and_control_keyboard()

运行结果:

在这里插入图片描述

http://www.lryc.cn/news/285567.html

相关文章:

  • 【优化技术专题】「性能优化系列」针对Java对象压缩及序列化技术的探索之路
  • Spring+SprinMVC+MyBatis配置方式简易模板
  • Windows ssh登录eNSP交换机
  • SwiftUI 纯手工打造 100% 可定制的导航栏
  • npm install 太慢?解决方法
  • DevOps系列文章之 GitLab CI/CD
  • 【CompletableFuture任务编排】游戏服务器线程模型及其线程之间的交互(以排行榜线程和玩家线程的交互为例子)
  • 什么是浏览器指纹?详解浏览器指纹识别技术,教你防止浏览器指纹识别
  • canvas绘制六芒星
  • 全网最详细!!Python 爬虫快速入门
  • gitgud.io+Sapphire注册账号教程
  • 【动态规划】【广度优先搜索】【状态压缩】847 访问所有节点的最短路径
  • python基础小知识:引用和赋值的区别
  • 欧科云链与《警察技术》联合发布技术专题.pdf
  • 【QT+QGIS跨平台编译】之一:【sqlite+Qt跨平台编译】(一套代码、一套框架,跨平台编译)
  • websocket实现聊天室(vue2 + node)
  • RabbitMQ-消息延迟
  • 【Oracle】如何给物化视图分区
  • 10个常考的前端手写题,你全都会吗?
  • vue组件间通信
  • 编程框架概述:MVC, MVP, MVVM, Flux/Redux, 和 Clean Architecture
  • 多维时序 | Matlab实现CNN-BiLSTM-Mutilhead-Attention卷积双向长短期记忆神经网络融合多头注意力机制多变量时间序列预测
  • np.argsort排序问题(关于位次)-含GitHub上在numpy项目下提问的回复-总结可行方案
  • Element中的el-input-number+SpringBoot+mysql
  • Jupyter Notebook五分钟基础速通
  • 基于SpringBoot的SSM整合案例
  • [SS]语义分割_转置卷积
  • 面板小程序命令行工具介绍
  • DBA技术栈MongoDB: 数据增改删除
  • Xcode查看APP文件目录