



"""simple docstring"""






import unittest

from transformers import 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 			__snake_case			:




     @staticmethod
     def 		SCREAMING_SNAKE_CASE							(					*__lowerCAmelCase	:		List[str]		,   **__lowerCAmelCase	:		str						):






            """simple docstring"""






            pass






@is_pipeline_test
@require_vision
class 			__snake_case			(      unittest.TestCase):




  @require_torch
  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[int]						):






         """simple docstring"""






         _lowerCamelCase		:							int            =					pipeline(
             model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''		,   )
         _lowerCamelCase		:							List[str]            =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
         _lowerCamelCase		:							List[Any]            =					image_classifier(__lowerCAmelCase		,   candidate_labels=['''a''', '''b''', '''c''']						)

         # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
         # python and torch versions.
         self.assertIn(
             nested_simplify(__lowerCAmelCase						)		,   [
                 [{'''score''': 0.3_33, '''label''': '''a'''}, {'''score''': 0.3_33, '''label''': '''b'''}, {'''score''': 0.3_33, '''label''': '''c'''}],
                 [{'''score''': 0.3_33, '''label''': '''a'''}, {'''score''': 0.3_33, '''label''': '''c'''}, {'''score''': 0.3_33, '''label''': '''b'''}],
             ]		,   )

         _lowerCamelCase		:							Optional[int]            =					image_classifier([image] * 5		,   candidate_labels=['''A''', '''B''', '''C''']		,   batch_size=2						)
         self.assertEqual(
             nested_simplify(__lowerCAmelCase						)		,   [
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
             ]		,   )




  @require_tf
  def 		SCREAMING_SNAKE_CASE							(					self	:		str						):






         """simple docstring"""






         _lowerCamelCase		:							Optional[int]            =					pipeline(
             model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''		,   framework='''tf'''						)
         _lowerCamelCase		:							Tuple            =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
         _lowerCamelCase		:							List[str]            =					image_classifier(__lowerCAmelCase		,   candidate_labels=['''a''', '''b''', '''c''']						)

         self.assertEqual(
             nested_simplify(__lowerCAmelCase						)		,   [{'''score''': 0.3_33, '''label''': '''a'''}, {'''score''': 0.3_33, '''label''': '''b'''}, {'''score''': 0.3_33, '''label''': '''c'''}]		,   )

         _lowerCamelCase		:							str            =					image_classifier([image] * 5		,   candidate_labels=['''A''', '''B''', '''C''']		,   batch_size=2						)
         self.assertEqual(
             nested_simplify(__lowerCAmelCase						)		,   [
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
                 [
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                     {'''score''': 0.3_33, '''label''': ANY(__lowerCAmelCase						)},
                 ],
             ]		,   )




  @slow
  @require_torch
  def 		SCREAMING_SNAKE_CASE							(					self	:		Union[str, Any]						):






         """simple docstring"""






         _lowerCamelCase		:							Optional[Any]            =					pipeline(
             task='''zero-shot-image-classification'''		,   model='''openai/clip-vit-base-patch32'''		,   )
         # This is an image of 2 cats with remotes and no planes
         _lowerCamelCase		:							List[Any]            =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
         _lowerCamelCase		:							Union[str, Any]            =					image_classifier(__lowerCAmelCase		,   candidate_labels=['''cat''', '''plane''', '''remote''']						)

         self.assertEqual(
             nested_simplify(__lowerCAmelCase						)		,   [
                 {'''score''': 0.5_11, '''label''': '''remote'''},
                 {'''score''': 0.4_85, '''label''': '''cat'''},
                 {'''score''': 0.0_04, '''label''': '''plane'''},
             ]		,   )

         _lowerCamelCase		:							Tuple            =					image_classifier([image] * 5		,   candidate_labels=['''cat''', '''plane''', '''remote''']		,   batch_size=2						)
         self.assertEqual(
             nested_simplify(__lowerCAmelCase						)		,   [
                 [
                     {'''score''': 0.5_11, '''label''': '''remote'''},
                     {'''score''': 0.4_85, '''label''': '''cat'''},
                     {'''score''': 0.0_04, '''label''': '''plane'''},
                 ],
             ]
             * 5		,   )




  @slow
  @require_tf
  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					pipeline(
             task='''zero-shot-image-classification'''		,   model='''openai/clip-vit-base-patch32'''		,   framework='''tf'''						)
         # This is an image of 2 cats with remotes and no planes
         _lowerCamelCase		:							List[str]            =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
         _lowerCamelCase		:							Dict            =					image_classifier(__lowerCAmelCase		,   candidate_labels=['''cat''', '''plane''', '''remote''']						)
         self.assertEqual(
             nested_simplify(__lowerCAmelCase						)		,   [
                 {'''score''': 0.5_11, '''label''': '''remote'''},
                 {'''score''': 0.4_85, '''label''': '''cat'''},
                 {'''score''': 0.0_04, '''label''': '''plane'''},
             ]		,   )

         _lowerCamelCase		:							List[str]            =					image_classifier([image] * 5		,   candidate_labels=['''cat''', '''plane''', '''remote''']		,   batch_size=2						)
         self.assertEqual(
             nested_simplify(__lowerCAmelCase						)		,   [
                 [
                     {'''score''': 0.5_11, '''label''': '''remote'''},
                     {'''score''': 0.4_85, '''label''': '''cat'''},
                     {'''score''': 0.0_04, '''label''': '''plane'''},
                 ],
             ]
             * 5		,   )








