A simple, cross-platform .NET console application for detecting motion in an MJPEG video stream, such as one from an ESP32-CAM. It compares consecutive frames and saves images when the difference exceeds a configured threshold.
- Connects to any MJPEG stream via URL.
- Compares frames to detect pixel-level differences.
- Calculates a percentage of motion between frames.
- Saves the original image and a "difference map" image when motion is detected.
- Configurable thresholds for motion sensitivity.
- Cross-platform: runs on Windows, Linux, and macOS.
This application acts as a client that processes a video stream. It is designed to work with a device like an ESP32-CAM, which acts as a server providing the MJPEG stream.
- The ESP32-CAM runs code that captures images from its camera and serves them over Wi-Fi as an MJPEG stream at a specific URL.
- This .NET application connects to that URL.
- It continuously receives video frames, comparing each new frame to the previous one.
- If the calculated difference (motion) is above a set threshold, it saves the current frame and a visual representation of the difference to a local
detecteddirectory.
These instructions explain how to run this .NET application on your host machine (e.g., a PC running Linux, Windows, or macOS).
- .NET 8 SDK or newer.
- An active MJPEG video stream source (see ESP32 setup below).
Before running, you must configure the application to point to your camera's stream URL.
-
Open the
Program.csfile. -
Modify the
ProcessorConfigobject with your settings. The most important setting isStreamUrl.var config = new ProcessorConfig { StreamUrl = "http://YOUR_ESP32_IP_ADDRESS/stream", // <-- Change this! CheckIntervalMs = 200, NormalizeSource = false, DifferenceThreshold = 25, PercentDifferenceThreshold = 0.003m, SaveOnDetect = true, OutputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "detected") };
- Open a terminal in the project's root directory.
- Run the application with the following command:
dotnet run
- The application will connect to the stream and begin processing. Detected motion images will be saved in the
bin/Debug/net8.0/detected/folder. - Press
Ctrl+Cto stop the application.
Important: This .NET code does not run on the ESP32. You must program your ESP32 separately to provide the video stream that this application will consume.
A great way to get started is by using the official example in the Arduino IDE.
- Setup Arduino IDE for ESP32: Follow a standard guide to add ESP32 board support to your Arduino IDE.
- Load the Example: Go to
File > Examples > ESP32 > Camera > CameraWebServer. - Configure the Sketch:
- Uncomment the correct
CAMERA_MODEL_...for your board (e.g.,CAMERA_MODEL_AI_THINKER). - Enter your Wi-Fi network's
ssidandpassword.
- Uncomment the correct
- Upload and Run: Upload the sketch to your ESP32-CAM.
- Find the URL: Open the Serial Monitor (
Tools > Serial Monitor) with a baud rate of115200. After the ESP32 connects to your Wi-Fi, it will print its IP address. The MJPEG stream URL will behttp://<IP_ADDRESS>/stream. - Use the URL: Copy this stream URL into the
StreamUrlproperty in this project'sProgram.csfile.