From b928398e3d14a531fc307e89f725aca6e02f7917 Mon Sep 17 00:00:00 2001 From: Enes Esvet Kuzucu <eneskuzucu@hotmail.com> Date: Mon, 13 May 2024 12:06:58 +0300 Subject: [PATCH] fix --- .DS_Store | Bin 0 -> 6148 bytes .github/.DS_Store | Bin 0 -> 6148 bytes .idea/.gitignore | 8 + .idea/image-input-handler.iml | 8 + .idea/inspectionProfiles/Project_Default.xml | 62 ++++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + ImageInputHandler/ImageInputHandler.py | 301 ------------------ ImageInputHandler/__init__.py | 0 __init__.py | 0 image_input_handler/__init__.py | 1 + .../image_input_handler.py | 0 setup.py | 57 ++-- 14 files changed, 126 insertions(+), 331 deletions(-) create mode 100644 .DS_Store create mode 100644 .github/.DS_Store create mode 100644 .idea/.gitignore create mode 100644 .idea/image-input-handler.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml delete mode 100644 ImageInputHandler/ImageInputHandler.py delete mode 100644 ImageInputHandler/__init__.py delete mode 100644 __init__.py create mode 100644 image_input_handler/__init__.py rename ImageInputHandler.py => image_input_handler/image_input_handler.py (100%) diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8654d6661bbc4f701edff5759dccc1d920f5ce31 GIT binary patch literal 6148 zcmeHK!AiqG5Pe&VM!eKpkNX8e{~(qK3f}qynnqe7X@a)*e2IVKhxh^BJ^E&6DJfAX z9;C_)%)H6$Om_1Yb~6CnbW==#5rBj$I5?&GLS$UDCxaL{Cc5yBB{s;5=Xsgey=)5{ zA_KB_C%C`@k65F#e|5<%#W`+J;hw_}^j2uN!E0PIvO<B9(I?&Rz0=FZWHzs>rbEUa zlT>w<<z=0-DjDCty}x|CUhcQNsxE$5*(`f2?JU|{W55_N2L3Pua==rPGext;fH7bU zY#EUA!BYj(h^=BcI%q-*KpZk0gue6=QsX105nDx$P@I+$wbaEH!)ZC=KIWwnTSYC0 zi_M3NGrQQKxG+24??XCVs%X|2Fa~xRIMT<F-2YeK_y4;^_GAng1OJKvH_T?)l%Evu x*51d-UF)fLR1wK*6}KgH!d1*%xr)csAdGu)h-t)Dkr9gh2m~6;7z4k`z$cBmPapsQ literal 0 HcmV?d00001 diff --git a/.github/.DS_Store b/.github/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..36b372c72cba58418798b30534145e40b446efdb GIT binary patch literal 6148 zcmeHKJ5EC}5S)b+K{P2TeTCe>ioyxF0D>q@p7bOlMEfevm7_8HDTp3)NfXUV>#^56 zwmik#w*YMOxmyD(086?fzI~XR@4L_Jt|CUH^Nc+PjQGNFcbsMa9dPag86!S$_|1PG z-o|n8ZJ8910#ZN<NC7EuK?SO~E*~#=rjC*VQs6ce@b5#TJNCjUF+Lp}q6Hu>7!Kn+ zdI@6l0I?TNiHy)Jsl=pOwHTIk##`m}!YMK7uxdW6Zno-BEN<udEz)5<QKJ-)0_O_c z=W^xs|C#<z|9?)>N(xAUn^M3Q+n4QzPpaBFdz{zWMt`My&WY~Ec~Ce+IVMIq=EBSI dQzT_x^Evl>;glG3#)D4O&w%S9lLFUP;0L2O71{s* literal 0 HcmV?d00001 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/image-input-handler.iml b/.idea/image-input-handler.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/image-input-handler.iml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="PYTHON_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$" /> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..04cb1f4 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,62 @@ +<component name="InspectionProjectProfileManager"> + <profile version="1.0"> + <option name="myName" value="Project Default" /> + <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" /> + <inspection_tool class="PyInterpreterInspection" enabled="false" level="WARNING" enabled_by_default="false" /> + <inspection_tool class="PyPackageRequirementsInspection" enabled="false" level="WARNING" enabled_by_default="false"> + <option name="ignoredPackages"> + <value> + <list size="40"> + <item index="0" class="java.lang.String" itemvalue="pandas" /> + <item index="1" class="java.lang.String" itemvalue="networkx" /> + <item index="2" class="java.lang.String" itemvalue="joblib" /> + <item index="3" class="java.lang.String" itemvalue="threadpoolctl" /> + <item index="4" class="java.lang.String" itemvalue="scikit-learn" /> + <item index="5" class="java.lang.String" itemvalue="python-dateutil" /> + <item index="6" class="java.lang.String" itemvalue="MarkupSafe" /> + <item index="7" class="java.lang.String" itemvalue="numpy" /> + <item index="8" class="java.lang.String" itemvalue="requests" /> + <item index="9" class="java.lang.String" itemvalue="torchvision" /> + <item index="10" class="java.lang.String" itemvalue="Jinja2" /> + <item index="11" class="java.lang.String" itemvalue="dlib" /> + <item index="12" class="java.lang.String" itemvalue="filelock" /> + <item index="13" class="java.lang.String" itemvalue="certifi" /> + <item index="14" class="java.lang.String" itemvalue="imutils" /> + <item index="15" class="java.lang.String" itemvalue="urllib3" /> + <item index="16" class="java.lang.String" itemvalue="itsdangerous" /> + <item index="17" class="java.lang.String" itemvalue="sympy" /> + <item index="18" class="java.lang.String" itemvalue="torchaudio" /> + <item index="19" class="java.lang.String" itemvalue="Flask" /> + <item index="20" class="java.lang.String" itemvalue="blinker" /> + <item index="21" class="java.lang.String" itemvalue="scipy" /> + <item index="22" class="java.lang.String" itemvalue="six" /> + <item index="23" class="java.lang.String" itemvalue="botocore" /> + <item index="24" class="java.lang.String" itemvalue="Flask-Cors" /> + <item index="25" class="java.lang.String" itemvalue="Werkzeug" /> + <item index="26" class="java.lang.String" itemvalue="opencv-python" /> + <item index="27" class="java.lang.String" itemvalue="torch" /> + <item index="28" class="java.lang.String" itemvalue="click" /> + <item index="29" class="java.lang.String" itemvalue="jmespath" /> + <item index="30" class="java.lang.String" itemvalue="boto3" /> + <item index="31" class="java.lang.String" itemvalue="s3transfer" /> + <item index="32" class="java.lang.String" itemvalue="mpmath" /> + <item index="33" class="java.lang.String" itemvalue="typing_extensions" /> + <item index="34" class="java.lang.String" itemvalue="charset-normalizer" /> + <item index="35" class="java.lang.String" itemvalue="idna" /> + <item index="36" class="java.lang.String" itemvalue="Pillow" /> + <item index="37" class="java.lang.String" itemvalue="head-segmentation" /> + <item index="38" class="java.lang.String" itemvalue="google-cloud-storage" /> + <item index="39" class="java.lang.String" itemvalue="unittest" /> + </list> + </value> + </option> + </inspection_tool> + <inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true"> + <option name="ignoredErrors"> + <list> + <option value="N806" /> + </list> + </option> + </inspection_tool> + </profile> +</component> \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ +<component name="InspectionProjectProfileManager"> + <settings> + <option name="USE_PROJECT_PROFILE" value="false" /> + <version value="1.0" /> + </settings> +</component> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..868e79b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/image-input-handler.iml" filepath="$PROJECT_DIR$/.idea/image-input-handler.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/ImageInputHandler/ImageInputHandler.py b/ImageInputHandler/ImageInputHandler.py deleted file mode 100644 index 379b576..0000000 --- a/ImageInputHandler/ImageInputHandler.py +++ /dev/null @@ -1,301 +0,0 @@ -import numpy as np -import cv2 -import base64 -import urllib3 -import re -from PIL import Image, UnidentifiedImageError -import io - -class UniversalImageInputHandler: - def __init__(self, img_input, img_is_a_mask=False, debug=False): - self.img_input = img_input - self.img_is_a_mask = img_is_a_mask - self.img = None - self.COMPATIBLE = False - self.debug=debug - - if self.debug: - print("debug On") - - self.read_image() - - def adjust_image_channels(self, img): - if img.ndim == 3 and img.shape[2] == 4: - img = img[:, :, :3] # Remove the alpha channel if present - if self.img_is_a_mask and img.ndim == 3: - img = img[:, :, 0] # Use the first channel for masks - return img - - def read_image(self): - - if self.debug: - print("checking image input type") - print(self.img_input) - - if isinstance(self.img_input, np.ndarray): - - if self.debug : - print("input is ndarray") - self.process_image(self.img_input) - elif isinstance(self.img_input, str): - if self.debug: - print("input is string") - if self.is_url(self.img_input): - if self.debug: - print("input is url") - self.handle_url_image(self.img_input) - elif self.is_path(self.img_input): - if self.debug: - print("input is path") - self.handle_path_image(self.img_input) - elif self.is_base64(self.img_input): - if self.debug: - print("input is base64 image") - self.handle_base64_image(self.img_input) - - def handle_url_image(self, url): - try: - user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) ..'} - http = urllib3.PoolManager(10, headers=user_agent) - response = http.urlopen('GET', url) - image = Image.open(io.BytesIO(response.data)) - img_arr = np.array(image) - self.process_image(img_arr) - except (UnidentifiedImageError, urllib3.exceptions.HTTPError) as e: - print(f"Failed to load image from URL: {e}") - - def handle_path_image(self, path): - img = cv2.imread(path, cv2.IMREAD_UNCHANGED) - if img is not None: - img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - self.process_image(img) - else: - print("Failed to load image from path.") - - def handle_base64_image(self, encoded_img): - try: - decoded_img = base64.b64decode(encoded_img) - img_np_arr = np.frombuffer(decoded_img, np.uint8) - img = cv2.imdecode(img_np_arr, cv2.IMREAD_UNCHANGED) - self.process_image(img) - except ValueError: - print("Invalid Base64 encoding.") - - def process_image(self, img): - img = self.adjust_image_channels(img) - self.img = img - self.COMPATIBLE = True - - # def is_path(self, s): - # path_regex = re.compile( - # r'^(/|\\|[a-zA-Z]:\\|\.\\|..\\|./|../)' - # r'(?:(?:[^\\/:*?"<>|\r\n]+\\|[^\\/:*?"<>|\r\n]+/)*' - # r'[^\\/:*?"<>|\r\n]*)$', - # re.IGNORECASE) - # return re.match(path_regex, s) is not None - - def is_path(self, s): - path_regex = re.compile( - r'^(/|\\|[a-zA-Z]:\\|\.\\|..\\|./|../)?' # Optional start with /, \, C:\, .\, ..\, ./, or ../ - r'(?:(?:[^\\/:*?"<>|\r\n]+\\|[^\\/:*?"<>|\r\n]+/)*' # Directory names - r'[^\\/:*?"<>|\r\n]*)$', # Last part of the path which can be a file - re.IGNORECASE) - return re.match(path_regex, s) is not None - - def is_url(self, s): - url_regex = re.compile( - r'^(https?://|ftp://)' - r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' - r'localhost|' - r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' - r'(?::\d+)?' - r'(?:/?|[/?]\S+)$', re.IGNORECASE) - return re.match(url_regex, s) is not None - - def is_base64(self, s): - try: - s = s.strip() - if len(s) % 4 != 0: - return False - base64.b64decode(s, validate=True) - return True - except ValueError: - return False - - - - - - - - - - - - - - - - - - -# import urllib3 -# import logging -# from PIL import Image,UnidentifiedImageError -# import io -# import re -# import numpy as np -# import base64 -# import cv2 -# -# -# -# -# -# class ImageObject: -# def __init__(self, img_input, img_is_a_mask=False): -# self.IMG_IS_NP_ARRAY = False -# self.IMG_HAS_3_CHANNEL = False -# self.IMG_HAS_4_CHANNEL = False -# self.IMG_IS_BASE64_ENCODED = False -# self.IMG_IS_LINK = False -# self.img_input = img_input -# self.img_is_a_mask = img_is_a_mask -# self.COMPATIBLE = False -# self.img_input_format = False -# -# def is_numpy_array(self, img): -# return isinstance(img, np.ndarray) -# -# def has_three_channels(self, img): -# return img.ndim == 3 and img.shape[-1] == 3 -# -# def has_four_channels(self, img): -# return img.ndim == 3 and img.shape[-1] == 4 -# -# def read_image(self): -# pass -# -# def load_img(self, path, COLORTRANSFORMATION): -# temp = cv2.imread(path, cv2.IMREAD_UNCHANGED) -# temp = cv2.cvtColor(temp, COLORTRANSFORMATION) -# return temp -# -# def read_img_with_user_agent(self, url): -# user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) ..'} -# http = urllib3.PoolManager(10, headers=user_agent) -# r1 = http.urlopen('GET', url) -# LINK_IS_IMAGE = False -# im2arr = [] -# try: -# image = Image.open(io.BytesIO(r1.data)) -# im2arr = np.array(image) -# LINK_IS_IMAGE = True -# except UnidentifiedImageError: -# pass -# return LINK_IS_IMAGE, im2arr, -# -# def is_path(self, s): -# # Regular expression for validating file paths -# path_regex = re.compile( -# r'^(/|\\|[a-zA-Z]:\\|\.\\|..\\|./|../)' # Starts with /, \, C:\, .\, ..\, ./, or ../ -# r'(?:(?:[^\\/:*?"<>|\r\n]+\\|[^\\/:*?"<>|\r\n]+/)*' # Directory names -# r'[^\\/:*?"<>|\r\n]*)$', # Last part of the path -# re.IGNORECASE) -# return re.match(path_regex, s) is not None -# -# def is_url(self, s): -# # A simple regular expression for validating URLs -# url_regex = re.compile( -# r'^(https?://|ftp://)' # HTTP, HTTPS, or FTP protocols -# r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' # 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) -# return re.match(url_regex, s) is not None -# -# def is_base64(self, s): -# """Check if the input string is Base64-encoded.""" -# try: -# s = s.strip() -# if len(s) % 4 != 0: -# return False -# base64.b64decode(s, validate=True) -# return True -# except ValueError: -# return False -# -# def decode_img(self, encoded_img, mask=False): -# -# if self.is_base64(encoded_img): -# decoded_img = base64.b64decode(encoded_img) -# img_np_arr = np.frombuffer(decoded_img, np.uint8) -# if mask: -# img = cv2.imdecode(img_np_arr, cv2.IMREAD_UNCHANGED) -# if img is not None and len(img.shape) == 3 and img.shape[2] == 2: -# pass # Not sure what 'pass' is intended for, possibly apply some processing here? -# else: -# img = img[:, :, 2] # Assuming this intends to extract a specific channel, needs more clarification. -# else: -# img = cv2.imdecode(img_np_arr, cv2.IMREAD_COLOR) -# return True, img -# else: -# return False, None -# -# def encode_img(self, img): -# _, img_buffer = cv2.imencode('.webp', img) -# encoded_img = base64.b64encode(img_buffer) -# # return encoded_img -# return encoded_img.decode('utf-8') -# -# def read_img_input(self): -# if isinstance(self.img_input, str): -# if self.is_url(self.img_input): -# LINK_IS_IMAGE, img = self.read_img_with_user_agent(self.img_input) -# if LINK_IS_IMAGE: -# if self.has_four_channels(img): -# img = img[:, :, :3] -# self.img = img -# elif self.has_three_channels(img): -# self.img = img -# if self.img_is_a_mask: -# self.img = self.img[:, :, 0] -# -# self.COMPATIBLE = True -# else: -# pass -# else: -# is_img_input_a_base64_img, img = self.decode_img(self.img_input) -# if is_img_input_a_base64_img: -# if self.has_four_channels(img): -# img = img[:, :, :3] -# self.img = img -# elif self.has_three_channels(img): -# self.img = img -# if self.img_is_a_mask: -# self.img = self.img[:, :, 0] -# self.COMPATIBLE = True -# elif self.is_path(self.img_input): -# img = self.load_img(self.img_input, cv2.COLOR_BGR2RGB) -# if self.has_four_channels(img): -# img = img[:, :, :3] -# self.img = img -# elif self.has_three_channels(img): -# self.img = img -# if self.img_is_a_mask: -# self.img = self.img[:, :, 0] -# self.COMPATIBLE = True -# -# elif isinstance(self.img_input, np.ndarray): -# img=self.img_input -# if self.has_four_channels(img): -# img = img[:, :, :3] -# self.img = img -# elif self.has_three_channels(img): -# self.img = img -# if self.img_is_a_mask: -# self.img = self.img[:, :, 0] -# self.COMPATIBLE = True -# -# diff --git a/ImageInputHandler/__init__.py b/ImageInputHandler/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/__init__.py b/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/image_input_handler/__init__.py b/image_input_handler/__init__.py new file mode 100644 index 0000000..a6ef86c --- /dev/null +++ b/image_input_handler/__init__.py @@ -0,0 +1 @@ +from .image_input_handler import UniversalImageInputHandler \ No newline at end of file diff --git a/ImageInputHandler.py b/image_input_handler/image_input_handler.py similarity index 100% rename from ImageInputHandler.py rename to image_input_handler/image_input_handler.py diff --git a/setup.py b/setup.py index 8ff1489..af75191 100644 --- a/setup.py +++ b/setup.py @@ -3,38 +3,9 @@ import subprocess import os -# cf_remote_version = ( -# subprocess.run(["git", "describe", "--tags"], stdout=subprocess.PIPE) -# .stdout.decode("utf-8") -# .strip() -# ) -# -# if "-" in cf_remote_version: -# # when not on tag, git describe outputs: "1.3.3-22-gdf81228" -# # pip has gotten strict with version numbers -# # so change it to: "1.3.3+22.git.gdf81228" -# # See: https://peps.python.org/pep-0440/#local-version-segments -# v,i,s = cf_remote_version.split("-") -# cf_remote_version = v + "+" + i + ".git." + s -# -# assert "-" not in cf_remote_version -# assert "." in cf_remote_version -# -# assert os.path.isfile("cf_remote/version.py") -# with open("cf_remote/VERSION", "w", encoding="utf-8") as fh: -# fh.write("%s\n" % cf_remote_version) -# -# with open("README.md", "r", encoding="utf-8") as fh: -# long_description = fh.read() - - - - - - setup( name='image_input_handler', # Package name - version='0.2.4', # Version of your package + version='0.2.5', # Version of your package author='Enes Kuzucu', # Your name description='A module to handle different formats of image input', # Short description @@ -58,3 +29,29 @@ ], python_requires='>=3.7', # Minimum version requirement of Python ) + + + +# cf_remote_version = ( +# subprocess.run(["git", "describe", "--tags"], stdout=subprocess.PIPE) +# .stdout.decode("utf-8") +# .strip() +# ) +# +# if "-" in cf_remote_version: +# # when not on tag, git describe outputs: "1.3.3-22-gdf81228" +# # pip has gotten strict with version numbers +# # so change it to: "1.3.3+22.git.gdf81228" +# # See: https://peps.python.org/pep-0440/#local-version-segments +# v,i,s = cf_remote_version.split("-") +# cf_remote_version = v + "+" + i + ".git." + s +# +# assert "-" not in cf_remote_version +# assert "." in cf_remote_version +# +# assert os.path.isfile("cf_remote/version.py") +# with open("cf_remote/VERSION", "w", encoding="utf-8") as fh: +# fh.write("%s\n" % cf_remote_version) +# +# with open("README.md", "r", encoding="utf-8") as fh: +# long_description = fh.read() \ No newline at end of file