



"""simple docstring"""
import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, 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				:





       '''simple docstring'''



       @staticmethod
       def 					__lowerCAmelCase      (   *A			,      **A						)				->   List[Any]:
           pass
@is_pipeline_test
@require_vision
@require_torch
class 				_UpperCAmelCase				(					unittest.TestCase ):





     '''simple docstring'''



     a__   							=MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING





     def 					__lowerCAmelCase      (   self			,      A			,      A			,      A						)				->   Dict:
         _UpperCAmelCase				:    Union[str, Any]											=       pipeline(
             '''zero-shot-object-detection'''			,      model='''hf-internal-testing/tiny-random-owlvit-object-detection'''						)

         _UpperCAmelCase				:    int											=       [
             {
                 '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
                 '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
             }
         ]
         return object_detector, examples





     def 					__lowerCAmelCase      (   self			,      A			,      A						)				->   List[Any]:
         _UpperCAmelCase				:    int											=       object_detector(examples[0]			,      threshold=0.0						)

         _UpperCAmelCase				:    List[Any]											=       len(A_						)
         self.assertGreater(A_			,      0						)
         self.assertEqual(
             A_			,      [
                 {
                     '''score''': ANY(A_						),
                     '''label''': ANY(A_						),
                     '''box''': {'''xmin''': ANY(A_						), '''ymin''': ANY(A_						), '''xmax''': ANY(A_						), '''ymax''': ANY(A_						)},
                 }
                 for i in range(A_						)
             ]			,      )





     @require_tf
     @unittest.skip('''Zero Shot Object Detection not implemented in TF'''						)
     def 					__lowerCAmelCase      (   self						)				->   Dict:
         pass





     @require_torch
     def 					__lowerCAmelCase      (   self						)				->   Union[str, Any]:
         _UpperCAmelCase				:    Tuple											=       pipeline(
             '''zero-shot-object-detection'''			,      model='''hf-internal-testing/tiny-random-owlvit-object-detection'''						)

         _UpperCAmelCase				:    Tuple											=       object_detector(
             '''./tests/fixtures/tests_samples/COCO/000000039769.png'''			,      candidate_labels=['''cat''', '''remote''', '''couch''']			,      threshold=0.64			,      )

         self.assertEqual(
             nested_simplify(A_			,      decimals=4						)			,      [
                 {'''score''': 0.7_235, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
                 {'''score''': 0.7_218, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
                 {'''score''': 0.7_184, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
                 {'''score''': 0.6_748, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
                 {'''score''': 0.6_656, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
                 {'''score''': 0.6_614, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
                 {'''score''': 0.6_456, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
                 {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
                 {'''score''': 0.6_419, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
             ]			,      )

         _UpperCAmelCase				:    int											=       object_detector(
             [
                 {
                     '''image''': '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
                     '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
                 }
             ]			,      threshold=0.64			,      )

         self.assertEqual(
             nested_simplify(A_			,      decimals=4						)			,      [
                 [
                     {'''score''': 0.7_235, '''label''': '''cat''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
                     {'''score''': 0.7_218, '''label''': '''remote''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
                     {'''score''': 0.7_184, '''label''': '''couch''', '''box''': {'''xmin''': 2_0_4, '''ymin''': 1_6_7, '''xmax''': 2_3_2, '''ymax''': 1_9_0}},
                     {'''score''': 0.6_748, '''label''': '''remote''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
                     {'''score''': 0.6_656, '''label''': '''cat''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
                     {'''score''': 0.6_614, '''label''': '''couch''', '''box''': {'''xmin''': 5_7_1, '''ymin''': 8_3, '''xmax''': 5_9_8, '''ymax''': 1_0_3}},
                     {'''score''': 0.6_456, '''label''': '''remote''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
                     {'''score''': 0.642, '''label''': '''remote''', '''box''': {'''xmin''': 6_7, '''ymin''': 2_7_4, '''xmax''': 9_3, '''ymax''': 2_9_7}},
                     {'''score''': 0.6_419, '''label''': '''cat''', '''box''': {'''xmin''': 4_9_4, '''ymin''': 1_0_5, '''xmax''': 5_2_1, '''ymax''': 1_2_7}},
                 ]
             ]			,      )





     @require_torch
     @slow
     def 					__lowerCAmelCase      (   self						)				->   Optional[int]:
         _UpperCAmelCase				:    Tuple											=       pipeline('''zero-shot-object-detection'''						)

         _UpperCAmelCase				:    Tuple											=       object_detector(
             '''http://images.cocodataset.org/val2017/000000039769.jpg'''			,      candidate_labels=['''cat''', '''remote''', '''couch''']			,      )
         self.assertEqual(
             nested_simplify(A_			,      decimals=4						)			,      [
                 {'''score''': 0.2_868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
                 {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
                 {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
                 {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
                 {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
             ]			,      )

         _UpperCAmelCase				:    List[Any]											=       object_detector(
             [
                 {
                     '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                     '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
                 },
                 {
                     '''image''': '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                     '''candidate_labels''': ['''cat''', '''remote''', '''couch'''],
                 },
             ]			,      )
         self.assertEqual(
             nested_simplify(A_			,      decimals=4						)			,      [
                 [
                     {'''score''': 0.2_868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
                     {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
                     {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
                     {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
                     {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
                 ],
                 [
                     {'''score''': 0.2_868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
                     {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
                     {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
                     {'''score''': 0.1_474, '''label''': '''remote''', '''box''': {'''xmin''': 3_3_5, '''ymin''': 7_4, '''xmax''': 3_7_1, '''ymax''': 1_8_7}},
                     {'''score''': 0.1_208, '''label''': '''couch''', '''box''': {'''xmin''': 4, '''ymin''': 0, '''xmax''': 6_4_2, '''ymax''': 4_7_6}},
                 ],
             ]			,      )





     @require_tf
     @unittest.skip('''Zero Shot Object Detection not implemented in TF'''						)
     def 					__lowerCAmelCase      (   self						)				->   List[str]:
         pass





     @require_torch
     @slow
     def 					__lowerCAmelCase      (   self						)				->   List[Any]:
         _UpperCAmelCase				:    Tuple											=       0.2
         _UpperCAmelCase				:    Tuple											=       pipeline('''zero-shot-object-detection'''						)

         _UpperCAmelCase				:    Any											=       object_detector(
             '''http://images.cocodataset.org/val2017/000000039769.jpg'''			,      candidate_labels=['''cat''', '''remote''', '''couch''']			,      threshold=A_			,      )
         self.assertEqual(
             nested_simplify(A_			,      decimals=4						)			,      [
                 {'''score''': 0.2_868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
                 {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
                 {'''score''': 0.2_537, '''label''': '''cat''', '''box''': {'''xmin''': 1, '''ymin''': 5_5, '''xmax''': 3_1_5, '''ymax''': 4_7_2}},
             ]			,      )





     @require_torch
     @slow
     def 					__lowerCAmelCase      (   self						)				->   Dict:
         _UpperCAmelCase				:    Optional[int]											=       2
         _UpperCAmelCase				:    str											=       pipeline('''zero-shot-object-detection'''						)

         _UpperCAmelCase				:    List[str]											=       object_detector(
             '''http://images.cocodataset.org/val2017/000000039769.jpg'''			,      candidate_labels=['''cat''', '''remote''', '''couch''']			,      top_k=A_			,      )
         self.assertEqual(
             nested_simplify(A_			,      decimals=4						)			,      [
                 {'''score''': 0.2_868, '''label''': '''cat''', '''box''': {'''xmin''': 3_2_4, '''ymin''': 2_0, '''xmax''': 6_4_0, '''ymax''': 3_7_3}},
                 {'''score''': 0.277, '''label''': '''remote''', '''box''': {'''xmin''': 4_0, '''ymin''': 7_2, '''xmax''': 1_7_7, '''ymax''': 1_1_5}},
             ]			,      )




"""simple docstring"""


from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream







class 					lowerCAmelCase_					(   _lowercase     ):



						'''simple docstring'''


						def __init__(			self  :   Any	,A_  :   Callable	,A_  :   Optional[Features] = None	,A_  :   str = None	,A_  :   bool = False	,A_  :   bool = False	,A_  :   Optional[dict] = None	,A_  :   Optional[int] = None	,**A_  :   int	,)  -> str:
							super().__init__(
							    features=A_	,cache_dir=A_	,keep_in_memory=A_	,streaming=A_	,num_proc=A_	,**A_	,)
							A     = Generator(
							    cache_dir=A_	,features=A_	,generator=A_	,gen_kwargs=A_	,**A_	,)




						def     _SCREAMING_SNAKE_CASE   (			self  :   Optional[int]   )  -> Optional[int]:
							# Build iterable dataset
							if self.streaming:
								A     = self.builder.as_streaming_dataset(split='train'   )
							# Build regular (map-style) dataset
							else:
								A     = None
								A     = None
								A     = None
								A     = None

								self.builder.download_and_prepare(
								    download_config=A_	,download_mode=A_	,verification_mode=A_	,base_path=A_	,num_proc=self.num_proc	,)
								A     = self.builder.as_dataset(
								    split='train'	,verification_mode=A_	,in_memory=self.keep_in_memory   )
							return dataset