- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceDetectionModule.py
More file actions
Latest commit
77 lines (63 loc) · 2.68 KB
/
Copy pathFaceDetectionModule.py
File metadata and controls
77 lines (63 loc) · 2.68 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
importcv2
importmediapipeasmp
importtime
classFaceDetector():
def__init__(self, minDetectionCon=0.5):
self.minDetectionCon=minDetectionCon
self.mpFaceDetection=mp.solutions.face_detection
self.mpDraw=mp.solutions.drawing_utils
self.faceDetection=self.mpFaceDetection.FaceDetection(
self.minDetectionCon)
deffindFaces(self, img, draw=True):
imgRGB=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results=self.faceDetection.process(imgRGB)
# print(self.results)
bboxs= []
ifself.results.detections:
forid, detectioninenumerate(self.results.detections):
bboxC=detection.location_data.relative_bounding_box
ih, iw, ic, =img.shape
bbox=int(bboxC.xmin*iw), int(bboxC.ymin*ih), \
int(bboxC.width*iw), int(bboxC.height*ih)
bboxs.append([id, bbox, detection.score])
ifdraw:
img=self.fancyDraw(img, bbox)
cv2.putText(img,
f'Accurary: {int(detection.score[0]*100)}%',
(bbox[0], bbox[1]-15), cv2.FONT_HERSHEY_DUPLEX,
0.5, (128, 242, 69), 1)
returnimg, bboxs
deffancyDraw(self, img, bbox, ln=30, t=5, rt=1):
x, y, w, h=bbox
x1, y1, =x+w, y+h
cv2.rectangle(img, bbox, (153, 255, 20), rt)
# top left x, y
cv2.line(img, (x, y), (x+ln, y), (153, 255, 20), t)
cv2.line(img, (x, y), (x, y+ln), (153, 255, 20), t)
# top right x1, y
cv2.line(img, (x1, y), (x1-ln, y), (153, 255, 20), t)
cv2.line(img, (x1, y), (x1, y+ln), (153, 255, 20), t)
# bottom left x, y1
cv2.line(img, (x, y1), (x+ln, y1), (153, 255, 20), t)
cv2.line(img, (x, y1), (x, y1-ln), (153, 255, 20), t)
# bottom right x1, y1
cv2.line(img, (x1, y1), (x1-ln, y1), (153, 255, 20), t)
cv2.line(img, (x1, y1), (x1, y1-ln), (153, 255, 20), t)
returnimg
defmain():
cap=cv2.VideoCapture("Poses/girlface.mp4")
pTime=0
detector=FaceDetector()
whileTrue:
success, img=cap.read()
img, bboxs=detector.findFaces(img)
print(bboxs)
cTime=time.time()
fps=1/(cTime-pTime)
pTime=cTime
cv2.putText(img, f'Fps: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_DUPLEX,
1, (128, 242, 69), 2)
cv2.imshow("Image", img)
cv2.waitKey(1)
if__name__=="__main__":
main()