
    n,h=                        d dl mZ d dlZd dlZd dlmZ d dlZ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Z G d de      Zy)    )annotationsN)Image)CustomDetection)ObjectsDetectedc                  0     e Zd Zd fdZddZddZ xZS )ONNXCustomDetectionc                    t         |   ||       d| _        t        j                  j                  dd      | _        y )N)pluginnativeIdT   zdetect-custom)super__init__prefer_relu
concurrentfuturesThreadPoolExecutordetectExecutor)selfr
   r   	__class__s      J/server/volume/plugins/@scrypted/onnx/zip/unzipped/ort/custom_detection.pyr   zONNXCustomDetection.__init__   s8    :(00CCAW    c                N   |D cg c]$  }|j                         j                  d      s#|& }}|st        d      |d   }g i | j                  j                  }|D ]  }t        j                         }g }t        j                  dk(  r|j                  d       dt        j                  v r7t        j                         dk(  r t        |      }|j                  dd	|if       |j                  d
       t        j                  |||      }	j                  |	       |	j                         d   }
|
j                  } fd}t        j                   j#                  |t%              d      }t        j                   j#                  t%              d      }||fS c c}w )Nz.onnxz6No Manifest.json file found in the provided files listr   darwinCoreMLExecutionProviderlinuxx86_64CUDAExecutionProvider	device_idCPUExecutionProvider)sess_options	providersc                     t        j                         j                  } j                         }|| <   t	        dj                  |              y )Nz Runtime initialized on thread {})	threadingcurrent_threadnamepopprintformat)thread_nameinterpretercompiled_modelscompiled_models_arrays     r   executor_initializerz;ONNXCustomDetection.loadModel.<locals>.executor_initializer7   sC    #22499K/335K+6OK(4;;KHIr   custom)initializermax_workersthread_name_prefixzcustom-prepare)r0   r1   )lowerendswith
ValueErrorr
   	deviceIdsonnxruntimeSessionOptionssysplatformappendmachineintInferenceSession
get_inputsr%   r   r   r   len)r   filesf
onnx_files	onnx_filer5   deviceIdr    r!   compiled_modelinput
input_namer-   executorprepareExecutorr+   r,   s                  @@r   	loadModelzONNXCustomDetection.loadModel   s   !&FA!'')*<*<W*EaF
FUVVqM	 "KK))	! 	$H&557L#%I||x'  !:;#,,&8+;+;+=+Ix=  "9K;R!ST34(99	N "((8"--/2EJ'	$*	J %%88,12' 9 
 %,,??12/ @ 

 
OXEE_ Gs
   $F"F"c                4   	K    j                   \  }} fd fd	fdt        j                         j                  |	fd       d {   t        j                         j                  |fd       d {   }|d   d   S 7 <7 w)Nc                     j                   j                  dd       r)j                   j                  dd       rt        j                  d      } | j	                  d      } | j                  t        j                        dz  } t        j                  j                   d         }t        j                  j                   d         }|j                  dddd      }|j                  dddd      }| |z
  |z  } t        j                  | j                  t        j                              } t        j                  t        | i      }n'j                  j                  j                  i      }t        |j                               d   d   }|S )	Nmeanstdr   axisr      r           o@r   )model_configgetnpexpand_dims	transposeastypefloat32arrayreshapeascontiguousarraymodelpredict	inputNamelistvalues)imrM   rN   out_dictresultsrF   r   s        r   ra   z1ONNXCustomDetection.predictModel.<locals>.predictM   s8     $$VT2t7H7H7L7LUTX7Y^^E2\\,/YYrzz*U2xx 1 1& 9:hht0078||Ar1a0kk!RA.4i3&))"))BJJ*?@ ==)R9::--t~~u.EF8??,-a03GNr   c                    t        j                        } t        j                  d      } | j                  d      } | j	                  t         j
                        dz  } j                  j                  dd       rj                  j                  dd       rt        j                  j                  d         }t        j                  j                  d         }|j                  dddd      }|j                  dddd      }| |z
  |z  } | j	                  t         j
                        } t        j                  |       } | S )	Nr   rO   rQ   rT   rM   rN   r   rU   )
rX   r]   rY   rZ   r[   r\   rV   rW   r^   r_   )re   rM   rN   rF   r   s      r   preparez1ONNXCustomDetection.predictModel.<locals>.prepareb   s    %BA.Bl+B2::&.B  $$VT2t7H7H7L7LUTX7Yxx 1 1& 9:hht0078||Ar1a0kk!RA.4i3&YYrzz*%%b)BIr   c                r    t        j                         j                     }|j                  d | i      }|S N)r#   r$   r%   run)input_tensorrE   output_tensorsr+   rG   s      r   ra   z1ONNXCustomDetection.predictModel.<locals>.predicts   s9    ,Y-E-E-G-L-LMN+//z<6PQN!!r   c                              S rk    )ri   s   r   <lambda>z2ONNXCustomDetection.predictModel.<locals>.<lambda>y   s	    WY r   c                             S rk   rp   )rm   ra   s   r   rq   z2ONNXCustomDetection.predictModel.<locals>.<lambda>|   s    gl3 r   r   )r`   asyncioget_event_looprun_in_executor)
r   rF   rI   rH   objsr+   rG   rm   ra   ri   s
   ``   @@@@@r   predictModelz ONNXCustomDetection.predictModelK   s     AE>_h	*	"	"
 %335EE.
 
 ++-==3
 
 Awqz

s$   ABB/BBBB)r   str)r@   z	list[str])rF   zImage.Imagereturnr   )__name__
__module____qualname__r   rJ   rw   __classcell__)r   s   @r   r   r      s    X
1Fh4r   r   )
__future__r   rs   numpyrX   PILr   r6   r8   r#   r9   predict.custom_detectr   scrypted_sdkr   concurrent.futuresr   r   rp   r   r   <module>r      s4    "     
   1 ( n/ nr   