Browse Source

Merge pull request #59 from PommeDroid/loader

Add http(s) support for input
master
deeppppp 3 years ago committed by GitHub
parent
commit
022cb0ff5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      argv/run/argument.py
  2. 25
      argv/run/config.py
  3. 30
      loader/__init__.py
  4. 24
      loader/fs.py
  5. 34
      loader/http.py
  6. 3
      processing/__init__.py
  7. 7
      processing/image.py
  8. BIN
      sample_input.png
  9. 1
      transform/gan/mask.py
  10. 2
      transform/opencv/mask.py
  11. 9
      utils.py

2
argv/run/argument.py

@ -75,7 +75,7 @@ def arg_input(parser): @@ -75,7 +75,7 @@ def arg_input(parser):
parser.add_argument(
"-i",
"--input",
help="Path of the photo or directory to transform .",
help="Path or http(s) url of the photo. Or path of the directory to transform .",
)

25
argv/run/config.py

@ -2,7 +2,10 @@ import os @@ -2,7 +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):
@ -38,12 +41,20 @@ def set_gpu_ids(args): @@ -38,12 +41,20 @@ 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))
if not is_a_supported_image_file_extension(args.input):
parser.error("Url {} is not file with a supported extension format.".format(args.input))
else:
parser.error("Input {} is not a valid file or directory or url.".format(args.input))
return args.input

30
loader/__init__.py

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
"""Loader."""
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

24
loader/fs.py

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
""" File Sytem Loading """
import os
from loader import Loader
from utils import read_image
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)

34
loader/http.py

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
import os
import re
import tempfile
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 run(uri):
"""
Run the loader 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)

3
processing/__init__.py

@ -7,9 +7,10 @@ from utils import camel_case_to_str, cv2_supported_extension @@ -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)

7
processing/image.py

@ -5,7 +5,8 @@ import sys @@ -5,7 +5,8 @@ import sys
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 utils import camel_case_to_str, write_image
from loader import Loader
class ImageProcessing(Processing):
@ -43,7 +44,9 @@ class ImageProcessing(Processing): @@ -43,7 +44,9 @@ 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)).run(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. "

BIN
sample_input.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 261 KiB

1
transform/gan/mask.py

@ -56,4 +56,3 @@ class MaskfinToNude(MaskImageTransformGAN): @@ -56,4 +56,3 @@ class MaskfinToNude(MaskImageTransformGAN):
:param args: <dict> args parameter to run the image transformation (default use Conf.args)
"""
super().__init__("maskfin_to_nude", input_index=input_index, args=args)

2
transform/opencv/mask.py

@ -155,7 +155,7 @@ class MaskdetToMaskfin(MaskImageTransformOpenCV): @@ -155,7 +155,7 @@ class MaskdetToMaskfin(MaskImageTransformOpenCV):
hairmin = to_int(self.__hair_size, a_min)
self.__draw_ellipse(a_max, a_min, angle, aurmax, aurmin, details, hairmax, hairmin, nipmax, nipmin, obj,
titmax, titmin, vagmax, vagmin, x, y)
titmax, titmin, vagmax, vagmin, x, y)
@staticmethod
def __draw_ellipse(a_max, a_min, angle, aurmax, aurmin, details, hairmax, hairmin, nipmax, nipmin, obj,

9
utils.py

@ -223,3 +223,12 @@ def is_a_supported_image_file_extension(path): @@ -223,3 +223,12 @@ 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