Friday, January 3, 2020

Python Motion Detection on a Raspberry Pi Zero

I have been working non-stop the last few months to make my main project software compatible with Apple’s latest OS version. I finally realized how badly this was depressing me and how much I didn’t want to be doing this and how much it was making me hate Apple. I was spending literally man months of time to rebuild from scratch things that weren’t broken before Catalina and it was making me miserable. My rants about the absolute stupidity of everything that Apple chose to do to this alleged OS update will have to wait as I can’t afford the dental bills to fix the damage that gritting my teeth that hard will cause.

So in the interest of sanity and preserving the small quantity of it with which I have been granted  I took a short vacation from everything MacOS. I have been fighting with the horrible firmware of even the best available IP security cams for some time as I struggle to write software that can connect to them and insert their video streams into the web and other interfaces of XTension. I believed that there should be software for the Raspberry Pi that could leverage that camera for a much more reasonable price that would be terrific. Sadly even the best software out there for it just wasn’t doing the things that I wanted or at least with the quality of image and recording that I wanted.

The idea of controlling everything about a security camera is compelling! Not having to setup a firewall to keep it from contacting it’s own “cloud” and making sure it isn’t setting up a UPnP passthrough even when you told it not to have revealed to me several times that even commercial products don’t really do what you think they are. I wanted one based on software that I have written myself and so knew exactly what it’s doing.

It should also run well on a Pi Zero W so that you can make something small, though if it runs better on the latest multi-core Pi4’s thats OK too. Those little machines are worth every penny and very very nice!

The built in “motion” system is really interesting, but it’s remarkably difficult to get it to run at a high framerate and a larger frame size. Who wants a non-HD security cam now days anyway? The separate OS versions available may be spectacular but I want a software package for the stock Pi. After fighting with trying to get openCV to compile on a Pi Zero for the last week I wanted something that didn’t need that either. (Which I suspect is why they went with a totally separate image for their security cam OS as getting that stuff to install properly is almost hilarious)

The python camera support for the Pi is absolutely awesome. I can stream 1080p video and I can record it to disk and then post process it to standard MP4 files in almost real time. Those were not insignificant accomplishments ;) But the real sticking point was going to be the video motion detection. Using the pycamera code as a starting point and the hardware H264 encoders ability to output it’s changes from one frame to another makes it absolutely possible to do this at least as well as any commercial product. I’ll eventually open source this project but for the moment I’m working on it just myself. Here’s the 720p stream, part of the web interface and also a stream of just the motion sensing. This works as high as 1080p but at lower framerates though I’m sure I can optimize the calculations a bit more for that.


The image on the right is a live stream of the motion content and I’m just really enjoying playing with this at the moment.

I am currently building the web interface to the ability to mask out portions of the video for motion detection. The math for that is working great and adds very little if any overhead. Lots of work yet to do of course before something that is download/install ready but it will come before long. This is making me happy! Apple is making me mad.

Lest anyone think I’ll abandon developing software for Apple products over how much they drive me crazy sometimes really needs to spend some time using any of the other truly commercial desktop OS’s available out there. Don’t worry. XTension is undergoing a lot of changes because of this, but it’s not going away and unless I sell enough of it to hire a support team in another time zone I wont even consider doing a windows version. Sorry but if Apple frustrates me then Microsoft would kill me outright.

.code { background:#f5f8fa; background-repeat:no-repeat; border: solid #5C7B90; border-width: 1px 1px 1px 20px; color: #000000; font: 13px 'Courier New', Courier, monospace; line-height: 16px; margin: 10px 0 10px 10px; max-height: 200px; min-height: 16px; overflow: auto; padding: 28px 10px 10px; width: 90%; } .code:hover { background-repeat:no-repeat; }