网站首页 > 技术文章 正文
简介
cvzone 是一个计算机视觉开源库,其核心是基于 opencv 和 mdiapipe,使用它可以很方便地进行图像处理和一些 AI 功能的实现。
安装与使用
使用 pip 安装,执行命令
pip install cvzone
cvzone 有几个典型的应用,比如人脸检测、手部跟踪、姿态估计、面部网格等。
人脸识别
cvzone 封装了人脸检测的模块 FaceDetectionModule,它整合了 mediapipe 中的 face_detection 方案
from cvzone.FaceDetectionModule import FaceDetector
import cv2
cap = cv2.VideoCapture('test.mp4')
detector = FaceDetector()
while True:
success, img = cap.read()
# 返回的图像img包含了检测后的人脸框,bboxs是一个列表,包含图像中所有见到的人脸数据,每一组人脸的数据是id, 位置, 相似度, 中心点位置坐标
img, bboxs = detector.findFaces(img)
if bboxs:
# 获取中心点位置,这里就直接取id为0即第一个人的中心点坐标
center = bboxs[0]["center"]
cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
手部跟踪
mediapipe 手部21个关键点信息可以看下面这张图
接着来看具体的代码示例
from cvzone.HandTrackingModule import HandDetector
import cv2
cap = cv2.VideoCapture("test.mp4")
detector = HandDetector(detectionCon=0.8, maxHands=2)
while True:
success, img = cap.read()
# 获取手部的关键点信息,共21个点。findHands默认返回带手部关键点标识的图像;如果不想显示,可以在findHands方法中增加参数draw=False
hands, img = detector.findHands(img)
if hands:
# 第一只手
hand1 = hands[0]
# 21个关键点坐标
lmList1 = hand1["lmList"]
# 手部坐标
bbox1 = hand1["bbox"]
# 手部的中心点坐标
centerPoint1 = hand1['center']
# 左手还是右手
handType1 = hand1["type"]
# 获取打开手指的列表
fingers1 = detector.fingersUp(hand1)
if len(hands) == 2:
# 第二只手
hand2 = hands[1]
lmList2 = hand2["lmList"]
bbox2 = hand2["bbox"]
centerPoint2 = hand2['center']
handType2 = hand2["type"]
fingers2 = detector.fingersUp(hand2)
# 计算2只手对应手指的距离,比如这里的食指指尖关键点
# findDistance方法可不带img参数,返回值也相应的不带绘制后的img
# length, info = detector.findDistance(lmList1[8], lmList2[8])
length, info, img = detector.findDistance(lmList1[8], lmList2[8], img) # with draw
cv2.imshow("Image", img)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
目前的最新版本,遇到两只手的情况就会报错
问题定位到文件 HandTrackingModule 的第 143 行,需要增加一个返回值,由于我们并没有使用到这个返回值,就用 _ 代替
最后的测试结果
姿态估计
使用上和之前的模块非常相似,比较好懂
from cvzone.PoseModule import PoseDetector
import cv2
cap = cv2.VideoCapture("gusture_test.mp4")
detector = PoseDetector()
while True:
success, img = cap.read()
img = detector.findPose(img)
lmList, bboxInfo = detector.findPosition(img, bboxWithHands=False)
if bboxInfo:
center = bboxInfo["center"]
cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
猜你喜欢
- 2024-09-11 给3D Slicer添加Python第三方插件库
- 2024-09-11 使用Python创建自己的Instagram滤镜
- 2024-09-11 薅羊毛我愿称Python为最强,Python 自动化带你轻松赚钱
- 2024-09-11 史上最全的OpenCV入门教程!这篇够你学习半个月了!万字长文入门
- 2024-09-11 使用python实现简易的人脸检测(python人脸检测106关键点)
- 2024-09-11 机器视觉技术(一)边缘提取(机器视觉边缘检测)
- 2024-09-11 Python-OpenCV 16. 图像特征检测与描述算法
- 2024-09-11 Flask—远程查看行空板USB摄像头视频
- 2024-09-11 OpenCV+Python识别车牌和字符分割的实现
- 2024-09-11 在Fedora中安装OpenCV-Python | 二
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)