- Python 3
- OpenCV library (cv2)
- NumPy library (numpy)
Install python virtual env sudo apt install python3-venv
You must first install motion using your package manager of choice, for debian sudo apt-get install motion
make sure motion has the right permissions for the /var/log/motion
& /var/log/motion/motion.log
.
Edit /etc/motion/motion.conf
and choose where to store the saved pictures, snapshots and movies with the target_dir
variable, the default is /var/lib/motion
.
sudo mkdir /snapshots
chown motion:adm /snapshots
cd /snapshots
python3 -m venv srvenv
(create virtual environment)source srvenv/bin/activate
(activate virtual environment)pip install opencv-python-headless numpy && pip list
(Install dependencies)deactivate
(deactivate virtual environment)
import cv2
import numpy as np
import os
import glob
import time
import argparse
def is_image_dark(image_path, darkness_threshold):
"""Check if the image is dark based on the darkness threshold."""
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if image is None:
return False
mean_brightness = np.mean(image)
return mean_brightness < darkness_threshold
def delete_dark_images(image_directory, darkness_threshold):
"""Delete dark images in the specified directory."""
# Get the list of image files sorted by creation time
image_files = sorted(glob.glob(os.path.join(image_directory, '*.jpg')), key=os.path.getctime)
for image_file in image_files:
print(f"checking image: {image_file}")
if is_image_dark(image_file, darkness_threshold):
os.remove(image_file)
print(f"Deleted dark image: {image_file}")
def main():
parser = argparse.ArgumentParser(description='Delete dark images from a directory.')
parser.add_argument('-f', '--image_directory', type=str, default='/path/to/motion/images',
help='Directory where motion saves images')
parser.add_argument('-t', '--darkness_threshold', type=int, default=50,
help='Threshold for darkness (0-255)')
parser.add_argument('-s', '--sleep_time', type=int, default=60,
help='Time to sleep between checks in seconds')
args = parser.parse_args()
while True:
delete_dark_images(args.image_directory, args.darkness_threshold)
time.sleep(args.sleep_time)
if __name__ == '__main__':
main()
Create new service file:
sudo nano /etc/systemd/system/delete-dark-images.service
[Unit]
Description=Delete Dark Images Service
After=network.target
[Service]
Type=simple
User=motion
Environment="PATH=/snapshots/srvenv/bin:$PATH"
WorkingDirectory=/snapshots
ExecStart=/snapshots/srvenv/bin/python /snapshots/delete_dark_images.py -f /snapshots -t 60 -s 600
Restart=always
[Install]
WantedBy=multi-user.target
-f
or--image_directory
: The directory where motion saves images.-t
or--darkness_threshold
: The threshold for darkness (0-255).-s
or--sleep_time
: The time to sleep between checks in seconds.
- Start:
sudo systemctl start delete-dark-images
- Stop:
sudo systemctl stop delete-dark-images
- Status:
sudo systemctl status delete-dark-images
- Enable on boot
sudo systemctl enable delete-dark-images
systemctl list-unit-files --type=service