I've tried writing a simple application thats supposed to detect pixel differences from the /dev/video device. Like motion does.
I don't know how the /dev/video device works, so most of it was guesswork. What I found is that it seems like the data ( from the specific webcam ) can be divided into sections of 8192 bytes. I assume each represents a frame. The first +-600 bytes from each "frame" is identical to the previous frame.
How can i interpret that data into a understandable matrice of pixels?
The program code for refference:
#!/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