Code Monkey home page Code Monkey logo

cvzone's Introduction

CVZone

Downloads Downloads Downloads

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


Examples

For sample usage and examples, please refer to the Examples folder in this repository. This folder contains various examples to help you understand how to make the most out of cvzone's features.

Video Documentation

YouTube Video

Table of Contents

  1. Installations
  2. Corner Rectangle
  3. PutTextRect
  4. Download Image from URL
  5. Overlay PNG
  6. Rotate Image
  7. Stack Images
  8. FPS
  9. Finding Contours
  10. Color Module
  11. Classification Module
  12. Face Detection
  13. Face Mesh Module
  14. Selfie Segmentation Module
  15. Hand Tracking Module
  16. Pose Module
  17. Serial Module
  18. Plot Module

Installations

To install the cvzone package, run the following command:

pip install cvzone

Corner Rectangle

Corner Rectangle CVZone
import cv2
import cvzone  # Importing the cvzone library

# Initialize the webcam
cap = cv2.VideoCapture(2)  # Capture video from the third webcam (0-based index)

# Main loop to continuously capture frames
while True:
    # Capture a single frame from the webcam
    success, img = cap.read()  # 'success' is a boolean that indicates if the frame was captured successfully, and 'img' contains the frame itself

    # Add a rectangle with styled corners to the image
    img = cvzone.cornerRect(
        img,  # The image to draw on
        (200, 200, 300, 200),  # The position and dimensions of the rectangle (x, y, width, height)
        l=30,  # Length of the corner edges
        t=5,  # Thickness of the corner edges
        rt=1,  # Thickness of the rectangle
        colorR=(255, 0, 255),  # Color of the rectangle
        colorC=(0, 255, 0)  # Color of the corner edges
    )

    # Show the modified image
    cv2.imshow("Image", img)  # Display the image in a window named "Image"

    # Wait for 1 millisecond between frames
    cv2.waitKey(1)  # Waits 1 ms for a key event (not being used here)

PutTextRect

putTextRect CVZone
import cv2
import cvzone  # Importing the cvzone library

# Initialize the webcam
cap = cv2.VideoCapture(2)  # Capture video from the third webcam (0-based index)

# Main loop to continuously capture frames
while True:
    # Capture a single frame from the webcam
    success, img = cap.read()  # 'success' is a boolean that indicates if the frame was captured successfully, and 'img' contains the frame itself

    # Add a rectangle and put text inside it on the image
    img, bbox = cvzone.putTextRect(
        img, "CVZone", (50, 50),  # Image and starting position of the rectangle
        scale=3, thickness=3,  # Font scale and thickness
        colorT=(255, 255, 255), colorR=(255, 0, 255),  # Text color and Rectangle color
        font=cv2.FONT_HERSHEY_PLAIN,  # Font type
        offset=10,  # Offset of text inside the rectangle
        border=5, colorB=(0, 255, 0)  # Border thickness and color
    )

    # Show the modified image
    cv2.imshow("Image", img)  # Display the image in a window named "Image"

    # Wait for 1 millisecond between frames
    cv2.waitKey(1)  # Waits 1 ms for a key event (not being used here)

Download Image from URL

import cv2
import cvzone

imgNormal = cvzone.downloadImageFromUrl(
    url='https://github.com/cvzone/cvzone/blob/master/Results/shapes.png?raw=true')

imgPNG = cvzone.downloadImageFromUrl(
    url='https://github.com/cvzone/cvzone/blob/master/Results/cvzoneLogo.png?raw=true',
    keepTransparency=True)
imgPNG =cv2.resize(imgPNG,(0,0),None,3,3)

cv2.imshow("Image Normal", imgNormal)
cv2.imshow("Transparent Image", imgPNG)
cv2.waitKey(0)

Overlay PNG

overlayPNG CVZone
import cv2
import cvzone

# Initialize camera capture
cap = cv2.VideoCapture(2)

# imgPNG = cvzone.downloadImageFromUrl(
#     url='https://github.com/cvzone/cvzone/blob/master/Results/cvzoneLogo.png?raw=true',
#     keepTransparency=True)

imgPNG = cv2.imread("cvzoneLogo.png",cv2.IMREAD_UNCHANGED)


while True:
    # Read image frame from camera
    success, img = cap.read()

    imgOverlay = cvzone.overlayPNG(img, imgPNG, pos=[-30, 50])
    imgOverlay = cvzone.overlayPNG(img, imgPNG, pos=[200, 200])
    imgOverlay = cvzone.overlayPNG(img, imgPNG, pos=[500, 400])

    cv2.imshow("imgOverlay", imgOverlay)
    cv2.waitKey(1)

Rotate Image

rotateImage CVZone
import cv2
from cvzone.Utils import rotateImage  # Import rotateImage function from cvzone.Utils

# Initialize the video capture
cap = cv2.VideoCapture(2)  # Capture video from the third webcam (index starts at 0)

# Start the loop to continuously get frames from the webcam
while True:
    # Read a frame from the webcam
    success, img = cap.read()  # 'success' will be True if the frame is read successfully, 'img' will contain the frame

    # Rotate the image by 60 degrees without keeping the size
    imgRotated60 = rotateImage(img, 60, scale=1,
                               keepSize=False)  # Rotate image 60 degrees, scale it by 1, and don't keep original size

    # Rotate the image by 60 degrees while keeping the size
    imgRotated60KeepSize = rotateImage(img, 60, scale=1,
                                       keepSize=True)  # Rotate image 60 degrees, scale it by 1, and keep the original size

    # Display the rotated images
    cv2.imshow("imgRotated60", imgRotated60)  # Show the 60-degree rotated image without keeping the size
    cv2.imshow("imgRotated60KeepSize", imgRotated60KeepSize)  # Show the 60-degree rotated image while keeping the size

    # Wait for 1 millisecond between frames
    cv2.waitKey(1)  # Wait for 1 ms, during which any key press can be detected (not being used here)

Stack Images

stackImages CVZone
import cv2
import cvzone

# Initialize camera capture
cap = cv2.VideoCapture(2)

# Start an infinite loop to continually capture frames
while True:
    # Read image frame from camera
    success, img = cap.read()

    # Convert the image to grayscale
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Resize the image to be smaller (0.1x of original size)
    imgSmall = cv2.resize(img, (0, 0), None, 0.1, 0.1)

    # Resize the image to be larger (3x of original size)
    imgBig = cv2.resize(img, (0, 0), None, 3, 3)

    # Apply Canny edge detection on the grayscale image
    imgCanny = cv2.Canny(imgGray, 50, 150)

    # Convert the image to HSV color space
    imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # Create a list of all processed images
    imgList = [img, imgGray, imgCanny, imgSmall, imgBig, imgHSV]

    # Stack the images together using cvzone's stackImages function
    stackedImg = cvzone.stackImages(imgList, 3, 0.7)

    # Display the stacked images
    cv2.imshow("stackedImg", stackedImg)

    # Wait for 1 millisecond; this also allows for keyboard inputs
    cv2.waitKey(1)

FPS

import cvzone
import cv2

# Initialize the FPS class with an average count of 30 frames for smoothing
fpsReader = cvzone.FPS(avgCount=30)

