o
    %h                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ e  dZdZe d	Zd
efddZdefddZdedefddZdedefddZdddZdd ZdedefddZdS )     N)logger)defaultdict)Image)BytesIO)load_dotenvInput_ImagesOutput_ImagesDOMAIN	timestampc              
      s   z(dt  dt d|  d}dt  dt d|  d}td| d|  ||fW S  tyG } ztd|  d td	t| d }~ww )
Nhttps:///z/before.jpgz
/after.jpgz!Generated image URLs: before_url=z, after_url=z0Error while generating image URLs for timestamp .z#Error while generating image URLs: )r	   BASE_INPUT_DIRr   info	Exception	exceptionstr)r
   before_image_urlafter_image_urle r   G/var/www/html/construction_image-detection-poc/src/utils/files_utils.pysave_input_images_async   s   
r   output_image_pathc              
      sl   zdt  dt d|  }td|  |W S  ty5 } ztd|  d tdt| d }~ww )Nr   r   zGenerated output image URL: z,Error while generating output image URL for r   z)Error while generating output image URL: )r	   BASE_OUTPUT_DIRr   r   r   r   r   )r   
output_urlr   r   r   r   generate_output_image_url   s   r   
image_pathreturnc                    sH   t j std   fdd}t|I d H }t|dS )NzImage not found: c                     s6   t  d} |  W  d    S 1 sw   Y  d S )Nrb)openread)fr   r   r   
read_image+   s   $z encode_image.<locals>.read_imageutf-8)	ospathexistsFileNotFoundErrorasyncio	to_threadbase64	b64encodedecode)r   r$   image_bytesr   r#   r   encode_image'   s   r0   captionc                 C   s"   |    }|dr|S |d S )z>Add a period at the end of the caption if it doesn't have one.r   )lowerstripendswith)r1   resultr   r   r   preprocess_caption2   s   r6   皙?c                    s   t t}t trGt fdddD rG d  } d  } d  }t|||D ]\}}}	||krF||t|}
||
 	|	|f q,|S )z>Convert model predictions to a dictionary of labels and boxes.c                 3   s    | ]}| v V  qd S )Nr   ).0keypredictionsr   r   	<genexpr>;   s    z%get_label_box_dict.<locals>.<genexpr>)scoreslabelsboxesr=   r>   r?   )
r   list
isinstancedictalltolistzipgetr   append)r;   score_thresholdid2label
label_dictr=   	label_idsr?   scorelabel_idboxlabelr   r:   r   get_label_box_dict7   s    rP   c                 C   s   | \}}}}|\}}}}	t ||}
t ||}t||}t||	}t d||
 t d||  }|| ||  }|| |	|  }|| | }|dkrL|| S dS )z;Compute Intersection over Union between two bounding boxes.r   )maxmin)box1box2x1y1x2y2x1_py1_px2_py2_px_inty_intx_int2y_int2intersection_area	area_box1	area_box2
union_arear   r   r   compute_iouG   s   



re   imagec                 C   s,   t  }| j|dd t| d}|S )z#Convert PIL Image to base64 string.PNG)formatr%   )r   saver,   r-   getvaluer.   )rf   buffered
img_base64r   r   r   image_to_base64Z   s   rm   )r7   N)r&   r,   r*   src.common.loggerr   collectionsr   PILr   ior   dotenvr   r   r   getenvr	   r   r   r   r0   r6   rP   re   rm   r   r   r   r   <module>   s&    

