o
    Wh3                     @   sf   d dl Z d dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ G dd dZG dd deZdS )	    N)BaseSolutionSolutionAnnotatorSolutionResults)LOGGER)check_imshowc                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )ParkingPtsSelectiona  
    A class for selecting and managing parking zone points on images using a Tkinter-based UI.

    This class provides functionality to upload an image, select points to define parking zones, and save the
    selected points to a JSON file. It uses Tkinter for the graphical user interface.

    Attributes:
        tk (module): The Tkinter module for GUI operations.
        filedialog (module): Tkinter's filedialog module for file selection operations.
        messagebox (module): Tkinter's messagebox module for displaying message boxes.
        master (tk.Tk): The main Tkinter window.
        canvas (tk.Canvas): The canvas widget for displaying the image and drawing bounding boxes.
        image (PIL.Image.Image): The uploaded image.
        canvas_image (ImageTk.PhotoImage): The image displayed on the canvas.
        rg_data (List[List[Tuple[int, int]]]): List of bounding boxes, each defined by 4 points.
        current_box (List[Tuple[int, int]]): Temporary storage for the points of the current bounding box.
        imgw (int): Original width of the uploaded image.
        imgh (int): Original height of the uploaded image.
        canvas_max_width (int): Maximum width of the canvas.
        canvas_max_height (int): Maximum height of the canvas.

    Methods:
        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.
        draw_box: Draws a bounding box on the canvas.
        remove_last_bounding_box: Removes the last bounding box and redraws the canvas.
        redraw_canvas: Redraws the canvas with the image and all bounding boxes.
        save_to_json: Saves the bounding boxes to a JSON file.

    Examples:
        >>> parking_selector = ParkingPtsSelection()
        >>> # Use the GUI to upload an image, select parking zones, and save the data
    c           	      C   sz  zddl }ddl m}m} W n" ty0   ddl}dddd| d}td	|  Y dS w t	d
ds8dS |||| _
| _| _| j
 | _| jd | jdd | j
j| jdd| _| jj| j
jd d| _d| _d| _d| _d| _d| _d| _d| _| j
| j}|j| j
jd d| jfd| jfd| jffD ]\}}| j
j |||dj| j
j!d q| "  | j#  dS )zhInitialize the ParkingPtsSelection class, setting up UI and properties for parking zone point selection.r   N)
filedialog
messageboxzrsudo apt install python3-tk (Debian/Ubuntu) | sudo dnf install python3-tkinter (Fedora) | sudo pacman -S tk (Arch)zgreinstall Python and enable the checkbox `tcl/tk and IDLE` on **Optional Features** during installationzYreinstall Python from https://www.python.org/downloads/macos/ or `brew install python-tk`)LinuxWindowsDarwinz+Unknown OS. Check your Python installation.z8 Tkinter is not configured or supported. Potential fix: T)warnz)Ultralytics Parking Zones Points SelectorFwhite)bg)sidezUpload ImagezRemove Last BBoxSave)textcommand)$tkinterr   r	   ImportErrorplatformgetsystemr   warningr   tkTkmastertitle	resizableCanvascanvaspackBOTTOMimagecanvas_imagecanvas_max_widthcanvas_max_heightrg_datacurrent_boximghimgwFrameTOPupload_imageremove_last_bounding_boxsave_to_jsonButtonLEFTinitialize_propertiesmainloop)	selfr   r   r	   r   install_cmdbutton_framer   cmd r8   \/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/solutions/parking_management.py__init__1   sN   
 zParkingPtsSelection.__init__c                 C   s6   d | _ | _g g | _| _d | _| _d\| _| _dS )zHInitialize properties for image, canvas, bounding boxes, and dimensions.Nr   )i   i  )r#   r$   r'   r(   r*   r)   r%   r&   r4   r8   r8   r9   r2   e   s   z)ParkingPtsSelection.initialize_propertiesc                 C   s   ddl m}m} || jjdgd| _| jsdS | jj\| _| _	| j| j	 }|dkr2t
| j| jnt| j| }|dkrDt
| j| j	nt|| }| jj||d || j||f| _| jjdd| jj| jd | jd	| j | j | j f dS )
zZUpload and display an image on the canvas, resizing it to fit within specified dimensions.r   )ImageImageTk)zImage Filesz*.png *.jpg *.jpeg)	filetypesN   )widthheightanchorr#   z
<Button-1>)PILr<   r=   openr   askopenfilenamer#   sizer*   r)   minr%   intr&   r    config
PhotoImageresizer$   create_imager   NWbindon_canvas_clickr'   clearr(   )r4   r<   r=   aspect_ratiocanvas_widthcanvas_heightr8   r8   r9   r-   l   s   " z ParkingPtsSelection.upload_imagec                 C   s   | j |j|jf | jj|jd |jd |jd |jd dd t| j dkr>| j| j   | 	| j  | j 
  dS dS )zCHandle mouse clicks to add points for bounding boxes on the canvas.   red)fill   N)r(   appendxyr    create_ovallenr'   copydraw_boxrQ   )r4   eventr8   r8   r9   rP      s   .z#ParkingPtsSelection.on_canvas_clickc                 C   s6   t dD ]}| jj|| ||d d  ddd qdS )zADraw a bounding box on the canvas using the provided coordinates.rX   r?   blue   )rW   r@   N)ranger    create_line)r4   boxir8   r8   r9   r_      s   &zParkingPtsSelection.draw_boxc                 C   s.   | j s| jdd dS | j   |   dS )zARemove the last bounding box from the list and redraw the canvas.WarningzNo bounding boxes to remove.N)r'   r	   showwarningpopredraw_canvasr;   r8   r8   r9   r.      s
   
z,ParkingPtsSelection.remove_last_bounding_boxc                 C   s@   | j d | j jdd| jj| jd | jD ]}| | qdS )z8Redraw the canvas with the image and all bounding boxes.allr   rB   N)r    deleterM   r   rN   r$   r'   r_   )r4   re   r8   r8   r9   rj      s
   
