Extractor: fix aspect ratio for square images,

now we can re-extract face manually from already aligned jpg image without quality loss.
This commit is contained in:
iperov 2019-02-22 17:28:08 +04:00
parent 5bbd8997d9
commit daebe10ca7

View file

@ -3,6 +3,7 @@ import os
import sys
import time
import multiprocessing
import shutil
from pathlib import Path
import numpy as np
import cv2
@ -156,8 +157,7 @@ class ExtractSubprocessor(SubprocessorBase):
self.original_image = cv2_imread(filename)
(h,w,c) = self.original_image.shape
self.view_scale = 1.0 if self.manual_window_size == 0 else self.manual_window_size / (w if w > h else h)
self.view_scale = 1.0 if self.manual_window_size == 0 else self.manual_window_size / ( h * (16.0/9.0) )
self.original_image = cv2.resize (self.original_image, ( int(w*self.view_scale), int(h*self.view_scale) ), interpolation=cv2.INTER_LINEAR)
(h,w,c) = self.original_image.shape
@ -290,6 +290,7 @@ class ExtractSubprocessor(SubprocessorBase):
filename_path = Path( data[0] )
image = cv2_imread( str(filename_path) )
if image is None:
print ( 'Failed to extract %s, reason: cv2_imread() fail.' % ( str(filename_path) ) )
else:
@ -303,6 +304,13 @@ class ExtractSubprocessor(SubprocessorBase):
return [str(filename_path), landmarks]
elif self.type == 'final':
src_dflimg = None
(h,w,c) = image.shape
if h == w:
#extracting from already extracted jpg image?
if filename_path.suffix == '.jpg':
src_dflimg = DFLJPG.load ( str(filename_path) )
result = []
faces = data[1]
@ -327,6 +335,10 @@ class ExtractSubprocessor(SubprocessorBase):
face_image = cv2.warpAffine(image, image_to_face_mat, (self.image_size, self.image_size), cv2.INTER_LANCZOS4)
face_image_landmarks = LandmarksProcessor.transform_points (image_landmarks, image_to_face_mat)
if src_dflimg is not None:
#if extracting from dflimg just copy it in order not to lose quality
shutil.copy ( str(filename_path), str(output_file) )
else:
cv2_imwrite(output_file, face_image, [int(cv2.IMWRITE_JPEG_QUALITY), 85] )
DFLJPG.embed_data(output_file, face_type = FaceType.toString(self.face_type),