-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathopenface_server.lua
92 lines (82 loc) · 2.51 KB
/
openface_server.lua
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
#!/usr/bin/env th
--
-- Copyright 2015 Carnegie Mellon University
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-- Warning: This is very unstable!
-- Please join us in improving it at:
-- https://github.com/cmusatyalab/openface/issues/1
-- https://github.com/cmusatyalab/openface/issues/4
require 'torch'
require 'nn'
require 'dpnn'
require 'image'
require "socket"
io.stdout:setvbuf 'no'
torch.setdefaulttensortype('torch.FloatTensor')
local cmd = torch.CmdLine()
cmd:text()
cmd:text('Face recognition server.')
cmd:text()
cmd:text('Options:')
cmd:option('-model', './models/openface/nn4.v1.t7', 'Path to model.')
cmd:option('-imgDim', 96, 'Image dimension. nn1=224, nn4=96')
cmd:option('-cuda', false)
cmd:text()
opt = cmd:parse(arg or {})
print(opt)
start_time = socket.gettime()*1000
net = torch.load(opt.model, 'ascii')
net:evaluate()
end_time = socket.gettime()*1000
elapsed_time = end_time-start_time
print('time elapsed: ' .. elapsed_time .. 'ms', 12, 32)
print(net)
local imgCuda = nil
if opt.cuda then
require 'cutorch'
require 'cunn'
net = net:cuda()
imgCuda = torch.CudaTensor(1, 3, opt.imgDim, opt.imgDim)
end
-- torch.setnumthreads(5)
local img = torch.Tensor(1, 3, opt.imgDim, opt.imgDim)
while true do
-- Read a path to an image on stdin and output the representation
-- as a CSV.
local imgPath = io.read("*line")
start_time = socket.gettime()*1000
if imgPath and imgPath:len() ~= 0 then
img[1] = image.load(imgPath, 3, byte)
img[1] = image.scale(img[1], opt.imgDim, opt.imgDim)
local rep = nil
if opt.cuda then
imgCuda:copy(img)
rep = net:forward(imgCuda):float()
else
rep = net:forward(img)
end
local sz = rep:size(1)
for i = 1,sz do
io.write(rep[i])
if i < sz then
io.write(',')
end
end
io.write('\n')
io.stdout:flush()
end
end_time = socket.gettime()*1000
elapsed_time = end_time-start_time
print('time elapsed: ' .. elapsed_time .. 'ms', 12, 32)
end