Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,32 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image, ImageDraw
|
| 3 |
import io
|
| 4 |
from fpdf import FPDF
|
| 5 |
import tempfile
|
| 6 |
import cv2
|
| 7 |
import os
|
| 8 |
-
import torch
|
| 9 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def detect_empty_spots(img_width, img_height, bottle_bboxes, min_gap=50):
|
| 16 |
empty_bboxes = []
|
|
@@ -40,8 +56,8 @@ def process_input(input_file):
|
|
| 40 |
else:
|
| 41 |
process_img_path = input_file
|
| 42 |
|
| 43 |
-
# Run inference with parameters
|
| 44 |
-
results = model(process_img_path, conf=0.25, iou=0.45, agnostic_nms=False, max_det=1000)
|
| 45 |
boxes = results[0].boxes
|
| 46 |
class_names = results[0].names
|
| 47 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import io
|
| 5 |
from fpdf import FPDF
|
| 6 |
import tempfile
|
| 7 |
import cv2
|
| 8 |
import os
|
| 9 |
+
import torch.serialization
|
| 10 |
import numpy as np
|
| 11 |
+
from urllib.request import urlretrieve
|
| 12 |
+
from ultralytics.nn.modules import Conv, Bottleneck, SPPF, C2f, Detect
|
| 13 |
+
from ultralytics.nn.tasks import DetectionModel
|
| 14 |
+
from torch.nn.modules.container import Sequential
|
| 15 |
|
| 16 |
+
# Allowlist all necessary YOLOv8 modules to avoid UnpicklingError
|
| 17 |
+
torch.serialization.add_safe_globals([DetectionModel, Sequential, Conv, Bottleneck, SPPF, C2f, Detect])
|
| 18 |
+
|
| 19 |
+
# Model path
|
| 20 |
+
model_path = 'model/yolov8n.pt'
|
| 21 |
+
|
| 22 |
+
# Download model if not found
|
| 23 |
+
if not os.path.exists(model_path):
|
| 24 |
+
os.makedirs('model', exist_ok=True)
|
| 25 |
+
url = 'https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt'
|
| 26 |
+
urlretrieve(url, model_path)
|
| 27 |
+
|
| 28 |
+
# Load the YOLOv8 model
|
| 29 |
+
model = YOLO(model_path)
|
| 30 |
|
| 31 |
def detect_empty_spots(img_width, img_height, bottle_bboxes, min_gap=50):
|
| 32 |
empty_bboxes = []
|
|
|
|
| 56 |
else:
|
| 57 |
process_img_path = input_file
|
| 58 |
|
| 59 |
+
# Run inference with YOLO parameters
|
| 60 |
+
results = model.predict(process_img_path, conf=0.25, iou=0.45, agnostic_nms=False, max_det=1000)
|
| 61 |
boxes = results[0].boxes
|
| 62 |
class_names = results[0].names
|
| 63 |
|