-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFace_detect.py
203 lines (172 loc) · 7.61 KB
/
Face_detect.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Importing Libraries
import cv2
import face_recognition
import glob
import math
import os
import datetime
import numpy as np
import tkinter as tk
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
root = tk.Tk()
root.config(bg="SpringGreen2")
Title = root.title("Video Analysis")
root.geometry('661x300')
root.resizable(0, 0)
def rootwindow():
path = PathTextBox.get('1.0', 'end-1c')
# Take video and store its frames in a folder
count = 0
frames = 0
cap = cv2.VideoCapture(path) # capturing the video from the given path
frameRate = cap.get(5) # frame rate
# Give full path name where
cwd = os.getcwd()
path = os.path.join(cwd,"Frames")
# Check if the "frames" folder already exists
if not os.path.exists(path):
# If it doesn't exist, create the "frames" folder
os.mkdir(path)
while cap.isOpened():
frameId = cap.get(1) # current frame number
ret, frame = cap.read()
if not ret:
break
if frameId % math.floor(frameRate) == 0:
filename = "frame%d.jpg" % count
count += 1
cv2.imwrite(os.path.join(path, filename), frame)
cap.release()
# Input Image
input_image = face_recognition.load_image_file('saved_img.jpg')
input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
# Input Image Face Recognition and encoding
faceLoc = face_recognition.face_locations(input_image)
encode_input = face_recognition.face_encodings(input_image)
# for face_Loc in faceLoc:
# y11, x22, y22, x11 = face_Loc
# cv2.rectangle(input_image, (x11, y11), (x22, y22), (0, 255, 0), 2)
# List all files in the folder
images = os.listdir(path)
matches_list = []
for img in images:
# Reading Different Frames
file_path = os.path.join(path,img)
input_test = face_recognition.load_image_file(file_path)
input_test = cv2.cvtColor(input_test, cv2.COLOR_BGR2RGB)
# Frames face location recognition and encoding
face_test = face_recognition.face_locations(input_test)
encode_test = face_recognition.face_encodings(input_test, face_test)
for encode_face, face_location in zip(encode_test, face_test):
matches = face_recognition.compare_faces(encode_face, encode_input)
faceDis = face_recognition.face_distance(encode_input, encode_face)
matchIndex = np.nanargmin(faceDis)
if matches[matchIndex]:
frames = frames + 1
if matches[0] == False:
face_detect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# face_detect = cv2.CascadeClassifier(r'C:\Users\subha\AppData\Local\Programs\Python310\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
face_data = face_detect.detectMultiScale(input_test,
scaleFactor=1.3,
minNeighbors=4)
y1, x2, y2, x1 = face_location
cv2.rectangle(input_test, (x1, y1), (x2, y2), (0, 255, 0), 2)
roi = input_test[y1:y2, x1:x2]
roi = cv2.GaussianBlur(roi, (23, 23), 30)
input_test[y1:y1 + roi.shape[0], x1:x1 + roi.shape[1]] = roi
cv2.imshow("Frame", input_test)
cv2.waitKey(0)
sec = int(frames - 1)
seconds = "Duration in seconds : " + str(sec)
dwell = "Video time : " + str(datetime.timedelta(seconds=sec))
new_Window = tk.Toplevel(root)
new_Window.geometry('300x100')
new_Window.config(bg="SpringGreen2")
new_Window.title('Result')
new_Window.resizable(0, 0)
label_1 = Label(new_Window, text=seconds)
label_1.place(x=150, y=30, anchor=CENTER)
label_2 = Label(new_Window, text=dwell)
label_2.place(x=150, y=65, anchor=CENTER)
def openfile():
name = askopenfilename(initialdir="/",
filetypes=[("Mp4 Files", "*.mp4")],
title="Choose a file."
)
PathTextBox.delete("1.0", END)
PathTextBox.insert(END, name)
# Capture Image from webcam
def open_webcam():
webcam = cv2.VideoCapture(0)
while True:
try:
check, frame = webcam.read()
cv2.imshow("Capturing", frame)
key = cv2.waitKey(1)
if key == ord('s'):
cv2.imwrite(filename='saved_img.jpg', img=frame)
webcam.release()
img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE)
# img_new = cv2.imshow("Captured Image", img_new)
cv2.waitKey(100)
cv2.destroyAllWindows()
img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
img_ = cv2.resize(gray, (28, 28))
# img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
break
elif key == ord('q'):
print("Turning off camera.")
webcam.release()
print("Camera off.")
cv2.destroyAllWindows()
break
except KeyboardInterrupt:
print("Turning off camera.")
webcam.release()
print("Camera off.")
print("Program ended.")
cv2.destroyAllWindows()
break
# Delete all older frames
def delete():
directory = r'D:\Video-Analysis-Using-Open-Cv-master\Frames'
files_in_directory = os.listdir(directory)
filtered_files = [file for file in files_in_directory if file.endswith(".jpg")]
for file in filtered_files:
path_to_file = os.path.join(directory, file)
os.remove(path_to_file)
def confirmation():
MsgBox = tk.messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application',
icon='warning')
if MsgBox == 'yes':
root.destroy()
else:
tk.messagebox.showinfo('Return', 'You will now return to the application screen')
label1 = Label(root, text=" Video Analysis Using OpenCV ", background='springgreen4', foreground="white",
font=("Comic Sans MS", 15), anchor=N)
label1.place(x=330, y=23, anchor=CENTER)
refresh = Button(root, text=" Refresh ", command=delete, bg="springgreen4", fg='white', font=("Comic Sans MS", 10),
borderwidth=0, relief="sunken")
refresh.place(x=587, y=110)
PathTextBox = Text(root, height=2, borderwidth=0, font=("Comic Sans MS", 10), relief="sunken")
PathTextBox.place(x=10, y=150)
Webcam = Button(root, text=" Open WebCam ", command=open_webcam, bg='springgreen4', fg='white',
font=("Comic Sans MS", 10), borderwidth=0, relief="sunken")
Webcam.place(x=135, y=77)
Video_upload = Button(root, text=" Browse video ", command=openfile, bg='springgreen4', fg='white',
font=("Comic Sans MS", 10), borderwidth=0, relief="sunken")
Video_upload.place(x=135, y=112)
PathLabel = Label(root, text=" Upload Image : ", font=("Comic Sans MS", 10))
PathLabel.place(x=10, y=80)
PathLabel1 = Label(root, text=" Upload Video : ", font=("Comic Sans MS", 10))
PathLabel1.place(x=10, y=115)
ReadButton = Button(root, text=" Submit ", command=rootwindow, bg='springgreen4', fg='white',
font=("Comic Sans MS", 13), borderwidth=0, relief="sunken")
ReadButton.place(x=286, y=200)
exitbutton = Button(root, text=" Exit ", command=confirmation, bg="springgreen4", fg='white',
font=("Comic Sans MS", 13), borderwidth=0, relief="sunken")
exitbutton.place(x=298, y=250)
root.mainloop()