Python practical module (38) cvzone

environment

Introduction

cvzone is an open source library for computer vision, its core is based on opencv and mdiapipe , it can be used for image processing and the realization of some AI functions very conveniently.

Installation and use

Use pip to install, execute the command

 pip install cvzone

cvzone has several typical applications, such as face detection, hand tracking

face recognition

cvzone encapsulates the face detection module FaceDetectionModule , which integrates face_detection scheme in mediapipe

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

hand tracking

The 21 key points of mediapipe hand can be seen in the picture below

Let’s look at the specific code example

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

In the current latest version, an error will be reported when encountering two hands

The problem is located on line 143 of the file HandTrackingModule , and a return value needs to be added. Since we have not used this return value, we use _ instead

final test result

pose estimation

It is very similar to the previous module in use, and it is easier to understand

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

Topics on Python Practical Modules

For more useful python modules, please move to

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

This article is transferred from https://xugaoxiang.com/2023/02/27/python-module-38-cvzone/
This site is only for collection, and the copyright belongs to the original author.