提交 33686cf2 编写于 作者: H hypox64

For chinese path in windows, use cv2.imdecode.

上级 803ec352
...@@ -145,6 +145,7 @@ result/ ...@@ -145,6 +145,7 @@ result/
/pix2pix /pix2pix
/pix2pixHD /pix2pixHD
/tmp /tmp
/tmp_files
/to_make_show /to_make_show
/test_media /test_media
/result /result
......
...@@ -21,7 +21,7 @@ def addmosaic_img(opt,netS): ...@@ -21,7 +21,7 @@ def addmosaic_img(opt,netS):
img = impro.imread(path) img = impro.imread(path)
mask = runmodel.get_ROI_position(img,netS,opt)[0] mask = runmodel.get_ROI_position(img,netS,opt)[0]
img = mosaic.addmosaic(img,mask,opt) img = mosaic.addmosaic(img,mask,opt)
cv2.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.jpg'),img) impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.jpg'),img)
def addmosaic_video(opt,netS): def addmosaic_video(opt,netS):
path = opt.media_path path = opt.media_path
...@@ -56,7 +56,7 @@ def styletransfer_img(opt,netG): ...@@ -56,7 +56,7 @@ def styletransfer_img(opt,netG):
img = impro.imread(opt.media_path) img = impro.imread(opt.media_path)
img = runmodel.run_styletransfer(opt, netG, img) img = runmodel.run_styletransfer(opt, netG, img)
suffix = os.path.basename(opt.model_path).replace('.pth','').replace('style_','') suffix = os.path.basename(opt.model_path).replace('.pth','').replace('style_','')
cv2.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(opt.media_path))[0]+'_'+suffix+'.jpg'),img) impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(opt.media_path))[0]+'_'+suffix+'.jpg'),img)
def styletransfer_video(opt,netG): def styletransfer_video(opt,netG):
path = opt.media_path path = opt.media_path
...@@ -89,7 +89,7 @@ def cleanmosaic_img(opt,netG,netM): ...@@ -89,7 +89,7 @@ def cleanmosaic_img(opt,netG,netM):
img_result = impro.replace_mosaic(img_origin,img_fake,x,y,size,opt.no_feather) img_result = impro.replace_mosaic(img_origin,img_fake,x,y,size,opt.no_feather)
else: else:
print('Do not find mosaic') print('Do not find mosaic')
cv2.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_clean.jpg'),img_result) impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_clean.jpg'),img_result)
def cleanmosaic_video_byframe(opt,netG,netM): def cleanmosaic_video_byframe(opt,netG,netM):
path = opt.media_path path = opt.media_path
......
...@@ -17,9 +17,9 @@ class Options(): ...@@ -17,9 +17,9 @@ class Options():
self.parser.add_argument('--model_path', type=str, default='./pretrained_models/add_hands_128.pth',help='pretrained model path') self.parser.add_argument('--model_path', type=str, default='./pretrained_models/add_hands_128.pth',help='pretrained model path')
self.parser.add_argument('--result_dir', type=str, default='./result',help='output media will be saved here') self.parser.add_argument('--result_dir', type=str, default='./result',help='output media will be saved here')
self.parser.add_argument('--tempimage_type', type=str, default='png',help='type of temp image, png | jpg, png is better but occupy more storage space') self.parser.add_argument('--tempimage_type', type=str, default='png',help='type of temp image, png | jpg, png is better but occupy more storage space')
self.parser.add_argument('--output_size', type=int, default=0,help='size of output file,if 0 -> origin')
self.parser.add_argument('--netG', type=str, default='auto', self.parser.add_argument('--netG', type=str, default='auto',
help='select model to use for netG(Clean mosaic and Transfer style) -> auto | unet_128 | unet_256| resnet_9blocks | HD | video') help='select model to use for netG(Clean mosaic and Transfer style) -> auto | unet_128 | unet_256 | resnet_9blocks | HD | video')
self.parser.add_argument('--output_size', type=int, default=0,help='size of output file,if 0 -> origin')
#AddMosaic #AddMosaic
self.parser.add_argument('--mosaic_mod', type=str, default='squa_avg',help='type of mosaic -> squa_avg | squa_random | squa_avg_circle_edge | rect_avg | random') self.parser.add_argument('--mosaic_mod', type=str, default='squa_avg',help='type of mosaic -> squa_avg | squa_random | squa_avg_circle_edge | rect_avg | random')
...@@ -35,7 +35,8 @@ class Options(): ...@@ -35,7 +35,8 @@ class Options():
self.parser.add_argument('--ex_mult', type=str, default='auto',help='mosaic area expansion') self.parser.add_argument('--ex_mult', type=str, default='auto',help='mosaic area expansion')
#StyleTransfer #StyleTransfer
self.parser.add_argument('--edges', action='store_true', help='if true, make edges first') self.parser.add_argument('--preprocess', type=str, default='resize', help='resize and cropping of images at load time [ resize | resize_scale_width | edges | gray] or resize,edges(use comma to split)')
self.parser.add_argument('--edges', action='store_true', help='if true, use edges to generate pictures,(input_nc = 1)')
self.parser.add_argument('--canny', type=int, default=150,help='threshold of canny') self.parser.add_argument('--canny', type=int, default=150,help='threshold of canny')
self.parser.add_argument('--only_edges', action='store_true', help='if true, output media will be edges') self.parser.add_argument('--only_edges', action='store_true', help='if true, output media will be edges')
...@@ -65,6 +66,12 @@ class Options(): ...@@ -65,6 +66,12 @@ class Options():
else: else:
print('Please input running mode!') print('Please input running mode!')
if self.opt.output_size == 0 and self.opt.mode == 'style':
self.opt.output_size = 512
if 'edges' in model_name or 'edges' in self.opt.preprocess:
self.opt.edges = True
if self.opt.netG == 'auto' and self.opt.mode =='clean': if self.opt.netG == 'auto' and self.opt.mode =='clean':
if 'unet_128' in model_name: if 'unet_128' in model_name:
self.opt.netG = 'unet_128' self.opt.netG = 'unet_128'
...@@ -77,12 +84,9 @@ class Options(): ...@@ -77,12 +84,9 @@ class Options():
else: else:
print('Type of Generator error!') print('Type of Generator error!')
if 'edges' in model_name:
self.opt.edges = True
if self.opt.ex_mult == 'auto': if self.opt.ex_mult == 'auto':
if 'face' in model_name: if 'face' in model_name:
self.opt.ex_mult = 1.2 self.opt.ex_mult = 1.1
else: else:
self.opt.ex_mult = 1.5 self.opt.ex_mult = 1.5
else: else:
......
...@@ -56,12 +56,12 @@ def main(): ...@@ -56,12 +56,12 @@ def main():
util.clean_tempfiles(tmp_init = False) util.clean_tempfiles(tmp_init = False)
main() # main()
# if __name__ == '__main__': if __name__ == '__main__':
# try: try:
# main() main()
# except Exception as e: except Exception as e:
# print('Error:',e) print('Error:',e)
# input('Please press any key to exit.\n') input('Please press any key to exit.\n')
# util.clean_tempfiles(tmp_init = False) util.clean_tempfiles(tmp_init = False)
# exit(0) exit(0)
import numpy as np
import cv2
import os
import sys
sys.path.append("..")
from util import image_processing as impro
from util import util
img_dir = './datasets_img/pix2pix/edges2cat/images'
output_dir = './datasets_img/pix2pix/edges2cat/train'
util.makedirs(output_dir)
img_names = os.listdir(img_dir)
for i,img_name in enumerate(img_names,2000):
try:
img = impro.imread(os.path.join(img_dir,img_name))
img = impro.resize(img, 286)
h,w = img.shape[:2]
edges = cv2.Canny(img,150,250)
edges = impro.ch_one2three(edges)
out_img = np.zeros((h,w*2,3), dtype=np.uint8)
out_img[:,0:w] = edges
out_img[:,w:2*w] = img
cv2.imwrite(os.path.join(output_dir,'%05d' % i+'.jpg'), out_img)
except Exception as e:
pass
...@@ -39,7 +39,7 @@ def pix2pix(opt): ...@@ -39,7 +39,7 @@ def pix2pix(opt):
def style(opt): def style(opt):
if opt.edges: if opt.edges:
netG = define_G(1, 3, 64, 'unet_256', norm='instance',use_dropout=True, init_type='normal', gpu_ids=[]) netG = define_G(1, 3, 64, 'resnet_9blocks', norm='instance',use_dropout=True, init_type='normal', gpu_ids=[])
else: else:
netG = define_G(3, 3, 64, 'resnet_9blocks', norm='instance',use_dropout=False, init_type='normal', gpu_ids=[]) netG = define_G(3, 3, 64, 'resnet_9blocks', norm='instance',use_dropout=False, init_type='normal', gpu_ids=[])
......
...@@ -37,11 +37,15 @@ def run_pix2pix(img,net,opt): ...@@ -37,11 +37,15 @@ def run_pix2pix(img,net,opt):
return img_fake return img_fake
def run_styletransfer(opt, net, img): def run_styletransfer(opt, net, img):
if opt.output_size != 0: if opt.output_size != 0:
img = impro.resize(img,opt.output_size) if 'resize' in opt.preprocess and 'resize_scale_width' not in opt.preprocess:
if opt.edges: img = impro.resize(img,opt.output_size)
if not opt.only_edges: elif 'resize_scale_width' in opt.preprocess:
img = img[0:256*int(img.shape[0]/256),0:256*int(img.shape[1]/256),:] img = cv2.resize(img, (opt.output_size,opt.output_size))
img = img[0:4*int(img.shape[0]/4),0:4*int(img.shape[1]/4),:]
if 'edges' in opt.preprocess:
if opt.canny > 100: if opt.canny > 100:
canny_low = opt.canny-50 canny_low = opt.canny-50
canny_high = np.clip(opt.canny+50,0,255) canny_high = np.clip(opt.canny+50,0,255)
...@@ -56,7 +60,6 @@ def run_styletransfer(opt, net, img): ...@@ -56,7 +60,6 @@ def run_styletransfer(opt, net, img):
return img return img
img = data.im2tensor(img,use_gpu=opt.use_gpu,gray=True,use_transform = False,is0_1 = False) img = data.im2tensor(img,use_gpu=opt.use_gpu,gray=True,use_transform = False,is0_1 = False)
else: else:
img = img[0:4*int(img.shape[0]/4),0:4*int(img.shape[1]/4),:]
img = data.im2tensor(img,use_gpu=opt.use_gpu) img = data.im2tensor(img,use_gpu=opt.use_gpu)
img = net(img) img = net(img)
img = data.tensor2im(img) img = data.tensor2im(img)
......
...@@ -45,4 +45,4 @@ def continuous_screenshot(videopath,savedir,fps): ...@@ -45,4 +45,4 @@ def continuous_screenshot(videopath,savedir,fps):
fps: save how many images per second fps: save how many images per second
''' '''
videoname = os.path.splitext(os.path.basename(videopath))[0] videoname = os.path.splitext(os.path.basename(videopath))[0]
os.system('ffmpeg -i '+videopath+' -vf fps='+str(fps)+' '+savedir+'/'+videoname+'_%05d.jpg') os.system('ffmpeg -i "'+videopath+'" -vf fps='+str(fps)+' '+savedir+'/'+videoname+'_%05d.jpg')
...@@ -2,20 +2,42 @@ import cv2 ...@@ -2,20 +2,42 @@ import cv2
import numpy as np import numpy as np
import random import random
import platform
system_type = 'Linux'
if 'Windows' in platform.platform():
system_type = 'Windows'
def imread(file_path,mod = 'normal'): def imread(file_path,mod = 'normal'):
''' '''
mod = 'normal' | 'gray' | 'all' mod = 'normal' | 'gray' | 'all'
''' '''
if mod == 'normal': if system_type == 'Linux':
cv_img = cv2.imread(file_path) if mod == 'normal':
elif mod == 'gray': img = cv2.imread(file_path)
cv_img = cv2.imread(file_path,0) elif mod == 'gray':
elif mod == 'all': img = cv2.imread(file_path,0)
cv_img = cv2.imread(file_path,-1) elif mod == 'all':
img = cv2.imread(file_path,-1)
# # imread for chinese path in windows but no EXIF
# cv_img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1) #For chinese path, use cv2.imdecode in windows.
return cv_img #It will loss EXIF, I can't fix it
else:
if mod == 'gray':
img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),0)
else:
img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)
return img
def imwrite(file_path,img):
'''
in other to save chinese path images in windows,
this fun just for save final output images
'''
if system_type == 'Linux':
cv2.imwrite(file_path, img)
else:
cv2.imencode('.jpg', img)[1].tofile(file_path)
def resize(img,size,interpolation=cv2.INTER_LINEAR): def resize(img,size,interpolation=cv2.INTER_LINEAR):
h, w = img.shape[:2] h, w = img.shape[:2]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册