Spaces:
Sleeping
Sleeping
Larry Poole commited on
Commit ·
bc56b08
1
Parent(s): 4271ee7
Fix broken app.py and launch correctly
Browse files
app.py
CHANGED
|
@@ -8,16 +8,19 @@ from huggingface_hub import hf_hub_download
|
|
| 8 |
REPO_ID = "jlarryp3/deer_buck_detector"
|
| 9 |
FILENAME = "best.onnx"
|
| 10 |
IMG_SIZE = 640
|
| 11 |
-
LOG_PATH = Path("
|
| 12 |
# -------------------------------------------
|
| 13 |
|
|
|
|
| 14 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, token=os.environ.get("HF_TOKEN"))
|
| 15 |
|
|
|
|
| 16 |
LOG_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 17 |
if not LOG_PATH.exists():
|
| 18 |
with LOG_PATH.open("w", newline="") as f:
|
| 19 |
csv.writer(f).writerow(["image","class","confidence","x1","y1","x2","y2"])
|
| 20 |
|
|
|
|
| 21 |
model = YOLO(model_path)
|
| 22 |
|
| 23 |
def predict(images):
|
|
@@ -31,7 +34,7 @@ def predict(images):
|
|
| 31 |
with LOG_PATH.open("a", newline="") as f:
|
| 32 |
writer = csv.writer(f)
|
| 33 |
for inp, res in zip(images, results):
|
| 34 |
-
frame = res[0].plot()[:, :, ::-1]
|
| 35 |
dets = []
|
| 36 |
for b in res[0].boxes:
|
| 37 |
cls_id = int(b.cls.item())
|
|
@@ -47,11 +50,15 @@ def predict(images):
|
|
| 47 |
demo = gr.Interface(
|
| 48 |
fn=predict,
|
| 49 |
inputs=gr.Files(file_types=["image"], label="Upload trail-cam images"),
|
| 50 |
-
outputs=[
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
title="Perfect10Tips — Deer Buck Detector",
|
| 53 |
description="Runs YOLO deer detection model hosted on HF and logs results to CSV."
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
|
|
|
| 57 |
demo.launch()
|
|
|
|
|
|
| 8 |
REPO_ID = "jlarryp3/deer_buck_detector"
|
| 9 |
FILENAME = "best.onnx"
|
| 10 |
IMG_SIZE = 640
|
| 11 |
+
LOG_PATH = Path("data/deer_log.csv") # writable inside the Space repo
|
| 12 |
# -------------------------------------------
|
| 13 |
|
| 14 |
+
# Download model (uses HF_TOKEN automatically if repo is private)
|
| 15 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME, token=os.environ.get("HF_TOKEN"))
|
| 16 |
|
| 17 |
+
# Ensure local data dir exists & CSV header
|
| 18 |
LOG_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 19 |
if not LOG_PATH.exists():
|
| 20 |
with LOG_PATH.open("w", newline="") as f:
|
| 21 |
csv.writer(f).writerow(["image","class","confidence","x1","y1","x2","y2"])
|
| 22 |
|
| 23 |
+
# Load YOLO model
|
| 24 |
model = YOLO(model_path)
|
| 25 |
|
| 26 |
def predict(images):
|
|
|
|
| 34 |
with LOG_PATH.open("a", newline="") as f:
|
| 35 |
writer = csv.writer(f)
|
| 36 |
for inp, res in zip(images, results):
|
| 37 |
+
frame = res[0].plot()[:, :, ::-1] # BGR->RGB for Gradio
|
| 38 |
dets = []
|
| 39 |
for b in res[0].boxes:
|
| 40 |
cls_id = int(b.cls.item())
|
|
|
|
| 50 |
demo = gr.Interface(
|
| 51 |
fn=predict,
|
| 52 |
inputs=gr.Files(file_types=["image"], label="Upload trail-cam images"),
|
| 53 |
+
outputs=[
|
| 54 |
+
gr.Gallery(label="Detections").style(grid=2, height=420),
|
| 55 |
+
gr.Textbox(label="Summary", lines=8)
|
| 56 |
+
],
|
| 57 |
title="Perfect10Tips — Deer Buck Detector",
|
| 58 |
description="Runs YOLO deer detection model hosted on HF and logs results to CSV."
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
+
os.environ.setdefault("YOLO_CONFIG_DIR", "/tmp/Ultralytics")
|
| 63 |
demo.launch()
|
| 64 |
+
|