z!ParkingPtsSelection.redraw_canvasc                    s   | j | j  | j| j    fdd| jD }ddlm} | }tj	||dd t
ddd	d
}||  W d   n1 sFw   Y  | jdd dS )zMSave the selected parking zone points to a JSON file with scaled coordinates.c                    s$   g | ]}d  fdd|D iqS )pointsc                    s(   g | ]\}}t | t |  fqS r8   )rI   ).0rZ   r[   scale_hscale_wr8   r9   
<listcomp>   s   ( z?ParkingPtsSelection.save_to_json.<locals>.<listcomp>.<listcomp>r8   )rn   re   ro   r8   r9   rr      s   $ z4ParkingPtsSelection.save_to_json.<locals>.<listcomp>r   )StringIOrX   )indentzbounding_boxes.jsonwzutf-8)encodingNSuccessz+Bounding boxes saved to bounding_boxes.json)r*   r    winfo_widthr)   winfo_heightr'   iors   jsondumprE   writegetvaluer	   showinfo)r4   datars   write_bufferfr8   ro   r9   r/      s   "z ParkingPtsSelection.save_to_jsonN)__name__
__module____qualname____doc__r:   r2   r-   rP   r_   r.   rj   r/   r8   r8   r8   r9   r      s    #4	r   c                       s(   e Zd ZdZ fddZdd Z  ZS )ParkingManagementa*  
    Manages parking occupancy and availability using YOLO model for real-time monitoring and visualization.

    This class extends BaseSolution to provide functionality for parking lot management, including detection of
    occupied spaces, visualization of parking regions, and display of occupancy statistics.

    Attributes:
        json_file (str): Path to the JSON file containing parking region details.
        json (List[Dict]): Loaded JSON data containing parking region information.
        pr_info (Dict[str, int]): Dictionary storing parking information (Occupancy and Available spaces).
        arc (Tuple[int, int, int]): RGB color tuple for available region visualization.
        occ (Tuple[int, int, int]): RGB color tuple for occupied region visualization.
        dc (Tuple[int, int, int]): RGB color tuple for centroid visualization of detected objects.

    Methods:
        process: Processes the input image for parking lot management and visualization.

    Examples:
        >>> from ultralytics.solutions import ParkingManagement
        >>> parking_manager = ParkingManagement(model="yolo11n.pt", json_file="parking_regions.json")
        >>> print(f"Occupied spaces: {parking_manager.pr_info['Occupancy']}")
        >>> print(f"Available spaces: {parking_manager.pr_info['Available']}")
    c                    s   t  jd
i | | jd | _| jdu rtd tdt| j}t	|| _W d   n1 s3w   Y  ddd| _
d| _d| _d	| _dS )zVInitialize the parking management system with a YOLO model and visualization settings.	json_fileNu@   ❌ json_file argument missing. Parking region details required.u#   ❌ Json file path can not be emptyr   )	Occupancy	Available)r   r      )r   r   r   )r   r      r8   )superr:   CFGr   r   r   
ValueErrorrE   r{   loadpr_infoarcoccdc)r4   kwargsr   	__class__r8   r9   r:      s   


zParkingManagement.__init__c                 C   s~  |  | t| jd}}t|| j}| jD ]y}tj|d tjdd}d}t	| j
| jD ]?\}}	t|d |d  d t|d |d  d }
}t||
|fd}|dkrm||| jjt|	 d	d
|
|d d} nq.|rx|d |d fn||f\}}tj||gd|r| jn| jdd q||| jd< | jd< ||| jd	d
d | }| | t|| jd | jd t| jdS )a  
        Process the input image for parking lot management and visualization.

        This function analyzes the input image, extracts tracks, and determines the occupancy status of parking
        regions defined in the JSON file. It annotates the image with occupied and available parking spots,
        and updates the parking information.

        Args:
            im0 (np.ndarray): The input inference image.

        Returns:
            (SolutionResults): Contains processed image `plot_im`, 'filled_slots' (number of occupied parking slots),
                'available_slots' (number of available parking slots), and 'total_tracks' (total number of tracked objects).

        Examples:
            >>> parking_manager = ParkingManagement(json_file="parking_regions.json")
            >>> image = cv2.imread("parking_lot.jpg")
            >>> results = parking_manager.process(image)
        r   rm   )dtype)r?   rb   Frb   r?   rU   )h         )r   r   r   
   T)isClosedcolor	thicknessr   r   )plot_imfilled_slotsavailable_slotstotal_tracks)extract_tracksr]   r{   r   
line_widthnparrayint32reshapezipboxesclssrI   cv2pointPolygonTestdisplay_objects_labelsmodelnames	polylinesr   r   r   display_analyticsresultdisplay_outputr   	track_ids)r4   im0esfs	annotatorregion	pts_arrayrg_occupiedre   clsxcycdistr   r8   r8   r9   process   s8   

2 $
zParkingManagement.process)r   r   r   r   r:   r   __classcell__r8   r8   r   r9   r      s    r   )r{   r   numpyr   ultralytics.solutions.solutionsr   r   r   ultralytics.utilsr   ultralytics.utils.checksr   r   r   r8   r8   r8   r9   <module>   s    #