added support of non-english characters in paths

This commit is contained in:
iperov 2019-02-05 19:33:55 +04:00
parent 0313fd9ae6
commit 5a1fb199f4
7 changed files with 47 additions and 23 deletions

View file

@ -7,6 +7,7 @@ import cv2
from tqdm import tqdm from tqdm import tqdm
from utils.DFLPNG import DFLPNG from utils.DFLPNG import DFLPNG
from utils.DFLJPG import DFLJPG from utils.DFLJPG import DFLJPG
from utils.cv2_utils import *
from utils import image_utils from utils import image_utils
import shutil import shutil
import numpy as np import numpy as np
@ -154,7 +155,7 @@ class ConvertSubprocessor(SubprocessorBase):
print ( 'no faces found for %s, copying without faces' % (filename_path.name) ) print ( 'no faces found for %s, copying without faces' % (filename_path.name) )
shutil.copy ( str(filename_path), str(output_filename_path) ) shutil.copy ( str(filename_path), str(output_filename_path) )
else: 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: if self.converter.get_mode() == ConverterBase.MODE_IMAGE:
image = self.converter.convert_image(image, None, self.debug) image = self.converter.convert_image(image, None, self.debug)
@ -197,7 +198,7 @@ class ConvertSubprocessor(SubprocessorBase):
faces_processed = len(faces) faces_processed = len(faces)
if not self.debug: 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) return (files_processed, faces_processed)

View file

@ -8,6 +8,7 @@ import numpy as np
import cv2 import cv2
from utils import Path_utils from utils import Path_utils
from utils.DFLJPG import DFLJPG from utils.DFLJPG import DFLJPG
from utils.cv2_utils import *
from utils import image_utils from utils import image_utils
from facelib import FaceType from facelib import FaceType
import facelib import facelib
@ -154,7 +155,7 @@ class ExtractSubprocessor(SubprocessorBase):
self.param['y'] = ( ( prev_rect[1] + prev_rect[3] ) / 2 ) * self.view_scale self.param['y'] = ( ( prev_rect[1] + prev_rect[3] ) / 2 ) * self.view_scale
if len(faces) == 0: if len(faces) == 0:
self.original_image = cv2.imread(filename) self.original_image = cv2_imread(filename)
(h,w,c) = self.original_image.shape (h,w,c) = self.original_image.shape
@ -289,9 +290,9 @@ class ExtractSubprocessor(SubprocessorBase):
def onClientProcessData(self, data): def onClientProcessData(self, data):
filename_path = Path( data[0] ) filename_path = Path( data[0] )
image = cv2.imread( str(filename_path) ) image = cv2_imread( str(filename_path) )
if image is None: 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: else:
if self.type == 'rects': if self.type == 'rects':
rects = self.e.extract_from_bgr (image) 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 = 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) 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), DFLJPG.embed_data(output_file, face_type = FaceType.toString(self.face_type),
landmarks = face_image_landmarks.tolist(), landmarks = face_image_landmarks.tolist(),
@ -341,7 +342,7 @@ class ExtractSubprocessor(SubprocessorBase):
result.append (output_file) result.append (output_file)
if self.debug: 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 result
return None return None

View file

@ -11,6 +11,7 @@ from utils import Path_utils
from utils import image_utils from utils import image_utils
from utils.DFLPNG import DFLPNG from utils.DFLPNG import DFLPNG
from utils.DFLJPG import DFLJPG from utils.DFLJPG import DFLJPG
from utils.cv2_utils import *
from facelib import LandmarksProcessor from facelib import LandmarksProcessor
from utils.SubprocessorBase import SubprocessorBase from utils.SubprocessorBase import SubprocessorBase
import multiprocessing import multiprocessing
@ -98,7 +99,7 @@ class BlurEstimatorSubprocessor(SubprocessorBase):
dflimg = None dflimg = None
if dflimg is not None: if dflimg is not None:
image = cv2.imread( str(filepath) ) image = cv2_imread( str(filepath) )
image = ( image * \ image = ( image * \
LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) \ LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) \
).astype(np.uint8) ).astype(np.uint8)
@ -139,14 +140,14 @@ def sort_by_blur(input_path):
def sort_by_brightness(input_path): def sort_by_brightness(input_path):
print ("Sorting by brightness...") 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...") print ("Sorting...")
img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True)
return img_list return img_list
def sort_by_hue(input_path): def sort_by_hue(input_path):
print ("Sorting by hue...") 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...") print ("Sorting...")
img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True) img_list = sorted(img_list, key=operator.itemgetter(1), reverse=True)
return img_list return img_list
@ -316,7 +317,7 @@ class HistSsimSubprocessor(SubprocessorBase):
img_list = [] img_list = []
for x in data: for x in data:
img = cv2.imread(x) img = cv2_imread(x)
img_list.append ([x, cv2.calcHist([img], [0], None, [256], [0, 256]), img_list.append ([x, cv2.calcHist([img], [0], None, [256], [0, 256]),
cv2.calcHist([img], [1], None, [256], [0, 256]), cv2.calcHist([img], [1], None, [256], [0, 256]),
cv2.calcHist([img], [2], 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) ) print ("%s is not a dfl image file" % (filepath.name) )
continue continue
image = cv2.imread(str(filepath)) image = cv2_imread(str(filepath))
face_mask = LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks()) face_mask = LandmarksProcessor.get_image_hull_mask (image.shape, dflimg.get_landmarks())
image = (image*face_mask).astype(np.uint8) image = (image*face_mask).astype(np.uint8)
@ -535,7 +536,7 @@ class FinalLoaderSubprocessor(SubprocessorBase):
print ("%s is not a dfl image file" % (filepath.name) ) print ("%s is not a dfl image file" % (filepath.name) )
raise Exception("") raise Exception("")
bgr = cv2.imread(str(filepath)) bgr = cv2_imread(str(filepath))
if bgr is None: if bgr is None:
raise Exception ("Unable to load %s" % (filepath.name) ) raise Exception ("Unable to load %s" % (filepath.name) )
@ -651,7 +652,7 @@ def sort_by_black(input_path):
img_list = [] img_list = []
for x in tqdm( Path_utils.get_image_paths(input_path), desc="Loading", ascii=True): 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 ]) img_list.append ([x, img[(img == 0)].size ])
print ("Sorting...") print ("Sorting...")

