bat.apis.google API documentation

# bat.apis.google

View Source

     1import io
     2from google.cloud import vision
     3
     4import concurrent.futures
     5
     6class CloudVision:
     7    def __init__(self, concurrency=1):
     8        self.concurrency = concurrency
     9
    10    def predict(self, image_path):
    11        try:
    12            with io.open(image_path, 'rb') as image_file:
    13                content = image_file.read()
    14
    15            image = vision.Image(content=content)
    16
    17            client = vision.ImageAnnotatorClient()
    18
    19            response = client.label_detection(image=image)
    20            labels = response.label_annotations
    21
    22        except Exception as e:
    23            print(e)
    24            return
    25
    26        return labels
    27
    28    def predictX(self, image_paths):
    29        y_preds = []
    30        y_index = []
    31        y_executors = {}
    32
    33        with concurrent.futures.ThreadPoolExecutor(max_workers=self.concurrency) as executor:
    34            for i, image_path in enumerate(image_paths):
    35                # Load the input image and construct the payload for the request
    36                y_executors[executor.submit(self.predict, image_path)] = i
    37
    38            for y_executor in concurrent.futures.as_completed(y_executors):
    39                y_index.append(y_executors[y_executor])
    40                y_preds.append(y_executor.result())
    41
    42            y_preds = [y for _, y in sorted(zip(y_index, y_preds))]
    43
    44        return y_preds
    45
    46    def print(self, y_preds):
    47        max_mid_len = 0
    48        max_desc_len = 0
    49
    50        for y in y_preds:
    51            if len(y.description) > max_desc_len:
    52                max_desc_len = len(y.description)
    53            if len(y.mid) > max_mid_len:
    54                max_mid_len = len(y.mid)
    55
    56        for y in y_preds:
    57            print('{:<{w_id}s} {:<{w_desc}s} {:.5f}'.format(y.mid, y.description, y.score, w_id=max_mid_len, w_desc=max_desc_len))

class CloudVision: View Source

     7class CloudVision:
     8    def __init__(self, concurrency=1):
     9        self.concurrency = concurrency
    10
    11    def predict(self, image_path):
    12        try:
    13            with io.open(image_path, 'rb') as image_file:
    14                content = image_file.read()
    15
    16            image = vision.Image(content=content)
    17
    18            client = vision.ImageAnnotatorClient()
    19
    20            response = client.label_detection(image=image)
    21            labels = response.label_annotations
    22
    23        except Exception as e:
    24            print(e)
    25            return
    26
    27        return labels
    28
    29    def predictX(self, image_paths):
    30        y_preds = []
    31        y_index = []
    32        y_executors = {}
    33
    34        with concurrent.futures.ThreadPoolExecutor(max_workers=self.concurrency) as executor:
    35            for i, image_path in enumerate(image_paths):
    36                # Load the input image and construct the payload for the request
    37                y_executors[executor.submit(self.predict, image_path)] = i
    38
    39            for y_executor in concurrent.futures.as_completed(y_executors):
    40                y_index.append(y_executors[y_executor])
    41                y_preds.append(y_executor.result())
    42
    43            y_preds = [y for _, y in sorted(zip(y_index, y_preds))]
    44
    45        return y_preds
    46
    47    def print(self, y_preds):
    48        max_mid_len = 0
    49        max_desc_len = 0
    50
    51        for y in y_preds:
    52            if len(y.description) > max_desc_len:
    53                max_desc_len = len(y.description)
    54            if len(y.mid) > max_mid_len:
    55                max_mid_len = len(y.mid)
    56
    57        for y in y_preds:
    58            print('{:<{w_id}s} {:<{w_desc}s} {:.5f}'.format(y.mid, y.description, y.score, w_id=max_mid_len, w_desc=max_desc_len))

CloudVision(concurrency=1) View Source

    8    def __init__(self, concurrency=1):
    9        self.concurrency = concurrency

def predict(self, image_path): View Source

    11    def predict(self, image_path):
    12        try:
    13            with io.open(image_path, 'rb') as image_file:
    14                content = image_file.read()
    15
    16            image = vision.Image(content=content)
    17
    18            client = vision.ImageAnnotatorClient()
    19
    20            response = client.label_detection(image=image)
    21            labels = response.label_annotations
    22
    23        except Exception as e:
    24            print(e)
    25            return
    26
    27        return labels

def predictX(self, image_paths): View Source

    29    def predictX(self, image_paths):
    30        y_preds = []
    31        y_index = []
    32        y_executors = {}
    33
    34        with concurrent.futures.ThreadPoolExecutor(max_workers=self.concurrency) as executor:
    35            for i, image_path in enumerate(image_paths):
    36                # Load the input image and construct the payload for the request
    37                y_executors[executor.submit(self.predict, image_path)] = i
    38
    39            for y_executor in concurrent.futures.as_completed(y_executors):
    40                y_index.append(y_executors[y_executor])
    41                y_preds.append(y_executor.result())
    42
    43            y_preds = [y for _, y in sorted(zip(y_index, y_preds))]
    44
    45        return y_preds

def print(self, y_preds): View Source

    47    def print(self, y_preds):
    48        max_mid_len = 0
    49        max_desc_len = 0
    50
    51        for y in y_preds:
    52            if len(y.description) > max_desc_len:
    53                max_desc_len = len(y.description)
    54            if len(y.mid) > max_mid_len:
    55                max_mid_len = len(y.mid)
    56
    57        for y in y_preds:
    58            print('{:<{w_id}s} {:<{w_desc}s} {:.5f}'.format(y.mid, y.description, y.score, w_id=max_mid_len, w_desc=max_desc_len))