Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
vardanagarwal
GitHub Repository: vardanagarwal/Proctoring-AI
Path: blob/master/face_detection/faces_detection.py
454 views
1
from mtcnn.mtcnn import MTCNN
2
import cv2
3
import dlib
4
import numpy as np
5
import os
6
7
detector1 = MTCNN()
8
detector2 = dlib.get_frontal_face_detector()
9
modelFile = "models/res10_300x300_ssd_iter_140000.caffemodel"
10
configFile = "models/deploy.prototxt.txt"
11
net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
12
13
classifier2 = cv2.CascadeClassifier('models/haarcascade_frontalface2.xml')
14
images = os.listdir('faces')
15
# os.makedirs('faces/dlib')
16
# os.makedirs('faces/mtcnn')
17
# os.makedirs('faces/dnn')
18
# os.makedirs('faces/haar')
19
20
for image in images:
21
img = cv2.imread(os.path.join('faces', image))
22
# img = cv2.resize(img, None, fx=2, fy=2)
23
height, width = img.shape[:2]
24
img1 = img.copy()
25
img2 = img.copy()
26
img3 = img.copy()
27
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
28
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
29
# detect faces in the image
30
faces1 = detector1.detect_faces(img_rgb)
31
32
faces2 = detector2(gray, 2)
33
blob = cv2.dnn.blobFromImage(cv2.resize(img, (300, 300)),
34
1.0, (300, 300), (104.0, 117.0, 123.0))
35
net.setInput(blob)
36
faces3 = net.forward()
37
faces4 = classifier2.detectMultiScale(img)
38
39
#MTCNN
40
for result in faces1:
41
x, y, w, h = result['box']
42
x1, y1 = x + w, y + h
43
cv2.rectangle(img, (x, y), (x1, y1), (0, 0, 255), 2)
44
#DLIB
45
for result in faces2:
46
x = result.left()
47
y = result.top()
48
x1 = result.right()
49
y1 = result.bottom()
50
cv2.rectangle(img1, (x, y), (x1, y1), (0, 0, 255), 2)
51
52
#OPENCV DNN
53
for i in range(faces3.shape[2]):
54
confidence = faces3[0, 0, i, 2]
55
if confidence > 0.5:
56
box = faces3[0, 0, i, 3:7] * np.array([width, height, width, height])
57
(x, y, x1, y1) = box.astype("int")
58
cv2.rectangle(img2, (x, y), (x1, y1), (0, 0, 255), 2)
59
#HAAR
60
for result in faces4:
61
x, y, w, h = result
62
x1, y1 = x + w, y + h
63
cv2.rectangle(img3, (x, y), (x1, y1), (0, 0, 255), 2)
64
65
# cv2.imwrite(os.path.join('faces', 'mtcnn', image), img)
66
# cv2.imwrite(os.path.join('faces', 'dlib', image), img1)
67
# cv2.imwrite(os.path.join('faces', 'dnn', image), img2)
68
# cv2.imwrite(os.path.join('faces', 'haar', image), img3)
69
cv2.imshow("mtcnn", img)
70
cv2.imshow("dlib", img1)
71
cv2.imshow("dnn", img2)
72
cv2.imshow("haar", img3)
73
cv2.waitKey(0)
74
cv2.destroyAllWindows()
75
76