Spaces:
Running
Running
abrar-adnan
commited on
Commit
•
96705f2
1
Parent(s):
c66acef
added video processing
Browse files- app.py +64 -3
- models/gaze-recognizer-v1.pkl +3 -0
app.py
CHANGED
@@ -1,12 +1,73 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
|
|
|
|
3 |
|
|
|
|
|
4 |
|
5 |
-
def
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
inputs= gr.Video(),
|
11 |
outputs = gr.Text()
|
12 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import cv2
|
4 |
+
import face_recognition
|
5 |
+
from fastai.vision.all import *
|
6 |
+
import time
|
7 |
|
8 |
+
version = 1
|
9 |
+
model_path = f'models/gaze-recognizer-v{version}.pkl'
|
10 |
|
11 |
+
def video_processing(video):
|
12 |
+
start_time = time.time()
|
13 |
+
# Loop through the frames of the video
|
14 |
+
model = load_learner(model_path)
|
15 |
+
video_capture = cv2.VideoCapture(video)
|
16 |
+
on_camera = 0
|
17 |
+
off_camera = 0
|
18 |
+
total = 0
|
19 |
+
while True:
|
20 |
+
# Read a single frame from the video
|
21 |
+
for i in range(24*30):
|
22 |
+
ret, frame = video_capture.read()
|
23 |
+
if not ret:
|
24 |
+
break
|
25 |
|
26 |
+
# If there are no more frames, break out of the loop
|
27 |
+
if not ret:
|
28 |
+
break
|
29 |
+
|
30 |
+
# Convert the frame to RGB color (face_recognition uses RGB)
|
31 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
32 |
+
|
33 |
|
34 |
+
# Find all the faces in the frame using a pre-trained convolutional neural network.
|
35 |
+
face_locations = face_recognition.face_locations(gray)
|
36 |
+
#face_locations = face_recognition.face_locations(gray, number_of_times_to_upsample=0, model="cnn")
|
37 |
+
|
38 |
+
if len(face_locations) > 0:
|
39 |
+
# Show the original frame with face rectangles drawn around the faces
|
40 |
+
for top, right, bottom, left in face_locations:
|
41 |
+
# cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
|
42 |
+
face_image = gray[top:bottom, left:right]
|
43 |
+
|
44 |
+
# Resize the face image to the desired size
|
45 |
+
resized_face_image = cv2.resize(face_image, (128,128))
|
46 |
+
|
47 |
+
# Predict the class of the resized face image using the model
|
48 |
+
result = model.predict(resized_face_image)
|
49 |
+
print(result[0])
|
50 |
+
if(result[0] == 'on_camera'): on_camera = on_camera + 1
|
51 |
+
elif(result[0] == 'off_camera'): off_camera = off_camera + 1
|
52 |
+
total = total + 1
|
53 |
+
|
54 |
+
# cv2.imshow('Video', frame)
|
55 |
+
|
56 |
+
# If the user presses the 'q' key, exit the loop
|
57 |
+
# if cv2.waitKey(1) & 0xFF == ord('q'):
|
58 |
+
# break
|
59 |
+
gaze_percentage = on_camera/total*100
|
60 |
+
# print(total,on_camera,off_camera)
|
61 |
+
# print(f'focus perfectage = {on_camera/total*100}')
|
62 |
+
# Release the video capture object and close all windows
|
63 |
+
video_capture.release()
|
64 |
+
cv2.destroyAllWindows()
|
65 |
+
end_time = time.time()
|
66 |
+
print(f'Time taken: {end_time-start_time}')
|
67 |
+
return gaze_percentage
|
68 |
+
|
69 |
+
|
70 |
+
demo = gr.Interface(fn = video_processing,
|
71 |
inputs= gr.Video(),
|
72 |
outputs = gr.Text()
|
73 |
)
|
models/gaze-recognizer-v1.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6f553b719d666374b7fd46f7df5310561bb3520d4fb0dcc7cf523b23d87caebe
|
3 |
+
size 87465936
|