Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Friday, 16 October 2015

Live Video on the Pi

In Release 22 we added a "video" device, which allows you to capture live video using a Raspberry Pi and PiCam and access the data live in Sniff. The code may also work on other Linux platforms, and with other webcams, but for now we've focused on getting it working with the specific hardware that people are most likely to have. We also hope to follow up with support for other OS'es but video capture is a tricky thing...

However in Sniff (on a Pi!) its really easy:

make display window device
make displayX number
make displayY number

when start
..tell display to "get event"
..wait 0.1 secs

make webcam device

when start
..tell webcam to "update"
..set displayX to 320
..set displayY to 240
..tell webcam to "draw"

First we make a window, and poll if for events (to keep the OS happy). Then we can just grab a frame by telling the webcam to "update". Then we draw it on screen by selecting the middle of the screen and telling the webcam to "draw". Thats it! You should now have a live feed from your camera, on screen!

In addition to just drawing the image, you can access the pixel values in it, so we could scale it down and copy it to a regular bitmap using:

make bitmap device
make scale number
when copyVidToBitmap
.make x number
.make y number
.set scale to 4
.set displayX to 640/scale
.set displayY to 480/scale
.tell bitmap to "new"
.repeat 640/scale using x
..repeat 480/scale using y
...set displayX to x*scale
...set displayY to y*scale
...tell webcam to "get pixel"
...set displayX to x
...set displayY to y
...tell bitmap to "set pixel"

We could save this out as a BMP, but in the demo we draw the smaller version over the larger one, and make it spin around!

..broadcast copyVidToBitmap and wait 
..set bitmapValue to 10 
..tell bitmap to "turn by" 
..set displayX to 220 
..set displayY to 140 

There are probably a few wrinkles to work out (and it probably works best on a Pi2!), but at least for basic stuff, getting your live video feed into Sniff is really easy. How about writing the code to do your own green screen on the images, or edge detect the data to make a live line-drawing?

No comments:

Post a Comment