BhumikaMak commited on
Commit
d00769c
·
verified ·
1 Parent(s): 5aa3f91

update: file encoding

Browse files
Files changed (1) hide show
  1. app.py +17 -27
app.py CHANGED
@@ -3,11 +3,13 @@ import netron
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,7 +19,6 @@ 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,40 +27,29 @@ def load_sample_image(sample_name):
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
 
@@ -109,7 +99,7 @@ with gr.Blocks(css=custom_css) as interface:
109
  )
110
 
111
  selected_models = gr.CheckboxGroup(
112
- choices=["yolov5"],
113
  value=["yolov5"],
114
  label="Select Model(s)",
115
  )
 
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
+ import base64
11
+ from yolov5 import xai_yolov5
12
+ from yolov8 import xai_yolov8s
13
 
14
  # Sample images directory
15
  sample_images = {
 
19
 
20
  # Preloaded model file path (update this path as needed)
21
  preloaded_model_file = os.path.join(os.getcwd(), "weight_files/yolov5.onnx") # Example path
 
22
 
23
  def load_sample_image(sample_name):
24
  """Load a sample image based on user selection."""
 
27
  return Image.open(image_path)
28
  return None
29
 
 
 
 
 
 
 
 
 
30
  def process_image(sample_choice, uploaded_image, yolo_versions):
31
+ """Process the image using selected YOLO models."""
32
  if uploaded_image is not None:
33
  image = uploaded_image # Use the uploaded image
34
  else:
35
  image = load_sample_image(sample_choice) # Use selected sample image
36
 
37
+ image = np.array(image)
38
+ image = cv2.resize(image, (640, 640))
39
  result_images = []
40
 
41
+ # Encode image to base64
42
+ _, buffer = cv2.imencode('.jpg', image)
43
+ image_base64 = base64.b64encode(buffer).decode('utf-8')
44
+
45
+ # Process image with each selected YOLO version
46
  for yolo_version in yolo_versions:
47
  if yolo_version == "yolov5":
48
+ result_images.append(xai_yolov5(image))
49
+ elif yolo_version == "yolov8s":
50
+ result_images.append(xai_yolov8s(image))
 
 
 
 
 
 
 
 
 
51
  else:
52
+ result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
53
 
54
  return result_images
55
 
 
99
  )
100
 
101
  selected_models = gr.CheckboxGroup(
102
+ choices=["yolov5", "yolov8s"],
103
  value=["yolov5"],
104
  label="Select Model(s)",
105
  )