
    Xgv                    v    d dl mZ d dlZd dlZd dl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  G d de      Zy)    )annotationsN)Image)FaceRecognizeDetectionc                  $    e Zd ZddZddZddZy)ONNXFaceRecognitionc           	     0   d|v rdn|}d}| j                  d| d| d| d| d| d      }t        |       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 )Nscryptedbestv1z.https://github.com/koush/onnx-models/raw/main//z.onnxdarwinCoreMLExecutionProviderlinuxx86_64CUDAExecutionProvider	device_idCPUExecutionProvider)sess_options	providersr   c                     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     J/server/volume/plugins/@scrypted/onnx/zip/unzipped/ort/face_recognition.pyexecutor_initializerz?ONNXFaceRecognition.downloadModel.<locals>.executor_initializer3   sC    #22499K/335K+6OK(4;;KHI    face)initializermax_workersthread_name_prefixzface-prepare)r&   r'   )downloadFiler   plugin	deviceIdsonnxruntimeSessionOptionssysplatformappendmachineintInferenceSession
get_inputsr   
concurrentfuturesThreadPoolExecutorlen)selfmodel	onnxmodelmodel_versiononnxfiler*   deviceIdr   r   compiled_modelinput
input_namer"   executorprepareExecutorr   r    s                  @@r!   downloadModelz!ONNXFaceRecognition.downloadModel   s   (E1Fu	$$<UG1YKuUoQugQyk7
 	h "KK))	! 	$H&557L#%I||x'  !:;#,,&8+;+;+=+Ix=  "9K;R!ST34(99|yN "((8"--/2EJ'	$*	J %%88,12% 9 
 %,,??12- @ 

 
OXEEr#   c                $  	K   | j                   \  }}fd	fdt        j                         j                  |	fd       d {   t        j                         j                  |fd       d {   }|d   d   S 7 <7 w)Nc                     t        j                        } t        j                  d      } | j                  d      } | j	                  t         j
                        dz  } t        j                  |       } | S )Nr   )axis)r            g     o@)nparrayexpand_dims	transposeastypefloat32ascontiguousarray)imr?   s    r!   preparez7ONNXFaceRecognition.predictDetectModel.<locals>.prepareI   sZ    %BA.Bl+B2::&.B%%b)BIr#   c                r    t        j                         j                     }|j                  d | i      }|S Nr   r   r   run)input_tensorr>   output_tensorsr   r@   s      r!   predictz7ONNXFaceRecognition.predictDetectModel.<locals>.predictQ   s9    ,Y-E-E-G-L-LMN+//z<6PQN!!r#   c                              S rT    )rR   s   r!   <lambda>z8ONNXFaceRecognition.predictDetectModel.<locals>.<lambda>W   s	    WY r#   c                             S rT   r[   )rW   rY   s   r!   r\   z8ONNXFaceRecognition.predictDetectModel.<locals>.<lambda>Z   s    gl3 r#   r   )detectModelasyncioget_event_looprun_in_executor)
r8   r?   rB   rA   objsr   r@   rW   rY   rR   s
    `   @@@@@r!   predictDetectModelz&ONNXFaceRecognition.predictDetectModelF   s     AEAQAQ>_h		"
 %335EE.
 
 ++-==3
 
 Awqz

s$   ABB/B?B BBc                   K   | j                   \  }}fdt        j                         j                  |fd       d {   }|d   S 7 	w)Nc                 r    t        j                         j                     } | j                  d i      }|S rT   rU   )r>   rX   r   r?   r@   s     r!   rY   z5ONNXFaceRecognition.predictFaceModel.<locals>.predictb   s9    ,Y-E-E-G-L-LMN+//z56IJN!!r#   c                              S rT   r[   )rY   s   r!   r\   z6ONNXFaceRecognition.predictFaceModel.<locals>.<lambda>h   s	    gi r#   r   )	faceModelr_   r`   ra   )r8   r?   rB   rA   rb   r   r@   rY   s    `   @@@r!   predictFaceModelz$ONNXFaceRecognition.predictFaceModel_   sT     AE>_h	"
 ++-=='
 
 Aw	
s   AA	A

AN)r9   str)r?   zImage.Image)r?   z
np.ndarray)__name__
__module____qualname__rC   rc   rh   r[   r#   r!   r   r      s    3Fj2r#   r   )
__future__r   r_   concurrent.futuresr4   r.   r-   r   numpyrJ   r+   PILr   predict.face_recognizer   r   r[   r#   r!   <module>rr      s2    "    
     9[0 [r#   