# Initialize the webcam and set it to capture
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FPS, 30)  # Set the frames per second to 30

# Main loop to capture frames and display FPS
while True:
    # Read a frame from the webcam
    success, img = cap.read()

    # Update the FPS counter and draw the FPS on the image
    # fpsReader.update returns the current FPS and the updated image
    fps, img = fpsReader.update(img, pos=(20, 50),
                                bgColor=(255, 0, 255), textColor=(255, 255, 255),
                                scale=3, thickness=3)

    # Display the image with the FPS counter
    cv2.imshow("Image", img)

    # Wait for 1 ms to show this frame, then continue to the next frame
    cv2.waitKey(1)

Finding Contours

import cv2           # Importing the OpenCV library for computer vision tasks
import cvzone       # Importing the cvzone library for additional functionalities
import numpy as np  # Importing NumPy library for numerical operations

# Download an image containing shapes from a given URL
imgShapes = cvzone.downloadImageFromUrl(
    url='https://github.com/cvzone/cvzone/blob/master/Results/shapes.png?raw=true')

# Perform edge detection using the Canny algorithm
imgCanny = cv2.Canny(imgShapes, 50, 150)

# Dilate the edges to strengthen the detected contours
imgDilated = cv2.dilate(imgCanny, np.ones((5, 5), np.uint8), iterations=1)

# Find contours in the image without any corner filtering
imgContours, conFound = cvzone.findContours(
    imgShapes, imgDilated, minArea=1000, sort=True,
    filter=None, drawCon=True, c=(255, 0, 0), ct=(255, 0, 255),
    retrType=cv2.RETR_EXTERNAL, approxType=cv2.CHAIN_APPROX_NONE)

# Find contours in the image and filter them based on corner points (either 3 or 4 corners)
imgContoursFiltered, conFoundFiltered = cvzone.findContours(
    imgShapes, imgDilated, minArea=1000, sort=True,
    filter=[3, 4], drawCon=True, c=(255, 0, 0), ct=(255, 0, 255),
    retrType=cv2.RETR_EXTERNAL, approxType=cv2.CHAIN_APPROX_NONE)

# Display the image with all found contours
cv2.imshow("imgContours", imgContours)

# Display the image with filtered contours (either 3 or 4 corners)
cv2.imshow("imgContoursFiltered", imgContoursFiltered)

# Wait until a key is pressed to close the windows
cv2.waitKey(0)

Color Module

import cvzone
import cv2

# Create an instance of the ColorFinder class with trackBar set to True.
myColorFinder = cvzone.ColorFinder(trackBar=True)

# Initialize the video capture using OpenCV.
# Using the third camera (index 2). Adjust index if you have multiple cameras.
cap = cv2.VideoCapture(2)

# Set the dimensions of the camera feed to 640x480.
cap.set(3, 640)
cap.set(4, 480)

# Custom color values for detecting orange.
# 'hmin', 'smin', 'vmin' are the minimum values for Hue, Saturation, and Value.
# 'hmax', 'smax', 'vmax' are the maximum values for Hue, Saturation, and Value.
hsvVals = {'hmin': 10, 'smin': 55, 'vmin': 215, 'hmax': 42, 'smax': 255, 'vmax': 255}

# Main loop to continuously get frames from the camera.
while True:
    # Read the current frame from the camera.
    success, img = cap.read()

    # Use the update method from the ColorFinder class to detect the color.
    # It returns the masked color image and a binary mask.
    imgOrange, mask = myColorFinder.update(img, hsvVals)

    # Stack the original image, the masked color image, and the binary mask.
    imgStack = cvzone.stackImages([img, imgOrange, mask], 3, 1)

    # Show the stacked images.
    cv2.imshow("Image Stack", imgStack)

    # Break the loop if the 'q' key is pressed.
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Classification Module

from cvzone.ClassificationModule import Classifier
import cv2

cap = cv2.VideoCapture(2)  # Initialize video capture
path = "C:/Users/USER/Documents/maskModel/"
maskClassifier = Classifier(f'{path}/keras_model.h5', f'{path}/labels.txt')

while True:
    _, img = cap.read()  # Capture frame-by-frame
    prediction = maskClassifier.getPrediction(img)
    print(prediction)  # Print prediction result
    cv2.imshow("Image", img)
    cv2.waitKey(1)  # Wait for a key press

Face Detection

import cvzone
from cvzone.FaceDetectionModule import FaceDetector
import cv2

# Initialize the webcam
# '2' means the third camera connected to the computer, usually 0 refers to the built-in webcam
cap = cv2.VideoCapture(2)

# Initialize the FaceDetector object
# minDetectionCon: Minimum detection confidence threshold
# modelSelection: 0 for short-range detection (2 meters), 1 for long-range detection (5 meters)
detector = FaceDetector(minDetectionCon=0.5, modelSelection=0)

# Run the loop to continually get frames from the webcam
while True:
    # Read the current frame from the webcam
    # success: Boolean, whether the frame was successfully grabbed
    # img: the captured frame
    success, img = cap.read()

    # Detect faces in the image
    # img: Updated image
    # bboxs: List of bounding boxes around detected faces
    img, bboxs = detector.findFaces(img, draw=False)

    # Check if any face is detected
    if bboxs:
        # Loop through each bounding box
        for bbox in bboxs:
            # bbox contains 'id', 'bbox', 'score', 'center'

            # ---- Get Data  ---- #
            center = bbox["center"]
            x, y, w, h = bbox['bbox']
            score = int(bbox['score'][0] * 100)

            # ---- Draw Data  ---- #
            cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
            cvzone.putTextRect(img, f'{score}%', (x, y - 10))
            cvzone.cornerRect(img, (x, y, w, h))

    # Display the image in a window named 'Image'
    cv2.imshow("Image", img)
    # Wait for 1 millisecond, and keep the window open
    cv2.waitKey(1)

Face Mesh Module

from cvzone.FaceMeshModule import FaceMeshDetector
import cv2

# Initialize the webcam
# '2' indicates the third camera connected to the computer, '0' would usually refer to the built-in webcam
cap = cv2.VideoCapture(2)

# Initialize FaceMeshDetector object
# staticMode: If True, the detection happens only once, else every frame
# maxFaces: Maximum number of faces to detect
# minDetectionCon: Minimum detection confidence threshold
# minTrackCon: Minimum tracking confidence threshold
detector = FaceMeshDetector(staticMode=False, maxFaces=2, minDetectionCon=0.5, minTrackCon=0.5)

# Start the loop to continually get frames from the webcam
while True:
    # Read the current frame from the webcam
    # success: Boolean, whether the frame was successfully grabbed
    # img: The current frame
    success, img = cap.read()

    # Find face mesh in the image
    # img: Updated image with the face mesh if draw=True
    # faces: Detected face information
    img, faces = detector.findFaceMesh(img, draw=True)

    # Check if any faces are detected
    if faces:
        # Loop through each detected face
        for face in faces:
            # Get specific points for the eye
            # leftEyeUpPoint: Point above the left eye
            # leftEyeDownPoint: Point below the left eye
            leftEyeUpPoint = face[159]
            leftEyeDownPoint = face[23]

            # Calculate the vertical distance between the eye points
            # leftEyeVerticalDistance: Distance between points above and below the left eye
            # info: Additional information (like coordinates)
            leftEyeVerticalDistance, info = detector.findDistance(leftEyeUpPoint, leftEyeDownPoint)

            # Print the vertical distance for debugging or information
            print(leftEyeVerticalDistance)

    # Display the image in a window named 'Image'
    cv2.imshow("Image", img)

    # Wait for 1 millisecond to check for any user input, keeping the window open
    cv2.waitKey(1)

