Last active
January 21, 2016 10:23
-
-
Save szagoruyko/fadd92dc6ce61b4b895b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't OpenCV open by default the images as BGR?