shaheerawan3 commited on
Commit
6b66502
·
verified ·
1 Parent(s): 5a50cb7

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +32 -0
model.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gdown
3
+ from ultralytics import YOLO
4
+ import torch
5
+ import streamlit as st
6
+
7
+ def download_pretrained_model():
8
+ """Download and store pre-trained model if not present."""
9
+ model_dir = 'pretrained_models'
10
+ model_path = os.path.join(model_dir, 'yolov8n.pt')
11
+ os.makedirs(model_dir, exist_ok=True)
12
+
13
+ if not os.path.exists(model_path):
14
+ st.info("Downloading pre-trained model...")
15
+ url = 'https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt'
16
+ gdown.download(url, model_path, quiet=False)
17
+ return model_path
18
+
19
+ class AircraftDetector:
20
+ def __init__(self, use_pretrained=True):
21
+ """Initialize the detector with selected model."""
22
+ custom_model_path = 'runs/detect/aircraft_detection/weights/best.pt'
23
+ pretrained_model_path = download_pretrained_model()
24
+
25
+ if use_pretrained or not os.path.exists(custom_model_path):
26
+ st.warning("Using pre-trained YOLOv8 model.")
27
+ self.model = YOLO(pretrained_model_path)
28
+ else:
29
+ st.success("Using custom-trained YOLOv8 model.")
30
+ self.model = YOLO(custom_model_path)
31
+
32
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")