Selfie Segmentation Module

import cvzone
from cvzone.SelfiSegmentationModule import SelfiSegmentation
import cv2

# Initialize the webcam. '2' indicates the third camera connected to the computer.
# '0' usually refers to the built-in camera.
cap = cv2.VideoCapture(2)

# Set the frame width to 640 pixels
cap.set(3, 640)
# Set the frame height to 480 pixels
cap.set(4, 480)

# Initialize the SelfiSegmentation class. It will be used for background removal.
# model is 0 or 1 - 0 is general 1 is landscape(faster)
segmentor = SelfiSegmentation(model=0)

# Infinite loop to keep capturing frames from the webcam
while True:
    # Capture a single frame
    success, img = cap.read()

    # Use the SelfiSegmentation class to remove the background
    # Replace it with a magenta background (255, 0, 255)
    # imgBG can be a color or an image as well. must be same size as the original if image
    # 'cutThreshold' is the sensitivity of the segmentation.
    imgOut = segmentor.removeBG(img, imgBg=(255, 0, 255), cutThreshold=0.1)

    # Stack the original image and the image with background removed side by side
    imgStacked = cvzone.stackImages([img, imgOut], cols=2, scale=1)

    # Display the stacked images
    cv2.imshow("Image", imgStacked)

    # Check for 'q' key press to break the loop and close the window
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Hand Tracking Module

from cvzone.HandTrackingModule import HandDetector
import cv2

# Initialize the webcam to capture video
# The '2' indicates the third camera connected to your computer; '0' would usually refer to the built-in camera
cap = cv2.VideoCapture(2)

# Initialize the HandDetector class with the given parameters
detector = HandDetector(staticMode=False, maxHands=2, modelComplexity=1, detectionCon=0.5, minTrackCon=0.5)

# Continuously get frames from the webcam
while True:
    # Capture each frame from the webcam
    # 'success' will be True if the frame is successfully captured, 'img' will contain the frame
    success, img = cap.read()

    # Find hands in the current frame
    # The 'draw' parameter draws landmarks and hand outlines on the image if set to True
    # The 'flipType' parameter flips the image, making it easier for some detections
    hands, img = detector.findHands(img, draw=True, flipType=True)

    # Check if any hands are detected
    if hands:
        # Information for the first hand detected
        hand1 = hands[0]  # Get the first hand detected
        lmList1 = hand1["lmList"]  # List of 21 landmarks for the first hand
        bbox1 = hand1["bbox"]  # Bounding box around the first hand (x,y,w,h coordinates)
        center1 = hand1['center']  # Center coordinates of the first hand
        handType1 = hand1["type"]  # Type of the first hand ("Left" or "Right")

        # Count the number of fingers up for the first hand
        fingers1 = detector.fingersUp(hand1)
        print(f'H1 = {fingers1.count(1)}', end=" ")  # Print the count of fingers that are up

        # Calculate distance between specific landmarks on the first hand and draw it on the image
        length, info, img = detector.findDistance(lmList1[8][0:2], lmList1[12][0:2], img, color=(255, 0, 255),
                                                  scale=10)

        # Check if a second hand is detected
        if len(hands) == 2:
            # Information for the second hand
            hand2 = hands[1]
            lmList2 = hand2["lmList"]
            bbox2 = hand2["bbox"]
            center2 = hand2['center']
            handType2 = hand2["type"]

            # Count the number of fingers up for the second hand
            fingers2 = detector.fingersUp(hand2)
            print(f'H2 = {fingers2.count(1)}', end=" ")

            # Calculate distance between the index fingers of both hands and draw it on the image
            length, info, img = detector.findDistance(lmList1[8][0:2], lmList2[8][0:2], img, color=(255, 0, 0),
                                                      scale=10)

        print(" ")  # New line for better readability of the printed output

    # Display the image in a window
    cv2.imshow("Image", img)

    # Keep the window open and update it for each frame; wait for 1 millisecond between frames
    cv2.waitKey(1)

Pose Module

from cvzone.PoseModule import PoseDetector
import cv2

# Initialize the webcam and set it to the third camera (index 2)
cap = cv2.VideoCapture(2)

# Initialize the PoseDetector class with the given parameters
detector = PoseDetector(staticMode=False,
                        modelComplexity=1,
                        smoothLandmarks=True,
                        enableSegmentation=False,
                        smoothSegmentation=True,
                        detectionCon=0.5,
                        trackCon=0.5)

# Loop to continuously get frames from the webcam
while True:
    # Capture each frame from the webcam
    success, img = cap.read()

    # Find the human pose in the frame
    img = detector.findPose(img)

    # Find the landmarks, bounding box, and center of the body in the frame
    # Set draw=True to draw the landmarks and bounding box on the image
    lmList, bboxInfo = detector.findPosition(img, draw=True, bboxWithHands=False)

    # Check if any body landmarks are detected
    if lmList:
        # Get the center of the bounding box around the body
        center = bboxInfo["center"]

        # Draw a circle at the center of the bounding box
        cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)

        # Calculate the distance between landmarks 11 and 15 and draw it on the image
        length, img, info = detector.findDistance(lmList[11][0:2],
                                                  lmList[15][0:2],
                                                  img=img,
                                                  color=(255, 0, 0),
                                                  scale=10)

        # Calculate the angle between landmarks 11, 13, and 15 and draw it on the image
        angle, img = detector.findAngle(lmList[11][0:2],
                                        lmList[13][0:2],
                                        lmList[15][0:2],
                                        img=img,
                                        color=(0, 0, 255),
                                        scale=10)

        # Check if the angle is close to 50 degrees with an offset of 10
        isCloseAngle50 = detector.angleCheck(myAngle=angle,
                                             targetAngle=50,
                                             offset=10)

        # Print the result of the angle check
        print(isCloseAngle50)

    # Display the frame in a window
    cv2.imshow("Image", img)

    # Wait for 1 millisecond between each frame
    cv2.waitKey(1)

Serial Module

from cvzone.SerialModule import SerialObject

# Initialize the Arduino SerialObject with optional parameters
# baudRate = 9600, digits = 1, max_retries = 5
arduino = SerialObject(portNo=None, baudRate=9600, digits=1, max_retries=5)

# Initialize a counter to keep track of iterations
count = 0

# Start an infinite loop
while True:
    # Increment the counter on each iteration
    count += 1

    # Print data received from the Arduino
    # getData method returns the list of data received from the Arduino
    print(arduino.getData())

    # If the count is less than 100
    if count < 100:
        # Send a list containing [1] to the Arduino
        arduino.sendData([1])
    else:
        # If the count is 100 or greater, send a list containing [0] to the Arduino
        arduino.sendData([0])

    # Reset the count back to 0 once it reaches 200
    # This will make the cycle repeat
    if count == 200:
        count = 0
