Browse Source

Add http(s) support for input

tags/v1.2.10
PommeDroid 2 years ago
parent
commit
932bb96820
7 changed files with 119 additions and 12 deletions
  1. 16
    8
      argv/run/config.py
  2. 33
    0
      loader/__init__.py
  3. 23
    0
      loader/fs.py
  4. 35
    0
      loader/http.py
  5. 2
    2
      processing/__init__.py
  6. 2
    2
      processing/image.py
  7. 8
    0
      utils.py

+ 16
- 8
argv/run/config.py View File

@@ -2,8 +2,10 @@ import os

import gpu_info
from argv.checkpoints import set_arg_checkpoints, check_arg_checkpoints
from utils import check_image_file_validity, is_a_supported_image_file_extension

from utils import check_image_file_validity, is_a_supported_image_file_extension, check_url
from loader import Loader
from loader.fs import FSLoader
from loader.http import HTTPLoader

def set_args_run_parser(args):
set_arg_checkpoints(args)
@@ -38,12 +40,18 @@ def set_gpu_ids(args):
def check_arg_input(parser, args):
if not args.input:
parser.error("-i, --input INPUT is required.")
if not os.path.isdir(args.input) and not os.path.isfile(args.input):
parser.error("Input {} file or directory doesn't exist.".format(args.input))
elif os.path.isfile(args.input) and not is_a_supported_image_file_extension(args.input):
parser.error("Input {} file not supported format.".format(args.input))
if os.path.isfile(args.input):
check_image_file_validity(args.input)

loader = Loader.get_loader(args.input)
if loader == FSLoader:
if os.path.isfile(args.input) and not is_a_supported_image_file_extension(args.input):
parser.error("Input {} file not supported format.".format(args.input))
if os.path.isfile(args.input):
check_image_file_validity(args.input)
elif loader == HTTPLoader:
if not check_url(args.input):
parser.error("Url {} of the http ressource doesn't exist or is not accesible.".format(args.input))
else:
parser.error("Input {} is not a valid file or directory or url.".format(args.input))
return args.input



+ 33
- 0
loader/__init__.py View File

@@ -0,0 +1,33 @@
"""Loader."""
from config import Config as Conf


class Loader:
""" Abstract Loader Class """

@staticmethod
def load(uri):
"""
Load the uri ressource
:return: <RGB> image
"""
pass

@staticmethod
def uri_validator(uri):
"""
Validate the uri for the loader
:return: <bool> True is a valid uri
"""
return False

@staticmethod
def get_loader(uri):
from loader.fs import FSLoader
from loader.http import HTTPLoader
for loader in (FSLoader, HTTPLoader):
if loader.uri_validator(uri):
return loader
return None



+ 23
- 0
loader/fs.py View File

@@ -0,0 +1,23 @@
""" File Sytem Loading """
from loader import Loader
from utils import read_image
import os


class FSLoader(Loader):
""" File System Loader Class """
@staticmethod
def load(uri):
"""
Load the file system ressource
:return: <RGB> image
"""
return read_image(uri)

@staticmethod
def uri_validator(uri):
"""
Validate the uri is a filesystem file
:return: <bool> True is a valid uri
"""
return os.path.exists(uri)

+ 35
- 0
loader/http.py View File

@@ -0,0 +1,35 @@
import os
import re
import tempfile
from urllib.parse import urlparse

from utils import dl_file, read_image
from loader import Loader


regex_url = re.compile(
r'^(?:http)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)

class HTTPLoader(Loader):
""" Abstract Loader Class """

@staticmethod
def load(uri):
"""
Load the http ressource
:return: <RGB> image
"""
_, tmp_path = tempfile.mkstemp()
dl_file(uri, tmp_path)
img = read_image(tmp_path)
os.remove(tmp_path)
return img

@staticmethod
def uri_validator(uri):
return regex_url.match(uri)

+ 2
- 2
processing/__init__.py View File

@@ -7,9 +7,10 @@ from utils import camel_case_to_str, cv2_supported_extension


class Processing:
""" Abstract Processing Class """
def __init__(self, args=None):
"""
Image Transformation Class Constructor.
Image Processing Class Constructor.

:param input_index: <tuple> index where to take the inputs (default is (-1) for previous transformation)
:param args: <dict> args parameter to run the image transformation (default use Conf.args)
@@ -77,7 +78,6 @@ class Processing:
"""
pass


class SimpleProcessing(Processing):
"""Simple Transform Class."""


+ 2
- 2
processing/image.py View File

@@ -6,7 +6,7 @@ from config import Config as Conf
from processing import Processing
from processing.utils import select_phases
from utils import read_image, camel_case_to_str, write_image
from loader import Loader

class ImageProcessing(Processing):
"""Image Processing Class."""
@@ -43,7 +43,7 @@ class ImageProcessing(Processing):

def _setup(self, *args):
try:
self.__image_steps = [read_image(x) if isinstance(x, str) else x for x in self.__image_steps]
self.__image_steps = [(Loader.get_loader(x)).load(x) if isinstance(x, str) else x for x in self.__image_steps]
except FileNotFoundError as e:
Conf.log.error(e)
Conf.log.error("{} is not able to resume because it not able to load required images. "

+ 8
- 0
utils.py View File

@@ -223,3 +223,11 @@ def is_a_supported_image_file_extension(path):
:return: <boolean> True if the extension is supported
"""
return os.path.splitext(path)[1] in cv2_supported_extension() + [".gif"]

def check_url(url):
"""
Check if a url exists withtout downloading it
:return: <boolean> True if return url exists
"""
resp = requests.head(url)
return resp.status_code < 400

Loading…
Cancel
Save