Spaces:
Running
Running
update: endpoint change
Browse files
app.py
CHANGED
@@ -3,12 +3,11 @@ import netron
|
|
3 |
import os
|
4 |
import threading
|
5 |
import time
|
|
|
|
|
6 |
from PIL import Image
|
7 |
import cv2
|
8 |
import numpy as np
|
9 |
-
import torch
|
10 |
-
from yolov5 import xai_yolov5
|
11 |
-
from yolov8 import xai_yolov8s
|
12 |
|
13 |
# Sample images directory
|
14 |
sample_images = {
|
@@ -18,6 +17,7 @@ sample_images = {
|
|
18 |
|
19 |
# Preloaded model file path (update this path as needed)
|
20 |
preloaded_model_file = os.path.join(os.getcwd(), "weight_files/yolov5.onnx") # Example path
|
|
|
21 |
|
22 |
def load_sample_image(sample_name):
|
23 |
"""Load a sample image based on user selection."""
|
@@ -26,24 +26,40 @@ def load_sample_image(sample_name):
|
|
26 |
return Image.open(image_path)
|
27 |
return None
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def process_image(sample_choice, uploaded_image, yolo_versions):
|
30 |
-
"""Process the image using selected YOLO models."""
|
31 |
if uploaded_image is not None:
|
32 |
image = uploaded_image # Use the uploaded image
|
33 |
else:
|
34 |
image = load_sample_image(sample_choice) # Use selected sample image
|
35 |
|
36 |
-
|
37 |
-
image = cv2.resize(image, (640, 640))
|
38 |
result_images = []
|
39 |
|
40 |
for yolo_version in yolo_versions:
|
41 |
if yolo_version == "yolov5":
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
else:
|
46 |
-
result_images.append((
|
47 |
|
48 |
return result_images
|
49 |
|
@@ -52,6 +68,7 @@ def serve_netron(model_file):
|
|
52 |
threading.Thread(target=netron.start, args=(model_file,), daemon=True).start()
|
53 |
time.sleep(1) # Give some time for the server to start
|
54 |
return "http://localhost:8080" # Default Netron URL
|
|
|
55 |
def view_model():
|
56 |
"""Handle model visualization using preloaded model file."""
|
57 |
if not os.path.exists(preloaded_model_file):
|
@@ -92,7 +109,7 @@ with gr.Blocks(css=custom_css) as interface:
|
|
92 |
)
|
93 |
|
94 |
selected_models = gr.CheckboxGroup(
|
95 |
-
choices=["yolov5"
|
96 |
value=["yolov5"],
|
97 |
label="Select Model(s)",
|
98 |
)
|
|
|
3 |
import os
|
4 |
import threading
|
5 |
import time
|
6 |
+
import requests
|
7 |
+
import json
|
8 |
from PIL import Image
|
9 |
import cv2
|
10 |
import numpy as np
|
|
|
|
|
|
|
11 |
|
12 |
# Sample images directory
|
13 |
sample_images = {
|
|
|
17 |
|
18 |
# Preloaded model file path (update this path as needed)
|
19 |
preloaded_model_file = os.path.join(os.getcwd(), "weight_files/yolov5.onnx") # Example path
|
20 |
+
onnx_endpoint = "http://localhost:8080/v1/models/yolov5:predict" # ONNX model endpoint
|
21 |
|
22 |
def load_sample_image(sample_name):
|
23 |
"""Load a sample image based on user selection."""
|
|
|
26 |
return Image.open(image_path)
|
27 |
return None
|
28 |
|
29 |
+
def preprocess_image(image):
|
30 |
+
"""Preprocess the image for ONNX model input."""
|
31 |
+
image = np.array(image)
|
32 |
+
image = cv2.resize(image, (640, 640))
|
33 |
+
_, buffer = cv2.imencode('.jpg', image)
|
34 |
+
image_bytes = buffer.tobytes()
|
35 |
+
return image_bytes
|
36 |
+
|
37 |
def process_image(sample_choice, uploaded_image, yolo_versions):
|
38 |
+
"""Process the image using selected YOLO models or the ONNX endpoint."""
|
39 |
if uploaded_image is not None:
|
40 |
image = uploaded_image # Use the uploaded image
|
41 |
else:
|
42 |
image = load_sample_image(sample_choice) # Use selected sample image
|
43 |
|
44 |
+
image_bytes = preprocess_image(image)
|
|
|
45 |
result_images = []
|
46 |
|
47 |
for yolo_version in yolo_versions:
|
48 |
if yolo_version == "yolov5":
|
49 |
+
# Call the ONNX endpoint
|
50 |
+
headers = {"Content-Type": "application/json"}
|
51 |
+
data = {"inputs": [image_bytes.tolist()]}
|
52 |
+
try:
|
53 |
+
response = requests.post(onnx_endpoint, headers=headers, data=json.dumps(data))
|
54 |
+
if response.status_code == 200:
|
55 |
+
results = response.json()
|
56 |
+
result_images.append((image, str(results))) # Example placeholder result
|
57 |
+
else:
|
58 |
+
result_images.append((image, f"Error: {response.status_code}"))
|
59 |
+
except Exception as e:
|
60 |
+
result_images.append((image, f"Failed: {str(e)}"))
|
61 |
else:
|
62 |
+
result_images.append((image, f"{yolo_version} not yet implemented."))
|
63 |
|
64 |
return result_images
|
65 |
|
|
|
68 |
threading.Thread(target=netron.start, args=(model_file,), daemon=True).start()
|
69 |
time.sleep(1) # Give some time for the server to start
|
70 |
return "http://localhost:8080" # Default Netron URL
|
71 |
+
|
72 |
def view_model():
|
73 |
"""Handle model visualization using preloaded model file."""
|
74 |
if not os.path.exists(preloaded_model_file):
|
|
|
109 |
)
|
110 |
|
111 |
selected_models = gr.CheckboxGroup(
|
112 |
+
choices=["yolov5"],
|
113 |
value=["yolov5"],
|
114 |
label="Select Model(s)",
|
115 |
)
|