Skip to content

Commit 785b439

Browse files
authored
Create Object-Detection-System.py
1 parent 0739902 commit 785b439

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import tkinter as tk
2+
from tkinter import filedialog, messagebox
3+
from PIL import Image, ImageTk
4+
import cv2
5+
from ultralytics import YOLO
6+
import ttkbootstrap as tb
7+
from ttkbootstrap.constants import *
8+
9+
# Initialize YOLO model
10+
model = YOLO("yolov8n.pt") # YOLOv8 Nano model
11+
12+
class ObjectDetectionApp:
13+
def __init__(self, root):
14+
self.root = root
15+
self.root.title("Object Detection System")
16+
self.root.geometry("900x600")
17+
self.root.resizable(False, False)
18+
19+
# ttkbootstrap style
20+
self.style = tb.Style("superhero")
21+
22+
# Frame for buttons
23+
self.frame = tb.Frame(root)
24+
self.frame.pack(side=TOP, pady=10)
25+
26+
self.load_btn = tb.Button(self.frame, text="Load Image", bootstyle=PRIMARY, command=self.load_image)
27+
self.load_btn.grid(row=0, column=0, padx=10)
28+
29+
self.detect_btn = tb.Button(self.frame, text="Detect Objects", bootstyle=SUCCESS, command=self.detect_objects)
30+
self.detect_btn.grid(row=0, column=1, padx=10)
31+
32+
# Label to display images
33+
self.image_label = tb.Label(root)
34+
self.image_label.pack(pady=10)
35+
36+
self.img_path = None
37+
self.display_img = None
38+
39+
def load_image(self):
40+
self.img_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.jpeg *.png")])
41+
if self.img_path:
42+
img = Image.open(self.img_path)
43+
# Pillow 10+ compatible resize
44+
img = img.resize((800, 500), Image.Resampling.LANCZOS)
45+
self.display_img = ImageTk.PhotoImage(img)
46+
self.image_label.config(image=self.display_img)
47+
48+
def detect_objects(self):
49+
if not self.img_path:
50+
messagebox.showwarning("Warning", "Please load an image first!")
51+
return
52+
53+
# Run YOLO detection
54+
results = model(self.img_path)[0]
55+
56+
# Load image with OpenCV
57+
img_cv = cv2.imread(self.img_path)
58+
59+
# Draw bounding boxes
60+
for box, cls, conf in zip(results.boxes.xyxy, results.boxes.cls, results.boxes.conf):
61+
x1, y1, x2, y2 = map(int, box)
62+
label = f"{model.names[int(cls)]} {conf:.2f}"
63+
cv2.rectangle(img_cv, (x1, y1), (x2, y2), (0, 255, 0), 2)
64+
cv2.putText(img_cv, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
65+
66+
# Convert BGR to RGB for Tkinter
67+
img_rgb = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)
68+
img_pil = Image.fromarray(img_rgb)
69+
img_pil = img_pil.resize((800, 500), Image.Resampling.LANCZOS)
70+
self.display_img = ImageTk.PhotoImage(img_pil)
71+
self.image_label.config(image=self.display_img)
72+
73+
74+
if __name__ == "__main__":
75+
root = tb.Window(themename="superhero")
76+
app = ObjectDetectionApp(root)
77+
root.mainloop()

0 commit comments

Comments
 (0)