-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataCollection.py
More file actions
107 lines (88 loc) · 3.79 KB
/
Copy pathdataCollection.py
File metadata and controls
107 lines (88 loc) · 3.79 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from time import time
import cv2
import cvzone
from cvzone.FaceDetectionModule import FaceDetector
####################################
classID = 1 # 0 is fake and 1 is real
outputFolderPath = 'Dataset/DataCollect'
confidence = 0.8
save = True
blurThreshold = 35 # Larger is more focus
debug = False
offsetPercentageW = 10
offsetPercentageH = 20
camWidth, camHeight = 640, 480
floatingPoint = 6
####################################
cap = cv2.VideoCapture(0)
cap.set(3, camWidth)
cap.set(4, camHeight)
detector = FaceDetector()
while True:
success, img = cap.read()
imgOut = img.copy()
img, bboxs = detector.findFaces(img, draw=False)
listBlur = [] # True False values indicating if the faces are blur or not
listInfo = [] # The normalized values and the class name for the label txt file
if bboxs:
# bboxInfo - "id","bbox","score","center"
for bbox in bboxs:
x, y, w, h = bbox["bbox"]
score = bbox["score"][0]
# print(x, y, w, h)
# ------ Check the score --------
if score > confidence:
# ------ Adding an offset to the face Detected --------
offsetW = (offsetPercentageW / 100) * w
x = int(x - offsetW)
w = int(w + offsetW * 2)
offsetH = (offsetPercentageH / 100) * h
y = int(y - offsetH * 3)
h = int(h + offsetH * 3.5)
# ------ To avoid values below 0 --------
if x < 0: x = 0
if y < 0: y = 0
if w < 0: w = 0
if h < 0: h = 0
# ------ Find Blurriness --------
imgFace = img[y:y + h, x:x + w]
cv2.imshow("Face", imgFace)
blurValue = int(cv2.Laplacian(imgFace, cv2.CV_64F).var())
if blurValue > blurThreshold:
listBlur.append(True)
else:
listBlur.append(False)
# ------ Normalize Values --------
ih, iw, _ = img.shape
xc, yc = x + w / 2, y + h / 2
xcn, ycn = round(xc / iw, floatingPoint), round(yc / ih, floatingPoint)
wn, hn = round(w / iw, floatingPoint), round(h / ih, floatingPoint)
# print(xcn, ycn, wn, hn)
# ------ To avoid values above 1 --------
if xcn > 1: xcn = 1
if ycn > 1: ycn = 1
if wn > 1: wn = 1
if hn > 1: hn = 1
listInfo.append(f"{classID} {xcn} {ycn} {wn} {hn}\n")
# ------ Drawing --------
cv2.rectangle(imgOut, (x, y, w, h), (255, 0, 0), 3)
cvzone.putTextRect(imgOut, f'Score: {int(score * 100)}% Blur: {blurValue}', (x, y - 0),
scale=2, thickness=3)
if debug:
cv2.rectangle(img, (x, y, w, h), (255, 0, 0), 3)
cvzone.putTextRect(img, f'Score: {int(score * 100)}% Blur: {blurValue}', (x, y - 0),
scale=2, thickness=3)
if save:
if all(listBlur) and listBlur != []:
# ------ Save Image --------
timeNow = time()
timeNow = str(timeNow).split('.')
timeNow = timeNow[0] + timeNow[1]
cv2.imwrite(f"{outputFolderPath}/{timeNow}.jpg", img)
# ------ Save Label Text File --------
for info in listInfo:
f = open(f"{outputFolderPath}/{timeNow}.txt", 'a')
f.write(info)
f.close()
cv2.imshow("Image", imgOut)
cv2.waitKey(1)