§
    –õqi.  ã            	       ód  — d Z ddlmZ ddlmZ ddlZddlmZm	Z	m
Z
 ddlZddlZddlZej                             ej                             ej                             ej                             e¦  «        ¦  «        ¦  «        ¦  «         ddlmZmZ  ej        e¦  «        Z G d„ d¦  «        ZdS )	a  
YOLO Object Detection Model for Property Verification

YOLOv8 (You Only Look Once) is a state-of-the-art object detection model that:
1. Divides the image into a grid
2. Predicts bounding boxes and class probabilities for each grid cell
3. Detects multiple objects in a single forward pass (very fast)
4. Returns detected objects with confidence scores

We use YOLOv8n (nano) - the smallest and fastest variant, perfect for real-time applications.
It's trained on the COCO dataset which includes 80 common object classes.
é    )ÚYOLO)ÚImageN)ÚListÚDictÚTuple)Ú
YOLO_MODELÚPROPERTY_OBJECTSc                   ó^   — e Zd ZdZd„ Zdedeeee	         ee         f         fd„Z
de	fd„ZdS )ÚYOLODetectorzX
    YOLO-based object detector for identifying property-related objects in images.
    c                 óD  — 	 t                                dt          › �¦  «         t          t          ¦  «        | _        t          t          ¦  «        | _        t                                d¦  «         dS # t          $ r#}t            	                    d|› �¦  «         ‚ d}~ww xY w)zm
        Initialize the YOLO model.
        The model will be downloaded automatically on first run.
        zLoading YOLO model: zYOLO model loaded successfullyzFailed to load YOLO model: N)
ÚloggerÚinfor   r   ÚmodelÚsetr	   Úproperty_objectsÚ	ExceptionÚerror)ÚselfÚes     úS/var/www/html/cool-property-finder/image-detection-backend/app/models/yolo_model.pyÚ__init__zYOLODetector.__init__#   s˜   € ð
	Ý�KŠKÐ;­zÐ;Ð;Ñ<Ô<Ð<Ý�jÑ)Ô)ˆDŒJÝ$'Õ(8Ñ$9Ô$9ˆDÔ!Ý�KŠKÐ8Ñ9Ô9Ð9Ð9Ð9øÝð 	ð 	ð 	Ý�LŠLÐ:°qÐ:Ð:Ñ;Ô;Ð;Øøøøøð	øøøs   ‚A.A2 Á2
BÁ<BÂBÚ
image_pathÚreturnc           	      ó´  — 	 |                       |d¬¦  «        }g }g }|D ]É}|j        }|D ]½}t          |j        d         ¦  «        }t	          |j        d         ¦  «        }	|j        |         }
|
t          |	d¦  «        dœ}|                     |¦  «         |
 	                    ¦   «         | j
        v r7|                     |
¦  «         t                               d|
› d|	d›d	�¦  «         Œ¾ŒÊt          |¦  «        dk    }t                               d
|› d|› �¦  «         |||fS # t          $ r,}t                               d|› �¦  «         dg g fcY d}~S d}~ww xY w)aË  
        Detect objects in an image and determine if it contains property-related items.
        
        Args:
            image_path: Path to the image file
            
        Returns:
            Tuple containing:
            - is_valid: Boolean indicating if property objects were detected
            - detections: List of all detected objects with confidence scores
            - property_objects_found: List of property-related objects found
        F)Úverboser   é   )ÚobjectÚ
confidencezProperty object detected: z (confidence: z.3fú)zYOLO Detection - Valid: z, Objects found: zError during YOLO detection: N)r   ÚboxesÚintÚclsÚfloatÚconfÚnamesÚroundÚappendÚlowerr   r   r   Úlenr   r   )r   r   ÚresultsÚ
detectionsÚproperty_objects_foundÚresultr    ÚboxÚclass_idr   Ú
class_nameÚdetection_infoÚis_validr   s                 r   Údetect_objectszYOLODetector.detect_objects1   sµ  € ð(	!ð —j’j °U�jÑ;Ô;ˆGàˆJØ%'Ð"ð "ð nð n�àœ�à ð nð n�Cå" 3¤7¨1¤:™œ�HÝ!& s¤x°¤{Ñ!3Ô!3�JØ!'¤¨hÔ!7�Jð #-Ý&+¨J¸Ñ&:Ô&:ð&ð &�Nð ×%Ò% nÑ5Ô5Ð5ð "×'Ò'Ñ)Ô)¨TÔ-BÐBÐBØ.×5Ò5°jÑAÔAÐAÝŸšÐ$lÀÐ$lÐ$lÐ[eÐ$lÐ$lÐ$lÐ$lÑmÔmÐmøð!nõ& Ð1Ñ2Ô2°QÒ6ˆHå�KŠKÐf°8ÐfÐfÐNdÐfÐfÑgÔgÐgà˜ZÐ)?Ð?Ð?øåð 	!ð 	!ð 	!Ý�LŠLÐ<¸Ð<Ð<Ñ=Ô=Ð=Ø˜"˜b�=Ð Ð Ð Ð Ð Ð øøøøð	!øøøs   ‚DD! Ä!
EÄ+!EÅEÅEc                 ó˜   — t           t          | j        ¦  «        t          | j        d¦  «        rt          | j        j        ¦  «        nddœS )z>
        Get information about the loaded YOLO model.
        r%   éP   )Ú
model_namer   Útotal_classes)r   Úlistr   Úhasattrr   r)   r%   )r   s    r   Úget_model_infozYOLODetector.get_model_infoh   sK   € õ
 %Ý $ TÔ%:Ñ ;Ô ;Ý6=¸d¼jÈ'Ñ6RÔ6RÐZ�S ¤Ô!1Ñ2Ô2Ð2ÐXZð
ð 
ð 	
ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   Úboolr   r   r3   r:   © r;   r   r   r      s   € € € € € ðð ðð ð ð5!¨ð 5!°°t¸TÀ$¼ZÈÈcÌÐ7RÔ1Sð 5!ð 5!ð 5!ð 5!ðn
 ð 
ð 
ð 
ð 
ð 
ð 
r;   r   )r?   Úultralyticsr   ÚPILr   ÚnumpyÚnpÚtypingr   r   r   ÚloggingÚsysÚosÚpathr'   ÚdirnameÚabspathÚ__file__Úconfigr   r	   Ú	getLoggerr<   r   r   rB   r;   r   ú<module>rQ      s   ððð ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ø €€€ð €
€
€
Ø 	€	€	€	Ø „‡‚�”—’ ¤§¢°´·²ÀÑ0IÔ0IÑ JÔ JÑKÔKÑ LÔ LÐ Là /Ð /Ð /Ð /Ð /Ð /Ð /Ð /à	ˆÔ	˜8Ñ	$Ô	$€ðR
ð R
ð R
ð R
ð R
ñ R
ô R
ð R
ð R
ð R
r;   