diff --git a/mainscripts/Converter.py b/mainscripts/Converter.py index 47500b0..c629102 100644 --- a/mainscripts/Converter.py +++ b/mainscripts/Converter.py @@ -7,6 +7,7 @@ import cv2 from tqdm import tqdm from utils.DFLPNG import DFLPNG from utils.DFLJPG import DFLJPG +from utils.cv2_utils import * from utils import image_utils import shutil import numpy as np @@ -154,7 +155,7 @@ class ConvertSubprocessor(SubprocessorBase): print ( 'no faces found for %s, copying without faces' % (filename_path.name) ) shutil.copy ( str(filename_path), str(output_filename_path) ) else: - image = (cv2.imread(str(filename_path)) / 255.0).astype(np.float32) + image = (cv2_imread(str(filename_path)) / 255.0).astype(np.float32) if self.converter.get_mode() == ConverterBase.MODE_IMAGE: image = self.converter.convert_image(image, None, self.debug) @@ -197,7 +198,7 @@ class ConvertSubprocessor(SubprocessorBase): faces_processed = len(faces) if not self.debug: - cv2.imwrite (str(output_filename_path), (image*255).astype(np.uint8) ) + cv2_imwrite (str(output_filename_path), (image*255).astype(np.uint8) ) return (files_processed, faces_processed) diff --git a/mainscripts/Extractor.py b/mainscripts/Extractor.py index 94a27a1..48b55cf 100644 --- a/mainscripts/Extractor.py +++ b/mainscripts/Extractor.py @@ -8,6 +8,7 @@ import numpy as np import cv2 from utils import Path_utils from utils.DFLJPG import DFLJPG +from utils.cv2_utils import * from utils import image_utils from facelib import FaceType import facelib @@ -154,7 +155,7 @@ class ExtractSubprocessor(SubprocessorBase): self.param['y'] = ( ( prev_rect[1] + prev_rect[3] ) / 2 ) * self.view_scale if len(faces) == 0: - self.original_image = cv2.imread(filename) + self.original_image = cv2_imread(filename) (h,w,c) = self.original_image.shape @@ -289,9 +290,9 @@ class ExtractSubprocessor(SubprocessorBase): def onClientProcessData(self, data): filename_path = Path( data[0] ) - image = cv2.imread( str(filename_path) ) + image = cv2_imread( str(filename_path) ) if image is None: - print ( 'Failed to extract %s, reason: cv2.imread() fail.' % ( str(filename_path) ) ) + print ( 'Failed to extract %s, reason: cv2_imread() fail.' % ( str(filename_path) ) ) else: if self.type == 'rects': rects = self.e.extract_from_bgr (image) @@ -327,7 +328,7 @@ class ExtractSubprocessor(SubprocessorBase): face_image = cv2.warpAffine(image, image_to_face_mat, (self.image_size, self.image_size), cv2.INTER_LANCZOS4) face_image_landmarks = facelib.LandmarksProcessor.transform_points (image_landmarks, image_to_face_mat) - cv2.imwrite(output_file, face_image, [int(cv2.IMWRITE_JPEG_QUALITY), 85] ) + cv2_imwrite(output_file, face_image, [int(cv2.IMWRITE_JPEG_QUALITY), 85] ) DFLJPG.embed_data(output_file, face_type = FaceType.toString(self.face_type), landmarks = face_image_landmarks.tolist(), @@ -341,7 +342,7 @@ class ExtractSubprocessor(SubprocessorBase): result.append (output_file) if self.debug: - cv2.imwrite(debug_output_file, debug_image, [int(cv2.IMWRITE_JPEG_QUALITY), 50] ) + cv2_imwrite(debug_output_file, debug_image, [int(cv2.IMWRITE_JPEG_QUALITY), 50] ) return result return None diff --git a/mainscripts/Sorter.py b/mainscripts/Sorter.py index 798e4b7..8b5d84f 100644 --- a/mainscripts/Sorter.py +++ b/mainscripts/Sorter.py @@ -11,6 +11,7 @@ from utils import Path_utils from utils import image_utils from utils.DFLPNG import DFLPNG from utils.DFLJPG import DFLJPG +from utils.cv2_utils import * from facelib import LandmarksProcessor from utils.SubprocessorBase import SubprocessorBase import multiprocessing @@ -98,7 +99,7 @@ class BlurEstimatorSubprocessor(SubprocessorBase): dflimg = None if dflimg is not None: - image = cv2.imread( str(filepath) ) + image = cv2_imread( str(filepath) ) image = ( image * \ LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) \ ).astype(np.uint8) @@ -139,14 +140,14 @@ def sort_by_blur(input_path): def sort_by_brightness(input_path): print ("Sorting by brightness...") - img_list = [ [x, np.mean ( cv2.cvtColor(cv2.imread(x), cv2.COLOR_BGR2HSV)[...,2].flatten() )] for x in tqdm( Path_utils.get_image_paths(input_path), desc="Loading", ascii=True) ] + img_list = [ [x, np.mean ( cv2.cvtColor(cv2_imread(x), cv2.COLOR_BGR2HSV)[...,2].flatten() )] for x in tqdm( Path_utils.get_image_paths(input_path), desc="Loading", ascii=True) ] print ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list def sort_by_hue(input_path): print ("Sorting by hue...") - img_list = [ [x, np.mean ( cv2.cvtColor(cv2.imread(x), cv2.COLOR_BGR2HSV)[...,0].flatten() )] for x in tqdm( Path_utils.get_image_paths(input_path), desc="Loading", ascii=True) ] + img_list = [ [x, np.mean ( cv2.cvtColor(cv2_imread(x), cv2.COLOR_BGR2HSV)[...,0].flatten() )] for x in tqdm( Path_utils.get_image_paths(input_path), desc="Loading", ascii=True) ] print ("Sorting...") img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) return img_list @@ -316,7 +317,7 @@ class HistSsimSubprocessor(SubprocessorBase): img_list = [] for x in data: - img = cv2.imread(x) + img = cv2_imread(x) img_list.append ([x, cv2.calcHist([img], [0], None, [256], [0, 256]), cv2.calcHist([img], [1], None, [256], [0, 256]), cv2.calcHist([img], [2], None, [256], [0, 256]) @@ -455,7 +456,7 @@ def sort_by_hist_dissim(input_path): print ("%s is not a dfl image file" % (filepath.name) ) continue - image = cv2.imread(str(filepath)) + image = cv2_imread(str(filepath)) face_mask = LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) image = (image*face_mask).astype(np.uint8) @@ -535,7 +536,7 @@ class FinalLoaderSubprocessor(SubprocessorBase): print ("%s is not a dfl image file" % (filepath.name) ) raise Exception("") - bgr = cv2.imread(str(filepath)) + bgr = cv2_imread(str(filepath)) if bgr is None: raise Exception ("Unable to load %s" % (filepath.name) ) @@ -651,7 +652,7 @@ def sort_by_black(input_path): img_list = [] for x in tqdm( Path_utils.get_image_paths(input_path), desc="Loading", ascii=True): - img = cv2.imread(x) + img = cv2_imread(x) img_list.append ([x, img[(img == 0)].size ]) print ("Sorting...") diff --git a/mainscripts/Util.py b/mainscripts/Util.py index 9f2f99b..deb210c 100644 --- a/mainscripts/Util.py +++ b/mainscripts/Util.py @@ -11,6 +11,7 @@ from utils import Path_utils from utils import image_utils from utils.DFLPNG import DFLPNG from utils.DFLJPG import DFLJPG +from utils.cv2_utils import * from facelib import LandmarksProcessor from utils.SubprocessorBase import SubprocessorBase import multiprocessing @@ -26,9 +27,9 @@ def convert_png_to_jpg_file (filepath): dfl_dict = dflpng.getDFLDictData() - img = cv2.imread (str(filepath)) + img = cv2_imread (str(filepath)) new_filepath = str(filepath.parent / (filepath.stem + '.jpg')) - cv2.imwrite ( new_filepath, img, [int(cv2.IMWRITE_JPEG_QUALITY), 85]) + cv2_imwrite ( new_filepath, img, [int(cv2.IMWRITE_JPEG_QUALITY), 85]) DFLJPG.embed_data( new_filepath, face_type=dfl_dict.get('face_type', None), @@ -40,12 +41,8 @@ def convert_png_to_jpg_file (filepath): source_landmarks=dfl_dict.get('source_landmarks', None) ) filepath.unlink() - def convert_png_to_jpg_folder (input_path): - if not all(ord(c) < 128 for c in input_path): - print ("Path to directory must contain only non unicode characters.") - return input_path = Path(input_path) print ("Converting PNG to JPG...\r\n") diff --git a/models/ModelBase.py b/models/ModelBase.py index 8b66a5f..216f2c3 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -8,6 +8,7 @@ from utils import Path_utils from utils import std_utils from utils import image_utils from utils.console_utils import * +from utils.cv2_utils import * import numpy as np import cv2 from samples import SampleGeneratorBase @@ -321,7 +322,7 @@ class ModelBase(object): if self.write_preview_history: if self.epoch % 10 == 0: img = (self.get_static_preview() * 255).astype(np.uint8) - cv2.imwrite ( str (self.preview_history_path / ('%.6d.jpg' %( self.epoch) )), img ) + cv2_imwrite ( str (self.preview_history_path / ('%.6d.jpg' %( self.epoch) )), img ) self.epoch += 1 diff --git a/samples/Sample.py b/samples/Sample.py index 01096ce..3b5c03a 100644 --- a/samples/Sample.py +++ b/samples/Sample.py @@ -1,7 +1,8 @@ from enum import IntEnum import cv2 import numpy as np - +from utils.cv2_utils import * + class SampleType(IntEnum): IMAGE = 0 #raw image @@ -37,7 +38,7 @@ class Sample(object): close_target_list=close_target_list if close_target_list is not None else self.close_target_list) def load_bgr(self): - img = cv2.imread (self.filename).astype(np.float32) / 255.0 + img = cv2_imread (self.filename).astype(np.float32) / 255.0 if self.mirror: img = img[:,::-1].copy() return img diff --git a/utils/cv2_utils.py b/utils/cv2_utils.py new file mode 100644 index 0000000..f4412ee --- /dev/null +++ b/utils/cv2_utils.py @@ -0,0 +1,22 @@ +import cv2 +import numpy as np +from pathlib import Path + +#allows to open non-english characters path +def cv2_imread(filename, flags=cv2.IMREAD_UNCHANGED): + try: + with open(filename, "rb") as stream: + bytes = bytearray(stream.read()) + numpyarray = np.asarray(bytes, dtype=np.uint8) + return cv2.imdecode(numpyarray, flags) + except: + return None + +def cv2_imwrite(filename, img, *args): + ret, buf = cv2.imencode( Path(filename).suffix, img, *args) + if ret == True: + try: + with open(filename, "wb") as stream: + stream.write( buf ) + except: + pass \ No newline at end of file