QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 2208|回复: 0
打印 上一主题 下一主题

python将红底证件照转成蓝底

[复制链接]
字体大小: 正常 放大
杨利霞        

5273

主题

82

听众

17万

积分

  • TA的每日心情
    开心
    2021-8-11 17:59
  • 签到天数: 17 天

    [LV.4]偶尔看看III

    网络挑战赛参赛者

    网络挑战赛参赛者

    自我介绍
    本人女,毕业于内蒙古科技大学,担任文职专业,毕业专业英语。

    群组: 2018美赛大象算法课程

    群组: 2018美赛护航培训课程

    群组: 2019年 数学中国站长建

    群组: 2019年数据分析师课程

    群组: 2018年大象老师国赛优

    跳转到指定楼层
    1#
    发表于 2022-9-7 11:41 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    python将红底证件照转成蓝底% C9 c+ F2 t& e% m- w( W! l
    前言
    ; u! \1 `! A7 Z9 y1 v/ Lemmm…快开学了,手头只有红底证件照,但是学院要求要蓝底,这可咋办呢。懒得下ps了。自己撸起来吧。
    / |3 }; ^; o6 L, ~4 H: K* ]7 J
    * x2 y' N1 H5 y( J6 r! u
    4 k0 G( Y+ c; B% L# C! b9 H1 M- y7 x9 p$ n
    ! e/ g/ n4 M, I+ B
    方法一: lableme2 x; B8 h/ k; H0 ]& C7 e, B( T6 O! V7 [
    lableme标注完后。得到一个json文件,然后将这种json文件转成掩码图.
    - @3 G9 b6 v" e/ f# J, ~# @2 J
    5 L* f7 N; |. Q/ q) j# 代码来自 https://blog.csdn.net/hello_dear_you/article/details/120130155/ D+ |6 d0 Z, w* w
    import json
    * ?) j- f: Q% w) H5 L& c8 fimport numpy as np
      E) h, b/ [- R: J- E3 F  n( Zimport cv2
    * @- M/ C# P8 x( p! N# read json file5 M+ G" \# m; P6 i
    with open("origin_json/mypic.json", "r") as f:* ~9 W( E  f. [$ M& y; R% v
        data = f.read()0 m5 R! F9 s( \5 J9 d
    0 x; ?4 f& i. N& y- p8 @, _
    # convert str to json objs4 N: S' h* |+ i* n2 y
    data = json.loads(data)# k: ^" `, {6 X' z( s# ~

    $ s. i6 i  ]# {; j; P4 }6 g% h# get the points 8 a) n+ ?, q4 E6 @. G
    points = data["shapes"][0]["points"]  r  A! u$ t7 b6 ]' K, _
    points = np.array(points, dtype=np.int32)   # tips: points location must be int32, i( ~4 \  h; q$ O2 ~, L) f

    2 S9 ~8 J( W4 G0 b/ Q0 y# read image to get shape- D. K5 X1 t& {" A9 k8 S
    image = cv2.imread("origin_png/person.jpg")$ r& k% h' Z8 Z. z8 @1 Y' b

    " |  i' N( |- r$ h6 Z# create a blank image
    - i8 x9 s, @6 J; ~mask = np.zeros_like(image, dtype=np.uint8)
    / L! E% F6 N4 e9 k" k" Q7 _# T: Q: @' h
    # fill the contour with 2559 ~2 P7 {' n0 ]$ [, v& ?
    cv2.fillPoly(mask, [points], (255, 255, 255))
    * ]4 ]9 X) e7 e1 k. C6 Y* T  S4 h0 _% `/ K% a* T
    # save the mask
    7 `& J- |8 L* Y* @cv2.imwrite("mask/person_mask.png", mask)6 A) s4 L; N' |- L& v: P! {$ m
    1& |1 ]1 d' |# E6 ]
    20 D' q, a7 c3 \" o% B
    3
    ) V4 S9 d% u) [2 ]4 A4
    3 b* }5 @: m) c2 A, V58 u( X7 z6 J  X1 {; ^& m+ |
    6/ I( O( T+ w6 p( t
    7
    % j3 ~# {6 a( X1 D  @' c6 ^8
    2 Z4 z# Q$ j& ]2 r9; c0 z; Z- s+ D+ Y. Z% j* u
    10
    + ]4 H- p: x6 \* h3 x; l11
    ! s4 U/ e. L9 ]2 c  }12
    ( S! U2 x- [; [8 y6 [1 V5 z# o( Y/ [1 p- m135 P' U" w4 A" p; [* b% U0 e
    144 ?- _* x( _! H, k% Q" m: l/ `
    15- d' i5 U" u5 o# B# X
    162 O2 M" r' J, l# Z
    17
    . k8 s; m( {) ~- T9 J4 D2 I187 y5 C4 q3 i! Q" P2 r
    19
    5 R  C1 U; f) f& N200 @$ y8 Q% T- d) ^" a9 v% C& e
    21
    ( t8 U4 G  W" [& m  `0 O; o22
    5 k) r5 R9 {8 Y( i- {7 j: a- j" i; [239 l% g; y5 p8 [0 w
    24
    / c9 W) i0 D( L0 o6 I) @: ~' Q- _25
      B# Y6 ?0 U" c26
    8 s0 k0 ^0 [+ C3 x, {$ u* o大概是这样:  |+ a4 @/ Q7 P! d
    , i+ H2 Q; Y- Q8 X- v

    9 N: M* }' d6 \/ `8 {然后利用这个mask生成图片. x( o8 i% l/ [/ \# t

    . d! y% V$ P; d1 A7 z# 参考自: https://www.jianshu.com/p/1961aa0c02ee9 ?* @3 w1 o( B' a. ~
    import cv25 o: ^4 D4 w+ A) O
    import numpy as np
    - T4 W0 T" \2 b0 S" @# B
    1 W# R" Y# B2 ~. q' n- r. f4 ]
    6 j& ]3 ?* \( d( R7 Morigin_png = 'origin_png/person.jpg'3 J" U3 D8 n+ z5 `; O1 g  Z
    # maskPath = 'mask/person_mask.png'
    " E3 Z: l* e7 P5 [9 wmaskPath = 'mask/bmv2.png'
    - V% r) x& l! M( E$ n2 S3 s% Rresult_png = 'result_png/result_png.png'
    ; h4 z2 J: D7 N5 u
      T/ A- I$ b9 M  Z  S
    ( P" c* H: ^. R) h# tmaskImg = cv2.imread(maskPath)
    % m6 `9 U+ u6 K: I' g- }0 n/ }) {img = cv2.imread(origin_png), T. l7 X3 G6 U2 K
    assert maskImg.shape == img.shape, 'maskImg.shape != origin_png.shape'( K+ P# g; Q2 U9 M: ?1 A

    2 x: \* N" U( Vh, w = img.shape[0], img.shape[1]# f! |+ g. E0 x" {  @# l4 E
    print('图片宽度: {}, 高度: {}'.format(h, w)): [7 W( o& b' n# ~

    - |' L/ T; Y% q1 s5 M+ @- Brgb = (19,122,171)
    * A- t3 [/ W7 B3 n0 ^7 x6 `bgr = (rgb[2], rgb[1], rgb[0])0 v4 @4 _0 g. f! ~3 A  P; o
    # (B, G, R)
    # k+ P  o* o+ |+ C  X1 \& P1 |for i in range(h):
      V4 }- a, K" b0 P* g    for j in range(w):
    5 `# g& U8 L( d- _        if (maskImg[i, j] == 0).all():, i  x. V6 M, \5 t4 {
                img[i, j] = bgr
    , R1 B: G8 Z9 H1 S; Icv2.imwrite(result_png, img)6 J$ u: @* Z1 z/ a  ~( B; k) i& l
    print('图片写入 {} 成功'.format(result_png))% p( z5 D8 \; o. g6 H3 g
    1# a  ^  b4 L+ \0 m
    2
    ( {' s9 i. \; e( }( [" X- j5 ~3  X- j$ C7 u( {
    4
    6 t  Z. G- p  g; H3 r! y58 P1 M, M1 o  G1 A
    6
    0 p3 z, z: J" a! m7
    * S' w7 Q6 b  [5 u8
    3 }0 a  L1 ?5 v) s* Y. C) b+ n97 I7 ~% D$ n( a) s8 H1 \
    10
    4 N5 k& ^# B" H( D9 N- {11
    3 c* X/ X9 p/ ~' R12; l' J! i( x, v' x2 |7 J: }3 G! g
    13
    5 _. |1 U; T$ b% K% g% X4 [7 S8 ?14
    ' o' T* H7 G/ E4 u$ m15
    2 a$ }2 p  p1 k, W16
    7 L! P6 `% N; x, K" ?7 T9 v9 A  r17) J$ v( P4 J3 m+ G+ Y
    182 Y( R3 I6 {: d+ K
    19
    / s: m) k- I; J5 R2 H& n20. c; E+ ~) b, V- }8 V$ p* b: C
    21( L" P" R2 T3 [4 {2 W6 F. G$ N7 }
    22
    ; X  B0 Y' `1 l, v- d7 H23
    , J4 k9 U! K. ?6 u# _24/ U: L8 L$ F6 W# D0 \; A8 y3 L
    25
    3 M# d$ p' ?/ W9 o6 O' v+ |$ u+ ~26, F' [" A' h9 Q% q7 M& F; m3 I
    27! u: `* `7 Q4 g. L
    由于人长得一般,就不放图了…
    4 G5 m# i- ~. d  s/ d0 P# |9 S! D* d; o1 ?: w) v1 U7 \4 Q
    缺点:( r4 l# ~0 f8 `! N2 R
    lableme标注时挺费力,并且难以避免人与背景边缘会有残留红色像素的情况。
    0 u. H1 p9 }3 V) \3 Z8 k2 m. e4 \8 r7 i$ Y( _+ J

    + j4 e: E6 Z3 C' U1 p4 _0 ~8 `4 k; _) y9 W) n& B
    4 m/ R$ V1 ?7 g/ P7 i( ]
    3 `. y5 v$ d. N; G$ `" G: Z# c1 h
    方法二: 阈值
    ( i6 d. j: J0 |( h2 I! e该方法通过比较像素的RGB与背景的RGB来区分是否为图像背景。* H! r! T5 d8 X; k7 k5 [
    % i0 i6 p9 v$ n) H: J: j5 h
    Opencv5 |8 l0 G6 C6 N2 j
    import cv2  h# K0 D8 u1 A% C
    import numpy as np/ }& x* l6 Q" `

    8 K* W. o. I5 G" H1 J' _" Y. A0 E5 I1 u. {; V/ {+ s5 G  E3 W
    def mean_square_loss(a_np, b_np):
    & ]8 N7 V6 G4 L& m, m    sl = np.square(a_np - b_np)
    3 L" U, i3 H: Y5 R2 I4 M4 w* K    return np.mean(sl)3 I) F, T- h6 x
    : h6 Y  l0 G( S9 g, i

    $ ^/ Y6 A6 _1 d) i, e, |3 Vdef change_red2blue(origin_png, result_png):8 C5 h- B1 M1 E/ K) x
        img = cv2.imread(origin_png)
    0 G. D, Z0 ?% N3 z5 m* t7 Q
    # Q: b( q3 L  q* s    h, w = img.shape[0], img.shape[1]: b' b& R5 `! }4 C3 _& p
        print('图片宽度: {}, 高度: {}'.format(h, w))
      P* C( S7 v7 ]3 t. G1 A4 B  A; ^1 O& V' {: a7 [) n$ [1 R! P5 z
        origin_rgb = (168,36,32)  # 可以用浏览器啥的控制台工具提取出背景的rgb值
    - c6 K# h9 t! T' _2 _    origin_bgr = (origin_rgb[2], origin_rgb[1], origin_rgb[0])% d( X$ H( D; j" }/ l3 ~, o
        target_rgb = (19,122,171) # 蓝底RBG
    $ C5 h* \0 R8 a9 R+ U& l8 _( r" q    target_bgr = (target_rgb[2], target_rgb[1], target_rgb[0])
    + J- c) k9 [9 C1 p7 d7 {
    5 E. z6 q7 Z. G6 U5 q' {    for i in range(h):6 @; a" Z' {" M* `
            for j in range(w):/ g: S7 Q& |# k$ s
                # (B, G, R)
    - s- R  A& {' m8 c* z0 U            if mean_square_loss(img[i, j], origin_bgr) < 50:
    # g; x; \2 ?3 p$ g1 I                img[i, j] = target_bgr
    4 v* n! {- [. d  a8 U" @' E8 M7 {$ L3 O* i% r' \3 R
        cv2.imwrite(result_png, img)
    " F) K6 K* w$ Z! U1 Q, h) W    print('图片写入 {} 成功'.format(result_png)), U- |8 Z3 }" [9 \% ^

    . o) A6 ^4 u: @+ m4 B- B: F; x% l$ ^
    if __name__ == '__main__':
    * D  g* n) R4 J" ^    # origin_png = 'result_png/result_png.png'3 Q, c; l- J$ p
        origin_png = 'origin_png/person.jpg'
    : T) b5 U  x: [3 Q    result_png = 'result_png/result_refine.png'7 P6 ]. M, c& \
        change_red2blue(origin_png, result_png)
    ' J. H2 Y9 T/ s( F" A' m4 `1; G' @: i; q) M5 v/ i: Z8 L" p
    2
    7 J8 @! r% X0 a$ g0 Q% [1 ?! C% u3+ G0 V6 I5 V( ~' e
    4% c: K& t. m# i
    5
    ; b. ?9 ^7 A, C+ g0 \# s+ f* u) w/ v62 o0 Y* A3 u- S6 q6 Z3 k( p$ e
    7
    % `4 [+ L+ a$ ^, Y8 Y" j7 e8
    * R! w% o/ h" S9 O& {% F9
    2 E% @- O6 \) `4 h" @3 [! Z. p10
    6 z% P: A# D: W  U# ^1 _8 u5 Z' v11/ z5 {5 j6 G/ I5 a* ^1 d/ \' I
    127 p4 G( K3 [0 o7 a! w  n
    13
      Q2 ^) ~$ D3 L& q14
    ) }9 \$ b" L! S0 X15
    3 W# i( I% d' D: V2 h16, Z! R5 w3 V" \* _
    17
    ! G2 r! Y& c: B2 Y. e  l182 X* R& Y9 s& g: L5 _0 t* W, L
    19# |/ s' M  X4 l& f( ~. Z
    20
    4 `9 ~! {, r! A! i/ @% i$ g21# T' e4 n2 |6 k
    22' F, v9 P% [6 s+ P' x
    23
    8 l8 e. O. {, S- |# t1 R24
    ; Z/ b( S9 v' N2 c* l; [258 [! Q2 t1 W" M+ S0 B
    265 P" _. k8 H" |  N. L7 Z+ F, B$ ~# w
    27
    3 ]% R6 J. _3 ?) H' e* k283 c; f1 u! c3 H
    29
    ( `6 z5 B! _1 U0 Y4 Z; a  B0 c30- Y$ a2 m- y  ]9 i# j7 m7 V
    31" c% a5 G8 x$ l# @! h
    32, o8 Q. O* p7 K9 M+ a9 M0 x4 }+ ]
    33
    ( L+ k$ W1 k9 l6 @! ?, h: _34
    2 c, Y3 P" g: _9 {. L355 v5 W: Q2 p$ U. _% E- }/ Y$ `1 F
    结果人与背景边缘仍会存在红色像素残留7 K  Q: R! z7 Z* f0 m% a' d

    ) s% u- y% u1 n1 R9 z2 ~: H# h+ D. ~4 I/ _, e+ @/ E: F

    2 l' o8 z3 L) k2 h
    # \+ E: Z# J+ N" f: D6 ?( h; o4 ~# U" C8 ^7 M/ X
    PIL
    ' l+ \' Z6 M# A7 Y/ o0 E3 a, afrom torchvision.transforms.functional import to_tensor, to_pil_image2 ]4 ?4 `% F( Q! @7 g! K7 A" ~
    from PIL import Image7 x; E* T0 v; V3 g
    import torch9 y, t, c) s. T# F& y5 x
    import time
    8 W) w0 T' K/ H5 H: R+ {  [2 S
    - k) W* L7 }3 \6 q1 p/ K6 t! G& Y2 [
    def mean_square_loss(a_ts, b_ts):3 e; ~8 q# r1 T" s9 h  i. O
        # print(a_ts.shape)$ T1 ]7 f9 J: B9 w
        # print(b_ts)' o4 j, A, M( ~, I2 t- X
        sl = (a_ts - b_ts) ** 2
    8 |5 B' `2 q/ n+ |1 x  g, a0 [7 t( O    return sl.sum()) N1 n2 C4 n" Q( l$ F
    # S5 B; Y3 |. X
    6 \  Z% V; g% I9 H9 h
    def change_red2blue(origin_png, result_png):
    ! C+ g! L' b2 F; K- S" _; ^    src = Image.open(origin_png)
    4 s, M) _. u. H6 o    src = to_tensor(src)2 M' F+ N' }/ L0 w7 A3 T3 n
        # print(src.shape)  # torch.Size([3, 800, 600])
    ) E2 c; z/ r" }7 ^    # channel: (R, G, B) / 255, j/ R8 d0 P/ @/ q
        h, w = src.shape[1], src.shape[2]& z& S3 [- L, f+ U2 q& a" B
    8 p+ v& y6 v+ u
        pha = torch.ones(h, w, 3)
    1 @  S+ t2 D, |8 u2 c
    - I. i3 s5 [9 M) }7 Q) E# Y; N    bg = torch.tensor([168,36,32]) / 255
    1 ~) Q; E. {+ m$ m. u    target_bg = torch.tensor([19,122,171]) / 255
    $ w: y8 A9 @6 F$ r9 ^; p- w' y$ }, e. U* C, n  @; G  u
        # C, H, W -> H, W, C
    9 N8 Q5 b1 Y, w    src = src.permute(1, 2, 0)
    9 `# ]$ S+ |, h6 @" W/ M( O1 ~( J1 f    for i in range(h):, U* B  n; W2 W4 `
            for j in range(w):& p  \; I  G+ y( R  m1 z
                if mean_square_loss(src[j], bg) < 0.025: # 0.025是阈值,超参数
    3 @. Y* z, O$ f4 G! R& A                pha[j] = torch.tensor([0.0, 0.0, 0.0])
    + m1 F& j0 }) t" J) _3 C; f6 g0 M0 p& {
        # H, W, C -> C, H, W
    3 n# j# @1 }7 P6 V; c' ]- l    src = src.permute(2, 0, 1)" `4 _; {) E/ X, E) C" v
        pha = pha.permute(2, 0, 1)/ n9 \( ~+ T  n- w) p6 _& O
        com = pha * src + (1 - pha) * target_bg.view(3, 1, 1)
    8 K; H$ A! N$ z$ Q    to_pil_image(com).save(result_png)
    ; e: X* ?' E* q1 x6 ]. [! d
    & q# s/ U0 p" o# z( |) |) \# u9 V) g2 v1 o- \6 I
    if __name__ == '__main__':
    2 m$ b0 M$ z( r. ]    origin_png = 'origin_png/person.jpg'
    , O2 m2 g7 ^3 v: W. c; L- c    result_png = 'result_png/com.png'
    . K2 b8 n1 h  i4 w    start_time = time.time()8 L( S9 q/ ~" g4 o3 R, W
        change_red2blue(origin_png, result_png)2 x& x; q1 v' c
        spend_time = round(time.time() - start_time, 2)
    9 m! ?+ ?$ }4 _    print('生成成功,共花了 {} 秒'.format(spend_time))
    1 W  b% I( [5 j: X1
    7 w- y8 E. R3 P2
    : }' p; v  ~% m3 J3
    $ `/ r: @2 t6 @7 B4 n2 Y8 x4
    9 H( ^3 T* c  n9 q: |5
    ' n! i7 V7 t& ~5 n, w) M+ m7 y/ n6
    / P, F% x# v  _! X- A  S, x4 d. R$ o7' B0 h. k/ z; R- z. `, V
    82 Q) e. b* f- u* Z+ ~( F
    90 I0 m* @6 I0 O4 ~; Z, k
    10/ j+ Z6 D, f6 E9 H2 i
    11
    5 L! `6 g' l2 q- Z* e12
    ; ]1 l9 C% i% F1 H" ?0 W+ s13' q7 X/ O+ k  a8 p1 i* o  a
    14
    6 ]' o; ?- f5 D15+ _% l* V3 Y5 T- h
    16
    0 h8 h3 @2 x- ]17
    & j0 b2 c/ k- j; [18
    , P2 p$ Q: c- n/ d, W% ^- R5 N19
    0 m# ]6 Q( v# \8 b+ R6 M5 Y4 W20
    ) ?3 W& e: _( p" I: Y1 K21
    ; t' F: y% p) `. z/ B0 D22
    , s" s- W6 h7 j& r5 M7 J0 h23( q4 p+ H7 h7 W& G# _- y1 T
    248 c0 E- i$ B+ P6 R  o
    259 }) `0 e8 X  \5 x4 z" I# Y
    26
    ' q" \4 b# f* m; V& w27
    . i0 L4 _, i: W9 a' ~28/ ^3 a' q9 W# @1 d- F7 Z
    297 J. O4 K  N1 D( h, i9 Q: |: c! {
    30
    ! u/ L5 [* D( p$ K: r* K31+ _4 d6 X- f) b8 n) m& Z& E9 |
    32
    1 Y9 R7 M6 T$ _& F33
    $ ]2 ^5 b; J4 L0 E# Y  |2 M4 q34
    " J$ l7 i' O* {) J, P35
    ; }* f% _/ P% }! f36
    ( Z" z( h/ E) s. C, n2 |) V37
    . I' \/ x; Y0 w4 o+ A6 y# b3 M38
    $ w8 p1 @  |' {6 T6 [) H39
    2 k9 I+ |3 U; W8 C" H3 ?' |407 T) _7 a% A6 Z6 f* [
    41" l! v6 U6 P7 W( _; g& S  G
    42
    $ i: U* W3 `4 l3 }' [5 V43; M9 K6 K( n' H- h; u; W  _. t
    442 F  _+ M  P: S- M& s/ b
    45. c$ _0 f3 I1 E$ a5 M
    462 F& g/ A, u3 D
    该方法质量较好,但一张图片大概需要12秒。* _5 z  P+ X3 S/ v8 v! T

    2 W) T7 Z  p( s8 h0 Y3 P7 Y0 r( `/ C! {( {, ~0 |- G- I
    ! b- C: s6 n" c! u/ S4 K* A
    方法四: Background MattingV2; Z7 p9 D! o$ J! f: `5 ?
    Real-Time High-Resolution Background Matting2 Q, g8 l/ T6 _9 V
    CVPR 2021 oral
    : }2 F* I0 N: }+ v5 y" Q  t: ^1 e9 K5 S- k7 [( x
    论文:https://arxiv.org/abs/2012.07810' D" N% d$ n, b+ E+ b; |
    代码:https://github.com/PeterL1n/BackgroundMattingV2
    ) D: G1 \2 ?, Y8 a/ j) i. i, ^7 I" Q$ @
    github的readme.md有inference的colab链接,可以用那个跑- _* ?: E6 @$ \/ Y6 a. w# S: b
    , d. _( L- p8 `
    由于这篇论文是需要输入一张图片(例如有人存在的草地上)和背景图片的(如果草地啥的), 然后模型会把人抠出来。
    + y3 }/ J; C! |8 X7 X' ^' I2 [( ^% I  ]  ]/ r% v) l
    于是这里我需要生成一个背景图片。
    / A2 P! j5 J1 n& u首先我先借助firefox的颜色拾取器(或者微信截图,或者一些在线工具,例如菜鸟工具),得到十六进制,再用在线转换工具转成rgb。$ t0 J; x$ ^# ?4 i/ P

    & U/ r9 |- M3 t然后生成一个背景图片。
    % Y3 o3 K! m+ r# D% w
    0 d! |2 `1 n2 t4 |4 u1 jimport cv24 j. b) F; J2 l  G4 M7 Y# v
    import numpy as np
    / J9 N1 V& N0 p! E4 V6 B. U8 |, O0 a0 z

    6 W; }9 B+ T# j7 a7 {3 X/ Uimage = cv2.imread("origin_png/person.jpg")
    ( e3 ]7 o! t3 |) ^( E" }origin_rgb = (168,36,32)  # 可以用浏览器啥的控制台工具提取出背景的rgb值+ w; p/ j1 K% J. V
    origin_bgr = (origin_rgb[2], origin_rgb[1], origin_rgb[0])
    4 ^- n! [. @( G4 G& Aimage[:, :] = origin_bgr
      v5 m9 U( M) q6 W, J& T3 F) \6 ?. Z& O
    * F0 H; ~8 q8 y6 Fcv2.imwrite("mask/bg.png", image)
    , F2 t; x% ]; D% o2 }; A* X4 ?1: _5 k1 v, W4 T% I5 _
    25 P3 J# B3 ]- s1 w$ E, w4 i
    38 L: e- c. G; T
    42 F; [$ r) e! Y, D$ L' ]
    5
    5 x% O, z6 k# H  M4 X/ J+ `6
    8 y- I$ p% c7 x% {7' }3 W( U- ]7 C8 r* H! \/ c
    8% L5 Y# B+ T  N7 ~& E4 d' d
    91 Q6 L5 T: N3 N5 j$ Z: O
    10$ Z; \  J2 ~& `3 \/ {! G  {. d1 G) M7 Z
    ! O! C5 a% F! R% o& q
    需要上传人的照片和背景照片, 如果名字和路径不一样则需要修改一下代码& T$ \% O! ?3 @3 b9 P+ |

    : z- N# L6 y! Q* e: q" ~src = Image.open('src.png')3 X% w/ ^3 u: u# \6 Z1 L/ L9 I. w3 h
    bgr = Image.open('bgr.png')
    / p' E6 Y9 z* o: Z3 K1
    : M* M* F) N, s+ L- w22 f; \$ [& K* D6 p7 w" b5 o
    另外原论文是边绿底,要变蓝底,白底,红底则可以修改RGB值,举个例子,原来是这样的(绿底, RGB120, 255, 155)0 s! ^" K1 k/ `; N. c

    8 v& K( W; Q1 a/ d$ q- d* r/ jcom = pha * fgr + (1 - pha) * torch.tensor([120/255, 255/255, 155/255], device='cuda').view(1, 3, 1, 1)
    6 m, e& L3 a  o7 x- ^  A$ f0 m  w1
    9 A1 D6 u/ C0 R0 Q# C7 ]$ E/ n) T6 |% z5 Q+ h
    那么加入我要换白底(255, 255, 255),就是; a9 d! `: e$ S$ M4 T1 j! N

    5 D! N3 _  [- p; L2 r8 [com = pha * fgr + (1 - pha) * torch.tensor([255/255, 255/255, 255/255], device='cuda').view(1, 3, 1, 1)
    ! `3 u" J  e$ Y4 ^' X7 w2 ?11 r- |" z( _! u3 a

    # v, Y0 c" A$ A/ W假如像我换蓝底(19,122,171)具体深浅可以调节一下RGB,就是2 [( q- O1 b6 n& D" q$ y

    1 t4 t7 ?5 e: @+ |; F( t0 Kcom = pha * fgr + (1 - pha) * torch.tensor([19/255, 122/255, 171/255], device='cuda').view(1, 3, 1, 1)
    , K- D1 z3 N5 v: g) \- p/ \1
    # w9 ]- z' V9 _& j" i/ g9 K总结: 其实这种方法从 任何颜色的照片 都可以 换成任何颜色的底。只要换下RGB.
    ( I* J; ]6 f# }4 K
    " G0 ]( M' Y3 T然后就输出图片了。可以看到效果相当好。不愧是oral。% S9 e% g1 @6 W/ M2 p

    . w0 {* P' [% x; C' [3 }3 l. r
    9 ~3 M- w! j, V+ J( Q! D5 m( |1 Q原论文可以实现发丝级效果
    5 z. t' \* ^: k( ?2 ~* s
    % X) H  M$ R# o; [. D
    + z; U! b- R' a. }/ w! h. {7 R. o; F: K8 G/ i

    - B) }& @6 y/ }- k. y* c+ _
    ( P8 j2 y% V: s+ T9 e, M9 [报错解决方案
      K- `- M6 s6 kcan’t divided by 4 / can’t divided by 16
    " ?6 W# O" k8 u( A- b" \0 y由于该骨干模型可能进行4倍或16倍下采样,因此如果您的证件照不是该倍数的话,有两种选择方案。一种是padding, 填充后再送入模型,然后出结果后再用clip函数裁剪。另一种方式是resize, 给resize到规定倍数的宽和高。
    $ E5 z5 y7 S: x7 }# Z5 P这两种方案需要的代码都可以从这篇博文找到: python图像填充与裁剪/resize( y" h+ r/ @; t8 g" v
    ————————————————: y- p: j+ v1 A5 |* x' D
    版权声明:本文为CSDN博主「Andy Dennis」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。9 G0 i1 a' `; ?6 y
    原文链接:https://blog.csdn.net/weixin_43850253/article/details/126376767
    : S0 i5 R7 O( t% i$ y' Z! V/ H9 L0 g3 t
    4 O7 v, c6 p3 Y. u$ r1 c5 p4 S4 J2 v2 U. X/ R; k8 g" ^0 w9 w
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    您需要登录后才可以回帖 登录 | 注册地址

    fastpost qq
    收缩
    • 电话咨询

    • 04714969085

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2026-11-26 00:17 , Processed in 0.207090 second(s), 50 queries .

    回顶部