
    Wpf=                         d Z ddlmZ ddlm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 d
Z G d dej                  Zedk    r ej                     dS dS )z)Tests for mediapipe.python.solution_base.    )absltest)parameterizedN)text_format)calculator_pb2)detection_pb2)solution_base)PacketDataTypea  
  input_stream: 'image_in'
  output_stream: 'image_out'
  node {
    name: 'ImageTransformation'
    calculator: 'ImageTransformationCalculator'
    input_stream: 'IMAGE:image_in'
    output_stream: 'IMAGE:image_out'
    options: {
      [mediapipe.ImageTransformationCalculatorOptions.ext] {
         output_width: 10
         output_height: 10
      }
    }
    node_options: {
      [type.googleapis.com/mediapipe.ImageTransformationCalculatorOptions] {
         output_width: 10
         output_height: 10
      }
    }
  }
c                   $   e Zd Zd Z ej        ddedfddedfdd	ed
f          d             Zd Z	d Z
d Z ej        ddddddf          d             Zd Zd Zd Zd Zd Z ej        ddddddf          d             Zd Z	 	 ddZdS ) SolutionBaseTestc                 H   |                      t          d          5  t          j                     d d d            n# 1 swxY w Y   |                      t          d          5  t          j        t	          j                    d           d d d            d S # 1 swxY w Y   d S )NzBMust provide exactly one of 'binary_graph_path' or 'graph_config'.z/tmp/no_such.binarypb)graph_configbinary_graph_path)assertRaisesRegex
ValueErrorr   SolutionBaser   CalculatorGraphConfig)selfs    c/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/mediapipe/python/solution_base_test.py%test_invalid_initialization_argumentsz6SolutionBaseTest.test_invalid_initialization_arguments5   s1   			P
 
 # #  """	# # # # # # # # # # # # # # #
 
		P
 
 5 5  %;==35 5 5 5	5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5s!   <A A "(BBBno_graph_input_output_streamz
      node {
        calculator: 'PassThroughCalculator'
        input_stream: 'in'
        output_stream: 'out'
      }
      z,does not have a corresponding output stream.calcualtor_io_mismatchz
      node {
        calculator: 'PassThroughCalculator'
        input_stream: 'in'
        input_stream: 'in2'
        output_stream: 'out'
      }
      z#must use matching tags and indexes."unkown_registered_stream_type_namez
      input_stream: 'in'
      output_stream: 'out'
      node {
        calculator: 'PassThroughCalculator'
        input_stream: 'in'
        output_stream: 'out'
      }
      z(Unable to find the type for stream "in".c                     t          j        |t          j                              }|                     ||          5  t          j        |           d d d            d S # 1 swxY w Y   d S )Nr   )r   Parser   r   r   r   r   )r   text_config
error_typeerror_messageconfig_protos        r   test_invalid_configz$SolutionBaseTest.test_invalid_configC   s    2 $[%3%I%K%KM ML			
M	:	: < < l;;;;< < < < < < < < < < < < < < < < < <s   A  A$'A$c                 |   d}t          j        |t          j                              }t	          j        |          5 }t          j                    }|j        	                                }t          j        d|           |j        	                                }t          j        d|           |
                    d|i          }|                     t          |d                     |                     |j        j        d           t          j                    }t          j        d|           t          j                    }	t          j        d	|	           |                     |j        j        d
         |           |                     |j        j        d         |	           d d d            d S # 1 swxY w Y   d S )Na  
      input_stream: 'input_detections'
      output_stream: 'output_detections'
      node {
        calculator: 'DetectionUniqueIdCalculator'
        input_stream: 'DETECTION_LIST:input_detections'
        output_stream: 'DETECTION_LIST:output_detections'
      }
    r   
score: 0.5z
score: 0.8input_detectionsoutput_detections   zscore: 0.5, detection_id: 1zscore: 0.8, detection_id: 2r      )r   r   r   r   r   r   r   DetectionList	detectionaddprocess
assertTruehasattr	assertLenr$   	DetectionassertEqual)
r   r   r   solutionr#   detection_1detection_2resultsexpected_detection_1expected_detection_2s
             r    test_valid_input_data_type_protoz1SolutionBaseTest.test_valid_input_data_type_protoa   s   K $[%3%I%K%KM ML		#	>	>	> -(&466$.2244kk222$.2244kk222  "46F!GHHg
oogg':;;<<<
nnW.8!<<<*46657KLLL*46657KLLL
w0:1=+- - -
w0:1=+- - -- - - - - - - - - - - - - - - - - -s   E&F11F58F5c                    d}t          j        |t          j                              }t	          j        |          5 }t          j                    }t          j        d|           |                     t          d          5  |
                    d|i           d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Na  
      input_stream: 'input_detections'
      output_stream: 'output_detections'
      node {
        calculator: 'DetectionUniqueIdCalculator'
        input_stream: 'DETECTIONS:input_detections'
        output_stream: 'DETECTIONS:output_detections'
      }
    r   r"   zbSolutionBase can only process non-audio and non-proto-list data. PROTO_LIST type is not supported.r#   )r   r   r   r   r   r   r   r.   r   NotImplementedErrorr*   )r   r   r   r0   r(   s        r   )test_invalid_input_data_type_proto_vectorz:SolutionBaseTest.test_invalid_input_data_type_proto_vector   sR   K $[%3%I%K%KM ML		#	>	>	> :()++ii000!!
