Ejemplo n.º 1
0
def get_emotion(current_face, gray):
    """
    inputs:
    current_face: (xmin, ymin, w, h)
    gray: grayscale frame
    
    outputs:
    emotion: from -1 to 1, 1 being most positive, -1 being most negative
    """
    cut_size = 44
    transform_test = transforms.Compose([
        transforms.TenCrop(cut_size),
        transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
    ])   
    
    # crop face from grayscale frame
    xmin = current_face[0]
    xmax = current_face[0] + current_face[2]
    ymin = current_face[1]
    ymax = current_face[1] + current_face[3]
    face = gray[ymin:ymax,xmin:xmax]
    
    # resize and transform
    face = (resize(face, (48,48), mode='symmetric')*255).astype('uint8')
    img = face[:, :, np.newaxis]
    img = np.concatenate((img, img, img), axis=2)
    img = Image.fromarray(img)
    inputs = transform_test(img)

    class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
    
    # set device, load model
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    net = vgg.VGG('VGG19')
    checkpoint = torch.load('PrivateTest_model.t7')
    net.load_state_dict(checkpoint['net'])
    net.to(device)
    net.eval()

    ncrops, c, h, w = np.shape(inputs)

    inputs = inputs.view(-1, c, h, w)
    inputs = inputs.to(device)
    with torch.no_grad():
        inputs = Variable(inputs)
    outputs = net(inputs)
    outputs_avg = outputs.view(ncrops, -1).mean(0)  # avg over crops
    weights = np.array([-0.4,-0.1,-0.1,0.8,-0.4,0.2])
    score = F.softmax(outputs_avg, dim=0)
    emotion_score = np.sum(score.cpu().detach().numpy()[:6]**0.5*weights)
    

    return emotion_score
    def img_10crop(self,img):
        """
        数据增强
        将图片在左上角,左下角,右上角,右下角,中心进行切割和并做镜像操作,这样的操作使得数据库扩大了10倍
        """
        cut_size = [44, 44]  # 44
        transform_test = transforms.Compose([
            transforms.TenCrop(cut_size),
            transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
        ])
        inputs = transform_test(img)

        return inputs
Ejemplo n.º 3
0
def getEmotionScore(imageDirectory, resizeImg):
    cut_size = 44

    transform_test = transforms.Compose([
        transforms.TenCrop(cut_size),
        transforms.Lambda(lambda crops: torch.stack(
            [transforms.ToTensor()(crop) for crop in crops])),
    ])

    raw_img = io.imread(imageDirectory)

    if resizeImg == True:
        raw_img = cropND(raw_img, (400, 400))

    io.imsave("screenshot.jpg", raw_img)

    gray = rgb2gray(raw_img)
    gray = resize(gray, (48, 48), mode='symmetric').astype(np.uint8)

    img = gray[:, :, np.newaxis]

    img = np.concatenate((img, img, img), axis=2)
    img = Image.fromarray(img)
    inputs = transform_test(img)

    net = VGG('VGG19')
    checkpoint = torch.load(
        os.path.join('FER2013_VGG19', 'PrivateTest_model.t7'))
    net.load_state_dict(checkpoint['net'])
    net.cuda()
    net.eval()

    ncrops, c, h, w = np.shape(inputs)

    inputs = inputs.view(-1, c, h, w)
    inputs = inputs.cuda()
    inputs = Variable(inputs, volatile=True)
    outputs = net(inputs)

    outputs_avg = outputs.view(ncrops, -1).mean(0)  # avg over crops

    score = F.softmax(outputs_avg)
    _, predicted = torch.max(outputs_avg.data, 0)

    scoreDict = {}

    for i in range(len(class_names)):
        scoreDict[class_names[i]] = float(score.data.cpu().numpy()[i])

    scoreDict["expression"] = str(class_names[int(predicted.cpu().numpy())])
    return scoreDict
