Help us understand the problem. What is going on with this article?

mruby でリアルタイムで映像分類する

@akuroda さんが mruby-gdを使って画素にアクセスする方法を教えてくれたので @kjunichi さんの mruby-webcam を使ってリアルタイムに映像分類する様にしてみました。

導入の方法は akuroda さんの記事を読んで下さい。

model = TfLite::Model.from_file 'mobilenet_v1_1.0_224_quant.tflite'
interpreter = TfLite::Interpreter.new(model)
interpreter.allocate_tensors
input = interpreter.input_tensor(0)
output = interpreter.output_tensor(0)
wanted_width = input.dim(1)
wanted_height = input.dim(2)
wanted_channel = input.dim(3)
data = Array.new(wanted_height * wanted_width * wanted_channel, 0)
labels = File.read('labels.txt').lines.map {|x| x.strip} 

cam = Webcam.new(ARGV[0]||0)
cam.set_size(wanted_width, wanted_height)
cam.each(true) {|img|
  decoded = GD::Image.new_from_jpeg_data(img)
  (0...wanted_width).each do |x|
    (0...wanted_height).each do |y|
      pixel = decoded.get_pixel(x, y)
      offset = (y * wanted_width + x) * wanted_channel
      data[offset+0] = decoded.red(pixel)
      data[offset+1] = decoded.green(pixel)
      data[offset+2] = decoded.blue(pixel)
    end
  end
  decoded.destroy
  input.data = data
  interpreter.invoke
  output.data.each_with_index do |v, i|
    puts "#{i} #{v} #{labels[i]}" if v > 50
  end 
  puts "---"
}

cam.each はまだマージされてないので今しばらくお待ちください。

https://github.com/kjunichi/mruby-webcam/pull/15

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした