Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
HypoX64 committed May 22, 2021
2 parents e52e6c8 + ef9b2e4 commit 7b9afd4
Show file tree
Hide file tree
Showing 24 changed files with 734 additions and 213 deletions.
42 changes: 40 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ test*
video_tmp/
result/
nohup.out
.vscode/

#./
/.vscode
/pix2pix
/pix2pixHD
/tmp
Expand All @@ -157,6 +158,7 @@ nohup.out
/deepmosaic_window
/sftp-config.json
/exe

#./make_datasets
/make_datasets/video
/make_datasets/tmp
Expand All @@ -172,6 +174,7 @@ nohup.out
#mediafile
*iter
*.pth
*.pt
*.jpeg
*.bmp
*.mp4
Expand All @@ -185,4 +188,39 @@ nohup.out
*.JPEG
*.exe
*.npy
*.psd
*.psd


##############################cpp###################################
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ You can use it to automatically remove the mosaics in images and videos, or add

### Examples
![image](./imgs/hand.gif)

origin | auto add mosaic | auto clean mosaic
:-:|:-:|:-:
![image](./imgs/example/lena.jpg) | ![image](./imgs/example/lena_add.jpg) | ![image](./imgs/example/lena_clean.jpg)
Expand All @@ -33,7 +34,7 @@ An interesting example:[Ricardo Milos to cat](https://www.bilibili.com/video/BV1
You can either run DeepMosaics via a pre-built binary package, or from source.<br>

### Try it on web
You can simply try to remove the mosaic on the face at this [website](http://118.89.27.46:5000/).<br>
You can simply try to remove the mosaic on the **face** at this [website](http://118.89.27.46:5000/).<br>
### Pre-built binary package
For Windows, we bulid a GUI version for easy testing.<br>
Download this version, and a pre-trained model via [[Google Drive]](https://drive.google.com/open?id=1LTERcN33McoiztYEwBxMuRjjgxh4DEPs) [[百度云,提取码1x0a]](https://pan.baidu.com/s/10rN3U3zd5TmfGpO_PEShqQ) <br>
Expand Down Expand Up @@ -61,7 +62,7 @@ Attentions:<br>
This code depends on opencv-python, torchvision available via pip install.
#### Clone this repo
```bash
git clone https://github.com/HypoX64/DeepMosaics
git clone https://github.com/HypoX64/DeepMosaics.git
cd DeepMosaics
```
#### Get Pre-Trained Models
Expand Down
7 changes: 4 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### 例子
![image](./imgs/hand.gif)

原始 | 自动打码 | 自动去码
:-:|:-:|:-:
![image](./imgs/example/lena.jpg) | ![image](./imgs/example/lena_add.jpg) | ![image](./imgs/example/lena_clean.jpg)
Expand All @@ -33,7 +34,7 @@
## 如何运行
可以通过我们预编译好的二进制包或源代码运行.<br>
### 在网页中运行
打开[这个网站](http://118.89.27.46:5000/)上传照片,将获得去除马赛克后的结果,受限与当地法律,目前只支持人脸.<br>
打开[这个网站](http://118.89.27.46:5000/)上传照片,将获得去除马赛克后的结果,受限于当地法律,**目前只支持人脸**.<br>
### 预编译的程序包
对于Windows用户,我们提供了包含GUI界面的免安装软件包.<br>
可以通过下面两种方式进行下载: [[Google Drive]](https://drive.google.com/open?id=1LTERcN33McoiztYEwBxMuRjjgxh4DEPs) [[百度云,提取码1x0a]](https://pan.baidu.com/s/10rN3U3zd5TmfGpO_PEShqQ) <br>
Expand All @@ -57,10 +58,10 @@
- [Pytorch 1.0+](https://pytorch.org/)
- CPU or NVIDIA GPU + CUDA CuDNN<br>
#### Python依赖项
代码依赖于opencv-python以及 torchvision,可有通过pip install 进行安装.
代码依赖于opencv-python以及 torchvision,可以通过pip install 进行安装.
#### 克隆源代码
```bash
git clone https://github.com/HypoX64/DeepMosaics
git clone https://github.com/HypoX64/DeepMosaics.git
cd DeepMosaics
```
#### 下载预训练模型
Expand Down
130 changes: 130 additions & 0 deletions cores/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import os
from queue import Queue
from threading import Thread
import time
import numpy as np
import cv2
from models import runmodel
from util import mosaic,util,ffmpeg,filt
from util import image_processing as impro
from .init import video_init


'''
---------------------Add Mosaic---------------------
'''
def addmosaic_img(opt,netS):
path = opt.media_path
print('Add Mosaic:',path)
img = impro.imread(path)
mask = runmodel.get_ROI_position(img,netS,opt)[0]
img = mosaic.addmosaic(img,mask,opt)
impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.jpg'),img)

def get_roi_positions(opt,netS,imagepaths,savemask=True):
# resume
continue_flag = False
if os.path.isfile(os.path.join(opt.temp_dir,'step.json')):
step = util.loadjson(os.path.join(opt.temp_dir,'step.json'))
resume_frame = int(step['frame'])
if int(step['step'])>2:
mask_index = np.load(os.path.join(opt.temp_dir,'mask_index.npy'))
return mask_index
if int(step['step'])>=2 and resume_frame>0:
pre_positions = np.load(os.path.join(opt.temp_dir,'roi_positions.npy'))
continue_flag = True
imagepaths = imagepaths[resume_frame:]

positions = []
t1 = time.time()
if not opt.no_preview:
cv2.namedWindow('mask', cv2.WINDOW_NORMAL)
print('Step:2/4 -- Find mosaic location')

img_read_pool = Queue(4)
def loader(imagepaths):
for imagepath in imagepaths:
img_origin = impro.imread(os.path.join(opt.temp_dir+'/video2image',imagepath))
img_read_pool.put(img_origin)
t = Thread(target=loader,args=(imagepaths,))
t.daemon = True
t.start()

for i,imagepath in enumerate(imagepaths,1):
img_origin = img_read_pool.get()
mask,x,y,size,area = runmodel.get_ROI_position(img_origin,netS,opt)
positions.append([x,y,area])
if savemask:
t = Thread(target=cv2.imwrite,args=(os.path.join(opt.temp_dir+'/ROI_mask',imagepath), mask,))
t.start()
if i%1000==0:
save_positions = np.array(positions)
if continue_flag:
save_positions = np.concatenate((pre_positions,save_positions),axis=0)
np.save(os.path.join(opt.temp_dir,'roi_positions.npy'),save_positions)
step = {'step':2,'frame':i+resume_frame}
util.savejson(os.path.join(opt.temp_dir,'step.json'),step)

#preview result and print
if not opt.no_preview:
cv2.imshow('mask',mask)
cv2.waitKey(1) & 0xFF
t2 = time.time()
print('\r',str(i)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=35),util.counttime(t1,t2,i,len(imagepaths)),end='')

if not opt.no_preview:
cv2.destroyAllWindows()

print('\nOptimize ROI locations...')
if continue_flag:
positions = np.concatenate((pre_positions,positions),axis=0)
mask_index = filt.position_medfilt(np.array(positions), 7)
step = {'step':3,'frame':0}
util.savejson(os.path.join(opt.temp_dir,'step.json'),step)
np.save(os.path.join(opt.temp_dir,'roi_positions.npy'),positions)
np.save(os.path.join(opt.temp_dir,'mask_index.npy'),np.array(mask_index))

return mask_index

def addmosaic_video(opt,netS):
path = opt.media_path
fps,imagepaths = video_init(opt,path)[:2]
length = len(imagepaths)
start_frame = int(imagepaths[0][7:13])
mask_index = get_roi_positions(opt,netS,imagepaths)[(start_frame-1):]

t1 = time.time()
if not opt.no_preview:
cv2.namedWindow('preview', cv2.WINDOW_NORMAL)

# add mosaic
print('Step:3/4 -- Add Mosaic:')
t1 = time.time()
# print(mask_index)
for i,imagepath in enumerate(imagepaths,1):
mask = impro.imread(os.path.join(opt.temp_dir+'/ROI_mask',imagepaths[np.clip(mask_index[i-1]-start_frame,0,1000000)]),'gray')
img = impro.imread(os.path.join(opt.temp_dir+'/video2image',imagepath))
if impro.mask_area(mask)>100:
try:#Avoid unknown errors
img = mosaic.addmosaic(img, mask, opt)
except Exception as e:
print('Warning:',e)
t = Thread(target=cv2.imwrite,args=(os.path.join(opt.temp_dir+'/addmosaic_image',imagepath),img))
t.start()
os.remove(os.path.join(opt.temp_dir+'/video2image',imagepath))

#preview result and print
if not opt.no_preview:
cv2.imshow('preview',img)
cv2.waitKey(1) & 0xFF
t2 = time.time()
print('\r',str(i)+'/'+str(length),util.get_bar(100*i/length,num=35),util.counttime(t1,t2,i,length),end='')

print()
if not opt.no_preview:
cv2.destroyAllWindows()
print('Step:4/4 -- Convert images to video')
ffmpeg.image2video( fps,
opt.temp_dir+'/addmosaic_image/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]+'_add.mp4'))
Loading

0 comments on commit 7b9afd4

Please sign in to comment.