#include <cvzone.h>

SerialData serialData(1,1); //(numOfValsRec,digitsPerValRec)
/*0 or 1 - 1 digit
0 to 99 -  2 digits
0 to 999 - 3 digits
 */
//SerialData serialData;   // if not receving only sending


int sendVals[2]; // min val of 2 even when sending 1
int valsRec[1];

int x = 0;

void setup() {

serialData.begin(9600);
pinMode(13,OUTPUT);
}

void loop() {

  // ------- To SEND --------
  x +=1;
  if (x==100){x=0;}
  sendVals[0] = x;
  serialData.Send(sendVals);

  // ------- To Recieve --------
  serialData.Get(valsRec);
  digitalWrite(13,valsRec[0]);

}

Plot Module

Sine Example

from cvzone.PlotModule import LivePlot
import cv2
import math


sinPlot = LivePlot(w=1200, yLimit=[-100, 100], interval=0.01)
xSin=0

while True:

    xSin += 1
    if xSin == 360: xSin = 0
    imgPlotSin = sinPlot.update(int(math.sin(math.radians(xSin)) * 100))

    cv2.imshow("Image Sin Plot", imgPlotSin)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Face Detection X Value Example

from cvzone.PlotModule import LivePlot
from cvzone.FaceDetectionModule import FaceDetector
import cv2
import cvzone

cap = cv2.VideoCapture(1)
detector = FaceDetector(minDetectionCon=0.85, modelSelection=0)

xPlot = LivePlot(w=1200, yLimit=[0, 500], interval=0.01)

while True:
    success, img = cap.read()

    img, bboxs = detector.findFaces(img, draw=False)
    val = 0
    if bboxs:
        # Loop through each bounding box
        for bbox in bboxs:
            # bbox contains 'id', 'bbox', 'score', 'center'
            # ---- Get Data  ---- #
            center = bbox["center"]
            x, y, w, h = bbox['bbox']
            score = int(bbox['score'][0] * 100)
            val = center[0]
            # ---- Draw Data  ---- #
            cv2.circle(img, center, 5, (255, 0, 255), cv2.FILLED)
            cvzone.putTextRect(img, f'{score}%', (x, y - 10))
            cvzone.cornerRect(img, (x, y, w, h))

    imgPlot = xPlot.update(val)

    cv2.imshow("Image Plot", imgPlot)
    cv2.imshow("Image", img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cvzone's People

Contributors

cvzone avatar murtazahassan avatar zamalali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cvzone's Issues

issue with calculate distance between fingers

Dears,
kindly be informed that i have an issue with calculating distance between two fingers ,when I calculate distance it is mandatory to draw circle at top of each tip , how can I prevent this drawing.
how can I solve this
regards,

there is a bug in example script that dir is cvzone\Examples\CornerRectangleExample.py.

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Traceback (most recent call last):
File "e:/Technological learning/CV/cvzone/cvzone/Examples/CornerRectangleExample.py", line 14, in
lmList, bbox = detector.findPosition(img, draw=False)
AttributeError: 'HandDetector' object has no attribute 'findPosition'
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-durgkkoo\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Errors in all cvzone examples

I tried to test the FPS sample code in the documentation and I get the error below.
I also tried all of the other examples but then all dont work for me.
I am using macOS, python==3.9.5, opencv-python==4.5.2.54 and cvzone==1.5.6

  File "/Users/ UserName/Documents/testing_cvzone/testing.py", line 1, in <module>
    import cvzone
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cvzone/__init__.py", line 1, in <module>
    from cvzone.Utils import stackImages, cornerRect, findContours,\
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cvzone/Utils.py", line 7, in <module>
    import cv2
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cv2/__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/cv2/mat_wrapper/__init__.py", line 33, in <module>
    cv._registerMatType(Mat)
AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType' (most likely due to a circular import)

What should I do to fix this?

Problem with "no attribute findPosition"

All packages are installed.

pip list

Package               Version
--------------------- --------
absl-py               0.13.0
attrs                 21.2.0
cvzone                1.5.0
cycler                0.10.0
evdev                 1.4.0
kiwisolver            1.3.2
matplotlib            3.4.3
mediapipe             0.8.7.1
numpy                 1.21.2
opencv-contrib-python 4.5.3.56
opencv-python         4.5.3.56
Pillow                8.3.2
pip                   21.2.4
protobuf              3.17.3
pynput                1.7.3
pyparsing             2.4.7
python-dateutil       2.8.2
python-xlib           0.31
setuptools            57.4.0
six                   1.16.0
wheel                 0.36.2

I get code from your example.

Error:

Traceback (most recent call last):
  File "/var/www/examples/CornerRectangle.py", line 14, in <module>
    lmList, bbox = detector.findPosition(img, draw=False)
AttributeError: 'HandDetector' object has no attribute 'findPosition'

Then I try to install all this packages globally (without venv), and have the same error.

Where is an error?

Can't find documentation for cvzone

I was trying to find documentation for the module but the page only contains the examples of different functions. Is there any proper documentation that lists every function

mediapipe not working

i have been trying to get rid of this error but it has failed to go away, i need help. "C:\Users\DELL\PycharmProjects\pythonProject3\venv\Scripts\python.exe C:\Users\DELL\PycharmProjects\pythonProject3\check.py
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\pythonProject3\check.py", line 2, in
import mediapipe as mp
File "C:\Users\DELL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mediapipe_init_.py", line 15, in
from mediapipe.python import *
File "C:\Users\DELL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\mediapipe\python_init_.py", line 17, in
from mediapipe.python._framework_bindings import resource_util
ImportError: DLL load failed while importing _framework_bindings: The specified module could not be found.

Process finished with exit code 1

color module

i want to detect more than one color at a time how to do that?

AttributeError

Hi!
I get that error " module 'cvzone' has no attribute 'SerialObject' ". And this is my code:

import cvzone
import cv2
from cvzone.HandTrackingModule import HandDetector

cap = cv2.VideoCapture(1)
#detector= cvzone.HandDetector(maxHands=1, detectionCon=0.7)
detector = HandDetector(maxHands=1, detectionCon=0.7)
mySerial = cvzone.SerialObject("COM3", 9600, 1)

Error In findFaceMesh

Version:

Package               Version
--------------------- --------
cvzone                1.4.1
mediapipe             0.8.7.1

Error message:

File "./venv/lib/python3.9/site-packages/cvzone/FaceMeshModule.py", line 48, in findFaceMesh
   self.mpDraw.draw_landmarks(img, faceLms, self.mpFaceMesh.FACE_CONNECTIONS,
AttributeError: module 'mediapipe.python.solutions.face_mesh' has no attribute 'FACE_CONNECTIONS'

Change to None works

FaceMeshModule.py:

self.mpDraw.draw_landmarks(img, faceLms, None,
                                               self.drawSpec, self.drawSpec)

Hand detector bbox with cv2.COLOR_BGR2GRAY causes crashing

EXPECTED BEHAVIOUR

Not randomly crashes when running with BGR2GRAY as a filter

ACTUAL BEHAVIOUR:

Crashes randomly with the error code:

    gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

OS: Linux and Windows has the same issue

cvzone v.1.22.2

Detect key points for legs image

Hello, I was testing the project, it is so good, but the problem is that when I want to detect the key points of half body it does not detect, specially when it is legs, like this image:
image

Is there a way that it can detect the poses in this kind o images?
Thank you so much

Not able to install cvzone

Hi!

I have instaled cvzone and it says it is OK. But when I run the hand program from examples I get the followinmg error:
pi@raspberrypi:~/.local/bin $ python3 handtestcvzone.py
Traceback (most recent call last):
File "/home/pi/.local/bin/handtestcvzone.py", line 1, in
from cvzone.HandTrackingModule import HandDetector
File "/home/pi/.local/lib/python3.9/site-packages/cvzone/HandTrackingModule.py", line 8, in
import mediapipe as mp
File "/usr/local/lib/python3.9/dist-packages/mediapipe/init.py", line 16, in
from mediapipe.python import *
File "/usr/local/lib/python3.9/dist-packages/mediapipe/python/init.py", line 17, in
from mediapipe.python._framework_bindings import resource_util
ModuleNotFoundError: No module named 'mediapipe.python._framework_bindings'

OK, looks like I have to install mediapipe. I tried:

pip install mediapipe

but I got the follwoing error:

pi@raspberrypi:~/.local/bin $ pip3 install mediapipe
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
ERROR: Could not find a version that satisfies the requirement mediapipe (from versions: none)
ERROR: No matching distribution found for mediapipe
WARNING: You are using pip version 21.3.1; however, version 22.0.4 is available.
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.

Any suggestions?

Issue with mediapipe module not found

Hi,
I am running the handtracker code you put in the lesson but i am getting below error:

Traceback (most recent call last):
File "E:/Alqadib/VirtuaLab/comp vision/main.py", line 1, in
from cvzone.HandTrackingModule import HandDetector
File "C:\Users\zeesh\AppData\Local\Programs\Python\Python39\lib\site-packages\cvzone\HandTrackingModule.py", line 10, in
import mediapipe as mp
ModuleNotFoundError: No module named 'mediapipe'

below is the code in case you wanna see, even tgough i installed it using: pip3 install mediapipe --user

`from cvzone.HandTrackingModule import HandDetector
import cv2
import socket

cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
success, img = cap.read()
h, w, _ = img.shape
detector = HandDetector(detectionCon=0.8, maxHands=2)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverAddressPort = ("127.0.0.1", 5052)

while True:
# Get image frame
success, img = cap.read()
# Find the hand and its landmarks
hands, img = detector.findHands(img) # with draw
# hands = detector.findHands(img, draw=False) # without draw
data = []

if hands:
    # Hand 1
    hand = hands[0]
    lmList = hand["lmList"]  # List of 21 Landmark points
    for lm in lmList:
        data.extend([lm[0], h - lm[1], lm[2]])

    sock.sendto(str.encode(str(data)), serverAddressPort)

# Display
cv2.imshow("Image", img)
cv2.waitKey(1)

`

SerialModule.py - readLine() should be within try block.

In SerialModule.py, in getData() line 67: the block of code containing the readLine() should be within a try block, maybe something like this (although there may be a better way):

    def getData(self):
        """
        :param numOfVals: number of vals to retrieve
        :return: list of data received
        """
        try:
            data = self.ser.readline()
            data = data.decode("utf-8")
            data = data.split('#')
            dataList = []
            [dataList.append(d) for d in data]
            return dataList[:-1]
        except Exception as e:
            print(e)
            self.ser.close

See this question on Stack Exchange, Get data from Arduino in Python.

Missing cvzone HandDetector/HandTrackingModule

hello I wanted to replicated the Ai robotic arm project from Murtaza’s Workshop Youtube channel (https://youtu.be/7KV5489rL3c) where he outputs $00000 string to control the arm. As for me when i am trying to write the code cvzone.HandDetector it gives an error "attribute not present" which means that there is no HandTrackingModule in current version of the cvzone so if it is something that can be solved then it would really be very very helpful for me as i am a beginner. thank you

PoseDetector findPosition with upBody parameter

The parameter upBody was removed from PoseDetector because mediapipe removed also. But we can kept the functionality with this code.

``
class PoseDetector:

def __init__(self, mode=False, smooth=True,
             detectionCon=0.5 , trackCon=0.5, 
             upBody = False):    # RESTORE PARAMETER
   ...
   self.upBody = upBody      
   ...

 def findPosition(self, img, draw=True, bboxWithHands=False):
        ...
        if bboxWithHands:
            x1 = self.lmList[16][1] - ad
            x2 = self.lmList[15][1] + ad
        else:
            x1 = self.lmList[12][1] - ad
            x2 = self.lmList[11][1] + ad
        
        # NEW CODE 
        if self.upBody:
            y2 = self.lmList[23][2] + ad
            y1 = self.lmList[1][2] - ad
        else:
            y2 = self.lmList[29][2] + ad
            y1 = self.lmList[1][2] - ad
        ...

...

module 'HandTrackingModule' has no attribute 'handDetector'

I tried to run the below mentioned Example code. but I got an Attribute error saying

AttributeError: module 'HandTrackingModule' has no attribute 'handDetector'.

Help me to resolve this issue.

the Example code is.
import cv2
import numpy as np
import HandTrackingModule as htm
import time
import autopy

#######
wCam, hCam = 640, 480
frameR = 100 # Frame Reduction
smoothening = 5
#######

pTime = 0
plocX, plocY = 0,0
clocX, clocY = 0,0

cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)

detector = htm.handDetector(maxHands=1)
wScr, hScr = autopy.screen.size()

while True:
# 1. Find hand Landmarks
success, img = cap.read()
img = detector.findHands(img)
lmList, bbox = detector.findPosition(img)

# 2. Get the tip of the index and middle fingers

if len(lmList) != 0:
    x1, y1 = lmList[8][1:]
    x2, y2 = lmList[12][1:]

    #print(x1,y1,x2,y2)

    # 3. Check which fingers are up
    fingers = detector.fingersUp()
    #print(fingers)
    cv2.rectangle(img, (frameR, frameR), (wCam - frameR, hCam - frameR),
                  (255, 0, 255), 2)

    # 4. Only Index Finger : Moving Mode
    if fingers[1]==1 and fingers[2] == 1:

        # 5. Covert Coordinates
        x3 = np.interp(x2, (frameR, wCam-frameR), (0, wScr))
        y3 = np.interp(y2, (frameR, hCam-frameR), (0, hScr))

        # 6. Smoothen Values
        clocX = plocX + (x3 - plocX) / smoothening
        clocY = plocY + (y3 - plocY) / smoothening

        # 7. Move Mouse
        autopy.mouse.move(wScr-clocX, clocY)
        cv2.circle(img,(x1,y1),15,(255,0,255),cv2.FILLED )
        plocX, plocY = clocX, clocY
        length, img, lineInfo = detector.findDistance(8, 12, img)

        if length < 40:
            # 10. Click mouse if distance short
            cv2.circle(img, (lineInfo[4], lineInfo[5])
                       , 15, (0, 255, 0), cv2.FILLED)
            autopy.mouse.click()
            autopy.mouse.click()
            time.sleep(2.0)

    # 8. Both Index and Middle fingers are up : Clicking Mode
    #if fingers[1] == 1 and fingers[2] == 1:
        # 9. Find distance between fingers



    # 11. Frame Rate
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (20, 50), cv2.FONT_HERSHEY_PLAIN, 3,
            (255, 0, 0), 3)
# 12. Display
cv2.imshow("Image", img)
cv2.waitKey(1)

'HandDetector' object has no attribute 'findPosition'

Hi,

I am not able to get the findPosition feature in cvzone. Here is a simple code:

import cv2
from cvzone.HandTrackingModule import HandDetector

cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)

