CVZone
This is a Computer vision package that makes its easy to run Image processing and AI functions. At the core it uses OpenCV and Mediapipe libraries.
Installation
You can simply use pip to install the latest version of cvzone.
pip install cvzone
60 FPS Face Detection
import cvzone import cv2 cap = cv2.VideoCapture(0) detector = cvzone.FaceDetector() while True: success, img = cap.read() img, bboxs = detector.findFaces(img) print(bboxs) cv2.imshow("Image", img) cv2.waitKey(1)
Hand Tracking
Basic Code Example
import cvzone import cv2 cap = cv2.VideoCapture(0) cap.set(3, 1280) cap.set(4, 720) detector = cvzone.HandDetector(detectionCon=0.5, maxHands=1) while True: # Get image frame success, img = cap.read() # Find the hand and its landmarks img = detector.findHands(img) lmList, bbox = detector.findPosition(img) # Display cv2.imshow("Image", img) cv2.waitKey(1)
Finding How many finger are up
if lmList: # Find how many fingers are up fingers = detector.fingersUp() totalFingers = fingers.count(1) cv2.putText(img, f'Fingers:{totalFingers}', (bbox[0] + 200, bbox[1] - 30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
Finding distace between two fingers
if lmList: # Find Distance Between Two Fingers distance, img, info = detector.findDistance(8, 12, img) cv2.putText(img, f'Dist:{int(distance)}', (bbox[0] + 400, bbox[1] - 30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
Find Hand Type - i.e. Left or Right
if lmList: # Find Hand Type myHandType = detector.handType() cv2.putText(img, f'Hand:{myHandType}', (bbox[0], bbox[1] - 30), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)
Pose Estimation
import cvzone import cv2 cap = cv2.VideoCapture(0) detector = cvzone.PoseDetector() while True: success, img = cap.read() img = detector.findPose(img) lmList = detector.findPosition(img, draw=False) if lmList: print(lmList[14]) cv2.circle(img, (lmList[14][1], lmList[14][2]), 15, (0, 0, 255), cv2.FILLED) cv2.imshow("Image", img) cv2.waitKey(1)
Face Mesh Detection
import cvzone import cv2 cap = cv2.VideoCapture(0) detector = cvzone.FaceMeshDetector(maxFaces=2) while True: success, img = cap.read() img, faces = detector.findFaceMesh(img) if faces: print(faces[0]) cv2.imshow("Image", img) cv2.waitKey(1)
Stack Images
import cvzone import cv2 cap = cv2.VideoCapture(0) cap.set(3, 1280) cap.set(4, 720) while True: success, img = cap.read() imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgList = [img, img, imgGray, img, imgGray, img,imgGray, img, img] stackedImg = cvzone.stackImages(imgList, 3, 0.4) cv2.imshow("stackedImg", stackedImg) cv2.waitKey(1)
Corner Rectangle
import cvzone import cv2 cap = cv2.VideoCapture(0) detector = cvzone.HandDetector() while True: # Get image frame success, img = cap.read() # Find the hand and its landmarks img = detector.findHands(img, draw=False) lmList, bbox = detector.findPosition(img, draw=False) if bbox: # Draw Corner Rectangle cvzone.cornerRect(img, bbox) # Display cv2.imshow("Image", img) cv2.waitKey(1)
FPS
import cvzone import cv2 fpsReader = cvzone.FPS() cap = cv2.VideoCapture(0) cap.set(3, 1280) cap.set(4, 720) while True: success, img = cap.read() fps, img = fpsReader.update(img,pos=(50,80),color=(0,255,0),scale=5,thickness=5) cv2.imshow("Image", img) cv2.waitKey(1)