/ dev/videoデバイスからのピクセルの違いを検出することになっている簡単なアプリケーションを書いてみました。動きのように。
/ dev / videoデバイスがどのように機能するかわからないので、ほとんどは当て推量でした。私が見つけたのは、(特定のWebカメラからの)データを8192バイトのセクションに分割できるように見えることです。それぞれがフレームを表していると思います。各「フレーム」の最初の+-600バイトは、前のフレームと同じです。
そのデータを理解可能なピクセルの行列に解釈するにはどうすればよいですか?
参照用のプログラムコード:
#!/usr/bin/ruby
# calculates a percentage difference between two array's
def percentage_difference( arrayA, arrayB )
top_size = arrayA.size > arrayB.size ? arrayA.size : arrayB.size
diff_elements = 0;
0.upto( top_size - 1 ) do |i|
diff_elements += 1 unless arrayA[i] == arrayB[i]
end
( 1.0 * diffelements ) / top_size
end
cam = File.open( '/dev/video' );
lastframe = [];
while( true ) do
# reads a frame from the open video device ( cam ) and converts to bytes;
newframe = cam.readpartial( num_of_bytes_per_frame ).bytes.map { |b| b }
# prints the percentage difference between the two frames
puts percentage_difference( lastframe, newframe );
lastframe = newframe;
end