detector = HandDetector(detectionCon=0.8)

while True:
    
    success, img = cap.read()
    img = detector.findHands(img)
    lmList, _ = detector.findPosition(img)
    
    cv2.imshow("Image", img)
    cv2.waitKey(1)

Here is the error:

'HandDetector' object has no attribute 'findPosition'

I am using VS Code on Windows 10. The openCV is working without any issue. Any idea how to resolve it?

'cvzone' has no attribute 'HandDetector'

I tried to run the below mentioned Example code. but I got an Attribute error saying

AttributeError: module 'cvzone' has no attribute 'HandDetector'.

Help me to resolve this issue.

the Example code is.

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)

can we use cvzone to detect hand and face in same time

dear all,
I have an issue when I use cvzone library to detect face and hand together. same time, and make a condition that if i found two hands
screen must be white but actually when i raise only one hand and try to print i found that it is some times to be two hand and screen go to be white then normal , any advice heare

below is my code

import cvzone
from cvzone.HandTrackingModule import HandDetector
from cvzone.FaceDetectionModule import FaceDetector
from cvzone.ClassificationModule import Classifier
from cvzone.PoseModule import PoseDetector
import pyautogui
import cv2
import numpy as np
import time
import mediapipe 
from cvzone.FPS import FPS
import os
import math
import csv
import time
############# our Variables ################################################################################################
#wCam, hCam = 640, 480
wCam, hCam = 1280, 720
cap = cv2.VideoCapture(0)
cap.set(3,wCam)
cap.set(4,hCam)
imageNumber=0
#define the size of  of camerimage that will be over layed on main screen
hs, ws = int(120 * 1), int(150 * 1) 
#show presnter image Mode
presnterImageMode = True
#variable to show white screen in applicatio in case morethan 2 faces detected
faceWhiteImageMode=False
#variable to show white screen in applicatio in case morethan 2 hands detected
handWiteImageMode =False
# handtype used from request to devine which hand will ussed in presntion
handType='Left'



#######################################detectors#############################################################################
detector = HandDetector(staticMode=False,maxHands=2,modelComplexity=1,detectionCon=0.5, minTrackCon=0.5)   
face_detector = FaceDetector(minDetectionCon=0.5, modelSelection=0)
############################################################################################################################
#import presentaion images
folderPath = 'presentaion/'
presnetaionPath = sorted(os.listdir(folderPath),key=len)


while True  :
    _,frame = cap.read()
    frame = cv2.flip(frame,1)
    frame = cv2.resize(frame,(wCam,hCam))
    currentImagePath = os.path.join(folderPath,presnetaionPath[imageNumber])
    currentImage = cv2.imread(currentImagePath)
    #start detect face
    frame , bboxs = face_detector.findFaces(frame,draw=False)
    if bboxs :
        if len(bboxs)>1:
            if faceWhiteImageMode is True:
                    frame = np.ones_like(frame)*255
                    currentImage = np.ones_like(currentImage)*255
                    cvzone.putTextRect(currentImage, f' More Than One Face Detected , Application Paused ',\
                                        (0, 100),border=5,scale=2,thickness=2)
        else:   
            faceWhiteImageMode =False
            for bbox in bboxs :
                facecenter = bbox["center"]
                x, y, w, h = bbox['bbox']
            #draw ine in center of center of face for good presention control
            cv2.line(frame,(0,facecenter[1]),(wCam,facecenter[1]),(0,255,0),10)
    
            #start detection hand
    hands , _ = detector.findHands(frame,draw=False,flipType=True)
    if hands :
        #cas we have two hands  
        if len(hands)>1 :
            handWiteImageMode=True
            if handWiteImageMode is True :
                    frame = np.ones_like(frame)*255
                    currentImage = np.ones_like(currentImage)*255
                    cvzone.putTextRect(currentImage, f' More Than One Hands Detected , Application Paused ', \
                                    (0, 100),border=5,scale=2,thickness=2)
            else:
                handWiteImageMode=False

                        
    #cas we have one hands  
        elif len(hands)==1:
            print('ok')

        #cas we have one hands 
        else:
            pass     
                    
        
    print(handWiteImageMode,len(hands))
    imgPNG = cv2.imread("C:/Users/Geka/Desktop/update.png",cv2.IMREAD_UNCHANGED)
    imgOverlay = cvzone.overlayPNG(currentImage, imgPNG, pos=[0, 650])
    cv2.imshow('imagePresntaion ',frame)
    #cv2.imshow('Presntaion X',currentImage)
    # Display the camera feed
    key = cv2.waitKey(10)
    if key == ord('q'):
        break
    
cap.release()
cv2.destroyAllWindows() 

file crashing

as soon as I run any code involving cvzone the file gets failed and is exited with exit code 1.Camera starts for 1 second and closes automatically.
I don't no what to do

from cvzone.HandTrackingModule import HandDetector
import cv2

cap = cv2.VideoCapture(0)
detector = HandDetector(detectionCon=0.8, maxHands=2)

while True:
# Get image frame
success, img = cap.read()
# Find the hand and its landmarks
hands, img = detector.findHands(img) # with draw
#hands = detector.findHands(img, draw=False) # without draw

if hands:
    # Hand 1
    hand1 = hands[0]
    lmList1 = hand1["lmList"]  # List of 21 Landmark points
    bbox1 = hand1["bbox"]  # Bounding box info x,y,w,h
    centerPoint1 = hand1['center']  # center of the hand cx,cy
    handType1 = hand1["type"]  # Handtype Left or Right

    fingers1 = detector.fingersUp(hand1)

    if len(hands) == 2:
        # Hand 2
        hand2 = hands[1]
        lmList2 = hand2["lmList"]  # List of 21 Landmark points
        bbox2 = hand2["bbox"]  # Bounding box info x,y,w,h
        centerPoint2 = hand2['center']  # center of the hand cx,cy
        handType2 = hand2["type"]  # Hand Type "Left" or "Right"

        fingers2 = detector.fingersUp(hand2)

        # Find Distance between two Landmarks. Could be same hand or different hands
        length, info, img = detector.findDistance(lmList1[8], lmList2[8], img)  # with draw
        # length, info = detector.findDistance(lmList1[8], lmList2[8])  # with draw
# Display
cv2.imshow("Image", img)
if cv2.waitKey(1) == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

for example in this code as soon as I bring 2 hands in the camera screen the file crashes

