2 Star 4 Fork 1

shiyewei/摄像头识别手部姿态实现打地鼠游戏

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mainFunction.py 5.82 KB
一键复制 编辑 原始数据 按行查看 历史
shiyewei 提交于 2021-09-03 05:56 . 上传文件
from handpose_recognize import *
import sys
import LogIn
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import recognize_character
import preFunction
import mediapipe as mp
import numpy as np
import autopy
import cv2
import myClass
class MyWindow(QMainWindow,LogIn.Ui_LogIn):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
self.scene = myClass.MyScene(self)
self.graphicsView.setScene(self.scene)
self.initail_condition() #初始化条件
self.my_thread = MyThread() # 实例化线程对象
self.score = 0
self.pushButton_start.clicked.connect(self.scene.startGame)
self.pushButton_pause.clicked.connect(self.scene.pauseGame)
self.pushButton_stop.clicked.connect(self.scene.stopGame)
self.timer_camera.timeout.connect(self.show_camera) #绑定信号与槽
def initail_condition(self):
self.wScr, self.hScr = autopy.screen.size() #获取屏幕宽和高
self.wCam, self.hCam = 640, 480 #获取摄像头宽和高
self.smoothening = 5 #用于平滑鼠标移动
self.plocX, self.plocY = 0, 0
self.clocX, self.clocY = 0, 0
self.mp_hands = mp.solutions.hands #实例化手部检测
self.hands = self.mp_hands.Hands(static_image_mode=False,
max_num_hands=1, #最多检测几只手
min_detection_confidence=0.7, #检测出手的置信度
min_tracking_confidence=0.5) #追踪是否为同一只手的置信度
self.mpDraw = mp.solutions.drawing_utils
self.cap = cv2.VideoCapture()
self.timer_camera = QTimer()
self.timer_camera.start(30) #设置定时器,30ms更新一次图片
video = "http://admin:admin@192.168.68.206:8081/"
self.cap.open(0)
self.cap.set(3, self.wCam) # width=1920
self.cap.set(4, self.hCam) # height=1080
def show_camera(self):
flag, self.image = self.cap.read() # 从视频流中读取
h, w, c = self.image.shape[0], self.image.shape[1], self.image.shape[2]
# print(h,w,c)
frame = cv2.flip(self.image, 1)
img_RGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = self.hands.process(img_RGB) #获取图片的手的信息
if results.multi_hand_landmarks:
self.mpDraw.draw_landmarks(frame, results.multi_hand_landmarks[0], self.mp_hands.HAND_CONNECTIONS)
#画出检测出的第一只手的信息(也只检测出一只)
handpoint_list = hand_point(results, h, w) #获取手部每个关键点的坐标
hand_pose = judge_handpose(handpoint_list) #通过各个关键点的位置判断手是什么姿势
if hand_pose == 'Index_up':
autopy.mouse.toggle(autopy.mouse.Button.LEFT, False)
index_x, index_y = handpoint_list[8]
screen_x = np.interp(index_x, (50, self.wCam-100), (0, self.wScr)) #将摄像头的长和宽映射到屏幕的长和宽
screen_y = np.interp(index_y, (50, self.hCam-100), (0, self.hScr))
self.clocX = self.plocX + (screen_x - self.plocX) / self.smoothening #平滑鼠标的移动
self.clocY = self.plocY + (screen_y - self.plocY) / self.smoothening
autopy.mouse.move(self.clocX, self.clocY) #鼠标移动
cv2.circle(frame, (index_x, index_y), 10, (255, 0, 255), cv2.FILLED)
self.plocX, self.plocY = self.clocX, self.clocY
elif hand_pose == 'Index_middle_up':
if p_to_p_distance(handpoint_list[8], handpoint_list[12]) < 50:
index_x, index_y = handpoint_list[8]
middle_x, middle_y = handpoint_list[12]
click_x, click_y = int((index_x + middle_x) / 2), int((index_y + middle_y) / 2)
cv2.circle(frame, (click_x, click_y), 10, (0, 255, 0), cv2.FILLED)
autopy.mouse.toggle(autopy.mouse.Button.LEFT, True) #鼠标点击
new_width, new_height = preFunction.resize_picture(frame, width=self.label_cap.width(),
height=self.label_cap.height())
#获取qypt中展示摄像头控件的宽和高
qt_img_detect = preFunction.cvimg_to_qtimg(frame) #转换图片格式
new_img = qt_img_detect.scaled(new_width, new_height, Qt.KeepAspectRatio)
self.label_cap.setPixmap(QPixmap.fromImage(new_img))
self.label_cap.setAlignment(Qt.AlignCenter) #在控件里显示图片
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
newLeft = int((screen.width()-size.width())/2)
newTop = int((screen.height()-size.height())/2-40)
self.move(newLeft,newTop) #让窗口显示在屏幕中间
def add_shadow(self):
# 给窗口添加阴影
self.effect_shadow = QGraphicsDropShadowEffect(self)
self.effect_shadow.setOffset(0, 0) # 偏移
self.effect_shadow.setBlurRadius(10) # 阴影半径
self.effect_shadow.setColor(Qt.gray) # 阴影颜色
self.widget.setGraphicsEffect(self.effect_shadow) # 将设置套用到widget窗口中
class MyThread(QThread): # 线程类
my_signal = pyqtSignal(str) # 自定义信号对象。参数str就代表这个信号可以传一个字符串
def __init__(self):
super(MyThread, self).__init__()
def run(self): # 线程执行函数
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = MyWindow()
MainWindow.center()
MainWindow.add_shadow()
MainWindow.show()
sys.exit(app.exec_())
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/shiyewei/number_2.git
git@gitee.com:shiyewei/number_2.git
shiyewei
number_2
摄像头识别手部姿态实现打地鼠游戏
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385