0  : :
 	,i8999: : : : : : : : : : : : : : :: : : : : : : : : : : : : : : : : :s6   AB>B&B>&B*	*B>-B*	.B>>CCc           	         d}t          j        |t          j                              }t	          j        |          5 }|                     t          d          5  |                    t          j
        dt          j                                      ddd                     d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Na  
      input_stream: 'image_in'
      output_stream: 'image_out'
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:image_in'
        output_stream: 'IMAGE:transformed_image_in'
      }
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:transformed_image_in'
        output_stream: 'IMAGE:image_out'
      }
    r   z0Input image must contain three channel rgb data.$   dtype      )r   r   r   r   r   r   r   r   r*   nparangeuint8reshape)r   r   r   r0   s       r   test_invalid_input_image_dataz.SolutionBaseTest.test_invalid_input_image_data   sp   K $[%3%I%K%KM ML		#	>	>	> I(!!
HJ J I I2RX666>>q!QGGHHHI I I I I I I I I I I I I I II I I I I I I I I I I I I I I I I Is6   CA	B/#C/B3	3C6B3	7CCC)graph_without_side_packetsa  
      input_stream: 'image_in'
      output_stream: 'image_out'
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:image_in'
        output_stream: 'IMAGE:transformed_image_in'
      }
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:transformed_image_in'
        output_stream: 'IMAGE:image_out'
      }
      Ngraph_with_side_packetsa$  
      input_stream: 'image_in'
      input_side_packet: 'allow_signal'
      input_side_packet: 'rotation_degrees'
      output_stream: 'image_out'
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:image_in'
        input_side_packet: 'ROTATION_DEGREES:rotation_degrees'
        output_stream: 'IMAGE:transformed_image_in'
      }
      node {
        calculator: 'GateCalculator'
        input_stream: 'transformed_image_in'
        input_side_packet: 'ALLOW:allow_signal'
        output_stream: 'image_out_to_transform'
      }
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:image_out_to_transform'
        input_side_packet: 'ROTATION_DEGREES:rotation_degrees'
        output_stream: 'IMAGE:image_out'
      }Tr   )allow_signalrotation_degreesc                 |    |                      t          j        |t          j                              |           d S )N)r   side_inputs)_process_and_verifyr   r   r   r   )r   r   rJ   s      r   test_solution_processz&SolutionBaseTest.test_solution_process   sO    P 	 &{'5'K'M'MO O  ! ! ! ! !    c                     d}t          j        |t          j                              }|                     t
          d          5  t          j        |ddi           d d d            d S # 1 swxY w Y   d S )Nar  
      input_stream: 'image_in'
      output_stream: 'image_out'
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:image_in'
        output_stream: 'IMAGE:transformed_image_in'
      }
      node {
        name: 'SignalGate'
        calculator: 'GateCalculator'
        input_stream: 'transformed_image_in'
        input_side_packet: 'ALLOW:allow_signal'
        output_stream: 'image_out_to_transform'
      }
      node {
        calculator: 'ImageTransformationCalculator'
        input_stream: 'IMAGE:image_out_to_transform'
        output_stream: 'IMAGE:image_out'
      }
    z@Modifying the calculator options of SignalGate is not supported.zSignalGate.invalid_fieldzI am invalidr   calculator_params)r   r   r   r   r   r   r   r   )r   r   r   s      r   test_invalid_calculator_optionsz0SolutionBaseTest.test_invalid_calculator_options   s    K* $[%3%I%K%KM ML			J
L 
L J J  #7HJ J J JJ J J J J J J J J J J J J J J J J Js   A**A.1A.c                     t          j        t          t          j                              }|                     t          d          5  t          j        |ddd           d d d            d S # 1 swxY w Y   d S )Nz)has both options and node_options fields.r   z ImageTransformation.output_widthz!ImageTransformation.output_heightrO   )	r   r   $CALCULATOR_OPTIONS_TEST_GRAPH_CONFIGr   r   r   r   r   r   r   r   s     r   1test_calculator_has_both_options_and_node_optionszBSolutionBaseTest.test_calculator_has_both_options_and_node_options   s    $%I%3%I%K%KM ML			
 K
M 
M   #2334                     s   A..A25A2c                    t          j        t          t          j                              }|                     d|j        d         j                   |j        d                             d           | 	                    |ddd           d S )NImageTransformationr   node_optionsrS   r   rP   
r   r   rT   r   r   r/   nodename
ClearFieldrK   rU   s     r   (test_modifying_calculator_proto2_optionsz9SolutionBaseTest.test_modifying_calculator_proto2_options  s    $%I%3%I%K%KM ML 	*L,=a,@,EFFFa##N333!0112
 
      rM   c                    t          j        t          t          j                              }|                     d|j        d         j                   |j        d                             d           | 	                    |ddd           d S )NrX   r   optionsrS   rZ   r[   rU   s     r   -test_modifying_calculator_proto3_node_optionsz>SolutionBaseTest.test_modifying_calculator_proto3_node_options  s    $%I%3%I%K%KM ML 	*L,=a,@,EFFFa##I...!0112
 
      rM   c                 \   t          j        t          t          j                              }|                     d|j        d         j                   |j        d                             d           |j        d                             d           | 	                    |ddd           d S )NrX   r   ra   rY   rS   rZ   r[   rU   s     r   test_adding_calculator_optionsz/SolutionBaseTest.test_adding_calculator_options  s    $%I%3%I%K%KM ML 	*L,=a,@,EFFFa##I...a##N333!0112
 
      rM   c                    t          j        |t          j                              }t	          j        dt          j                                      ddd          }t          j	        ||          5 }t          d          D ]X}|                    |          }|                     t	          j        ||j                             |                                 Y	 d d d            d S # 1 swxY w Y   d S )N   r<   r>   )r   rJ      )r   r   r   r   r@   rA   rB   rC   r   r   ranger*   r+   array_equal	image_outreset)r   r   rJ   r   input_imager0   _outputss           r   test_solution_resetz$SolutionBaseTest.test_solution_reset.  s/   P $[%3%I%K%KM ML)Bbh///771a@@K		#!{
