Skip to content

Commit dc372d9

Browse files
authored
Validate local detect sources before inference
Add early local source validation to detect.py so missing local files and directories fail clearly before model setup. Preserve URL, webcam, screenshot, and matching glob behavior.\n\nThanks @Isha2790 for the contribution.
1 parent 6d6b4ec commit dc372d9

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

detect.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import os
3434
import platform
3535
import sys
36+
from glob import glob, has_magic
3637
from pathlib import Path
3738

3839
import torch
@@ -148,11 +149,18 @@ def run(
148149
```
149150
"""
150151
source = str(source)
151-
save_img = not nosave and not source.endswith(".txt") # save inference images
152152
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
153153
is_url = source.lower().startswith(("rtsp://", "rtmp://", "http://", "https://"))
154154
webcam = source.isnumeric() or source.endswith(".streams") or (is_url and not is_file)
155155
screenshot = source.lower().startswith("screen")
156+
157+
if not (webcam or screenshot or is_url) and not (
158+
Path(source).exists() or (has_magic(source) and glob(source, recursive=True))
159+
):
160+
raise FileNotFoundError(f"Source path '{source}' does not exist")
161+
162+
save_img = not nosave and not source.endswith(".txt") # save inference images
163+
156164
if is_url and is_file:
157165
source = check_file(source) # download
158166

0 commit comments

Comments
 (0)