
    uki=                         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(                          yy)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y) SolutionBaseTestc                 *   | j                  t        d      5  t        j                          d d d        | j                  t        d      5  t        j                  t	        j
                         d       d d d        y # 1 sw Y   RxY w# 1 sw Y   y xY w)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    ^/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/mediapipe/python/solution_base_test.py%test_invalid_initialization_argumentsz6SolutionBaseTest.test_invalid_initialization_arguments5   s    			P
 #   "	#
 
		P
 5   %;;=35	5 5# #
5 5s   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                               }| j	                  ||      5  t        j                  |       d d d        y # 1 sw Y   y xY w)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   sV    2 $$[%3%I%I%KML			
M	: <  l;< < <s   AA$c                    d}t        j                  |t        j                               }t	        j
                  |      5 }t        j                         }|j                  j                         }t        j                  d|       |j                  j                         }t        j                  d|       |j                  d|i      }| j                  t        |d             | j                  |j                  j                  d       t        j                         }t        j                  d|       t        j                         }	t        j                  d	|	       | j!                  |j                  j                  d
   |       | j!                  |j                  j                  d   |	       d d d        y # 1 sw Y   y xY w)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   su   K $$[%3%I%I%KML		#	#	> -(&446$..224kk2$..224kk2  "46F!GHg
oogg':;<
nnW..88!<*44657KL*44657KL
w00::1=+-
w00::1=+-- - -s   E/F99Gc                 ~   d}t        j                  |t        j                               }t	        j
                  |      5 }t        j                         }t        j                  d|       | j                  t        d      5  |j                  d|i       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)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   s    K $$[%3%I%I%KML		#	#	> :())+ii0!!
0 :
 	,i89:: :: :: :s%   AB3B'B3'B0	,B33B<c           	         d}t        j                  |t        j                               }t	        j
                  |      5 }| j                  t        d      5  |j                  t        j                  dt        j                        j                  ddd             d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)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   s    K $$[%3%I%I%KML		#	#	> I(!!
HJ I2RXX6>>q!QGHII II II Is%   B;AB/B;/B8	4B;;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                 v    | j                  t        j                  |t        j                               |       y )N)r   side_inputs)_process_and_verifyr   r   r   r   )r   r   rJ   s      r   test_solution_processz&SolutionBaseTest.test_solution_process   s8    P 	 &&{'5'K'K'MO  !    c                     d}t        j                  |t        j                               }| j	                  t
        d      5  t        j                  |ddi       d d d        y # 1 sw Y   y xY w)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   st    K* $$[%3%I%I%KML			J
L J   #7HJJ J Js   A$$A-c                     t        j                  t        t        j                               }| j                  t        d      5  t        j                  |ddd       d d d        y # 1 sw Y   y xY w)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   sg    $$%I%3%I%I%KML			
 K
M   #2334  s   A''A0c                    t        j                  t        t        j                               }| j                  d|j                  d   j                         |j                  d   j                  d       | j                  |ddd       y )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%I%KML 	*L,=,=a,@,E,EFa##N3!0112
  rM   c                    t        j                  t        t        j                               }| j                  d|j                  d   j                         |j                  d   j                  d       | j                  |ddd       y )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%I%KML 	*L,=,=a,@,E,EFa##I.!0112
  rM   c                 R   t        j                  t        t        j                               }| j                  d|j                  d   j                         |j                  d   j                  d       |j                  d   j                  d       | j                  |ddd       y )NrX   r   ra   rY   rS   rZ   r[   rU   s     r   test_adding_calculator_optionsz/SolutionBaseTest.test_adding_calculator_options  s    $$%I%3%I%I%KML 	*L,=,=a,@,E,EFa##I.a##N3!0112
  rM   c                    t        j                  |t        j                               }t	        j
                  dt        j                        j                  ddd      }t        j                  ||      5 }t        d      D ]R  }|j                  |      }| j                  t	        j                  ||j                               |j                          T 	 d d d        y # 1 sw Y   y xY w)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%I%KML))Bbhh/771a@K		#	#!{
< ?GRy !"";/{G4E4EFG  s   6A!C!!C*c                    d}t        j                  |t        j                               }t	        j
                  dt        j                        j                  ddd      }t        j                  |dt        j                  i      5 }t        d      D ]B  }|j                  |      }| j                  t	        j                  ||j                                D 	 d d d        t        j                  |dt        j"                  i      5 }t        d      D ]B  }|j                  |      }| j                  t	        j                  ||j                                D 	 d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)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`  sW   K $$[%3%I%I%KML))Bbhh/771a@K		#	#!0.2F2F 
 M !)Ry M!"";/{G4J4JKLM	M 
	#	#!0.2L2L 
 M !*Ry M!##K0{G4J4JKLM	M MM MM Ms   AE$	AE0$E-0E9Nc                    t        j                  dt         j                        j                  ddd      }t	        j
                  |||      5 }|j                  |      }|j                  d|i      }d d d        | j                  t        j                  |j                               | j                  t        j                  |j                               y # 1 sw Y   hxY w)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     ))Bbhh/771a@K		#	#!+
- = 19  -g!!:{";<h= 	OOBNN;0A0ABCOOBNN;0B0BCD= =s   %CC$)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 "=!!#A D
 G#I $< >
 <#> $H J
 E#GH0<1H0<-<:,I, "=!! # - /. -	&
N!O&
N!J>
 "=!! # - /. -	&
N	O&
N	M< '+,0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 * 9( $0UE}-- UEp
 z(--/ rM   