
    Xg                    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)TextRecognitionc                  $    e Zd ZddZddZddZy)ONNXTextRecognitionc           	     $   |}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v4z.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/text_recognition.pyexecutor_initializerz?ONNXTextRecognition.downloadModel.<locals>.executor_initializer3   sC    #22499K/335K+6OK(4;;KHI    text)initializermax_workersthread_name_prefixztext-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!ONNXTextRecognition.downloadModel   s   	$$<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t        j                         j                  |fd       d {   }|d   S 7 	w)Nc                 r    t        j                         j                     } | j                  d i      }|S Nr   r   r   runr<   output_tensorsr   r=   r>   s     r   predictz7ONNXTextRecognition.predictDetectModel.<locals>.predictI   9    ,Y-E-E-G-L-LMN+//z56IJN!!r!   c                              S rD    rI   s   r   <lambda>z8ONNXTextRecognition.predictDetectModel.<locals>.<lambda>O   	    gi r!   r   )detectModelasyncioget_event_looprun_in_executorr6   r=   r@   r?   objsr   r>   rI   s    `   @@@r   predictDetectModelz&ONNXTextRecognition.predictDetectModelF   sV     AEAQAQ>_h	"
 ++-=='
 
 Aw	
s   AA	A

Ac                   K   j                  t        j                        | j                  \  }}fdt	        j
                         j                  |fd       d {   }|d   S 7 	w)Nc                 r    t        j                         j                     } | j                  d i      }|S rD   rE   rG   s     r   rI   z5ONNXTextRecognition.predictTextModel.<locals>.predictX   rJ   r!   c                              S rD   rL   rM   s   r   rN   z6ONNXTextRecognition.predictTextModel.<locals>.<lambda>^   rO   r!   r   )astypenpfloat32	textModelrQ   rR   rS   rT   s    `   @@@r   predictTextModelz$ONNXTextRecognition.predictTextModelT   sf     RZZ(AE>_h	"
 ++-=='
 
 Aw	
s   A"A4(A2)
A4N)r7   str)r=   zImage.Image)r=   z
np.ndarray)__name__
__module____qualname__rA   rV   r^   rL   r!   r   r   r      s    3Fjr!   r   )
__future__r   rQ   concurrent.futuresr2   r,   r+   r   numpyr[   r)   PILr   predict.text_recognizer   r   rL   r!   r   <module>rh      s1    "    
     2Q/ Qr!   