Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
vardanagarwal
GitHub Repository: vardanagarwal/Proctoring-AI
Path: blob/master/face_detection/video_face.py
454 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Thu Jul 2 03:40:59 2020
4
5
@author: hp
6
"""
7
import cv2
8
import dlib
9
from mtcnn.mtcnn import MTCNN
10
import numpy as np
11
12
detector1 = MTCNN()
13
detector2 = dlib.get_frontal_face_detector()
14
modelFile = "models/res10_300x300_ssd_iter_140000.caffemodel"
15
configFile = "models/deploy.prototxt.txt"
16
net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
17
classifier2 = cv2.CascadeClassifier('models/haarcascade_frontalface2.xml')
18
19
cap = cv2.VideoCapture('video/occlusion.mp4')
20
font = cv2.FONT_HERSHEY_SIMPLEX
21
while(True):
22
ret, img = cap.read()
23
if ret == True:
24
img = cv2.resize(img, None, fx=0.5, fy=0.5)
25
height, width = img.shape[:2]
26
img1 = img.copy()
27
img2 = img.copy()
28
img3 = img.copy()
29
# detect faces in the image
30
faces1 = detector1.detect_faces(img)
31
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
32
faces2 = detector2(gray, 1)
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
# display faces on the original image
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
cv2.putText(img, 'mtcnn', (30, 30), font, 1, (255, 255, 0), 2, cv2.LINE_AA)
45
46
for result in faces2:
47
x = result.left()
48
y = result.top()
49
x1 = result.right()
50
y1 = result.bottom()
51
cv2.rectangle(img1, (x, y), (x1, y1), (0, 0, 255), 2)
52
cv2.putText(img1, 'dlib', (30, 30), font, 1, (255, 255, 0), 2, cv2.LINE_AA)
53
54
for i in range(faces3.shape[2]):
55
confidence = faces3[0, 0, i, 2]
56
if confidence > 0.5:
57
box = faces3[0, 0, i, 3:7] * np.array([width, height, width, height])
58
(x, y, x1, y1) = box.astype("int")
59
cv2.rectangle(img2, (x, y), (x1, y1), (0, 0, 255), 2)
60
cv2.putText(img2, 'dnn', (30, 30), font, 1, (255, 255, 0), 2, cv2.LINE_AA)
61
62
for result in faces4:
63
x, y, w, h = result
64
x1, y1 = x + w, y + h
65
cv2.rectangle(img3, (x, y), (x1, y1), (0, 0, 255), 2)
66
cv2.putText(img3, 'haar', (30, 30), font, 1, (255, 255, 0), 2, cv2.LINE_AA)
67
68
h1 = cv2.hconcat([img3, img1])
69
h2 = cv2.hconcat([img, img2])
70
fin = cv2.vconcat([h1, h2])
71
72
cv2.imshow("mtcnn", img)
73
cv2.imshow("dlib", img1)
74
cv2.imshow("dnn", img2)
75
cv2.imshow("haar", img3)
76
if cv2.waitKey(1) & 0xFF == ord('q'):
77
break
78
else:
79
break
80
81
82
83
cap.release()
84
cv2.destroyAllWindows()
85
86