欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入免费知识星球。

Python实用模块(三十八)cvzone

实用模块 迷途小书童 1年前 (2023-02-27) 1218次浏览 0个评论

环境

  • windows 10 64bit
  • python 3.8
  • cvzone 1.5.6

简介

cvzone 是一个计算机视觉开源库,其核心是基于 opencvmdiapipe,使用它可以很方便地进行图像处理和一些 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()

cvzone

手部跟踪

mediapipe 手部21个关键点信息可以看下面这张图

cvzone

接着来看具体的代码示例

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()

目前的最新版本,遇到两只手的情况就会报错

cvzone

问题定位到文件 HandTrackingModule 的第 143 行,需要增加一个返回值,由于我们并没有使用到这个返回值,就用 _ 代替

cvzone

最后的测试结果

cvzone

姿态估计

使用上和之前的模块非常相似,比较好懂

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()

cvzone

Python实用模块专题

更多有用的 python 模块,请移步

https://xugaoxiang.com/category/python/modules/

喜欢 (0)

您必须 登录 才能发表评论!