This repository was archived by the owner on Apr 1, 2026. It is now read-only.
forked from Ahmednull/L2CS-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
160 lines (130 loc) · 5.61 KB
/
Copy pathdemo.py
File metadata and controls
160 lines (130 loc) · 5.61 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
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
import argparse
import numpy as np
import cv2
import time
import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision import transforms
import torch.backends.cudnn as cudnn
import torchvision
from PIL import Image
from utils import select_device, draw_gaze
from PIL import Image, ImageOps
from face_detection import RetinaFace
from model import L2CS
def parse_args():
"""Parse input arguments."""
parser = argparse.ArgumentParser(
description='Gaze evalution using model pretrained with L2CS-Net on Gaze360.')
parser.add_argument(
'--gpu',dest='gpu_id', help='GPU device id to use [0]',
default="0", type=str)
parser.add_argument(
'--snapshot',dest='snapshot', help='Path of model snapshot.',
default='output/snapshots/L2CS-gaze360-_loader-180-4/_epoch_55.pkl', type=str)
parser.add_argument(
'--cam',dest='cam_id', help='Camera device id to use [0]',
default=0, type=int)
parser.add_argument(
'--arch',dest='arch',help='Network architecture, can be: ResNet18, ResNet34, ResNet50, ResNet101, ResNet152',
default='ResNet50', type=str)
args = parser.parse_args()
return args
def getArch(arch,bins):
# Base network structure
if arch == 'ResNet18':
model = L2CS( torchvision.models.resnet.BasicBlock,[2, 2, 2, 2], bins)
elif arch == 'ResNet34':
model = L2CS( torchvision.models.resnet.BasicBlock,[3, 4, 6, 3], bins)
elif arch == 'ResNet101':
model = L2CS( torchvision.models.resnet.Bottleneck,[3, 4, 23, 3], bins)
elif arch == 'ResNet152':
model = L2CS( torchvision.models.resnet.Bottleneck,[3, 8, 36, 3], bins)
else:
if arch != 'ResNet50':
print('Invalid value for architecture is passed! '
'The default value of ResNet50 will be used instead!')
model = L2CS( torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], bins)
return model
if __name__ == '__main__':
args = parse_args()
cudnn.enabled = True
arch=args.arch
batch_size = 1
cam = args.cam_id
gpu = select_device(args.gpu_id, batch_size=batch_size)
snapshot_path = args.snapshot
transformations = transforms.Compose([
transforms.Resize(448),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
model=getArch(arch, 90)
print('Loading snapshot.')
saved_state_dict = torch.load(snapshot_path)
model.load_state_dict(saved_state_dict)
model.cuda(gpu)
model.eval()
softmax = nn.Softmax(dim=1)
detector = RetinaFace(gpu_id=0)
idx_tensor = [idx for idx in range(90)]
idx_tensor = torch.FloatTensor(idx_tensor).cuda(gpu)
x=0
cap = cv2.VideoCapture(cam)
# Check if the webcam is opened correctly
if not cap.isOpened():
raise IOError("Cannot open webcam")
with torch.no_grad():
while True:
success, frame = cap.read()
start_fps = time.time()
faces = detector(frame)
if faces is not None:
for box, landmarks, score in faces:
if score < .95:
continue
x_min=int(box[0])
if x_min < 0:
x_min = 0
y_min=int(box[1])
if y_min < 0:
y_min = 0
x_max=int(box[2])
y_max=int(box[3])
bbox_width = x_max - x_min
bbox_height = y_max - y_min
# x_min = max(0,x_min-int(0.2*bbox_height))
# y_min = max(0,y_min-int(0.2*bbox_width))
# x_max = x_max+int(0.2*bbox_height)
# y_max = y_max+int(0.2*bbox_width)
# bbox_width = x_max - x_min
# bbox_height = y_max - y_min
# Crop image
img = frame[y_min:y_max, x_min:x_max]
img = cv2.resize(img, (224, 224))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
img=transformations(im_pil)
img = Variable(img).cuda(gpu)
img = img.unsqueeze(0)
# gaze prediction
gaze_pitch, gaze_yaw = model(img)
pitch_predicted = softmax(gaze_pitch)
yaw_predicted = softmax(gaze_yaw)
# Get continuous predictions in degrees.
pitch_predicted = torch.sum(pitch_predicted.data[0] * idx_tensor) * 4 - 180
yaw_predicted = torch.sum(yaw_predicted.data[0] * idx_tensor) * 4 - 180
pitch_predicted= pitch_predicted.cpu().detach().numpy()* np.pi/180.0
yaw_predicted= yaw_predicted.cpu().detach().numpy()* np.pi/180.0
draw_gaze(x_min,y_min,bbox_width, bbox_height,frame,(pitch_predicted,yaw_predicted),color=(0,0,255))
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), (0,255,0), 1)
myFPS = 1.0 / (time.time() - start_fps)
cv2.putText(frame, 'FPS: {:.1f}'.format(myFPS), (10, 20),cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 255, 0), 1, cv2.LINE_AA)
cv2.imshow("Demo",frame)
if cv2.waitKey(1) & 0xFF == 27:
break
success,frame = cap.read()