b4one commited on
Commit
3ac9a65
·
1 Parent(s): 8269c19

Initial commit for YOLOv8 object detection

Browse files
Files changed (3) hide show
  1. app.py +33 -66
  2. best_yolo8_model/README.md +4 -0
  3. best_yolo8_model/best.pt +3 -0
app.py CHANGED
@@ -1,70 +1,37 @@
 
 
1
  import gradio as gr
2
- import openvino.runtime as ov
3
- import numpy as np
4
- from PIL import Image, ImageDraw
5
- import os
6
 
7
- # Define local model path
8
- MODEL_PATH = "./best_int8_openvino_model/best.xml" # Path to OpenVINO model file
9
 
 
10
  def load_model(model_path):
11
- # Load OpenVINO model
12
- print(f"Loading model from {model_path}")
13
- core = ov.Core()
14
- compiled_model = core.compile_model(model_path, "CPU")
15
- return compiled_model
16
-
17
- # Load the model
18
- compiled_model = load_model(MODEL_PATH)
19
- input_layer = compiled_model.input(0)
20
- output_layer = compiled_model.output(0)
21
-
22
- # Define inference function
23
- def predict(image):
24
- # Preprocess the image
25
- image = image.resize((640, 640)) # Resize to model input size
26
- image_array = np.array(image).astype(np.float32)
27
- image_array = np.transpose(image_array, (2, 0, 1)) # Channels-first for OpenVINO
28
- image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
29
-
30
- # Perform inference
31
- results = compiled_model([image_array])
32
- output_layer_name = list(results.keys())[0] # Assuming single output layer
33
- boxes = []
34
- for result in results[output_layer_name]:
35
- box = {
36
- 'bounding_box': result[:4].tolist(), # Ensure coordinates are converted to Python list
37
- 'confidence': float(result[4]), # Convert to Python float
38
- 'class_id': int(result[5]) # Convert to Python int
39
- }
40
- boxes.append(box)
41
-
42
- # Draw bounding boxes
43
- draw = ImageDraw.Draw(image)
44
- for box in boxes:
45
- x_min, y_min, x_max, y_max = map(int, box['bounding_box'])
46
-
47
- # Ensure coordinates are within bounds
48
- x_min = max(0, min(x_min, image.width - 1))
49
- y_min = max(0, min(y_min, image.height - 1))
50
- x_max = max(0, min(x_max, image.width - 1))
51
- y_max = max(0, min(y_max, image.height - 1))
52
-
53
- # Draw rectangle and class label
54
- draw.rectangle([x_min, y_min, x_max, y_max], outline="red", width=3)
55
- draw.text((x_min, y_min), f"Class {box['class_id']}: {box['confidence']:.2f}", fill="red")
56
-
57
- return image
58
-
59
-
60
- # Gradio Interface
61
- interface = gr.Interface(
62
- fn=predict, # Use the updated predict function
63
- inputs=gr.Image(type="pil"), # Accept an image as input
64
- outputs=gr.Image(type="pil"), # Return the annotated image as output
65
- title="OpenVINO Object Detection" # Title of the app
66
- )
67
-
68
- # Launch the app
69
- if __name__ == "__main__":
70
- interface.launch(share=True)
 
1
+ from ultralytics import YOLO
2
+ from PIL import Image
3
  import gradio as gr
 
 
 
 
4
 
5
+ # Define the path to the YOLOv8 model
6
+ model_path = "./best_yolo8_model/best.pt" # Adjust to your local model path
7
 
8
+ # Function to load the YOLOv8 model
9
  def load_model(model_path):
10
+ print(f"Loading YOLOv8 model from: {model_path}")
11
+ detection_model = YOLO(model_path) # Directly load the local model
12
+ return detection_model
13
+
14
+ # Function to perform inference
15
+ def predict(pil_img):
16
+ try:
17
+ # Perform prediction
18
+ results = detection_model.predict(source=pil_img, conf=0.5, iou=0.6, save=False, save_txt=False)
19
+
20
+ # Visualize bounding boxes on the image
21
+ img_with_boxes = results[0].plot()
22
+ out_pil_img = Image.fromarray(img_with_boxes[..., ::-1]) # Convert BGR to RGB
23
+
24
+ return out_pil_img
25
+ except Exception as e:
26
+ return f"Error during prediction: {str(e)}"
27
+
28
+ # Load the YOLOv8 model
29
+ detection_model = load_model(model_path)
30
+
31
+ # Define Gradio interface
32
+ gr.Interface(
33
+ fn=predict,
34
+ inputs=gr.Image(type="pil"),
35
+ outputs=gr.Image(type="pil"),
36
+ title="YOLOv8 Object Detection"
37
+ ).launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
best_yolo8_model/README.md ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ What the model does.
2
+ Dataset used.
3
+ Training details (e.g., epochs, optimizer).
4
+ License (if required)
best_yolo8_model/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d21c0ac1a455143c65f21ea2e68b2299b4263d96d0851091ebf6610f712cf043
3
+ size 22520739