ValueError: too many values to unpack (expected 2)

i have problem with HandTrackingModule in apple M1

WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1709540722.925530 1 gl_context.cc:344] GL version: 2.1 (2.1 Metal - 83), renderer: Apple M1
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Total MCQ Objects Created: 4
Traceback (most recent call last):
File "/Users/Desktop/pythonProject/main.py", line 66, in
length, info = detector.findDistance(lmList[8], lmList[12])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/CV/lib/python3.11/site-packages/cvzone/HandTrackingModule.py", line 146, in findDistance
x1, y1 = p1
^^^^^^
ValueError: too many values to unpack (expected 2)

UnboundLocalError: local variable 'fingers' referenced before assignment

Hi, I was using the function "fingersUp" and I got the following error "UnboundLocalError: local variable 'fingers' referenced before assignment". When I went to your file "HandTrackingModule.py" between lines 106 and 128 I made the following change:

       myHandType = myHand["type"]
        fingers = []
        myLmList = myHand["lmList"]
        if self.results.multi_hand_landmarks:
            fingers = []
            # Thumb
            if myHandType == "Right":
                if myLmList[self.tipIds[0]][0] > myLmList[self.tipIds[0] - 1][0]:
                    fingers.append(1)
                else:
                    fingers.append(0)
            else:
                if myLmList[self.tipIds[0]][0] < myLmList[self.tipIds[0] - 1][0]:
                    fingers.append(1)
                else:
                    fingers.append(0)

            # 4 Fingers
            for id in range(1, 5):
                if myLmList[self.tipIds[id]][1] < myLmList[self.tipIds[id] - 2][1]:
                    fingers.append(1)
                else:
                    fingers.append(0)
        return fingers

When I did that the error went away.

Pose estimation

I tried to run the pose estimation code on my webcam and it works great for single person but when another person appears in front of the camera it ignores the second person.
so does it support detecting multiple persons or not?

Exception has occurred: ModuleNotFoundError

Exception has occurred: ModuleNotFoundError
No module named 'cvzone'
File "C:\Users\Bella\Desktop\sign language\venv\test'.py", line 1, in
from cvzone.HandTrackingModule import HandDetector

hi! i have already installed cvzone and restarted my laptop, how can I fix this error?

from cvzone.HandTrackingModule import HandDetector
import cv2

cap = cv2.VideoCapture(0)
detector = HandDetector(detectionCon=0.8, maxHands=2)
while True:
# Get image frame
success, img = cap.read()
# Find the hand and its landmarks
hands, img = detector.findHands(img) # with draw
# hands = detector.findHands(img, draw=False) # without draw

if hands:
    # Hand 1
    hand1 = hands[0]
    lmList1 = hand1["lmList"]  # List of 21 Landmark points
    bbox1 = hand1["bbox"]  # Bounding box info x,y,w,h
    centerPoint1 = hand1['center']  # center of the hand cx,cy
    handType1 = hand1["type"]  # Handtype Left or Right

    fingers1 = detector.fingersUp(hand1)

    if len(hands) == 2:
        # Hand 2
        hand2 = hands[1]
        lmList2 = hand2["lmList"]  # List of 21 Landmark points
        bbox2 = hand2["bbox"]  # Bounding box info x,y,w,h
        centerPoint2 = hand2['center']  # center of the hand cx,cy
        handType2 = hand2["type"]  # Hand Type "Left" or "Right"

        fingers2 = detector.fingersUp(hand2)

        # Find Distance between two Landmarks. Could be same hand or different hands
        length, info, img = detector.findDistance(lmList1[8], lmList2[8], img)  # with draw
        # length, info = detector.findDistance(lmList1[8], lmList2[8])  # with draw
# Display
cv2.imshow("Image", img)
cv2.waitKey(1)

cap.release()
cv2.destroyAllWindows()

I got the code from the examples here

Issue with Library

Getting "Cannot find reference error for imread in _int_py"

It does not seem like the cv2 library is installed
Capture

thumb fingerUp detection

if myLmList[self.tipIds[0]][0] > myLmList[self.tipIds[0] - 1][0]:

< > should be swapped for thumb:

            if myHandType == "Right":
                if myLmList[self.tipIds[0]][0] < myLmList[self.tipIds[0] - 1][0]:
                    fingers.append(1)
                else:
                    fingers.append(0)
            else:
                if myLmList[self.tipIds[0]][0] > myLmList[self.tipIds[0] - 1][0]:
                    fingers.append(1)
                else:
                    fingers.append(0)

about PoseDetector()

On August 1, mediapipe modified the parameters of the mpPose.Pose object, resulting in an error when calling PoseDetector

Classifier still uses "self.model = tensorflow.keras.models.load_model(self.model_path)", which causes "AttributeError: module 'tensorflow' has no attribute 'keras'" error

I installed Tensorflow==2.16.1, mediapipe==0.10.1, and cvzone==1.6
When I try to use : from cvzone.ClassificationModule import Classifier
it throws an error. I have tried to change the versions of all the libraries but never works, a new type of error emerges everytime.

Right now with the latest version of all the required libraries it is throwing this error :
deserialized_obj = cls.from_config(cls_config)
File "C:\Users\DELL\Documents\Github\fingerspelling\venv\lib\site-packages\keras\src\ops\operation.py", line 210, in from_config
raise TypeError(
TypeError: Error when deserializing class 'DepthwiseConv2D' using config={'name': 'expanded_conv_depthwise', 'trainable': True, 'dtype': 'float32', 'kernel_size': [3, 3], 'strides': [1, 1], 'padding': 'same', 'data_format': 'channels_last', 'dilation_rate': [1, 1], 'groups': 1, 'activation': 'linear', 'use_bias': False, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'bias_regularizer': None, 'activity_regularizer': None, 'bias_constraint': None, 'depth_multiplier': 1, 'depthwise_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1, 'mode': 'fan_avg', 'distribution': 'uniform', 'seed': None}}, 'depthwise_regularizer': None, 'depthwise_constraint': None}.

Exception encountered: Unrecognized keyword arguments passed to DepthwiseConv2D: {'groups': 1}

ssh without screen

WARNING: Logging before InitGoogleLogging() is written to STDERR
I20220105 09:11:29.031734 22956 gl_context_egl.cc:163] Successfully initialized EGL. Major : 1 Minor: 5
W20220105 09:11:29.031843 22956 gl_context_egl.cc:168] Creating a context with OpenGL ES 3 failed: UNKNOWN: ; eglChooseConfig() returned no matching EGL configuration for RGBA8888 D16 ES3 request.
W20220105 09:11:29.031869 22956 gl_context_egl.cc:169] Fall back on OpenGL ES 2.
Traceback (most recent call last):
File "omer.py", line 18, in
detector1 = PoseDetector()
File "/home/robotik/.local/lib/python3.6/site-packages/cvzone/PoseModule.py", line 36, in init
min_tracking_confidence=self.trackCon)
File "/usr/local/lib/python3.6/dist-packages/mediapipe/python/solutions/pose.py", line 188, in init
outputs=['pose_landmarks'])
File "/usr/local/lib/python3.6/dist-packages/mediapipe/python/solution_base.py", line 254, in init
self._graph.start_run(input_side_packets)
RuntimeError: ; eglChooseConfig() returned no matching EGL configuration for RGBA8888 D16 ES2 request.

