手勢(shì)檢測(cè) |Part 1 數(shù)據(jù)集收集 |c(diǎn)vzone Hand Detector

感謝up我導(dǎo)[微笑]
我剛剛跑完,在我電腦上有兩個(gè)小問(wèn)題:
1.offset那里不能直接設(shè)置成20像素,因?yàn)閤-20或者y-20他倆有可能會(huì)小于零,當(dāng)小于零的時(shí)候會(huì)直接報(bào)錯(cuò)退出imshow的展示窗口
2.我用0.08*y和0.08*x分別代替x和y方向的offset,但是生成圖片會(huì)出現(xiàn)一個(gè)問(wèn)題,crop會(huì)直接把紅色的邊界框一起切下來(lái)
下面是代碼:
import math
import os.path
import CV2
import numpy as np
from cvzone.HandTrackingModule import HandDetector
# 調(diào)用攝像頭,并展示窗口
cap = CV2.VideoCapture(0)
detector = HandDetector(maxHands=1)
folder6 = "./images/six"
imgsize = 300
counter = 0
while True:
success, img = cap.read()
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
img_white = np.ones((imgsize, imgsize, 3), np.uint8) * 255
a_y = 0.08*y
a_x = 0.08*x
imgcrop = img[y - int(a_y):y + h + int(a_y), x - int(a_x):x + w + int(a_x)]
aspectRatio = h / w
if aspectRatio > 1:
k = imgsize / h
wcal = math.ceil(k * w)
resized_img = CV2.resize(imgcrop, (wcal, imgsize))
w_offset = math.ceil((imgsize - wcal) / 2)
img_white[:, w_offset:wcal + w_offset] = resized_img
else:
k = imgsize/w
hcal = math.ceil(k*h)
resized_img = CV2.resize(imgcrop, (imgsize, hcal))
h_offset = math.ceil((imgsize - hcal) / 2)
img_white[h_offset:hcal+h_offset, :] =resized_img
CV2.imshow('imgcrop', img_white)
CV2.imshow('image', img)
key = CV2.waitKey(1)
if key == ord("s"):
counter += 1
CV2.imwrite(os.path.join(folder6,f'{counter}.jpg'),img_white)
print(counter)