Skip to content

A tool for detect the presence of my cat using a neural network

License

Notifications You must be signed in to change notification settings

olafrv/pycatdetector

Repository files navigation

About PyCatDetector

Python Cat Detector is a tool to detect the presence of my cat "Cookie" near her litterbox and alerts me to clean it.

Basically, the application grabs video frames from an IP camera RTSP stream, uses a neural network to identify a cat object, and play a message in a speaker.

All hardware and software used by pycatdetector (i.e. Camera, Linux Server and Speaker) can reach each other via my local WiFi network.

Architecture & Workflow

The software architecture is pretty simple as follows:

sequenceDiagram
    participant Camera
    participant Recorder
    participant Detector
    participant Encoder
    participant Screener
    participant Notifier
    participant Channel
    loop Read (RSTP)
      Camera->>Recorder: Image
    end
    loop Read (Queue)
      Recorder->>Detector: Image
    end
    loop Read (Queue)
      Detector->>Screener: Image
    end
    Detector->>Encoder: write(Video, Image)
    loop Read (Queue)
      Detector->>Notifier: Label/Score
    end
    Notifier->>Channel: notify(Label)
Loading

Requirements

General

This Python package has been tested and designed for:

AVX Instruction Set

NOTICE: If your Linux is a Virtual Machine running on a Windows host you
need to disable Windows Security > Core Isolation > Memory Protection.

Linux (or WSL2)

Check if your CPU (or vCPU) is supporting the AVX instruction set:

cat /proc/cpuinfo | grep -i avx

PyTorch

Run the following to check if AVX is enabled: test_aaa_torch.py.

See more information on:
https://pytorch.org/get-started/locally/#windows-verification

Apache MXNet (Legacy)

During runtime check if AVX, define the following environment variable:

# Apache MXNet
export MKLDNN_VERBOSE=1
# unset MKLDNN_VERBOSE

See more information on:
https://mxnet.apache.org/versions/1.9.1/api/python/docs/tutorials/performance/backend/mkldnn/index.html

Usage

Configuration

sudo apt install make
git clone "https://github.com/olafrv/pycatdetector.git"
cd pycatdetector
mv config.example.yaml config.yaml # edit manually afterwards

Run (Python)

make install         # install end user requirements
make check-config    # checks for YAML parsing errors
make run             # with python3 interpreter

Run (Docker)

# If you don't have it (latest docker official version)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/olafrv/my_collections/refs/heads/main/scripts/bash/install-docker.sh)"
make docker.build    # build docker image with python requirements
make docker.run      # A) without compose, and removed after stopped
make docker.start    # B.1) with compose running in background as service
make docker.stop     # B.2) stop the composed container service
make docker.sh       # B.3) open bash in the container service
make docker.clean    # delete local docker images to save space

Uninstall

make uninstall       # remove python requirements and disposable folders

Advanced/Developement

# Metadata and Requirements
make metadata             # show release METADATA file content
make install.dev          # install development requirements
make package.outdated     # check outdated python packages
# Binary build with Nuitka3
make build                # build binary with nuitka3
make clean                # delete local disposable files
make run.bin              # run ./dist/main.bin dynamically linked binary
# Profiling and Testing
make profile              # run python3 profiling over main.py
make profile.view         # check profiling results with snake
make test                 # run ./pycatdetector/tests/*
make test.coverage        # check call coverage during runtime
make test.coverage.report # check test coverage after runtime
# GitHub Release
# See Makefile for required GITHUB_* required variables
# Adjust METADATA file before running the following commands
make github.push          # push docker image to github registry
make github.release       # create a new github relase

References

OpenCV (cv2)

PyTorch - Torchvision

GluonCV (Legacy)

Apache MXNet (Legacy)

PIL

Deep Learning (MXNet)

PEP 8 – Style Guide for Python Code

PEP 0 – Index of Python Enhancement Proposals (PEPs)