< 
< 
< ?GRyy  !"";//{G4EFFGGG                 s   2A)C))C-0C-c                    d}t          j        |t          j                              }t	          j        dt          j                                      ddd          }t          j	        |dt          j        i          5 }t          d          D ]D}|                    |          }|                     t	          j        ||j                             E	 d d d            n# 1 swxY w Y   t          j	        |dt          j        i          5 }t          d          D ]D}|                    |          }|                     t	          j        ||j                             E	 d d d            d S # 1 swxY w Y   d S )Nz
      input_stream: 'union_type_image_in'
      output_stream: 'image_type_out'
      node {
        calculator: 'ToImageCalculator'
        input_stream: 'IMAGE:union_type_image_in'
        output_stream: 'IMAGE:image_type_out'
      }
    rf   r<   r>   union_type_image_in)r   stream_type_hintsrg   )r   r   r   r   r@   rA   rB   rC   r   r   r	   IMAGErh   r*   r+   ri   image_type_outIMAGE_FRAME)r   r   r   rl   r0   rm   rn   	solution2s           r   test_solution_stream_type_hintsz0SolutionBaseTest.test_solution_stream_type_hints`  s+   K $[%3%I%K%KM ML)Bbh///771a@@K		#!0.2F 
 
 
 M !)Ryy M M!"";//{G4JKKLLLLM	M M M M M M M M M M M M M M M 
	#!0.2L 
 
 
 M !*Ryy M M!##K00{G4JKKLLLLM	M M M M M M M M M M M M M M M M M Ms&    AC""C&)C&AE22E69E6Nc                    t          j        dt           j                                      ddd          }t	          j        |||          5 }|                    |          }|                    d|i          }d d d            n# 1 swxY w Y   |                     t          j        ||j	                             |                     t          j        ||j	                             d S )Nrf   r<   r>   )r   rJ   rP   image_in)
r@   rA   rB   rC   r   r   r*   r+   ri   rj   )r   r   rJ   rP   rl   r0   rn   outputs2s           r   rK   z$SolutionBaseTest._process_and_verify|  s    )Bbh///771a@@K		#!+
- 
- 
- = 19  --g!!:{";<<h= = = = = = = = = = = = = = = 	OOBN;0ABBCCCOOBN;0BCCDDDDDs   -BB
B
)NN)__name__
__module____qualname__r   r   named_parametersRuntimeErrorr   r    r6   r9   rD   rL   rQ   rV   r_   rb   rd   ro   rw   rK    rM   r   r   r   3   s       5 5 5 "=!#A D
 G#I $< >
 <#> $H J
 E#GH H0< <1H H0<- - -<: : :,I I I, "=! # - /.  -	&
 &
N! !O&
 &
N!J J J>
 
 
       "=! # - /.  -	&
 &
N	 	O&
 &
N	M M M< '+,0E E E E E ErM   r   __main__)__doc__absl.testingr   r   numpyr@   google.protobufr   mediapipe.frameworkr   mediapipe.framework.formatsr   mediapipe.pythonr   mediapipe.python.solution_baser	   rT   TestCaser   r{   mainr   rM   r   <module>r      s   0 / ! ! ! ! ! ! & & & & & &     ' ' ' ' ' ' . . . . . . 5 5 5 5 5 5 * * * * * * 9 9 9 9 9 9( $0UE UE UE UE UE}- UE UE UEp
 z(-///// rM   