View file

@ -11,6 +11,7 @@ from utils import Path_utils
from utils import image_utils from utils import image_utils
from utils.DFLPNG import DFLPNG from utils.DFLPNG import DFLPNG
from utils.DFLJPG import DFLJPG from utils.DFLJPG import DFLJPG
from utils.cv2_utils import *
from facelib import LandmarksProcessor from facelib import LandmarksProcessor
from utils.SubprocessorBase import SubprocessorBase from utils.SubprocessorBase import SubprocessorBase
import multiprocessing import multiprocessing
@ -26,9 +27,9 @@ def convert_png_to_jpg_file (filepath):
dfl_dict = dflpng.getDFLDictData() dfl_dict = dflpng.getDFLDictData()
img = cv2.imread (str(filepath)) img = cv2_imread (str(filepath))
new_filepath = str(filepath.parent / (filepath.stem + '.jpg')) 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, DFLJPG.embed_data( new_filepath,
face_type=dfl_dict.get('face_type', None), 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) ) source_landmarks=dfl_dict.get('source_landmarks', None) )
filepath.unlink() filepath.unlink()
def convert_png_to_jpg_folder (input_path): 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) input_path = Path(input_path)
print ("Converting PNG to JPG...\r\n") print ("Converting PNG to JPG...\r\n")

View file

@ -8,6 +8,7 @@ from utils import Path_utils
from utils import std_utils from utils import std_utils
from utils import image_utils from utils import image_utils
from utils.console_utils import * from utils.console_utils import *
from utils.cv2_utils import *
import numpy as np import numpy as np
import cv2 import cv2
from samples import SampleGeneratorBase from samples import SampleGeneratorBase
@ -321,7 +322,7 @@ class ModelBase(object):
if self.write_preview_history: if self.write_preview_history:
if self.epoch % 10 == 0: if self.epoch % 10 == 0:
img = (self.get_static_preview() * 255).astype(np.uint8) 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 self.epoch += 1

View file

@ -1,7 +1,8 @@
from enum import IntEnum from enum import IntEnum
import cv2 import cv2
import numpy as np import numpy as np
from utils.cv2_utils import *
class SampleType(IntEnum): class SampleType(IntEnum):
IMAGE = 0 #raw image 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) close_target_list=close_target_list if close_target_list is not None else self.close_target_list)
def load_bgr(self): 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: if self.mirror:
img = img[:,::-1].copy() img = img[:,::-1].copy()
return img return img

22
utils/cv2_utils.py Normal file
View file

@ -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