'''simple docstring'''







import numpy as np
from cva import COLOR_BGR2GRAY, cvtColor, imread
from numpy import array, uinta
from PIL import Image

from digital_image_processing import change_contrast as cc
from digital_image_processing import convert_to_negative as cn
from digital_image_processing import sepia as sp
from digital_image_processing.dithering import burkes as bs
from digital_image_processing.edge_detection import canny
from digital_image_processing.filters import convolve as conv
from digital_image_processing.filters import gaussian_filter as gg
from digital_image_processing.filters import local_binary_pattern as lbp
from digital_image_processing.filters import median_filter as med
from digital_image_processing.filters import sobel_filter as sob
from digital_image_processing.resize import resize as rs

lowercase      :   Any      =       imread(R"""digital_image_processing/image_data/lena_small.jpg""")
lowercase      :   List[str]      =       cvtColor(img, COLOR_BGR2GRAY)



def        lowerCamelCase__				(      ):
     snake_case			:      Optional[int]        =			cn.convert_to_negative(__lowercase				)
     # assert negative_img array for at least one True
     assert negative_img.any()



def        lowerCamelCase__				(      ):
     with Image.open("""digital_image_processing/image_data/lena_small.jpg"""				) as img:
          # Work around assertion for response
          assert str(cc.change_contrast(__lowercase     ,   110				)				).startswith(
              """<PIL.Image.Image image mode=RGB size=100x100 at"""				)



def        lowerCamelCase__				(      ):
     snake_case			:      Union[str, Any]        =			canny.gen_gaussian_kernel(9     ,   sigma=1.4				)
     # Assert ambiguous array
     assert resp.all()



def        lowerCamelCase__				(      ):
     snake_case			:      Optional[Any]        =			imread("""digital_image_processing/image_data/lena_small.jpg"""     ,   0				)
     # assert ambiguous array for all == True
     assert canny_img.all()
     snake_case			:      Any        =			canny.canny(__lowercase				)
     # assert canny array for at least one True
     assert canny_array.any()



def        lowerCamelCase__				(      ):
     assert gg.gaussian_filter(__lowercase     ,   5     ,   sigma=0.9				).all()



def        lowerCamelCase__				(      ):
     # laplace diagonals
     snake_case			:      int        =			array([[0.25, 0.5, 0.25], [0.5, -3, 0.5], [0.25, 0.5, 0.25]]				)
     snake_case			:      Tuple        =			conv.img_convolve(__lowercase     ,   __lowercase				).astype(__lowercase				)
     assert res.any()



def        lowerCamelCase__				(      ):
     assert med.median_filter(__lowercase     ,   3				).any()



def        lowerCamelCase__				(      ):
     snake_case ,		snake_case			:      Dict        =			sob.sobel_filter(__lowercase				)
     assert grad.any() and theta.any()



def        lowerCamelCase__				(      ):
     snake_case			:      int        =			sp.make_sepia(__lowercase     ,   20				)
     assert sepia.all()



def        lowerCamelCase__				(      __lowercase = "digital_image_processing/image_data/lena_small.jpg"				):
     snake_case			:      int        =			bs.Burkes(imread(__lowercase     ,   1				)     ,   120				)
     burkes.process()
     assert burkes.output_img.any()



def        lowerCamelCase__				(      __lowercase = "digital_image_processing/image_data/lena_small.jpg"     ,   ):
     snake_case			:      Dict        =			rs.NearestNeighbour(imread(__lowercase     ,   1				)     ,   400     ,   200				)
     nn.process()
     assert nn.output.any()







def        lowerCamelCase__				(      ):
     snake_case			:      int        =			"""digital_image_processing/image_data/lena.jpg"""

     # Reading the image and converting it to grayscale.
     snake_case			:      Tuple        =			imread(__lowercase     ,   0				)

     # Test for get_neighbors_pixel function() return not None
     snake_case			:      Optional[int]        =			0
     snake_case			:      Tuple        =			0
     snake_case			:      Any        =			image[x_coordinate][y_coordinate]

     snake_case			:      Any        =			lbp.get_neighbors_pixel(
         __lowercase     ,   __lowercase     ,   __lowercase     ,   __lowercase				)

     assert neighbors_pixels is not None

     # Test for local_binary_pattern function()
     # Create a numpy array as the same height and width of read image
     snake_case			:      List[Any]        =			np.zeros((image.shape[0], image.shape[1])				)

     # Iterating through the image and calculating the local binary pattern value
     # for each pixel.
     for i in range(0     ,   image.shape[0]				):
          for j in range(0     ,   image.shape[1]				):
               snake_case			:      Optional[Any]        =			lbp.local_binary_value(__lowercase     ,   __lowercase     ,   __lowercase				)

     assert lbp_image.any()



