PyCharm Code Inspect fixes for Solutions and Examples (#18393)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Muhammad Rizwan Munawar 2024-12-25 23:36:35 +05:00 committed by GitHub
parent 3e65fc2421
commit b1af683d7a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 92 additions and 62 deletions

View file

@ -34,7 +34,6 @@ class ParkingPtsSelection:
canvas_max_height (int): Maximum height of the canvas.
Methods:
setup_ui: Sets up the Tkinter UI components.
initialize_properties: Initializes the necessary properties.
upload_image: Uploads an image, resizes it to fit the canvas, and displays it.
on_canvas_click: Handles mouse clicks to add points for bounding boxes.
@ -55,20 +54,22 @@ class ParkingPtsSelection:
from tkinter import filedialog, messagebox
self.tk, self.filedialog, self.messagebox = tk, filedialog, messagebox
self.setup_ui()
self.initialize_properties()
self.master.mainloop()
def setup_ui(self):
"""Sets up the Tkinter UI components for the parking zone points selection interface."""
self.master = self.tk.Tk()
self.master = self.tk.Tk() # Reference to the main application window or parent widget
self.master.title("Ultralytics Parking Zones Points Selector")
self.master.resizable(False, False)
# Canvas for image display
self.canvas = self.tk.Canvas(self.master, bg="white")
self.canvas = self.tk.Canvas(self.master, bg="white") # Canvas widget for displaying images or graphics
self.canvas.pack(side=self.tk.BOTTOM)
self.image = None # Variable to store the loaded image
self.canvas_image = None # Reference to the image displayed on the canvas
self.canvas_max_width = None # Maximum allowed width for the canvas
self.canvas_max_height = None # Maximum allowed height for the canvas
self.rg_data = None # Data related to region or annotation management
self.current_box = None # Stores the currently selected or active bounding box
self.imgh = None # Height of the current image
self.imgw = None # Width of the current image
# Button frame with buttons
button_frame = self.tk.Frame(self.master)
button_frame.pack(side=self.tk.TOP)
@ -80,6 +81,9 @@ class ParkingPtsSelection:
]:
self.tk.Button(button_frame, text=text, command=cmd).pack(side=self.tk.LEFT)
self.initialize_properties()
self.master.mainloop()
def initialize_properties(self):
"""Initialize properties for image, canvas, bounding boxes, and dimensions."""
self.image = self.canvas_image = None
@ -105,7 +109,7 @@ class ParkingPtsSelection:
)
self.canvas.config(width=canvas_width, height=canvas_height)
self.canvas_image = ImageTk.PhotoImage(self.image.resize((canvas_width, canvas_height), Image.LANCZOS))
self.canvas_image = ImageTk.PhotoImage(self.image.resize((canvas_width, canvas_height)))
self.canvas.create_image(0, 0, anchor=self.tk.NW, image=self.canvas_image)
self.canvas.bind("<Button-1>", self.on_canvas_click)
@ -144,8 +148,13 @@ class ParkingPtsSelection:
"""Saves the selected parking zone points to a JSON file with scaled coordinates."""
scale_w, scale_h = self.imgw / self.canvas.winfo_width(), self.imgh / self.canvas.winfo_height()
data = [{"points": [(int(x * scale_w), int(y * scale_h)) for x, y in box]} for box in self.rg_data]
with open("bounding_boxes.json", "w") as f:
json.dump(data, f, indent=4)
from io import StringIO # Function level import, as it's only required to store coordinates, not every frame
write_buffer = StringIO()
json.dump(data, write_buffer, indent=4)
with open("bounding_boxes.json", "w", encoding="utf-8") as f:
f.write(write_buffer.getvalue())
self.messagebox.showinfo("Success", "Bounding boxes saved to bounding_boxes.json")