






import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY
@is_pipeline_test
class  lowerCAmelCase_       (				unittest.TestCase					):
							UpperCamelCase_		:Any		      =    MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
							UpperCamelCase_		:Optional[int]		      =    TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




							def 						__snake_case					(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Any	):
											lowerCAmelCase__      			=						AudioClassificationPipeline(model=__SCREAMING_SNAKE_CASE ,							feature_extractor=__SCREAMING_SNAKE_CASE	)

											# test with a raw waveform
											lowerCAmelCase__      			=						np.zeros((34_000,)	)
											lowerCAmelCase__      			=						np.zeros((14_000,)	)
											return audio_classifier, [audioa, audio]




							def 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any]	):
											lowerCAmelCase__			,		lowerCAmelCase__      			=						examples
											lowerCAmelCase__      			=						audio_classifier(__SCREAMING_SNAKE_CASE	)
											# by default a model is initialized with num_labels=2
											self.assertEqual(
											    __SCREAMING_SNAKE_CASE ,							[
											        {'''score''': ANY(__SCREAMING_SNAKE_CASE	), '''label''': ANY(__SCREAMING_SNAKE_CASE	)},
											        {'''score''': ANY(__SCREAMING_SNAKE_CASE	), '''label''': ANY(__SCREAMING_SNAKE_CASE	)},
											    ] ,							)
											lowerCAmelCase__      			=						audio_classifier(__SCREAMING_SNAKE_CASE ,							top_k=1	)
											self.assertEqual(
											    __SCREAMING_SNAKE_CASE ,							[
											        {'''score''': ANY(__SCREAMING_SNAKE_CASE	), '''label''': ANY(__SCREAMING_SNAKE_CASE	)},
											    ] ,							)

											self.run_torchaudio(__SCREAMING_SNAKE_CASE	)




							@require_torchaudio
							def 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any]	):
											import datasets

											# test with a local file
											lowerCAmelCase__      			=						datasets.load_dataset('''hf-internal-testing/librispeech_asr_dummy''' ,							'''clean''' ,							split='''validation'''	)
											lowerCAmelCase__      			=						dataset[0]['''audio''']['''array''']
											lowerCAmelCase__      			=						audio_classifier(__SCREAMING_SNAKE_CASE	)
											self.assertEqual(
											    __SCREAMING_SNAKE_CASE ,							[
											        {'''score''': ANY(__SCREAMING_SNAKE_CASE	), '''label''': ANY(__SCREAMING_SNAKE_CASE	)},
											        {'''score''': ANY(__SCREAMING_SNAKE_CASE	), '''label''': ANY(__SCREAMING_SNAKE_CASE	)},
											    ] ,							)




							@require_torch
							def 						__snake_case					(   self     :   List[str]	):
											lowerCAmelCase__      			=						'''anton-l/wav2vec2-random-tiny-classifier'''

											lowerCAmelCase__      			=						pipeline('''audio-classification''' ,							model=__SCREAMING_SNAKE_CASE	)

											lowerCAmelCase__      			=						np.ones((8_000,)	)
											lowerCAmelCase__      			=						audio_classifier(__SCREAMING_SNAKE_CASE ,							top_k=4	)

											lowerCAmelCase__      			=						[
											    {'''score''': 0.0_842, '''label''': '''no'''},
											    {'''score''': 0.0_838, '''label''': '''up'''},
											    {'''score''': 0.0_837, '''label''': '''go'''},
											    {'''score''': 0.0_834, '''label''': '''right'''},
											]
											lowerCAmelCase__      			=						[
											    {'''score''': 0.0_845, '''label''': '''stop'''},
											    {'''score''': 0.0_844, '''label''': '''on'''},
											    {'''score''': 0.0_841, '''label''': '''right'''},
											    {'''score''': 0.0_834, '''label''': '''left'''},
											]
											self.assertIn(nested_simplify(__SCREAMING_SNAKE_CASE ,							decimals=4	) ,							[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)

											lowerCAmelCase__      			=						{'''array''': np.ones((8_000,)	), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
											lowerCAmelCase__      			=						audio_classifier(__SCREAMING_SNAKE_CASE ,							top_k=4	)
											self.assertIn(nested_simplify(__SCREAMING_SNAKE_CASE ,							decimals=4	) ,							[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)




							@require_torch
							@slow
							def 						__snake_case					(   self     :   Optional[int]	):
											import datasets

											lowerCAmelCase__      			=						'''superb/wav2vec2-base-superb-ks'''

											lowerCAmelCase__      			=						pipeline('''audio-classification''' ,							model=__SCREAMING_SNAKE_CASE	)
											lowerCAmelCase__      			=						datasets.load_dataset('''anton-l/superb_dummy''' ,							'''ks''' ,							split='''test'''	)

											lowerCAmelCase__      			=						np.array(dataset[3]['''speech'''] ,							dtype=np.floataa	)
											lowerCAmelCase__      			=						audio_classifier(__SCREAMING_SNAKE_CASE ,							top_k=4	)
											self.assertEqual(
											    nested_simplify(__SCREAMING_SNAKE_CASE ,							decimals=3	) ,							[
											        {'''score''': 0.981, '''label''': '''go'''},
											        {'''score''': 0.007, '''label''': '''up'''},
											        {'''score''': 0.006, '''label''': '''_unknown_'''},
											        {'''score''': 0.001, '''label''': '''down'''},
											    ] ,							)




							@require_tf
							@unittest.skip('''Audio classification is not implemented for TF'''	)
							def 						__snake_case					(   self     :   List[Any]	):
											pass













import numpy as np
from cva import destroyAllWindows, imread, imshow, waitKey
class  lowerCAmelCase_       :




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

											lowerCAmelCase__      			=						img
											lowerCAmelCase__      			=						img.shape[1]
											lowerCAmelCase__      			=						img.shape[0]
											lowerCAmelCase__      			=						dst_width
											lowerCAmelCase__      			=						dst_height

											lowerCAmelCase__      			=						self.src_w / self.dst_w
											lowerCAmelCase__      			=						self.src_h / self.dst_h

											lowerCAmelCase__      			=						lowerCAmelCase__      			=						(
											    np.ones((self.dst_h, self.dst_w, 3) ,							np.uinta	) * 255
											)




							def 						__snake_case					(   self     :   List[str]	):
											for i in range(self.dst_h	):
															for j in range(self.dst_w	):
																			lowerCAmelCase__      			=						self.img[self.get_y(SCREAMING_SNAKE_CASE_	)][self.get_x(SCREAMING_SNAKE_CASE_	)]




							def 						__snake_case					(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   int	):
											return int(self.ratio_x * x	)




							def 						__snake_case					(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   int	):
											return int(self.ratio_y * y	)


if __name__ == "__main__":
			_UpperCAmelCase				,   _UpperCAmelCase			:		List[str]   				=    800, 600
			_UpperCAmelCase			:		Tuple   				=    imread("image_data/lena.jpg", 1)
			_UpperCAmelCase			:		str   				=    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()





