Skip to content

Commit d1dacf7

Browse files
committed
update 133-135(Colorization, ENet, Style-transfer
1 parent 056cd2b commit d1dacf7

18 files changed

+247
-1
lines changed

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,15 @@ code_128 | [DNN- Call the Detection Model of Tensorflow](python/code_128) |
170170
code_129 | [DNN- Call the Openpose Implementation Attitude Assessment](python/code_129) | ✔️
171171
code_130 | [DNN- Call YOLO Object Detection Network](python/code_130) | ✔️
172172
code_131 | [DNN- YOLOv3-tiny Real-time Object Detection](python/code_131) | ✔️
173-
code_132 | [DNN- Single and Multiple Image Detection](python/code_132) | ✔️
173+
code_132 | [DNN- Single and Multiple Image Detection](python/code_132) | ✔️
174+
code_133 | [DNN- Colorful Image Colorization ](python/code_133) | ✔️
175+
code_134 | [DNN- ENet Image Segmentation](python/code_134) | ✔️
176+
code_135 | [DNN- Real-time Fast Image Style Transfer](python/code_135) | ✔️
177+
178+
---
179+
180+
### Appendix
181+
182+
⛳️ The weight can be download from Google Driver:
183+
184+
🌱 [Weight for DNN](https://drive.google.com/drive/folders/1mg6VXpkvEmyL1scaelX5FnW8uw1gk9iq?usp=sharing)

README_CN.md

+11
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,14 @@ code_129 | [DNN- 调用openpose模型实现姿态评估](python/code_129) |
170170
code_130 | [DNN- 调用YOLO对象检测网络](python/code_130) | ✔️
171171
code_131 | [DNN- YOLOv3-tiny版本实时对象检测](python/code_131) | ✔️
172172
code_132 | [DNN- 单张与多张图像的推断](python/code_132) | ✔️
173+
code_133 | [DNN- 图像颜色化模型使用 ](python/code_133) | ✔️
174+
code_134 | [DNN- ENet实现图像分割](python/code_134) | ✔️
175+
code_135 | [DNN- 实时快速的图像风格迁移](python/code_135) | ✔️
176+
177+
---
178+
179+
### 附录
180+
181+
⛳️ DNN模块的一些模型下载可以从下面的谷歌云中获取:
182+
183+
🌱 [Weight for DNN](https://drive.google.com/drive/folders/1mg6VXpkvEmyL1scaelX5FnW8uw1gk9iq?usp=sharing)

python/code_133/2.jpg

23.6 KB
Loading

python/code_133/2color.jpg

63.9 KB
Loading

python/code_133/4.jpg

114 KB
Loading

python/code_133/4color.jpg

131 KB
Loading

python/code_133/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## OpenCV DNN 图像颜色化模型使用
2+
3+
OpenCV DNN在4.0还支持灰度图像的彩色化模型,是根据2016年ECCV的论文而来,基于卷积神经网络模型,通过对Lab色彩空间进行量化分割,映射到最终的CNN输出结果,最后转换为RGB彩色图像。
4+
5+
相关论文详见:
6+
7+
Arxiv: [https://arxiv.org/pdf/1603.08511.pdf](https://arxiv.org/pdf/1603.08511.pdf)
8+
9+
OpenCV DNN使用该模型时候,除了正常的Caffe模型与配置文件之外,还需要一个Lab的量化表。
10+
11+
原图:
12+
13+
<img src='2.jpg'>
14+
15+
Colorization:
16+
17+
<img src='2color.jpg'>
18+
19+
20+
原图:
21+
22+
<img src='4.jpg'>
23+
24+
Colorization:
25+
26+
<img src='4color.jpg'>

python/code_133/opencv_133.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import numpy as np
2+
import cv2 as cv
3+
4+
W_in = 224
5+
H_in = 224
6+
modelTxt = "../model/color/colorization_deploy_v2.prototxt";
7+
modelBin = "../model/color/colorization_release_v2.caffemodel";
8+
pts_txt = "../model/color/pts_in_hull.npy";
9+
10+
# Select desired model
11+
net = cv.dnn.readNetFromCaffe(modelTxt, modelBin)
12+
pts_in_hull = np.load(pts_txt) # load cluster centers
13+
14+
# populate cluster centers as 1x1 convolution kernel
15+
pts_in_hull = pts_in_hull.transpose().reshape(2, 313, 1, 1)
16+
net.getLayer(net.getLayerId('class8_ab')).blobs = [pts_in_hull.astype(np.float32)]
17+
net.getLayer(net.getLayerId('conv8_313_rh')).blobs = [np.full([1, 313], 2.606, np.float32)]
18+
19+
frame = cv.imread("4.jpg")
20+
h, w = frame.shape[:2]
21+
img_rgb = (frame[:,:,[2, 1, 0]] * 1.0 / 255).astype(np.float32)
22+
23+
img_lab = cv.cvtColor(img_rgb, cv.COLOR_RGB2Lab)
24+
img_l = img_lab[:,:,0] # pull out L channel
25+
(H_orig,W_orig) = img_rgb.shape[:2] # original image size
26+
27+
# resize image to network input size
28+
img_rs = cv.resize(img_rgb, (W_in, H_in))
29+
img_lab_rs = cv.cvtColor(img_rs, cv.COLOR_RGB2Lab)
30+
img_l_rs = img_lab_rs[:,:,0]
31+
img_l_rs -= 50 # subtract 50 for mean-centering
32+
33+
# run network
34+
net.setInput(cv.dnn.blobFromImage(img_l_rs))
35+
ab_dec = net.forward()[0,:,:,:].transpose((1,2,0))
36+
37+
(H_out,W_out) = ab_dec.shape[:2]
38+
ab_dec_us = cv.resize(ab_dec, (W_orig, H_orig))
39+
img_lab_out = np.concatenate((img_l[:,:,np.newaxis],ab_dec_us),axis=2)
40+
41+
img_bgr_out = np.clip(cv.cvtColor(img_lab_out, cv.COLOR_Lab2BGR), 0, 1)
42+
print(img_bgr_out.shape)
43+
frame = cv.resize(frame, (w, h))
44+
cv.imshow('origin', frame)
45+
gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
46+
cv.imshow('gray', gray)
47+
#cv.imwrite('gray.jpg', gray)
48+
# fix 4.0 imshow issue
49+
cv.normalize(img_bgr_out, img_bgr_out, 0, 255, cv.NORM_MINMAX)
50+
cv.imshow('colorized', cv.resize(np.uint8(img_bgr_out), (w, h)))
51+
cv.imwrite('4color.jpg', cv.resize(np.uint8(img_bgr_out), (w, h)))
52+
cv.waitKey(0)
53+
cv.destroyAllWindows()

python/code_134/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## OpenCV DNN ENet实现图像分割
2+
3+
OpenCV DNN支持ENet网络模型的图像分割,这里采用的预先训练的ENet网络:
4+
5+
[GitHub - e-lab/ENet-training](https://github.com/e-lab/ENet-training)
6+
7+
基于Cityscapes数据集的训练预测结果
8+
9+
<img src=street.jpg>
10+
11+
<img src=enet_result.png>

python/code_134/enet_result.png

397 KB
Loading

python/code_134/opencv_134.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
# load CNN model
5+
bin_model = "../model/enet/model-best.net";
6+
net = cv.dnn.readNetFromTorch(bin_model)
7+
# read input data
8+
frame = cv.imread("street.jpg")
9+
frame = cv.resize(frame,(0,0), fx=0.5, fy=0.5)
10+
blob = cv.dnn.blobFromImage(frame, 0.00392, (512, 256), (0, 0, 0), True, False);
11+
cv.imshow("input", frame)
12+
h, w, c = frame.shape
13+
14+
# Run a model
15+
net.setInput(blob)
16+
score = net.forward()
17+
# Put efficiency information.
18+
t, _ = net.getPerfProfile()
19+
label = 'Inference time: %.2f ms' % (t * 1000.0 / cv.getTickFrequency())
20+
score = np.squeeze(score)
21+
score = score.transpose((1, 2, 0))
22+
score = np.argmax(score, 2)
23+
mask = np.uint8(score)
24+
mask = cv.cvtColor(mask, cv.COLOR_GRAY2BGR)
25+
cv.normalize(mask, mask, 0, 255, cv.NORM_MINMAX)
26+
cmask = cv.applyColorMap(mask, cv.COLORMAP_JET)
27+
cmask = cv.resize(cmask, (w, h))
28+
dst = cv.addWeighted(frame, 0.7, cmask, 0.3, 0)
29+
cv.putText(dst, label, (50, 50), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
30+
cv.imshow("dst", dst)
31+
cv.imwrite("enet_result.png", dst)
32+
cv.waitKey(0)
33+
cv.destroyAllWindows()

python/code_134/street.jpg

180 KB
Loading

python/code_135/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## OpenCV DNN 实时快速的图像风格迁移
2+
3+
OpenCV DNN模块现在还支持图像风格迁移网络模型的加载与使用,支持的模型是基于李飞飞等人在论文《Perceptual Losses for Real-Time Style Transfer and Super-Resolution》中提到的快速图像风格迁移网络,基于感知损失来提取特征,生成图像特征与高分辨率图像。
4+
5+
整个网络模型是基于DCGAN + 5个残差层构成,是一个典型的全卷积网络。
6+
7+
模型下载地址:
8+
9+
[GitHub - jcjohnson/fast-neural-style](https://github.com/jcjohnson/fast-neural-style)
10+
11+
这个网络可以支持任意尺寸的图像输入,作者提供了很多种预训练的风格迁移模型提供使用,我下载了下面的预训练模型。:
12+
- composition_vii.t7
13+
- starry_night.t7
14+
- la_muse.t7
15+
- the_wave.t7
16+
- mosaic.t7
17+
- the_scream.t7
18+
- feathers.t7
19+
- candy.t7
20+
- udnie.t7
21+
22+
这些模型都是torch框架支持的二进制权重文件,加载模型之后,就可以调用forward得到结果,通过对输出结果反向加上均值,rescale到0~255的RGB色彩空间,即可显示。
23+
24+
Tyep | Image
25+
---|---
26+
Feathers.t7 | <img src=result_6.png>
27+
Candy.t7 | <img src=result_7.png>

python/code_135/opencv_135.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
base_dir = "../model/fast_style/"
5+
styles = ["composition_vii.t7", "starry_night.t7", "la_muse.t7", "the_wave.t7",
6+
"mosaic.t7", "the_scream.t7", "feathers.t7", "candy.t7", "udnie.t7"]
7+
index = 6
8+
net = cv.dnn.readNetFromTorch(base_dir + styles[index])
9+
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV);
10+
cap = cv.VideoCapture(0)
11+
while cv.waitKey(1) < 0:
12+
hasFrame, frame = cap.read()
13+
if not hasFrame:
14+
cv.waitKey()
15+
break
16+
cv.imshow("frame", frame)
17+
inWidth = 256
18+
inHeight = 256
19+
h, w = frame.shape[:2]
20+
inp = cv.dnn.blobFromImage(frame, 1.0, (inWidth, inHeight),
21+
(103.939, 116.779, 123.68), swapRB=False, crop=False)
22+
23+
# 执行风格迁移
24+
net.setInput(inp)
25+
out = net.forward()
26+
print(out.shape)
27+
t, _ = net.getPerfProfile()
28+
freq = cv.getTickFrequency() / 1000
29+
label = "FPS : %.2f" % (1000 / (t / freq))
30+
31+
# 解析输出
32+
out = out.reshape(3, out.shape[2], out.shape[3])
33+
print("ddddddddd", out.shape)
34+
out[0] += 103.939
35+
out[1] += 116.779
36+
out[2] += 123.68
37+
out /= 255.0
38+
out = out.transpose(1, 2, 0)
39+
print("new shape", out.shape)
40+
#out = np.random.random((256,256,3))
41+
out = np.clip(out, 0, 1)
42+
out = (255 * out).astype('uint8')
43+
# rescale与中值模糊,消除极值点噪声
44+
#cv.normalize(out, out, 0, 255, cv.NORM_MINMAX)
45+
46+
out = cv.medianBlur(out, 5)
47+
48+
# resize and show
49+
result = np.uint8(cv.resize(out, (w, h)))
50+
cv.putText(result, label, (5, 25), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
51+
cv.imshow('Fast Style Demo', result)
52+
cv.imwrite("result_%d.png"%index, result)
53+
54+

python/code_135/result_6.png

407 KB
Loading

python/code_135/result_7.png

418 KB
Loading

python/model/enet/enet-classes.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Unlabeled
2+
Road
3+
Sidewalk
4+
Building
5+
Wall
6+
Fence
7+
Pole
8+
TrafficLight
9+
TrafficSign
10+
Vegetation
11+
Terrain
12+
Sky
13+
Person
14+
Rider
15+
Car
16+
Truck
17+
Bus
18+
Train
19+
Motorcycle
20+
Bicycle

python/model/enet/model-best.net

3.08 MB
Binary file not shown.

0 commit comments

Comments
 (0)