Skip to content

Instantly share code, notes, and snippets.

@szagoruyko
Last active January 21, 2016 10:23
Show Gist options
  • Save szagoruyko/fadd92dc6ce61b4b895b to your computer and use it in GitHub Desktop.
Save szagoruyko/fadd92dc6ce61b4b895b to your computer and use it in GitHub Desktop.
require 'cv.highgui'
require 'cv.videoio'
require 'cv.imgproc'
require 'nn'
--require 'clnn'
local cap = cv.VideoCapture{device=0}
if not cap:isOpened() then
print("Failed to open the default camera")
os.exit(-1)
end
cv.namedWindow{winname="edges", flags=cv.WINDOW_AUTOSIZE}
local _, frame = cap:read{}
local net = torch.load'nin.t7':float()
local img_mean = torch.load'ilsvrc_2012_mean.t7'.img_mean:float()
local synset_words = torch.load('synset.t7','ascii')
local M = 227
while true do
local w = frame:size(2)
local h = frame:size(1)
local crop = cv.getRectSubPix{src=frame, patchSize={h,h}, center={w/2, h/2}}
local im = cv.resize{src=crop, dsize={256,256}}
local im2 = im:float() - img_mean
local I = cv.resize{src=im2, dsize={M,M}}:permute(3,1,2):clone()
local _,classes = net:forward(I):view(-1):float():sort(true)
for i=1,5 do
cv.putText{
img=crop,
text = synset_words[classes[i]],
org={10,10 + i * 25},
fontFace=cv.FONT_HERSHEY_DUPLEX,
fontScale=1,
color={255, 255, 0},
thickness=1
}
end
cv.imshow{winname="edges", image=crop}
if cv.waitKey(30) >= 0 then break end
cap:read{image=frame}
end
@fmassa
Copy link

fmassa commented Oct 26, 2015

Doesn't OpenCV open by default the images as BGR?

@szagoruyko
Copy link
Author

@fmassa NIN expects images in BGR and we give it BGR, right?

@fmassa
Copy link

fmassa commented Jan 21, 2016

Indeed !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment