- 在线时间
- 1630 小时
- 最后登录
- 2024-1-29
- 注册时间
- 2017-5-16
- 听众数
- 82
- 收听数
- 1
- 能力
- 120 分
- 体力
- 571622 点
- 威望
- 12 点
- 阅读权限
- 255
- 积分
- 176709
- 相册
- 1
- 日志
- 0
- 记录
- 0
- 帖子
- 5313
- 主题
- 5273
- 精华
- 3
- 分享
- 0
- 好友
- 163
TA的每日心情 | 开心 2021-8-11 17:59 |
|---|
签到天数: 17 天 [LV.4]偶尔看看III 网络挑战赛参赛者 网络挑战赛参赛者 - 自我介绍
- 本人女,毕业于内蒙古科技大学,担任文职专业,毕业专业英语。
 群组: 2018美赛大象算法课程 群组: 2018美赛护航培训课程 群组: 2019年 数学中国站长建 群组: 2019年数据分析师课程 群组: 2018年大象老师国赛优 |
python将红底证件照转成蓝底* i/ f5 w; k% H; |% l3 R$ r
前言3 |; @6 h+ k; Y2 R5 B6 Q/ x' P& [
emmm…快开学了,手头只有红底证件照,但是学院要求要蓝底,这可咋办呢。懒得下ps了。自己撸起来吧。2 ?0 T+ x5 \5 Z- Z4 \& ?
3 n: W6 f" f2 ~( p8 j& J
* B+ U/ [* F( C0 Y _( r: m% G; \7 O3 i, M' i9 k
7 P/ B$ G) i# f4 F
方法一: lableme6 L3 B0 ]6 Y0 Z+ t9 ~8 ?
lableme标注完后。得到一个json文件,然后将这种json文件转成掩码图.
* F/ W; x* Q* g _- }
2 L K- c5 y6 J1 M# 代码来自 https://blog.csdn.net/hello_dear_you/article/details/120130155
% A5 }! ]9 _: |- U5 i: ~* _import json* Y' C# _% Y' a5 V9 {! V
import numpy as np
2 }' Z$ M1 l a Bimport cv25 k# Z; R, H* B8 E
# read json file
4 c$ }% ^& ]% t, v2 Z1 Y. M$ l! R% Owith open("origin_json/mypic.json", "r") as f:
1 h" W4 ?0 H/ y/ M. U$ S \ data = f.read()
; H- a: K9 Y- I" V% i9 J# m. r# U, Q8 Y5 _
# convert str to json objs
) b1 G% W2 h) fdata = json.loads(data). H7 c. N& l. `( J
1 E, Y. J. x: {- S3 T4 `# get the points
( x( W; T, D& l8 k2 H( E0 zpoints = data["shapes"][0]["points"]
) A. Y, f9 J% N* rpoints = np.array(points, dtype=np.int32) # tips: points location must be int32
: x" i% n; ]8 U" Z' _! Y7 C! y8 h! E0 S% p8 h
# read image to get shape& s7 d5 h3 y+ R
image = cv2.imread("origin_png/person.jpg")" A; `' e7 `9 V2 l% n) j
* U- Z3 X+ X, X4 f
# create a blank image8 t4 w# D7 ~. A5 @* a9 B P
mask = np.zeros_like(image, dtype=np.uint8)5 \, Y. Y2 d" r
2 o& ^4 H, ~: Q0 s# fill the contour with 255
, G9 L6 m- E5 D( V& o( J& ~/ F& H' Ccv2.fillPoly(mask, [points], (255, 255, 255))4 B& Q' p- A6 X; p3 z( ?2 _4 R. Y2 }
9 O2 G1 |3 H$ ?
# save the mask
' X( R/ Y4 Y% Jcv2.imwrite("mask/person_mask.png", mask)
! w+ D5 |' g8 S6 m+ t K) L. G9 W) C1
" o6 J, r, h v2 }& v8 Q8 ^2
* R H: l T6 f# }% |3! Q) M" W- W. n$ v3 Z& f& r" C) k- l8 [
4
4 z6 k& f. w' ^' @& r54 J& b. p9 s P: S( E8 \% s }1 A: S
6
" A* ~; t9 p! O5 Y7
* v) s' T7 L" z, e9 c) c; \87 A7 _& k6 q2 h# c. v
9
6 `& m# ~" k; U5 b: a% s/ v105 A8 k; G8 S. C: L% e" `
11* }' t& _4 R' w7 |+ }
12
5 k4 M, M8 {+ u0 N13
" M5 i- u+ M: l+ m y: o! t7 e14+ e6 [* d7 }& W- H: r
153 ?3 A, P- h0 ~ Z; W1 R+ F; v
16& l7 i! p# ~3 r* P6 x
17
9 L$ g2 F. G( {2 K1 l1 i2 e: h183 a* u" J; |; R
19
/ U y! i c% C. f1 ?; d20
( ?6 S) ` N+ }* C21
* f7 E7 K$ o4 L, |! L* V ~) s22# I! t8 B7 d" w' s$ t
23
" p: ~9 h: h( B24( ]% b. J0 ^. C( _
25) c! P& d! a: u6 Q7 S
26
/ F$ Z6 a) \8 R2 V: x8 ^大概是这样:
! @' h5 [ n9 q4 i; V" k/ E( _$ z/ o0 L5 b
x+ l1 {" ?; A4 Y
然后利用这个mask生成图片 S# j, p7 b6 a1 _& o
1 C7 E7 M' ?& z8 u( U# c) |* z
# 参考自: https://www.jianshu.com/p/1961aa0c02ee! j5 a: t& F; C$ A$ P2 p v
import cv22 A1 t1 K* R2 X6 K' i- c- c
import numpy as np
: S5 U; I" i! |, X' J1 x1 g
1 X. \6 {2 [2 Z6 g A* \7 u: J
# D; @. }& e0 h( Z ~origin_png = 'origin_png/person.jpg'
/ S* c- c( z# z# maskPath = 'mask/person_mask.png'% w4 ]. `# {+ H3 h
maskPath = 'mask/bmv2.png'' c0 R) t3 }. R. M' M T" ~
result_png = 'result_png/result_png.png'
/ q+ I2 @0 O- p$ n! B. ~% @5 a* y3 m. \% m) P7 t
# m; A* A' A. w6 H
maskImg = cv2.imread(maskPath)+ Q C9 K6 m% u2 S5 N9 [# ?9 X$ |
img = cv2.imread(origin_png)0 z5 G3 q" H+ k+ }$ @
assert maskImg.shape == img.shape, 'maskImg.shape != origin_png.shape'
- n4 p& J' k# P4 W% O" p7 K- G
5 O7 ]. o7 t% z# U! L( l+ O+ J2 G% vh, w = img.shape[0], img.shape[1]
6 [9 V* u4 m/ X) _0 lprint('图片宽度: {}, 高度: {}'.format(h, w))
$ i! @! Z- }; |$ i5 B
8 h0 {9 [$ Q% Argb = (19,122,171)
3 J$ b- M0 P, z" g+ N w2 i2 _bgr = (rgb[2], rgb[1], rgb[0])
2 b$ i9 o6 K) ^# (B, G, R)
9 G1 m5 T# m' j$ K/ ^for i in range(h):! F5 D6 E: \; a8 X
for j in range(w):- Q$ O' W( k' C2 r: o- ?
if (maskImg[i, j] == 0).all():2 H$ q2 R$ s) N I& w. y) e
img[i, j] = bgr) t6 C6 {3 x4 [7 [: g
cv2.imwrite(result_png, img)4 D7 ?! G8 O f( O( b3 u
print('图片写入 {} 成功'.format(result_png))
0 a" ]- H0 W3 [5 u0 U11 R: R2 A0 Q" Z/ W f! b, w
2' p& y/ c: `" Y7 L. l! w
3" B& ~( K5 ^$ R/ y3 o3 U2 X
4, e8 B/ H6 _8 a7 M3 Y
56 t; h- g0 f* A _! X
6
+ _, O# K! W, u; J, T& Z74 X! d5 R' d7 }9 s/ O" S [
8
4 j! B) h! L; Y* W& B% Z# T9+ l! i# R( o* y8 K, }
10
: a" G+ i2 y% \" `11
& f1 p8 V" r' X/ v12
" X) H7 l4 X1 v- O13+ `6 e% ~/ P5 T3 X! y7 M7 r
14
& v T2 Q: e! r- O159 @0 y+ r& H/ m4 F# i! D# j
16
+ R% f8 f! Z3 X5 d8 v' C1 Z; n178 N9 Y+ X8 O$ Y1 t2 a! u
18
$ l/ m6 Z5 n. c19
5 w; ?7 V" Z% `" M4 f; u205 J* H8 d1 F$ e7 p9 I' i5 G
212 U* A' t8 e' P9 j. V+ a
22
9 R) [8 h8 [8 j! f) C" S23 |3 l: A& S7 M2 r
24' W0 {; m' `. s6 ^
25" [0 U' r3 N( m, ~9 p- c4 e
26
6 \+ Z: N& j( x27
+ c( A! e( d' ~" f4 F" w* T/ ]由于人长得一般,就不放图了…
: S+ G5 a$ C, v9 f/ C$ y# O# @, F0 y1 I2 y9 S* E4 k
缺点:
$ q; j) x3 P% B) j* _, Llableme标注时挺费力,并且难以避免人与背景边缘会有残留红色像素的情况。, F. H8 V! N/ O
* G1 v: m! m( m
4 k# W- x) l& {
1 x) C" m8 X. Q, _% x5 R; t. @3 z: ^
3 x6 a" d+ @. X7 V5 p5 t8 @
方法二: 阈值$ h) K2 h5 G3 z ?* S$ v
该方法通过比较像素的RGB与背景的RGB来区分是否为图像背景。
/ z* o1 g1 `7 ]' w2 P6 H6 G7 \* U: `6 A2 {$ L) \! {
Opencv
0 S4 A* N+ n9 [" T1 t/ o- Timport cv2
& x! J: V, c4 q, i) O( m7 pimport numpy as np) {: I! ~6 p- a. f# e1 [) V$ g
& k, W) j) @0 M) ]9 H0 N
6 y! v" [/ {9 X: G, y* l( G
def mean_square_loss(a_np, b_np):
' \& a# H" p% I1 } sl = np.square(a_np - b_np)4 a- h+ D* j# s# s3 r/ J2 N9 W7 Y. w
return np.mean(sl)
1 |" G. m: ^) l/ M6 |
2 }: u5 A" [' j: R6 e0 b/ R3 k) Z2 q& @/ H# h6 s; ~) Z
def change_red2blue(origin_png, result_png):
, Q6 p8 _0 ^9 _7 _ img = cv2.imread(origin_png)& {5 L0 P5 |# M: _! O7 D
: B- i6 v3 r# T' D. p/ ~6 p+ K
h, w = img.shape[0], img.shape[1]4 R* o- t) }3 t4 b
print('图片宽度: {}, 高度: {}'.format(h, w))* q1 |/ R, \4 q/ \/ A6 `9 y# z
; k, s0 o, v. v& C origin_rgb = (168,36,32) # 可以用浏览器啥的控制台工具提取出背景的rgb值
5 q6 B# t5 K8 T& }/ O5 W origin_bgr = (origin_rgb[2], origin_rgb[1], origin_rgb[0])
$ u$ \! N u- r | Y: }- v target_rgb = (19,122,171) # 蓝底RBG" a6 `9 n6 S+ w4 {. j1 w
target_bgr = (target_rgb[2], target_rgb[1], target_rgb[0])1 V, Z8 S8 {. a% d
9 Y* W' N: U6 k6 u; n7 k for i in range(h):
. `1 l# Q5 [. Q' b* b for j in range(w):
5 ~7 P# B; ^- c& M # (B, G, R)! s3 ?" X1 V/ m
if mean_square_loss(img[i, j], origin_bgr) < 50:) `% S+ r4 z( a
img[i, j] = target_bgr
, Z! {/ Z' p. h& N1 A: E$ l+ b6 A* z# H
cv2.imwrite(result_png, img) ]0 Q. Z& p. o( F. h
print('图片写入 {} 成功'.format(result_png))% v1 Y+ ]9 P3 `7 w L! \
: i) @. U8 o( n: u$ P8 \
" D' M, [! I1 f5 K0 Y4 c% cif __name__ == '__main__':
5 N/ h" l6 ~$ y) D/ T, E0 _ # origin_png = 'result_png/result_png.png'6 T3 G# B0 {# X& T! R' @
origin_png = 'origin_png/person.jpg'* Y1 Q1 J4 e/ w* [5 `, K
result_png = 'result_png/result_refine.png'# J* ?6 K# d. R6 P) z3 j9 q
change_red2blue(origin_png, result_png)
2 X( F/ T" B% `6 M% n1' w/ @5 `# b$ R1 N+ }* {
27 P* j4 G. K, g1 v5 u3 n, @2 h0 [* B! ~
3
# h$ F/ X. i0 i; v+ Z4
2 q# ], W, h2 O1 R$ @/ Q: o$ y3 e. y5
; Y+ d5 Z, {# y& @& }6
9 W7 A9 `1 ~+ f' M6 U0 u7
l) x. S" _% V) s0 e% `5 {84 u" I, Q+ i' r% [
9
' X8 b6 j: _7 L' }( n10- }4 K" V4 G% l3 h8 v3 F8 J
11
9 z3 B; W- ]5 p- y( N12
) n1 Q9 w5 x4 C' q3 U13
- {8 D+ n3 ^. E! P7 [8 u9 M& L14+ _6 E1 o9 |. }- q( G
157 \% {# t6 |- E& H7 G- B: g
16! {9 c' ^* I7 n V" i, i' A8 [- B
17; O: ?" v3 z8 u: t6 m/ P! r
18
: B% o ]- j3 d v$ D/ I19
* @" i1 F& n5 u" e2 ]! ~' x) P20$ Y( o" f3 \! t: @
21' S- y6 B3 I/ X0 \( D+ x
221 E4 o5 j5 A8 y9 g a2 R: m
23" j1 S2 A( e' Y. x* l2 [0 c
240 ^0 g$ J, z4 n8 J$ g J+ W
257 W1 H0 e9 `) V6 |% D; y e
26) l: Y! A; q/ p' j0 [& m8 I- m' q
272 W1 k0 c* I0 L# C; J
28) {) o1 V# p- x6 `& P
296 S; n" i6 h7 G4 a
30& Z' _$ B# D6 A) r& p
31
" L$ F) ~) k, P9 M32
5 g' l. ?( K' ^0 y! ?2 B. h$ h m4 I& X33
! q* ?* g+ n7 Q9 a34
! K1 y( g, n1 ?. H35* a/ z, R, h% `
结果人与背景边缘仍会存在红色像素残留7 D- I d& z' q
) }6 ^/ @/ m! M# ^) n- |
9 F$ P3 ~% T. x* I
* E0 j e5 b/ o1 O" M9 k, v
+ X+ f; |* l; z% N k. R& p
$ T" ?; n! e' n8 jPIL
% H Q3 y9 l! M+ @; }9 q3 @% C8 gfrom torchvision.transforms.functional import to_tensor, to_pil_image! r. h, i2 `% I4 V0 G# ~/ l
from PIL import Image' n) v( j p$ g7 R
import torch1 {4 p: e- \6 B% v- ]0 i# J7 G1 s) `
import time
3 N7 S% E0 w; A- V {
3 L5 m4 U6 [7 R: L1 G8 _, ?# a* }1 f, j% r; D8 ]- C6 G
def mean_square_loss(a_ts, b_ts):
; x" n5 k/ |) T" a # print(a_ts.shape)7 v# U2 ^& M7 t- {. l2 T
# print(b_ts)
' Q- I; ? F4 Y sl = (a_ts - b_ts) ** 2
' Y2 L6 r4 g/ b' H3 _3 s$ U, W d return sl.sum()
! M! ?( c" q; K) R, `) f: \ Q3 T: K0 G: M) j/ T: ]& E
/ [5 L4 n, O4 [) d) O4 a5 t) {) z
def change_red2blue(origin_png, result_png):) @( X# o: P# I
src = Image.open(origin_png): R& O4 d* S# C+ f
src = to_tensor(src)9 i* ?6 H [/ M4 D
# print(src.shape) # torch.Size([3, 800, 600])
8 O8 E% E) {) O4 ? # channel: (R, G, B) / 255, `% ?4 {- I7 v
h, w = src.shape[1], src.shape[2]
4 y, O5 ]1 z3 E. M: W: ^$ N w3 m Z
# p2 E2 |: E# N3 v% X4 {- g pha = torch.ones(h, w, 3)+ j' d; T+ {+ M7 r
, J8 B# W H; ]% N8 z
bg = torch.tensor([168,36,32]) / 2559 a# ^/ C. l5 ~" }1 o' p) f3 h. c
target_bg = torch.tensor([19,122,171]) / 255
/ y% t+ J- `2 c
5 C! R6 C% S8 O$ i$ Y' x' r& _ # C, H, W -> H, W, C( Z$ D. P q$ ^& H
src = src.permute(1, 2, 0)
( p+ K9 z; f3 C. Y9 _; S for i in range(h):
8 t B! k0 `' L. v for j in range(w):2 X9 h! C$ Z; E5 x
if mean_square_loss(src[j], bg) < 0.025: # 0.025是阈值,超参数
. t9 h, q) y8 I/ i" T pha[j] = torch.tensor([0.0, 0.0, 0.0])
9 o9 T. R* J D# v* Y; _
0 C% \* `" Y$ P& m # H, W, C -> C, H, W
# z8 ]* W' N' O* M, ]1 b: l src = src.permute(2, 0, 1)
4 i4 {# \. _' p% ^ pha = pha.permute(2, 0, 1)! T* p+ T1 `2 X/ O" u' H
com = pha * src + (1 - pha) * target_bg.view(3, 1, 1)
9 a6 G8 A1 c+ ?. `, j) u U6 s to_pil_image(com).save(result_png)
: Q* _: ]# e+ s0 `% z0 A) ?: d }" a* x+ o( p/ V; U
) T, Z5 e; x5 S& \$ k8 r; p m
if __name__ == '__main__':
% `; z) j6 V8 W/ o+ w origin_png = 'origin_png/person.jpg'
- Q2 e( A2 b r& j. ` result_png = 'result_png/com.png'
0 l) ]& x4 | e3 x; G$ d8 B start_time = time.time()0 R2 ]- ~! C& P5 R6 i5 n+ A
change_red2blue(origin_png, result_png); Y ], h' L* f1 M* O' a
spend_time = round(time.time() - start_time, 2)8 T v* d" T' V7 S/ w
print('生成成功,共花了 {} 秒'.format(spend_time))
2 Q! T5 x4 O2 x9 M- b- E; }1
! Z. l! I: W" y- O b2, f. R; B2 L9 I
3
3 Y1 r m/ u# L# p4$ G0 i# n) N8 O1 j. x6 N
5
6 K/ M# S& q. Z7 F0 B( o+ c6
6 H9 K e2 C" |9 N- e# J7
- d6 R% ^5 W/ Y( S, D8
% p# m; e, S4 B1 s! ]9
5 x* N. C7 { I, I+ u) s; f10. M; L( A; q5 `+ P
11
) N: f+ V/ M6 I( ^: Z7 L/ J3 n12 @, i# z3 G# M5 a$ b
13
) B* ~) G/ q2 P& g14 D/ n# @8 R6 m4 f0 D5 k* b% V
15
: Z6 x% C* B$ i16
" m9 _/ M3 [2 V3 x# D7 C4 g17
; K% D) c- p9 d5 c. _( C18* n$ P4 X6 Y8 R Y. I
190 Q; C) D/ C& i1 ?
20
% Z; K% [% \4 y. w: s21
' u) y6 @- J/ @22
3 _' q1 E0 ^: X( o23
0 g; r# a8 s+ _$ z- I: S3 i2 N' M244 X$ R. k9 w0 F! r8 C
25
9 ^7 X3 X9 U1 k: I7 }8 ?; U* _26: i" w% ?' f; y1 W
27
* P! w/ ?. n C( \( J28
5 a3 c6 p5 [4 \29
; Q* d6 ^: K+ [2 A4 u30
* ^6 W3 N3 ?; }0 c0 ^4 h- X4 P31; V" d! s( D; V5 y
32
7 j5 |: k. k$ |: o# y' q33- g- @6 v q; O- Y, e
349 J, G' u; [, ?: H8 S- ^) [/ T
35: {0 V2 O* [8 ^: T. @
36
) E- o- s# J( ^) _. c t37
. r4 t2 d! ^2 O6 U6 ]- c38! v/ Q4 a% G$ D8 G
396 g% A. V) c+ E* z* [+ c# g; Z
40
: ]( ]2 [/ H3 Z' c! o q. B6 o41' R2 M0 A+ W3 y/ d# c3 O& J% G
42
; c" Z2 S7 P* s8 P8 B2 G" ~8 ]43! y4 W3 O% k7 R/ R1 @
44
, g: S/ j$ d3 B. |9 d45
$ r j, `; n5 }8 B7 _46: _2 V8 N1 L: S1 }2 |: X8 ]: u
该方法质量较好,但一张图片大概需要12秒。! Y' V2 G ~3 z- q4 o5 C# j' D& S
+ }! f p% @1 i3 Y W, o* u
0 L: [7 G+ n; C* u4 {8 e- m0 B1 U( I1 z+ A$ h
方法四: Background MattingV2- J' P4 P. M$ s# K
Real-Time High-Resolution Background Matting
. h3 Z$ D; Q x$ kCVPR 2021 oral, b+ q* J: x- m) v5 J
( j) L. X, Y" b4 y, R# M% z论文:https://arxiv.org/abs/2012.07810
1 S" E& K8 G# r代码:https://github.com/PeterL1n/BackgroundMattingV2/ |/ }3 \- Z6 p4 `' Q' G4 M) q6 v
) [2 g3 t i2 x7 z" I; a0 J/ Sgithub的readme.md有inference的colab链接,可以用那个跑
8 m/ z; r1 Z& x: F* h4 ?$ `; Q" X) v9 N3 O4 [
由于这篇论文是需要输入一张图片(例如有人存在的草地上)和背景图片的(如果草地啥的), 然后模型会把人抠出来。7 g0 y& U. D6 G3 u' E
: [" ?. g7 M7 s2 V9 j2 m
于是这里我需要生成一个背景图片。' F& P+ z- f9 G6 R- I4 ~4 s
首先我先借助firefox的颜色拾取器(或者微信截图,或者一些在线工具,例如菜鸟工具),得到十六进制,再用在线转换工具转成rgb。
+ G! b! [% @/ x: G2 ?+ Z( u
: y% Z L% ]. W% x5 J然后生成一个背景图片。7 E" P& O$ v+ k: L) m* i+ g
e% Y, i( n2 N; t- v
import cv22 t$ n/ C0 i! G$ i. M
import numpy as np
9 T# ?6 j8 S* X0 q$ y: e3 j& ?8 g0 s, Q: {4 w* O! c- m! \
/ K+ k+ \# Z- G" c6 J, Iimage = cv2.imread("origin_png/person.jpg"); W e, j" Q( b0 J
origin_rgb = (168,36,32) # 可以用浏览器啥的控制台工具提取出背景的rgb值0 M- `: o: t) g$ E D
origin_bgr = (origin_rgb[2], origin_rgb[1], origin_rgb[0])
5 a% q/ g8 @- Y/ z `5 Yimage[:, :] = origin_bgr4 v; f0 Q+ k' |2 ^- D
- X( N$ W, C1 G; P# z7 p9 Y
cv2.imwrite("mask/bg.png", image)1 Y( b* ^7 n* H+ _
1$ w. l0 j% c0 Y+ Y1 n8 x9 Z7 d
2$ n1 ^" m0 \" P& ~" |0 S* k* t
3# T" e2 ]* r* o$ |1 w4 c
4 \! p2 u* V6 ]( \
5# }! u# ^& K& s2 A% [- ~& F. M! P
69 w. i# a: X9 ` C2 j
7+ W8 T [' ?; }- k
8, l9 X: a& C+ t, [( ~6 ]! h
9
; U8 F. C% D; j3 f0 q& F( T* ?10
( T* N$ i( `* b- q: u. ^! s- Q2 {7 j8 K
2 _, D- m# |8 O" s需要上传人的照片和背景照片, 如果名字和路径不一样则需要修改一下代码& u" u) D R3 _: l6 k% ?/ q- \. `
* J$ @3 P- S% j5 i3 I% u Jsrc = Image.open('src.png')
& S4 g6 i2 |" Kbgr = Image.open('bgr.png')1 A/ ~7 ?$ N. y- }9 D
1- i: w6 b$ E, R( C
2- d0 F- e% T7 G( o! \8 L
另外原论文是边绿底,要变蓝底,白底,红底则可以修改RGB值,举个例子,原来是这样的(绿底, RGB120, 255, 155)
/ X4 L4 D! y. w! t, m \
+ k5 Z ~/ h4 N6 G4 H, rcom = pha * fgr + (1 - pha) * torch.tensor([120/255, 255/255, 155/255], device='cuda').view(1, 3, 1, 1) y# G5 r/ i/ x, d& N
1
7 I) T3 U- N8 @9 c: {+ G' r
6 c5 W- ~3 H8 s9 q- [那么加入我要换白底(255, 255, 255),就是; u% H' K+ J) ~8 g0 r
2 c' I1 Z9 k% W! H8 T+ K; ]
com = pha * fgr + (1 - pha) * torch.tensor([255/255, 255/255, 255/255], device='cuda').view(1, 3, 1, 1)" e5 _) X% h; ?# O
1
, k. i& Q5 R1 z) F1 q) }, @
5 _- E) i$ J; z& D1 C# s( U假如像我换蓝底(19,122,171)具体深浅可以调节一下RGB,就是. o; m+ `2 Z; I, x
7 K- F# K( p+ m! wcom = pha * fgr + (1 - pha) * torch.tensor([19/255, 122/255, 171/255], device='cuda').view(1, 3, 1, 1)) r4 b* o; l/ O" g; j
1- o& \; z% c* u: D0 ]" i R
总结: 其实这种方法从 任何颜色的照片 都可以 换成任何颜色的底。只要换下RGB.0 L/ ]& B2 r8 C2 _) t
9 A' s% U5 `+ c" L5 E5 W6 j然后就输出图片了。可以看到效果相当好。不愧是oral。
A- x+ d( z7 L) J9 i* o
' s8 J. Z7 s! j
" D3 f" t# U% M$ {! d原论文可以实现发丝级效果
! j& |/ R, M+ F; R% |* R" z& s" k
: l5 y! M8 ^" C7 J5 `& D
) H, r& I: g7 Z, B( a: T$ E4 Z1 m4 n# @+ @4 e/ o
$ j& J% K, j3 t' g: Z' M2 _
报错解决方案
( I$ L7 t# L6 o7 r2 x- ^$ Ican’t divided by 4 / can’t divided by 16
% e' a3 I6 t0 b% x; l0 f由于该骨干模型可能进行4倍或16倍下采样,因此如果您的证件照不是该倍数的话,有两种选择方案。一种是padding, 填充后再送入模型,然后出结果后再用clip函数裁剪。另一种方式是resize, 给resize到规定倍数的宽和高。
' r1 m& L# |2 M. G/ n- o% ]9 _这两种方案需要的代码都可以从这篇博文找到: python图像填充与裁剪/resize) T4 J6 \ n4 G# O/ i6 q
————————————————
$ P( N+ r4 b& M+ ]) @. }& T1 Z版权声明:本文为CSDN博主「Andy Dennis」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。* ]; L! f7 K( ~6 e7 J
原文链接:https://blog.csdn.net/weixin_43850253/article/details/126376767+ d4 D3 {0 X! S( \3 ]* u
8 H( e: L$ l9 Q; p. }% o2 z
1 a i- p2 |6 N3 E* I8 b: _# d |
zan
|