Watch a Folder for New Images
Watch a directory and auto-upload new images to a Roboflow project.
Python: watchdog + SDK
watchdog + SDKpip install roboflow watchdogimport os
import time
from pathlib import Path
import roboflow
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
WATCH_DIR = Path("./incoming")
PROJECT_ID = "my-detector"
BATCH_NAME = "auto-ingest"
rf = roboflow.Roboflow()
project = rf.workspace().project(PROJECT_ID)
class IngestHandler(FileSystemEventHandler):
def on_created(self, event):
if event.is_directory:
return
path = Path(event.src_path)
if path.suffix.lower() not in {".jpg", ".jpeg", ".png"}:
return
# Wait briefly so the file is fully written before we read it.
time.sleep(1)
try:
project.upload_image(
image_path=str(path),
split="train",
batch_name=BATCH_NAME,
num_retry_uploads=3,
)
print(f"uploaded {path.name}")
except Exception as e:
print(f"failed {path.name}: {e}")
observer = Observer()
observer.schedule(IngestHandler(), str(WATCH_DIR), recursive=True)
observer.start()
try:
while True:
time.sleep(60)
except KeyboardInterrupt:
observer.stop()
observer.join()Shell: inotifywait + CLI
inotifywait + CLIPeriodic poll (no daemon)
Going further
Last updated
Was this helpful?