Ejemplo n.º 4
0
def img2mood(raw_img):

    cut_size = 44

    transform_test = transforms.Compose([
        transforms.TenCrop(cut_size),
        transforms.Lambda(lambda crops: torch.stack(
            [transforms.ToTensor()(crop) for crop in crops])),
    ])
    #raw_img = io.imread('images/1.jpg')
    gray = rgb2gray(raw_img)
    gray = resize(gray, (48, 48), mode='symmetric').astype(np.uint8)

    img = gray[:, :, np.newaxis]

    img = np.concatenate((img, img, img), axis=2)
    img = Image.fromarray(img)
    inputs = transform_test(img)

    class_names = [
        'Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'
    ]

    net = VGG('VGG19')
    checkpoint = torch.load(os.path.join('FER2013_VGG19',
                                         'PrivateTest_model.t7'),
                            map_location='cpu')
    net.load_state_dict(checkpoint['net'])
    #net.cuda()
    net.eval()

    ncrops, c, h, w = np.shape(inputs)

    inputs = inputs.view(-1, c, h, w)
    #inputs = inputs.cuda()
    inputs = Variable(inputs, volatile=True)
    outputs = net(inputs)

    outputs_avg = outputs.view(ncrops, -1).mean(0)  # avg over crops

    score = F.softmax(outputs_avg)
    _, predicted = torch.max(outputs_avg.data, 0)

    emojis_img = io.imread('images/emojis/%s.png' %
                           str(class_names[int(predicted.cpu().numpy())]))

    print("The Expression is %s" %
          str(class_names[int(predicted.cpu().numpy())]))
    return int(predicted.cpu().numpy()), emojis_img
    '''
Ejemplo n.º 5
0
def predict_emotion(file_name):
    raw_img = io.imread(file_name)
    cut_size = 44

    transform_test = transforms.Compose([
        transforms.TenCrop(cut_size),
        transforms.Lambda(lambda crops: torch.stack(
            [transforms.ToTensor()(crop) for crop in crops])),
    ])

    gray = rgb2gray(raw_img)
    gray = resize(gray, (48, 48), mode='symmetric').astype(np.uint8)

    img = gray[:, :, np.newaxis]

    img = np.concatenate((img, img, img), axis=2)  #(48,48,3)
    img = Image.fromarray(img)  #转化成PIL文件
    inputs = transform_test(img)  #transform

    class_names = [
        'Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'
    ]

    net = VGG('VGG19')  #加载网络模型
    checkpoint = torch.load(os.path.join(
        'F:/Study/First_Grade/Winter_Vacation/Real-time-face-recognition-master/Facial-Expression-Recognition.Pytorch-master/FER2013_VGG19',
        'PrivateTest_model.t7'),
                            map_location='cpu')
    net.load_state_dict(checkpoint['net'])
    # net.cuda()
    net.eval()

    ncrops, c, h, w = np.shape(inputs)

    inputs = inputs.view(-1, c, h, w)
    # inputs = inputs.cuda()
    inputs = Variable(inputs, volatile=True)
    outputs = net(inputs)

    outputs_avg = outputs.view(ncrops, -1).mean(0)  # avg over crops
    #使用数据增强后的结果测试效果会更强
    score = F.softmax(outputs_avg)
    # print(type(score)) #tensor
    _, predicted = torch.max(score.data, 0)

    return score, predicted
total_epoch = 60  # 60

path = os.path.join(opt.dataset + '_' + opt.model, str(opt.fold))

# Data
print('==> Preparing data..')
transform_train = transforms.Compose([
    transforms.RandomCrop(cut_size),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
])

# 数据扩增
# 随机切割,扩充数据集,减缓了过拟合的作用
transform_test = transforms.Compose([
    transforms.TenCrop(cut_size),
    transforms.Lambda(lambda crops: torch.stack(
        [transforms.ToTensor()(crop) for crop in crops])),
])

trainset = CK(split='Training', fold=opt.fold, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset,
                                          batch_size=opt.bs,
                                          shuffle=True,
                                          num_workers=0)
testset = CK(split='Testing', fold=opt.fold, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset,
                                         batch_size=5,
                                         shuffle=False,
                                         num_workers=0)
def computeResult(data):
  try:
    cut_size = 44

    transform_test = transforms.Compose([
        transforms.TenCrop(cut_size),
        transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
    ])

    #Uses image data in array to compute the image
    raw_img = np.array(data, dtype=np.uint8)
   
    gray = rgb2gray(raw_img)
    gray = resize(gray, (48,48), mode='symmetric').astype(np.uint8)

    img = gray[:, :, np.newaxis]

    img = np.concatenate((img, img, img), axis=2)
    # img = Image.fromarray(img)

    img = Image.fromarray(img)

    inputs = transform_test(img)

    class_names = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']

    net = VGG('VGG19')
    checkpoint = torch.load(os.path.join('FER2013_VGG19', 'PrivateTest_model.t7'))
    net.load_state_dict(checkpoint['net'])
    net.cuda()
    net.eval()

    ncrops, c, h, w = np.shape(inputs)

    inputs = inputs.view(-1, c, h, w)
    inputs = inputs.cuda()
    inputs = Variable(inputs, volatile=True)
    outputs = net(inputs)

    outputs_avg = outputs.view(ncrops, -1).mean(0)  # avg over crops

    score = F.softmax(outputs_avg)
    _, predicted = torch.max(outputs_avg.data, 0)

    plt.rcParams['figure.figsize'] = (13.5,5.5)
    axes=plt.subplot(1, 3, 1)
    plt.imshow(raw_img)
    plt.xlabel('Input Image', fontsize=16)
    axes.set_xticks([])
    axes.set_yticks([])
    plt.tight_layout()


    plt.subplots_adjust(left=0.05, bottom=0.2, right=0.95, top=0.9, hspace=0.02, wspace=0.3)

    plt.subplot(1, 3, 2)
    ind = 0.1+0.6*np.arange(len(class_names))    # the x locations for the groups
    width = 0.4       # the width of the bars: can also be len(x) sequence
    color_list = ['red','orangered','darkorange','limegreen','darkgreen','royalblue','navy']
    for i in range(len(class_names)):
        plt.bar(ind[i], score.data.cpu().numpy()[i], width, color=color_list[i])
    plt.title("Classification results ",fontsize=20)
    plt.xlabel(" Expression Category ",fontsize=16)
    plt.ylabel(" Classification Score ",fontsize=16)
    plt.xticks(ind, class_names, rotation=45, fontsize=14)

    axes=plt.subplot(1, 3, 3)
    emojis_img = io.imread('./images/emojis/%s.png' % str(class_names[int(predicted.cpu().numpy())]))
    plt.imshow(emojis_img)
    plt.xlabel('Emoji Expression', fontsize=16)
    axes.set_xticks([])
    axes.set_yticks([])
    plt.tight_layout()
    # show emojis

    #plt.show()
    plt.savefig(os.path.join('./images/results/' + 'results.jpg'))
    plt.close()

    print("Result:" + "%s" %str(class_names[int(predicted.cpu().numpy())]))
  except:
    print('Cannot find image')
Ejemplo n.º 8
0
    def show_anim(robot, cnt):
        # nonlocal model

        # read the image sequence for classification
        img_path = config.img_path
        img_names = [
            os.path.join(img_path, 'face_round{}_cnt{}.jpg'.format(cnt, i))
            for i in range(10)
        ]
        face_seq = [io.imread(img) for img in img_names]

        anim_prob = np.zeros(len(config.anim_names))
        for face in face_seq:
            ## use the model loaded to predict probabilities
            ## input one figure and get a one-dim array of size 7
            '''

			complete here !!!

			'''

            cut_size = 44

            transform_test = transforms.Compose([
                transforms.TenCrop(cut_size),
                transforms.Lambda(lambda crops: torch.stack(
                    [transforms.ToTensor()(crop) for crop in crops])),
            ])

            raw_img = face
            gray = rgb2gray(raw_img)
            gray = resize(gray, (48, 48), mode='symmetric').astype(np.uint8)

            img = gray[:, :, np.newaxis]

            img = np.concatenate((img, img, img), axis=2)
            img = Image.fromarray(img)
            inputs = transform_test(img)

            ncrops, c, h, w = np.shape(inputs)

            inputs = inputs.view(-1, c, h, w)
            #inputs = inputs.cuda()
            inputs = Variable(inputs, volatile=True)
            outputs = net(inputs)

            outputs_avg = outputs.view(ncrops, -1).mean(0)  # avg over crops

            score = F.softmax(outputs_avg)
            _, predicted = torch.max(outputs_avg.data, 0)

            prob = score.data.cpu().numpy()

            anim_prob += prob
            # e.g.
            # anim_prob += model(face)
        anim_prob = anim_prob / sum(anim_prob)

        anim_name = np.random.choice(config.anim_names, 1, p=anim_prob)[0]

        print('Playing Animation: ', anim_name)
        print('The Expression is: ',
              class_names[anim_prob.tolist().index(max(anim_prob))])
        robot.anim.play_animation(anim_name)
Ejemplo n.º 9
0
import transforms as transforms
from skimage import io
from skimage.transform import resize
from models import *
from Fer_DataLoader import Fer_DataLoader
a= 0

f= Fer_DataLoader()
train_data = f.get_test_data()
total = 0
for inputs,_ in train_data:
    cut_size = 44
    if total == 200 :
        break
    transform_test = transforms.Compose([
        transforms.TenCrop(48),
        transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
    ])
    
    
    '''
    raw_img = io.imread(i)
    #gray = rgb2gray(raw_img)
    gray = resize(raw_img, (48,48), mode='symmetric').astype(np.uint8)

    img = gray

    img = Image.fromarray(img)
    inputs = transform_test(img)
    '''
    class_names = ['Angry','disgusted','fearful','happy','neutral','sad','surprised']
# Data
# print('==> Preparing data..')
transform_train = transforms.Compose([
    transforms.RandomCrop(cut_size),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
])

transform_train_mask = transforms.Compose([
    transforms.RandomCrop(cut_size_mask),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
])

transform_test = transforms.Compose([
    transforms.TenCrop(cut_size),
    transforms.Lambda(lambda crops: torch.stack(
        [transforms.ToTensor()(crop) for crop in crops])),
])

transform_test_mask = transforms.Compose([
    transforms.TenCrop(cut_size_mask),
    transforms.Lambda(lambda crops: torch.stack(
        [transforms.ToTensor()(crop) for crop in crops])),
])

# trainset = CK(split='Training', fold=opt.fold, transform=transform_train)
trainset = CK_Mask(split='Training',
                   fold=opt.fold,
                   transform=transform_train_mask)
trainloader = torch.utils.data.DataLoader(trainset,
Ejemplo n.º 11
0
import os
import sys
import torch
import yaml
from PIL import Image

import numpy as np

from classifer_fer.models import *
from utils import get_dataset

sys.path.append(os.path.join(os.getcwd(), 'classifer_fer'))
import transforms as transforms

transform_test = transforms.Compose([
    transforms.TenCrop(44),
    transforms.Lambda(lambda crops: torch.stack(
        [transforms.ToTensor()(crop) for crop in crops])),
])

net = VGG('VGG19')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

checkpoint = torch.load('classifer_fer/FER2013_VGG19/PrivateTest_model.t7',
                        map_location=device)

net.load_state_dict(checkpoint['net'])
# net.cuda()
net.eval()
# cfg = yaml.load(open('config_celeba_grey.yaml', 'r'))