Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import time
import cv2
import numpy as np
from Func.CloseProgram import Close
from Func.Helpers import CalculateWidth, CalculateHeight
from cvzone.HandTrackingModule import HandDetector
offset = 20
size = 300
folder = "Data/E"
counter = 0
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=1)
if not cap.isOpened():
print("Failed to open video capture.")
exit()
while True:
success, img = cap.read()
if not success:
print('can\'t read image')
break
hands, img = detector.findHands(img)
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
imgFixed = np.ones((size, size, 3), np.uint8)
imgCropped = img[y - offset:y + h + offset, x - offset:x + w + offset]
aspectRatio = h / w
if aspectRatio > 1:
CalculateWidth(size, h, w, imgCropped, imgFixed)
else:
CalculateHeight(size, h, w, imgCropped, imgFixed)
cv2.imshow('ImgCropped', imgCropped)
cv2.imshow('ImgFixed', imgFixed)
cv2.imshow("img", img)
key = cv2.waitKey(1)
if key == ord('p'):
Close(cap)
break
if key == ord('s'):
counter += 1
cv2.imwrite(f'{folder}/Image_{time.time()}.jpg', imgFixed)
print(counter)