本代码基于 Advance Computer Vision with Python 进行修改,更加适合中国宝宝体质
我的相关代码及数据集已经上传GitHub仓库,欢迎使用 Advance-Computer-Vision-with-Python
FingerCounter.py
# 此脚本只能检测右手的手势
import cv2
import time
import os
import HandTrackingModule as htm
# 设置摄像头的宽度和高度,即分辨率
wCam, hCam = 640, 480
# 打开摄像头
cap = cv2.VideoCapture(0)
cap.set(3, wCam) # 设置摄像头宽度
cap.set(4, hCam) # 设置摄像头高度
# 指定手指图像文件夹路径
folderPath = "E:\\Advance Computer Vision with Python\\main\\Project 2 Finger Counter\\FingerImages"
myList = os.listdir(folderPath) # 获取文件夹中的所有文件名列表
print(myList)
overlayList = [] # 用于存储手指图像的列表
for imPath in myList:
# 读取每张手指图像
image = cv2.imread(f"{
folderPath}/{
imPath}")
overlayList.append(image) # 将图像添加到列表中
print(len(overlayList)) # 输出图像数量
pTime = 0 # 初始化上一帧时间
# 创建手部检测器对象,设置检测置信度为0.75
detector = htm.handDetector(detectionCon=0.75)
# 手指尖的ID列表
tipIds = [4, 8, 12, 16, 20]
while True:
success, img = cap.read() # 从摄像头读取帧
img = cv2.flip(img, 1) # 水平翻转图像
img = detector.findHands(img) # 检测手并绘制手部关键点
lmList = detector.findPosition(img, draw=False) # 获取手部关键点位置列表
if len(lmList) !=