0

given below is the code that is available on the website as well as on the tutorial videohttps://www.youtube.com/watch?v=wa2ARoUUdU8&t=118s

import cv2
from cvzone.HandTrackingModule import HandDetector
import numpy as np
import math
import time

cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)

offset = 20
imgSize = 300

folder = "Data/C"
counter = 0

while True:
    success, img = cap.read()
    hands, img = detector.findHands(img)
    if hands:
        hand = hands[0]
        x, y, w, h = hand['bbox']

        imgWhite = np.ones((imgSize, imgSize, 3), np.uint8) * 255
        imgCrop = img[y - offset:y + h + offset, x - offset:x + w + offset]

        imgCropShape = imgCrop.shape

        aspectRatio = h / w

        if aspectRatio > 1:
            k = imgSize / h
            wCal = math.ceil(k * w)
            imgResize = cv2.resize(imgCrop, (wCal, imgSize))
            imgResizeShape = imgResize.shape
            wGap = math.ceil((imgSize - wCal) / 2)
            imgWhite[:, wGap:wCal + wGap] = imgResize

        else:
            k = imgSize / w
            hCal = math.ceil(k * h)
            imgResize = cv2.resize(imgCrop, (imgSize, hCal))
            imgResizeShape = imgResize.shape
            hGap = math.ceil((imgSize - hCal) / 2)
            imgWhite[hGap:hCal + hGap, :] = imgResize

        cv2.imshow("ImageCrop", imgCrop)
        cv2.imshow("ImageWhite", imgWhite)

    cv2.imshow("Image", img)
    key = cv2.waitKey(1)
    if key == ord("s"):
        counter += 1
        cv2.imwrite(f'{folder}/Image_{time.time()}.jpg', imgWhite)
        print(counter)

there is a traceback error while running this code(probably problem in importing cvzone module)(screenshots attached)screenshot of error

I tried reinstalling the packages with the same version, used in the video. How do I deal with this problem?

  • Perhaps [this question](https://stackoverflow.com/questions/72706073/attributeerror-partially-initialized-module-cv2-has-no-attribute-gapi-wip-gs) may be of help. – B Remmelzwaal Jan 28 '23 at 21:17
  • Please post the full traceback message as text in a code block like your code sample. – tdelaney Jan 28 '23 at 21:45
  • I think you can simplify the example. This code fails on the first line `import cv2`, so a one line example would do. What other .py files do you have in this script's directory? Could any be improperly imported? – tdelaney Jan 28 '23 at 21:48
  • 2
    Do not post images of code or error messages. Use text. Please read the help section of this forum. – fmw42 Jan 28 '23 at 22:54

0 Answers0