-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataCollection.py
More file actions
77 lines (66 loc) · 2.23 KB
/
dataCollection.py
File metadata and controls
77 lines (66 loc) · 2.23 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# import cv2
# from cvzone.HandTrackingModule import HandDetector
#
# cap = cv2.VideoCapture(0)
# detector = HandDetector(maxHands=2)
# offset = 20
#
# while True:
# success, frame = cap.read()
# hands, frame = detector.findHands(frame)
# if hands:
# hand = hands[0]
# x, y, w, h = hand['bbox']
# imageCrop = frame[y - offset:y + h + offset, x - offset:x + w + offset]
# cv2.imshow('crop', imageCrop)
#
# cv2.imshow('frame', frame)
# cv2.waitKey(1)
import cv2
from cvzone.HandTrackingModule import HandDetector
import numpy as np
import math
import time
cap = cv2.VideoCapture(0)
detector = HandDetector(maxHands=2)
offset = 20
imgSize = 300
folder = "Data/ca"
counter = 0
while True:
success, frame = cap.read()
if not success:
break
hands, frame = detector.findHands(frame)
if hands:
hand = hands[0]
x, y, w, h = hand['bbox']
imgWhite = np.ones((imgSize, imgSize, 3), np.uint8) * 255
# Ensure the bounding box is within the frame dimensions
if w > 0 and h > 0 and x >= 0 and y >= 0 and (x + w) <= frame.shape[1] and (y + h) <= frame.shape[0]:
imageCrop = frame[y - offset:y + h + offset, x - offset:x + w + offset]
aspectRatio = h / w
if aspectRatio > 1:
k = imgSize / h
wCalc = math.ceil(k * w)
imageResize = cv2.resize(imageCrop, (wCalc, imgSize))
imgResizeShape = imageResize.shape
wGap = math.ceil((imgSize - wCalc) / 2)
imgWhite[:, wGap:wCalc + wGap] = imageResize
else:
k = imgSize / w
hCalc = math.ceil(k * h)
imageResize = cv2.resize(imageCrop, (imgSize, hCalc))
imgResizeShape = imageResize.shape
hGap = math.ceil((imgSize - hCalc) / 2)
imgWhite[hGap:hCalc + hGap, :] = imageResize
cv2.imshow('crop', imageCrop)
cv2.imshow('Image White', imgWhite)
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
if key == ord('s'):
counter += 1
cv2.imwrite(f'{folder}/Image_{time.time()}.png', imgWhite)
print(counter)
cap.release()
cv2.destroyAllWindows()