'hand detector' object has no attribute 'hands,

I tried to run the below mendioned example code.but I got an Attribute error saying

AttributeError: 'handDetector' object has no attribute 'hands'

Help me to resolve this issue

The Examble code is,

import cv2
import mediapipe as mp
import time

class handDetector():
def int(self, mode = False, MaxHands = 2, detectionCon = 0.5, trackCon = 0.5):
self.mode = mode
self.MaxHands = MaxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.MaxHands, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
self.tipIds=[4, 8, 12, 16, 20]

def findHands(self, img, draw=True):
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    self.results = self.hands.process(imgRGB)
    if self.results.multi_hand_landmarks:
        for handles in self.results.multi_hand_landmarks:
            if draw:
                self.mpDraw.draw_landmarks(img, handles, self.mpHands.HAND_CONNECTIONS)
    return img

def findPosition(self, img, handNo=0, draw=True):
    self.lmlist = []
    if self.results.multi_hand_landmarks:
        myHand = self. results.multi_hand_landmarks[handNo]
        for id,lm in enumerate(myHand.landmark):
            h, w, c = img.shape
            cx, cy = int(lm.x * w), int(lm.y * h)
            self.lmlist.append([id, cx, cy])
            if draw:
                cv2.circle(img, (cx, cy), 15, (255, 0, 0), cv2.FILLED)
    return self.lmlist

def fingerUp(self):
    fingers = []
    if self.lmlist[self.tipIds[0]][1]<self.lmlist [self.tipIds[0]-1][1]:
        fingers.append(1)
    else:
        fingers.append(0)

    for id in range(1, 5):
        if self.lmlist[self.tipIds[id]][2]<self.lmlist[self.tipIds[id]-2][2]:
            fingers.append(1)
        else:
            fingers.append(0)
    return fingers

def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(0)
detector = handDetector()
while True:
success, img = cap.read()
img = detector.findHands(img)
lmlist = detector.findPosition(img)
if len(lmlist)!= 0:
print(lmlist[4])
cTime=time.time()
fps=1/(cTime-pTime)
pTime=cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0 , 0), 3)
cv2.imshow('img',img)
cv2.waitKey(1)
if name=="main":
main()

Installation Error

when i am going to install this using pip install cvzone this command it shows an error :(


`C:\Users\Thishakya>pip install cvzone
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting cvzone
  Using cached https://files.pythonhosted.org/packages/ce/d4/b2e4e5a18b1b1b7de159f28494a0b320c9624c354f82bcd135306a4c6c87/cvzone-1.5.3.tar.gz
Collecting opencv-python (from cvzone)
  Using cached https://files.pythonhosted.org/packages/a1/d6/8422797e35f8814b1d9842530566a949d9b5850a466321a6c1d5a99055ee/opencv-python-4.3.0.38.tar.gz
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\python27\python.exe' 'c:\python27\lib\site-packages\pip\_vendor\pep517\_in_process.py' get_requires_for_build_wheel 'c:\users\thisha~1\appdata\local\temp\tmpzrfart'
       cwd: c:\users\thisha~1\appdata\local\temp\pip-install-v1econ\opencv-python
  Complete output (22 lines):
  Traceback (most recent call last):
    File "c:\python27\lib\site-packages\pip\_vendor\pep517\_in_process.py", line 207, in <module>
      main()
    File "c:\python27\lib\site-packages\pip\_vendor\pep517\_in_process.py", line 197, in main
      json_out['return_val'] = hook(**hook_input['kwargs'])
    File "c:\python27\lib\site-packages\pip\_vendor\pep517\_in_process.py", line 54, in get_requires_for_build_wheel
      return hook(config_settings)
    File "c:\users\thisha~1\appdata\local\temp\pip-build-env-aeysgp\overlay\Lib\site-packages\setuptools\build_meta.py", line 146, in get_requires_for_build_wheel
      return self._get_build_requires(config_settings, requirements=['wheel'])
    File "c:\users\thisha~1\appdata\local\temp\pip-build-env-aeysgp\overlay\Lib\site-packages\setuptools\build_meta.py", line 127, in _get_build_requires
      self.run_setup()
    File "c:\users\thisha~1\appdata\local\temp\pip-build-env-aeysgp\overlay\Lib\site-packages\setuptools\build_meta.py", line 243, in run_setup
      self).run_setup(setup_script=setup_script)
    File "c:\users\thisha~1\appdata\local\temp\pip-build-env-aeysgp\overlay\Lib\site-packages\setuptools\build_meta.py", line 142, in run_setup
      exec(compile(code, __file__, 'exec'), locals())
    File "setup.py", line 448, in <module>
      main()
    File "setup.py", line 99, in main
      % {"ext": re.escape(sysconfig.get_config_var("EXT_SUFFIX"))}
    File "c:\python27\lib\re.py", line 210, in escape
      s = list(pattern)
  TypeError: 'NoneType' object is not iterable
  ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\python27\python.exe' 'c:\python27\lib\site-packages\pip\_vendor\pep517\_in_process.py' get_requires_for_build_wheel 'c:\users\thisha~1\appdata\local\temp\tmpzrfart' Check the logs for full command output.
WARNING: You are using pip version 19.2.3, however version 20.3.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\Users\Thishakya> Getting requirements to build wheel ... errorpip install -e .
'Getting' is not recognized as an internal or external command,
operable program or batch file.`


TypeError: create_bool(): incompatible function arguments. The following argument types are supported: 1. (arg0: bool) -> mediapipe.python._framework_bindings.packet.Packet

hi, when im trying Pose Estimation an error occured. says its abt the arguments in "detector = PoseDetector(upBody=1)".
details refer to below:

Traceback (most recent call last):
  File "F:/embedding_sys_proj/arduino_files/esp32 cam/rev_imgs_from_esp32cam/eyesteady.py", line 54, in <module>
    detector = PoseDetector(upBody=1)
  File "D:\python37\lib\site-packages\cvzone\PoseModule.py", line 35, in __init__
    self.detectionCon, self.trackCon)
  File "D:\python37\lib\site-packages\mediapipe\python\solutions\pose.py", line 166, in __init__
    outputs=['pose_landmarks', 'pose_world_landmarks', 'segmentation_mask'])
  File "D:\python37\lib\site-packages\mediapipe\python\solution_base.py", line 260, in __init__
    for name, data in (side_inputs or {}).items()
  File "D:\python37\lib\site-packages\mediapipe\python\solution_base.py", line 260, in <dictcomp>
    for name, data in (side_inputs or {}).items()
  File "D:\python37\lib\site-packages\mediapipe\python\solution_base.py", line 513, in _make_packet
    return getattr(packet_creator, 'create_' + packet_data_type.value)(data)
TypeError: create_bool(): incompatible function arguments. The following argument types are supported:
    1. (arg0: bool) -> mediapipe.python._framework_bindings.packet.Packet

Invoked with: 0.5
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-_xlv4eex\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

is this something wrong abt the version of mediapipe?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.