| | |
| | | import sys, os, argparse |
| | | |
| | | import numpy as np |
| | | import cv2 |
| | | import matplotlib.pyplot as plt |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | from torch.autograd import Variable |
| | |
| | | import torchvision |
| | | import torch.nn.functional as F |
| | | |
| | | import cv2 |
| | | import matplotlib.pyplot as plt |
| | | import sys |
| | | import os |
| | | import argparse |
| | | |
| | | import datasets |
| | | import hopenet |
| | | import utils |
| | | import datasets, hopenet, utils |
| | | |
| | | def parse_args(): |
| | | """Parse input arguments.""" |
| | |
| | | pitch_error += torch.sum(torch.abs(pitch_predicted - label_pitch)) |
| | | roll_error += torch.sum(torch.abs(roll_predicted - label_roll)) |
| | | |
| | | # Save images with pose cube. |
| | | # TODO: fix for larger batch size |
| | | # Save first image in batch with pose cube or axis. |
| | | if args.save_viz: |
| | | name = name[0] |
| | | if args.dataset == 'BIWI': |
| | |
| | | if args.batch_size == 1: |
| | | error_string = 'y %.2f, p %.2f, r %.2f' % (torch.sum(torch.abs(yaw_predicted - label_yaw)), torch.sum(torch.abs(pitch_predicted - label_pitch)), torch.sum(torch.abs(roll_predicted - label_roll))) |
| | | cv2.putText(cv2_img, error_string, (30, cv2_img.shape[0]- 30), fontFace=1, fontScale=1, color=(0,0,255), thickness=1) |
| | | utils.plot_pose_cube(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0]) |
| | | # utils.plot_pose_cube(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0], size=100) |
| | | utils.draw_axis(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0], tdx = 200, tdy= 200, size=100) |
| | | cv2.imwrite(os.path.join('output/images', name + '.jpg'), cv2_img) |
| | | |
| | | print('Test error in degrees of the model on the ' + str(total) + |
| | |
| | | import sys, os, argparse |
| | | |
| | | import numpy as np |
| | | import cv2 |
| | | import matplotlib.pyplot as plt |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | from torch.autograd import Variable |
| | |
| | | import torchvision |
| | | import torch.nn.functional as F |
| | | from PIL import Image |
| | | |
| | | import cv2 |
| | | import matplotlib.pyplot as plt |
| | | import sys, os, argparse |
| | | |
| | | import datasets, hopenet, utils |
| | | |
| | |
| | | if not os.path.exists(args.video_path): |
| | | sys.exit('Video does not exist') |
| | | |
| | | # ResNet101 with 3 outputs. |
| | | # model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 23, 3], 66) |
| | | # ResNet50 |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66, 0) |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) |
| | | |
| | | print 'Loading snapshot.' |
| | | # Load snapshot |
| | |
| | | img_shape = img.size() |
| | | img = img.view(1, img_shape[0], img_shape[1], img_shape[2]) |
| | | img = Variable(img).cuda(gpu) |
| | | |
| | | yaw, pitch, roll, angles = model(img) |
| | | |
| | | yaw_predicted = F.softmax(yaw) |
| | | pitch_predicted = F.softmax(pitch) |
| | | roll_predicted = F.softmax(roll) |
| | | # Get continuous predictions in degrees. |
| | | yaw_predicted = torch.sum(yaw_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | pitch_predicted = torch.sum(pitch_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | roll_predicted = torch.sum(roll_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | yaw_predicted = angles[:,0].data[0].cpu() |
| | | pitch_predicted = angles[:,1].data[0].cpu() |
| | | roll_predicted = angles[:,2].data[0].cpu() |
| | | # Print new frame with cube and axis |
| | | txt_out.write(str(frame_num) + ' %f %f %f\n' % (yaw_predicted, pitch_predicted, roll_predicted)) |
| | | # utils.plot_pose_cube(frame, yaw_predicted, pitch_predicted, roll_predicted, (x_min + x_max) / 2, (y_min + y_max) / 2, size = bbox_width) |
| | |
| | | import sys, os, argparse |
| | | |
| | | import numpy as np |
| | | import cv2 |
| | | import matplotlib.pyplot as plt |
| | | |
| | | import torch |
| | | import torch.nn as nn |
| | | from torch.autograd import Variable |
| | |
| | | import torchvision |
| | | import torch.nn.functional as F |
| | | from PIL import Image |
| | | |
| | | import cv2 |
| | | import matplotlib.pyplot as plt |
| | | import sys, os, argparse |
| | | |
| | | import datasets, hopenet, utils |
| | | |
| | |
| | | if not os.path.exists(args.video_path): |
| | | sys.exit('Video does not exist') |
| | | |
| | | # ResNet101 with 3 outputs. |
| | | # model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 23, 3], 66) |
| | | # ResNet50 |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66, 0) |
| | | # ResNet50 structure |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) |
| | | |
| | | print 'Loading snapshot.' |
| | | # Load snapshot |
| | |
| | | y_min = max(y_min, 0) |
| | | x_max = min(frame.shape[1], x_max) |
| | | y_max = min(frame.shape[0], y_max) |
| | | # Crop image |
| | | # Crop face loosely |
| | | img = frame[y_min:y_max,x_min:x_max] |
| | | img = Image.fromarray(img) |
| | | |
| | |
| | | img_shape = img.size() |
| | | img = img.view(1, img_shape[0], img_shape[1], img_shape[2]) |
| | | img = Variable(img).cuda(gpu) |
| | | |
| | | yaw, pitch, roll, angles = model(img) |
| | | |
| | | yaw_predicted = F.softmax(yaw) |
| | | pitch_predicted = F.softmax(pitch) |
| | | roll_predicted = F.softmax(roll) |
| | | # Get continuous predictions in degrees. |
| | | yaw_predicted = torch.sum(yaw_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | pitch_predicted = torch.sum(pitch_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | roll_predicted = torch.sum(roll_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | yaw_predicted = angles[:,0].data[0].cpu() |
| | | pitch_predicted = angles[:,1].data[0].cpu() |
| | | roll_predicted = angles[:,2].data[0].cpu() |
| | | |
| | | # Print new frame with cube and axis |
| | | txt_out.write(str(frame_num) + ' %f %f %f\n' % (yaw_predicted, pitch_predicted, roll_predicted)) |
| | | # utils.plot_pose_cube(frame, yaw_predicted, pitch_predicted, roll_predicted, (x_min + x_max) / 2, (y_min + y_max) / 2, size = bbox_width) |