欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入我们的社群。

Python实用模块(三十八)cvzone

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

环境

  • windows 10 64bit
  • python 3.8
  • cvzone 1.5.6

简介

cvzone 是一个计算机视觉开源库,其核心是基于 opencvmdiapipe,使用它可以很方便地进行图像处理和一些 AI 功能的实现。

安装与使用

使用 pip 安装,执行命令

  1. pip install cvzone

cvzone 有几个典型的应用,比如人脸检测、手部跟踪

人脸识别

cvzone 封装了人脸检测的模块 FaceDetectionModule,它整合了 mediapipe 中的 face_detection 方案

  1. from cvzone.FaceDetectionModule import FaceDetector
  2. import cv2
  3. cap = cv2.VideoCapture('test.mp4')
  4. detector = FaceDetector()
  5. while True:
  6. success, img = cap.read()
  7. # 返回的图像img包含了检测后的人脸框,bboxs是一个列表,包含图像中所有见到的人脸数据,每一组人脸的数据是id, 位置, 相似度, 中心点位置坐标
  8. img, bboxs = detector.findFaces(img)
  9. if bboxs:
  10. # 获取中心点位置,这里就直接取id为0即第一个人的中心点坐标
  11. center = bboxs[0]["center"]
  12. cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
  13. cv2.imshow("Image", img)
  14. if cv2.waitKey(1) & 0xFF == ord('q'):
  15. break
  16. cap.release()
  17. cv2.destroyAllWindows()

cvzone

手部跟踪

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

cvzone

接着来看具体的代码示例

  1. from cvzone.HandTrackingModule import HandDetector
  2. import cv2
  3. cap = cv2.VideoCapture("test.mp4")
  4. detector = HandDetector(detectionCon=0.8, maxHands=2)
  5. while True:
  6. success, img = cap.read()
  7. # 获取手部的关键点信息,共21个点。findHands默认返回带手部关键点标识的图像;如果不想显示,可以在findHands方法中增加参数draw=False
  8. hands, img = detector.findHands(img)
  9. if hands:
  10. # 第一只手
  11. hand1 = hands[0]
  12. # 21个关键点坐标
  13. lmList1 = hand1["lmList"]
  14. # 手部坐标
  15. bbox1 = hand1["bbox"]
  16. # 手部的中心点坐标
  17. centerPoint1 = hand1['center']
  18. # 左手还是右手
  19. handType1 = hand1["type"]
  20. # 获取打开手指的列表
  21. fingers1 = detector.fingersUp(hand1)
  22. if len(hands) == 2:
  23. # 第二只手
  24. hand2 = hands[1]
  25. lmList2 = hand2["lmList"]
  26. bbox2 = hand2["bbox"]
  27. centerPoint2 = hand2['center']
  28. handType2 = hand2["type"]
  29. fingers2 = detector.fingersUp(hand2)
  30. # 计算2只手对应手指的距离,比如这里的食指指尖关键点
  31. # findDistance方法可不带img参数,返回值也相应的不带绘制后的img
  32. # length, info = detector.findDistance(lmList1[8], lmList2[8])
  33. length, info, img = detector.findDistance(lmList1[8], lmList2[8], img) # with draw
  34. cv2.imshow("Image", img)
  35. cv2.waitKey(1)
  36. cap.release()
  37. cv2.destroyAllWindows()

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

cvzone

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

cvzone

最后的测试结果

cvzone

姿态估计

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

  1. from cvzone.PoseModule import PoseDetector
  2. import cv2
  3. cap = cv2.VideoCapture("gusture_test.mp4")
  4. detector = PoseDetector()
  5. while True:
  6. success, img = cap.read()
  7. img = detector.findPose(img)
  8. lmList, bboxInfo = detector.findPosition(img, bboxWithHands=False)
  9. if bboxInfo:
  10. center = bboxInfo["center"]
  11. cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
  12. cv2.imshow("Image", img)
  13. if cv2.waitKey(1) & 0xFF == ord('q'):
  14. break
  15. cap.release()
  16. cv2.destroyAllWindows()

cvzone

Python实用模块专题

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

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

喜欢 (0)

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