0

Python实用模块(三十八)cvzone

 1 year ago
source link: https://xugaoxiang.com/2023/02/27/python-module-38-cvzone/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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()
42cc654a27f9f2de.webp

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

81cf740c4127a331.webp

接着来看具体的代码示例

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

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

d417f5d0ef46033a.webp

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

d23786b05efa1694.webp

最后的测试结果

e7a07285f1dc6e38.webp

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

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()
817626dffe61f929.webp

Python实用模块专题

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK