






"""simple docstring"""


import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image
else:




		class 						__UpperCamelCase     :
							@staticmethod
							def        UpperCAmelCase__ (							*UpperCAmelCase							:	Dict     , **UpperCAmelCase							:	Union[str, Any]   )	->       Optional[Any]:
													pass




@is_pipeline_test
@require_torch
@require_vision
class 						__UpperCamelCase     (     unittest.TestCase						):
					lowercase_    :				Tuple      =  MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
					def        UpperCAmelCase__ (							self							:	List[Any]     , UpperCAmelCase							:	int     , UpperCAmelCase							:	Optional[int]     , UpperCAmelCase							:	Optional[Any]   )	->       Any:
											lowerCAmelCase   :int						=			pipeline('visual-question-answering'     , model='hf-internal-testing/tiny-vilt-random-vqa'   )
											lowerCAmelCase   :List[Any]						=			[
											    {
											        'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'   ),
											        'question': 'How many cats are there?',
											    },
											    {
											        'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
											        'question': 'How many cats are there?',
											    },
											]
											return vqa_pipeline, examples
					def        UpperCAmelCase__ (							self							:	Optional[Any]     , UpperCAmelCase							:	str     , UpperCAmelCase							:	List[Any]   )	->       List[Any]:
											lowerCAmelCase   :Tuple						=			vqa_pipeline(UpperCAmelCase     , top_k=1   )
											self.assertEqual(
											    UpperCAmelCase     , [
											        [{'score': ANY(UpperCAmelCase   ), 'answer': ANY(UpperCAmelCase   )}],
											        [{'score': ANY(UpperCAmelCase   ), 'answer': ANY(UpperCAmelCase   )}],
											    ]     , )
					@require_torch
					def        UpperCAmelCase__ (							self							:	Union[str, Any]   )	->       int:
											lowerCAmelCase   :Tuple						=			pipeline('visual-question-answering'     , model='hf-internal-testing/tiny-vilt-random-vqa'   )
											lowerCAmelCase   :str						=			'./tests/fixtures/tests_samples/COCO/000000039769.png'
											lowerCAmelCase   :Optional[int]						=			'How many cats are there?'

											lowerCAmelCase   :str						=			vqa_pipeline(image=UpperCAmelCase     , question='How many cats are there?'     , top_k=2   )
											self.assertEqual(
											    UpperCAmelCase     , [{'score': ANY(UpperCAmelCase   ), 'answer': ANY(UpperCAmelCase   )}, {'score': ANY(UpperCAmelCase   ), 'answer': ANY(UpperCAmelCase   )}]   )

											lowerCAmelCase   :Tuple						=			vqa_pipeline({'image': image, 'question': question}     , top_k=2   )
											self.assertEqual(
											    UpperCAmelCase     , [{'score': ANY(UpperCAmelCase   ), 'answer': ANY(UpperCAmelCase   )}, {'score': ANY(UpperCAmelCase   ), 'answer': ANY(UpperCAmelCase   )}]   )
					@slow
					@require_torch
					def        UpperCAmelCase__ (							self							:	Optional[Any]   )	->       List[str]:
											lowerCAmelCase   :List[Any]						=			pipeline('visual-question-answering'     , model='dandelin/vilt-b32-finetuned-vqa'   )
											lowerCAmelCase   :List[Any]						=			'./tests/fixtures/tests_samples/COCO/000000039769.png'
											lowerCAmelCase   :List[str]						=			'How many cats are there?'

											lowerCAmelCase   :Union[str, Any]						=			vqa_pipeline(image=UpperCAmelCase     , question=UpperCAmelCase     , top_k=2   )
											self.assertEqual(
											    nested_simplify(UpperCAmelCase     , decimals=4   )     , [{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]   )

											lowerCAmelCase   :Dict						=			vqa_pipeline({'image': image, 'question': question}     , top_k=2   )
											self.assertEqual(
											    nested_simplify(UpperCAmelCase     , decimals=4   )     , [{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]   )

											lowerCAmelCase   :List[Any]						=			vqa_pipeline(
											    [{'image': image, 'question': question}, {'image': image, 'question': question}]     , top_k=2   )
											self.assertEqual(
											    nested_simplify(UpperCAmelCase     , decimals=4   )     , [[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]] * 2     , )
					@require_tf
					@unittest.skip('Visual question answering not implemented in TF'   )
					def        UpperCAmelCase__ (							self							:	Dict   )	->       List[str]:
											pass







"""simple docstring"""


import numpy as np
from cva import destroyAllWindows, imread, imshow, waitKey




class 						__UpperCamelCase     :
					def __init__(							self							:	Union[str, Any]     , UpperCAmelCase							:	Union[str, Any]     , UpperCAmelCase							:	int     , UpperCAmelCase							:	int   )	->       Tuple:
											if dst_width < 0 or dst_height < 0:
																	raise ValueError('Destination width/height should be > 0'   )

											lowerCAmelCase   :List[Any]						=			img
											lowerCAmelCase   :Optional[int]						=			img.shape[1]
											lowerCAmelCase   :Union[str, Any]						=			img.shape[0]
											lowerCAmelCase   :Optional[int]						=			dst_width
											lowerCAmelCase   :Optional[int]						=			dst_height

											lowerCAmelCase   :Any						=			self.src_w / self.dst_w
											lowerCAmelCase   :Dict						=			self.src_h / self.dst_h

											lowerCAmelCase   :int						=			(
											    np.ones((self.dst_h, self.dst_w, 3)     , np.uinta   ) * 255
											)
					def        UpperCAmelCase__ (							self							:	Union[str, Any]   )	->       List[str]:
											for i in range(self.dst_h   ):
																	for j in range(self.dst_w   ):
																							lowerCAmelCase   :Optional[Any]						=			self.img[self.get_y(UpperCAmelCase   )][self.get_x(UpperCAmelCase   )]
					def        UpperCAmelCase__ (							self							:	Optional[int]     , UpperCAmelCase							:	int   )	->       int:
											return int(self.ratio_x * x   )
					def        UpperCAmelCase__ (							self							:	List[str]     , UpperCAmelCase							:	int   )	->       int:
											return int(self.ratio_y * y   )


if __name__ == "__main__":
		__SCREAMING_SNAKE_CASE,						__SCREAMING_SNAKE_CASE   =   8_00, 6_00
		__SCREAMING_SNAKE_CASE   =   imread('image_data/lena.jpg', 1)
		__SCREAMING_SNAKE_CASE   =   NearestNeighbour(im, dst_w, dst_h)
		n.process()

		imshow(
		    F"""Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}""", n.output
		)
		waitKey(0)
		destroyAllWindows()