
I trained a model to keep my dog from being naughty
So Sammy, my lovable chocolate Labrador, has generally been an absolutely lovely member of the family since we got him as a tiny puppy. Lately though, he has been a bit of a bad boy.
You see, he is not allowed on the sofa, and he knows he is not allowed on the sofa. Yet we strongly suspected he was still sneaking up there at night time. There was no easy way to catch him in the act short of setting my alarm clock to wake up at all hours, and I definitely was not going to do that. So I did what any self-respecting engineer would do and tried to solve it with some tech:
That turned into a whole adventure involving a $37 night camera, self-hosted Convex, custom AI models and toilet paper. Let's take a look at how the Sammy Sofa Saga unfolded.
The suspect and the toilet paper trap
During the day, Sammy is usually a very good boy. He knows he isn't allowed on the sofa (that's what us English people call a couch) or in the kitchen, and he doesn't go near either of them while we are around:
Sammy sitting politely on the floor beside the sofa during the day
It's when we all go to bed that things start to get suspicious. In the mornings we kept finding dog fur on the sofa and indentations on the cushions. My wife thought maybe the fur was getting tracked up on our socks and the dents were from my son getting off the sofa, but I was pretty sure we had a naughty Sammy on our hands.
By the way, bit of a tangent, apparently sneaking food when the lights go out is an actual thing in dog research. There is a paper called Dogs Steal in the Dark showing dogs take forbidden food once the lights go off, so there's at least some scientific precedent for what Sammy was up to:
The PubMed page for the scientific paper "Dogs steal in the dark"
Before trying to train him out of it, I just wanted to know for sure whether he was getting up there at night. So I had a brainwave and laid strips of toilet paper right across the sofa before bed, figuring if he jumped up he would crumple or break it:
Strips of novelty toilet paper laid across the sofa cushions to test whether Sammy was getting up overnight
The first morning I woke up and... no, it was still completely untouched. I tried this for quite a few nights and the toilet paper was never disturbed. At that point my wife was starting to look more and more right, even in my own mind.
You might be wondering why we didn't just shut him out of the living room at night. I did think of that, but his bed lives in the living room and I was told in no uncertain terms that we weren't moving it out every single night just to move it back in the morning. Which meant I was going to have to get a bit more high tech.
The stakeout
I picked up a TP-Link Tapo C110 night camera for A$37:
The TP-Link Tapo C110 wired security camera from Officeworks
Because it was so cheap I didn't have high hopes for the picture, but I was actually quite surprised at how clear the infrared footage was. I pointed it at the sofa, had my agent set it up to record all night onto an old M1 Mac mini I call Bruce, and went to bed.
Woke up the next morning not expecting much, and bam: 11:46 pm, caught red-pawed.
Interestingly, he wasn't sleeping up there like I thought he was. He hops up around ten at night, basically the moment we all go to bed, sniffs around doing a bit of mining between the cushions for the crumbs my son has dropped, and then hops back off again:
Over the first couple of weeks he was up there more than half of the nights.
The problem was the camera was only telling me the morning after, and that's no good for training a dog. I can't exactly sit Sammy down in front of a laptop and explain that this is very poor canine behaviour. We needed to catch him in the act, the second his paw hit the sofa, and do something about it right there and then.
Going live with YOLO and self-hosted Convex
The first problem was getting a computer to watch the video as it streamed out of the camera and work out whether the dog was on the sofa or not.
I had my agent try a bunch of different vision models on Bruce. Apple has a built-in model, and it wasn't very good. It kept calling Sammy a cat:
Apple's built-in vision model labelling Sammy, a chocolate Labrador, as "Cat 70%"
He's not a cat. Another model reckoned he was on the sofa in a completely empty room, so that was no good either.
The one we landed on was YOLO (You Only Look Once), which is small enough to check the frame a few times a second on Bruce:
To define the naughty zones (the sofa, plus the kitchen and kids' playroom), I drew a polygon right over the sofa seats in the app so if his cheeky paws landed inside that shape for a second, it counted as a sofa visit:
The zone editor in Sammy Cam with an orange polygon drawn around the sofa seats and a dashed exclusion over the coffee table
Now, the second problem was what to do with all of that data once we had it. The camera sits on the wall in my living room, so I wanted everything kept private inside the house. Running a self-hosted Convex instance right on Bruce alongside the detector was the obvious choice:
Architecture inside the house: the Tapo camera feeds the AI model and self-hosted Convex on Bruce, which updates my phone with nothing leaving to the cloud
The nice thing about using Convex here is that everything stays up to date without any extra plumbing. When the camera spots Sammy overnight, the sighting is waiting on my phone in the morning, or straight away if I happen to be awake in the middle of the night, and it shows up on our family wall touchscreen (Cannvas) so everyone can see what Sammy has been up to:
Sammy Cam running live on Cannvas, our family wall touchscreen
The telling off
Once I could detect the moment he hopped onto the sofa, I had a few ideas for what to do about it.
My first idea was playing a recording of me telling Sammy off through the Google Home Hub in the kitchen. The catch was that it took three to five seconds to wake up and start playing, and I worried that by the time my voice came through he wouldn't associate it with being on the sofa. I wanted to wire some spare speakers to a Raspberry Pi right next to the couch to cut that delay, except I couldn't find my spare speakers at the time.
So I stuck with the Home Hub for now and added something faster: a lamp next to the TV with a couple of smart bulbs in it:
The floor lamp next to the TV fitted with smart bulbs to flash when Sammy gets on the sofa
Because the lamp could react straight away, I hooked it up to flash a few times whenever Sammy was detected, and log the whole event into Convex so I could inspect it in the dashboard:
The Sammy Cam dashboard showing an "On the sofa" event paired with a "Told off" row logged in Convex
I switched it on, went to bed, and got a great night's sleep fully expecting the Sammy Sofa Saga to be over. Next morning I looked at the footage: there was Sammy happily on the sofa, with a distinct lack of lights turning on. A few bugs to iron out first.
I fixed those and switched it back on for night two. This time it definitely went off, four times in five minutes. The only problem was it wasn't Sammy getting told off, it was my son and his friend who were up late playing on the sofa. Whoops 😅 Meanwhile the actual Sammy was just wandering around on the floor minding his own business.
A few nights later, at 5:28 in the morning, I got woken up by notifications pinging my phone telling me Sammy was on the sofa. Except it wasn't Sammy. It was an orange blanket, and the system decided to tell that orange blanket off for eight minutes:
The living room at 5:29 am with the lamp lit over an empty sofa and the orange blanket sitting on the left cushion
At that point I added a dry run toggle in Settings so I could watch what the system would have done without actually flashing lights or yelling at blankets, and went back to the drawing board:
The Deterrent settings panel in Sammy Cam switched to "Dry run" with the Home Assistant TV lamp configured to flash 3 times
Training a custom model (and getting humbled by a slipper)
To be fair to stock YOLO, telling a chocolate Labrador apart from a brown blanket on a black and white night camera is kind of hard. YOLO is a general model trained to spot cars, people, dogs, cats and everything else, whereas at night I only needed it to be good at finding one thing: Sammy.
It had been a long time since I last trained a model, so I wasn't sure what to expect. With coding agents handling all the implementation details these days, it turned out to be way less painful than I thought.
We needed a lot of labelled data, so my agent found Meta's SAM 3, a much bigger and slower model that takes about five seconds to look at a single frame:
Meta's Segment Anything Model 3 (SAM 3) page
Way too slow for live detection, but accurate enough to act as a teacher. We ran it over roughly 3,000 frames of our night footage to draw a border around Sammy in every one:
We then used those labelled frames to train a small, fast YOLO model, which took a couple of hours on another Mac mini I had with a bit more memory. That bumped detection on night frames from 68% on stock YOLO up to 88%.
Around the same time, we added a little tick and cross button next to every sighting in the app so I could mark whether it was actually Sammy. Those verdicts save straight into Convex and feed into the next round of training:
After another training pass with those marked mistakes, accuracy climbed to 98% (I accidentally said 90% on camera, turns out it was actually 98%):
Night detection score jumping from 68% on the general YOLO model to 98% on our custom trained model
So I had high hopes when we put this shiny new model live on its very first night. And it spotted Sammy... 141 times.
Turns out 123 of those sightings were a slipper sitting on the arm of the sofa:
The custom model putting a blue bounding box around a dark slipper on the left arm of the sofa at 00:50 am
And the slipper wasn't the only thing tripping it up. It thought his chew bone was Sammy 108 times in one night, flagged a plant pot as a person, and stared at some bottles on the kitchen bench as a "person" for over three hours:
A blue bounding box flagging bottles on the kitchen bench as a person overnight
Then there were the actual bugs. Infrared night vision lights up every moth and tiny fly that passes near the lens, and apparently a moth right in front of the camera looks a lot like a person:
A moth glowing bright white right in front of the infrared lens at 22:23
There was also a glowing orb drifting by the doorway that we still haven't explained:
The glowing orb by the doorway flagged by the detector at 23:33, with zoomed comparison frames on the right
In the end, we fixed most of these with boring basic rules rather than more model training. For example, only counting something as Sammy once it has actually moved and gone somewhere. That knocked out slippers, chew bones, bottles and plant pots straight away.
Cutting the delay from 13.5 seconds down to 3
Now that the detector actually knew what Sammy was, we still had a speed problem. When we measured end to end from his paws landing on the sofa to my voice coming out of the speaker, it took 13.5 seconds. Since he's usually up there for less than a minute, he was getting plenty of crumb mining in before getting told off.
We went through the pipeline with a fine-tooth comb to see where the seconds were going. About three seconds was just the video feed reaching Bruce, which I initially blamed on the $37 camera. Turned out to be a single ffmpeg setting (-threads 1 so MJPEG stopped buffering frames ahead), and changing that dropped the feed delay down to about half a second:
The other big wait was the Google Home Hub taking three to five seconds to wake up. Since I still didn't have the spare speakers back yet, my voice was always going to arrive late, so we leaned on the smart lamp to fire first. Putting those fixes together brought the delay from 13.5 seconds down to about 3 seconds for the lamp:
Confident it could finally spot Sammy quickly and accurately, I flipped dry run off and switched the deterrent back on.
Did it actually work?
On 26 October at 2:44 in the morning, Sammy hopped onto the sofa. Watch what happens when the lamp flashes:
And he's off! Right there was the first time Sammy Cam actually worked. Funny thing is, my voice didn't even play that night because the Google Home Hub was having a wobble. Just the lamp flashing was enough.
The next night he tried his luck again, and this time both the lamp and my voice triggered (though my voice only started as he was already jumping off). Four nights later, on 31 October, he had one more go and was off within a couple of seconds of the lamp coming on.
And that was it. Before switching the deterrent on he was on the sofa more than half of the nights we watched. Since 31 October, he hasn't been up there once:
Bar chart of Sammy's sofa visits per night from 24 Sep to 23 Nov, dropping to zero after the third telling off on 31 Oct
My favourite clip of all came a couple of weeks later. He walks right up to the corner where he would normally jump up, rests his nose on the cushion, has a good think about it, and walks away:
Good boy, Sammy.
Oh, and I eventually did get my spare speakers back from my mate and plugged them into a Raspberry Pi sitting right beside the sofa. Instead of waiting three to five seconds for the Home Hub to wake up, it now plays within about 20 milliseconds. Turns out I didn't even need it, because he hasn't been back on the sofa since 😅
The Logitech speakers hooked up to the Raspberry Pi beside the sofa, still waiting for their first telling off
Conclusion
So just to recap: a $37 night camera, an old Mac mini running a custom YOLO model trained from SAM 3 labels, a self-hosted Convex backend keeping everything in sync inside the house, a couple of smart bulbs, and a simple movement rule to stop it yelling at slippers.
Having self-hosted Convex sitting on Bruce made a quirky home project like this way easier to iterate on. Everything stayed private on my own hardware, the dashboard and wall screen updated live without extra sync code, and adding a quick tick or cross to save training feedback into the database was dead simple. And honestly, when a custom vision model starts flagging slippers and plant pots, a plain old movement rule often beats throwing more training data at it.
Sammy no longer goes on the sofa, which is awesome for keeping dog fur off the cushions. Now I just have a new problem: all the crumbs building up in the cracks without Sammy around to hoover them up 😉
Until next time, cheerio! 👋
Convex is the reactive backend platform that keeps up with you and your agents. Database, functions, workflow, sync, search, file storage, and more. All TypeScript, zero glue.