def cleanmosaic_video_byframe(opt, netG, netM): path = opt.media_path fps, imagepaths = video_init(opt, path)[:2] positions = get_mosaic_positions(opt, netM, imagepaths, savemask=True) # clean mosaic for i, imagepath in enumerate(imagepaths, 0): x, y, size = positions[i][0], positions[i][1], positions[i][2] img_origin = impro.imread(os.path.join('./tmp/video2image', imagepath)) img_result = img_origin.copy() if size != 0: img_mosaic = img_origin[y - size:y + size, x - size:x + size] if opt.traditional: img_fake = runmodel.traditional_cleaner(img_mosaic, opt) else: img_fake = runmodel.run_pix2pix(img_mosaic, netG, opt) mask = cv2.imread(os.path.join('./tmp/mosaic_mask', imagepath), 0) img_result = impro.replace_mosaic(img_origin, img_fake, mask, x, y, size, opt.no_feather) cv2.imwrite(os.path.join('./tmp/replace_mosaic', imagepath), img_result) print('\r', 'Clean Mosaic:' + str(i + 1) + '/' + str(len(imagepaths))) ffmpeg.image2video( fps, './tmp/replace_mosaic/output_%05d.' + opt.tempimage_type, './tmp/voice_tmp.mp3', os.path.join( opt.result_dir, os.path.splitext(os.path.basename(path))[0] + '_clean.mp4'))
def cleanmosaic_video_byframe(opt, netG, netM): path = opt.media_path fps, imagepaths = video_init(opt, path)[:2] positions = get_mosaic_positions(opt, netM, imagepaths, savemask=True) t1 = time.time() if not opt.no_preview: cv2.namedWindow('clean', cv2.WINDOW_NORMAL) # clean mosaic print('Step:3/4 -- Clean Mosaic:') length = len(imagepaths) for i, imagepath in enumerate(imagepaths, 0): x, y, size = positions[i][0], positions[i][1], positions[i][2] img_origin = impro.imread( os.path.join(opt.temp_dir + '/video2image', imagepath)) img_result = img_origin.copy() if size > 100: try: #Avoid unknown errors img_mosaic = img_origin[y - size:y + size, x - size:x + size] if opt.traditional: img_fake = runmodel.traditional_cleaner(img_mosaic, opt) else: img_fake = runmodel.run_pix2pix(img_mosaic, netG, opt) mask = cv2.imread( os.path.join(opt.temp_dir + '/mosaic_mask', imagepath), 0) img_result = impro.replace_mosaic(img_origin, img_fake, mask, x, y, size, opt.no_feather) except Exception as e: print('Warning:', e) cv2.imwrite(os.path.join(opt.temp_dir + '/replace_mosaic', imagepath), img_result) os.remove(os.path.join(opt.temp_dir + '/video2image', imagepath)) #preview result and print if not opt.no_preview: cv2.imshow('clean', img_result) cv2.waitKey(1) & 0xFF t2 = time.time() print('\r', str(i + 1) + '/' + str(length), util.get_bar(100 * i / length, num=35), util.counttime(t1, t2, i + 1, len(imagepaths)), end='') print() if not opt.no_preview: cv2.destroyAllWindows() print('Step:4/4 -- Convert images to video') ffmpeg.image2video( fps, opt.temp_dir + '/replace_mosaic/output_%06d.' + opt.tempimage_type, opt.temp_dir + '/voice_tmp.mp3', os.path.join( opt.result_dir, os.path.splitext(os.path.basename(path))[0] + '_clean.mp4'))
def cleanmosaic_img_server(opt,img_origin,netG,netM): x,y,size,mask = runmodel.get_mosaic_position(img_origin,netM,opt) img_result = img_origin.copy() if size > 100 : img_mosaic = img_origin[y-size:y+size,x-size:x+size] if opt.traditional: img_fake = runmodel.traditional_cleaner(img_mosaic,opt) else: img_fake = runmodel.run_pix2pix(img_mosaic,netG,opt) img_result = impro.replace_mosaic(img_origin,img_fake,mask,x,y,size,opt.no_feather) return img_result
def cleanmosaic_img(opt,netG,netM): path = opt.media_path print('Clean Mosaic:',path) img_origin = impro.imread(path) x,y,size,mask = runmodel.get_mosaic_position(img_origin,netM,opt) cv2.imwrite('./mask/'+os.path.basename(path), mask) img_result = img_origin.copy() if size != 0 : img_mosaic = img_origin[y-size:y+size,x-size:x+size] if opt.traditional: img_fake = runmodel.traditional_cleaner(img_mosaic,opt) else: img_fake = runmodel.run_pix2pix(img_mosaic,netG,opt) img_result = impro.replace_mosaic(img_origin,img_fake,mask,x,y,size,opt.no_feather) else: print('Do not find mosaic') impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_clean.jpg'),img_result)