|
|
@ -1,30 +1,58 @@ |
|
|
|
# encoding=utf-8 |
|
|
|
|
|
|
|
from collections import namedtuple |
|
|
|
|
|
|
|
import time |
|
|
|
import cv2 |
|
|
|
import requests |
|
|
|
import numpy as np |
|
|
|
|
|
|
|
from towhee._types import Image |
|
|
|
from towhee.operator import PyOperator, SharedType |
|
|
|
from towhee import register |
|
|
|
from towhee.utils import ndarray_utils |
|
|
|
from towhee.utils.log import engine_log |
|
|
|
|
|
|
|
|
|
|
|
class Timer: |
|
|
|
""" |
|
|
|
Timer |
|
|
|
""" |
|
|
|
def __init__(self, text: str = 'Elapsed time: {:0.4f} seconds'): |
|
|
|
self._text = text |
|
|
|
self._start_time = None |
|
|
|
|
|
|
|
def start(self): |
|
|
|
if self._start_time is not None: |
|
|
|
raise RuntimeError('Timer is running. Use .stop() to stop it') |
|
|
|
self._start_time = time.perf_counter() |
|
|
|
|
|
|
|
def stop(self): |
|
|
|
if self._start_time is None: |
|
|
|
raise RuntimeError('Timer is not running. Use .start() to start it') |
|
|
|
elapsed_time = time.perf_counter() - self._start_time |
|
|
|
self._start_time = None |
|
|
|
engine_log.error(self._text.format(elapsed_time)) |
|
|
|
return elapsed_time |
|
|
|
|
|
|
|
def __enter__(self): |
|
|
|
self.start() |
|
|
|
return self |
|
|
|
|
|
|
|
def __exit__(self, *exc_info): |
|
|
|
self.stop() |
|
|
|
|
|
|
|
|
|
|
|
class ImageDecoder(PyOperator): |
|
|
|
def __init__(self): |
|
|
|
super().__init__() |
|
|
|
|
|
|
|
def _load_from_remote(image_url: str) -> np.ndarray: |
|
|
|
try: |
|
|
|
with Timer("Image download time: {:0.4f} seconds"): |
|
|
|
r = requests.get(image_url, timeout=(20, 20)) |
|
|
|
if r.status_code // 100 != 2: |
|
|
|
engine_log.error('Download image from %s failed, error msg: %s, request code: %s ' % (image_url, |
|
|
|
r.text, |
|
|
|
r.status_code)) |
|
|
|
return None |
|
|
|
with Timer("Image decode time: {:0.4f} seconds"): |
|
|
|
arr = np.asarray(bytearray(r.content), dtype=np.uint8) |
|
|
|
return cv2.imdecode(arr, -1) |
|
|
|
except Exception as e: |
|
|
|