Skip to content

Commit ef9b2e4

Browse files
committed
V0.5.1 (#19 #33 #53)
1 parent e100118 commit ef9b2e4

24 files changed

+734
-213
lines changed

.gitignore

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ test*
142142
video_tmp/
143143
result/
144144
nohup.out
145+
.vscode/
146+
145147
#./
146-
/.vscode
147148
/pix2pix
148149
/pix2pixHD
149150
/tmp
@@ -157,6 +158,7 @@ nohup.out
157158
/deepmosaic_window
158159
/sftp-config.json
159160
/exe
161+
160162
#./make_datasets
161163
/make_datasets/video
162164
/make_datasets/tmp
@@ -172,6 +174,7 @@ nohup.out
172174
#mediafile
173175
*iter
174176
*.pth
177+
*.pt
175178
*.jpeg
176179
*.bmp
177180
*.mp4
@@ -185,4 +188,39 @@ nohup.out
185188
*.JPEG
186189
*.exe
187190
*.npy
188-
*.psd
191+
*.psd
192+
193+
194+
##############################cpp###################################
195+
# Prerequisites
196+
*.d
197+
198+
# Compiled Object files
199+
*.slo
200+
*.lo
201+
*.o
202+
*.obj
203+
204+
# Precompiled Headers
205+
*.gch
206+
*.pch
207+
208+
# Compiled Dynamic libraries
209+
*.so
210+
*.dylib
211+
*.dll
212+
213+
# Fortran module files
214+
*.mod
215+
*.smod
216+
217+
# Compiled Static libraries
218+
*.lai
219+
*.la
220+
*.a
221+
*.lib
222+
223+
# Executables
224+
*.exe
225+
*.out
226+
*.app

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ You can use it to automatically remove the mosaics in images and videos, or add
99

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

3536
### Try it on web
36-
You can simply try to remove the mosaic on the face at this [website](http://118.89.27.46:5000/).<br>
37+
You can simply try to remove the mosaic on the **face** at this [website](http://118.89.27.46:5000/).<br>
3738
### Pre-built binary package
3839
For Windows, we bulid a GUI version for easy testing.<br>
3940
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>
@@ -61,7 +62,7 @@ Attentions:<br>
6162
This code depends on opencv-python, torchvision available via pip install.
6263
#### Clone this repo
6364
```bash
64-
git clone https://github.com/HypoX64/DeepMosaics
65+
git clone https://github.com/HypoX64/DeepMosaics.git
6566
cd DeepMosaics
6667
```
6768
#### Get Pre-Trained Models

README_CN.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### 例子
1212
![image](./imgs/hand.gif)
13+
1314
原始 | 自动打码 | 自动去码
1415
:-:|:-:|:-:
1516
![image](./imgs/example/lena.jpg) | ![image](./imgs/example/lena_add.jpg) | ![image](./imgs/example/lena_clean.jpg)
@@ -33,7 +34,7 @@
3334
## 如何运行
3435
可以通过我们预编译好的二进制包或源代码运行.<br>
3536
### 在网页中运行
36-
打开[这个网站](http://118.89.27.46:5000/)上传照片,将获得去除马赛克后的结果,受限与当地法律,目前只支持人脸.<br>
37+
打开[这个网站](http://118.89.27.46:5000/)上传照片,将获得去除马赛克后的结果,受限于当地法律,**目前只支持人脸**.<br>
3738
### 预编译的程序包
3839
对于Windows用户,我们提供了包含GUI界面的免安装软件包.<br>
3940
可以通过下面两种方式进行下载: [[Google Drive]](https://drive.google.com/open?id=1LTERcN33McoiztYEwBxMuRjjgxh4DEPs) [[百度云,提取码1x0a]](https://pan.baidu.com/s/10rN3U3zd5TmfGpO_PEShqQ) <br>
@@ -57,10 +58,10 @@
5758
- [Pytorch 1.0+](https://pytorch.org/)
5859
- CPU or NVIDIA GPU + CUDA CuDNN<br>
5960
#### Python依赖项
60-
代码依赖于opencv-python以及 torchvision,可有通过pip install 进行安装.
61+
代码依赖于opencv-python以及 torchvision,可以通过pip install 进行安装.
6162
#### 克隆源代码
6263
```bash
63-
git clone https://github.com/HypoX64/DeepMosaics
64+
git clone https://github.com/HypoX64/DeepMosaics.git
6465
cd DeepMosaics
6566
```
6667
#### 下载预训练模型

cores/add.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import os
2+
from queue import Queue
3+
from threading import Thread
4+
import time
5+
import numpy as np
6+
import cv2
7+
from models import runmodel
8+
from util import mosaic,util,ffmpeg,filt
9+
from util import image_processing as impro
10+
from .init import video_init
11+
12+
13+
'''
14+
---------------------Add Mosaic---------------------
15+
'''
16+
def addmosaic_img(opt,netS):
17+
path = opt.media_path
18+
print('Add Mosaic:',path)
19+
img = impro.imread(path)
20+
mask = runmodel.get_ROI_position(img,netS,opt)[0]
21+
img = mosaic.addmosaic(img,mask,opt)
22+
impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.jpg'),img)
23+
24+
def get_roi_positions(opt,netS,imagepaths,savemask=True):
25+
# resume
26+
continue_flag = False
27+
if os.path.isfile(os.path.join(opt.temp_dir,'step.json')):
28+
step = util.loadjson(os.path.join(opt.temp_dir,'step.json'))
29+
resume_frame = int(step['frame'])
30+
if int(step['step'])>2:
31+
mask_index = np.load(os.path.join(opt.temp_dir,'mask_index.npy'))
32+
return mask_index
33+
if int(step['step'])>=2 and resume_frame>0:
34+
pre_positions = np.load(os.path.join(opt.temp_dir,'roi_positions.npy'))
35+
continue_flag = True
36+
imagepaths = imagepaths[resume_frame:]
37+
38+
positions = []
39+
t1 = time.time()
40+
if not opt.no_preview:
41+
cv2.namedWindow('mask', cv2.WINDOW_NORMAL)
42+
print('Step:2/4 -- Find mosaic location')
43+
44+
img_read_pool = Queue(4)
45+
def loader(imagepaths):
46+
for imagepath in imagepaths:
47+
img_origin = impro.imread(os.path.join(opt.temp_dir+'/video2image',imagepath))
48+
img_read_pool.put(img_origin)
49+
t = Thread(target=loader,args=(imagepaths,))
50+
t.daemon = True
51+
t.start()
52+
53+
for i,imagepath in enumerate(imagepaths,1):
54+
img_origin = img_read_pool.get()
55+
mask,x,y,size,area = runmodel.get_ROI_position(img_origin,netS,opt)
56+
positions.append([x,y,area])
57+
if savemask:
58+
t = Thread(target=cv2.imwrite,args=(os.path.join(opt.temp_dir+'/ROI_mask',imagepath), mask,))
59+
t.start()
60+
if i%1000==0:
61+
save_positions = np.array(positions)
62+
if continue_flag:
63+
save_positions = np.concatenate((pre_positions,save_positions),axis=0)
64+
np.save(os.path.join(opt.temp_dir,'roi_positions.npy'),save_positions)
65+
step = {'step':2,'frame':i+resume_frame}
66+
util.savejson(os.path.join(opt.temp_dir,'step.json'),step)
67+
68+
#preview result and print
69+
if not opt.no_preview:
70+
cv2.imshow('mask',mask)
71+
cv2.waitKey(1) & 0xFF
72+
t2 = time.time()
73+
print('\r',str(i)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=35),util.counttime(t1,t2,i,len(imagepaths)),end='')
74+
75+
if not opt.no_preview:
76+
cv2.destroyAllWindows()
77+
78+
print('\nOptimize ROI locations...')
79+
if continue_flag:
80+
positions = np.concatenate((pre_positions,positions),axis=0)
81+
mask_index = filt.position_medfilt(np.array(positions), 7)
82+
step = {'step':3,'frame':0}
83+
util.savejson(os.path.join(opt.temp_dir,'step.json'),step)
84+
np.save(os.path.join(opt.temp_dir,'roi_positions.npy'),positions)
85+
np.save(os.path.join(opt.temp_dir,'mask_index.npy'),np.array(mask_index))
86+
87+
return mask_index
88+
89+
def addmosaic_video(opt,netS):
90+
path = opt.media_path
91+
fps,imagepaths = video_init(opt,path)[:2]
92+
length = len(imagepaths)
93+
start_frame = int(imagepaths[0][7:13])
94+
mask_index = get_roi_positions(opt,netS,imagepaths)[(start_frame-1):]
95+
96+
t1 = time.time()
97+
if not opt.no_preview:
98+
cv2.namedWindow('preview', cv2.WINDOW_NORMAL)
99+
100+
# add mosaic
101+
print('Step:3/4 -- Add Mosaic:')
102+
t1 = time.time()
103+
# print(mask_index)
104+
for i,imagepath in enumerate(imagepaths,1):
105+
mask = impro.imread(os.path.join(opt.temp_dir+'/ROI_mask',imagepaths[np.clip(mask_index[i-1]-start_frame,0,1000000)]),'gray')
106+
img = impro.imread(os.path.join(opt.temp_dir+'/video2image',imagepath))
107+
if impro.mask_area(mask)>100:
108+
try:#Avoid unknown errors
109+
img = mosaic.addmosaic(img, mask, opt)
110+
except Exception as e:
111+
print('Warning:',e)
112+
t = Thread(target=cv2.imwrite,args=(os.path.join(opt.temp_dir+'/addmosaic_image',imagepath),img))
113+
t.start()
114+
os.remove(os.path.join(opt.temp_dir+'/video2image',imagepath))
115+
116+
#preview result and print
117+
if not opt.no_preview:
118+
cv2.imshow('preview',img)
119+
cv2.waitKey(1) & 0xFF
120+
t2 = time.time()
121+
print('\r',str(i)+'/'+str(length),util.get_bar(100*i/length,num=35),util.counttime(t1,t2,i,length),end='')
122+
123+
print()
124+
if not opt.no_preview:
125+
cv2.destroyAllWindows()
126+
print('Step:4/4 -- Convert images to video')
127+
ffmpeg.image2video( fps,
128+
opt.temp_dir+'/addmosaic_image/output_%06d.'+opt.tempimage_type,
129+
opt.temp_dir+'/voice_tmp.mp3',
130+
os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.mp4'))

0 commit comments

Comments
 (0)