QQ登录

只需要一步,快速开始

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

深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提...

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

5273

主题

82

听众

17万

积分

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

    [LV.4]偶尔看看III

    网络挑战赛参赛者

    网络挑战赛参赛者

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

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

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

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

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

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

    跳转到指定楼层
    1#
    发表于 2021-7-14 15:18 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta

    1 T  q* B- ~8 `深度学习和目标检测系列教程 8-300:目标检测常见的标注工具LabelImg和将xml文件提取图像信息
    ) G9 `& E5 }2 [2 f图像标注主要用于创建数据集进行图片的标注。本篇博客将推荐一款非常实用的图片标注工具LabelImg,重点介绍其安装使用过程。如果想简单点,请直接下载打包版(下载地址见结尾),无需编译,直接打开即可!- G6 H0 V6 f$ O7 \2 @" D

    1 M6 J5 M6 j$ N

    , T( U' b3 h% V- K8 X5 d  o感谢原作者对Github的贡献,博主发现软件已经更新,可以关注最新版本。这个工具是一个用 Python 和 Qt 编写的完整的图形界面。最有意思的是,它的标注信息可以直接转换成XML文件,这和PASCAL VOC和ImageNet使用的XML是一样的。
    5 F- x7 b* r- R/ n: L8 h' [) c
    : J  j) d+ I3 D% l4 i/ B: L7 f, \
    3 Z2 \" N* V* y) X5 A  ~' X  A
    附注。作者在5月份更新了代码,现在最新版本号是1.3.0,博主亲测,源码在Windows 10和Ubuntu 16.04上正常运行。8 B* ~  R* J7 S0 @# s$ h$ Y! ?
    2 _( _' ^" `+ V! [, X& l: v( v
    ! D6 g* G9 \2 ~% m
    具体的安装查看Github教程:https://github.com/wkentaro/labelme/#installation# d/ d3 s" f! f
    ; @1 R* l3 l( ^$ R
    . W1 M( R5 [: ~6 E( K
    在原作者的github下载源码:https://github.com/tzutalin/labelImg
    % F* L7 X1 S8 g1 X- R/ {$ t$ C。解压名为labelImg-master的文件夹,进入当前目录的命令行窗口,输入如下语句依次打开软件。
    $ T: J. a1 p" t1 `
    7 ~7 b7 L( m- B/ ~$ `

    % u% m( u: m7 @% V$ T& g0 Q5 @python labelImg.py
    ' I2 B; [- d- B1 o1& N; e5 H) L8 [  v
    * S% Q& x! h) d6 M. D

    / W4 S+ h& e& D: G) x# t/ \1 R7 f+ Z

    % V+ E/ v3 Z$ E- b0 V6 e5 q1 _具体使用5 o) G" D0 B8 G& Y9 h9 Z
    修改默认的XML文件保存位置,使用快捷键“Ctrl+R”,更改为自定义位置,这里的路径一定不能包含中文,否则不会保存。
    9 u: D0 u1 b$ k3 ?( p( o3 Y; ]7 T
    0 {* O0 q6 Q/ T0 t$ c" s

    # Z2 D  f# p8 z7 h  S使用notepad++打开源文件夹中的data/predefined_classes.txt,修改默认分类,如person、car、motorcycle这三个分类。
    ; z1 G/ T3 s' }0 U7 a  o3 r% A- A; [- o

    5 ]( D4 v& F) g9 E+ w! _6 I“打开目录”打开图片文件夹,选择第一张图片开始标注,用“创建矩形框”或“Ctrl+N”启动框,点击结束框,双击选择类别。完成一张图片点击“保存”保存后,XML文件已经保存到本地了。单击“下一张图片”转到下一张图片。
    4 u  C# b; x, K. w& x6 m5 o* m- C6 V+ \/ e4 S! h
    / K1 X; v( n5 K
    贴标过程可以随时返回修改,保存的文件会覆盖上一个。
    4 e$ h, Y7 e( z4 m/ E; M6 h! b7 Z% w$ Q5 t. W+ D+ N8 A  {
    9 g* V6 `1 S2 P0 W6 o& y- K/ d" |
    完成注解后,打开XML文件,发现和PASCAL VOC格式一样。
    2 h6 l! g2 W% G# {- ^- B; i7 k4 A! k/ A. s$ n+ _' z

    , F! n9 n/ P/ f6 n( T  a将xml文件提取图像信息, {8 s  D/ g" v4 b7 \
    下面列举如何将xml文件提取图像信息,图片保存到image文件夹,xml保存标注内容。图片和标注的文件名字一样的。
    5 `0 o  [. K; F) C2 t1 E' B- o8 C5 ]# \- M

    ' N8 N" Y3 a' D! r" }! @* ^/ u  O' d3 n. }+ ?8 j! ~' }* Y

    + f5 c" y6 M/ K下面是images图片中的一个。
    0 w, X& Q+ ^1 W" E7 @# O, B
    6 H, X6 z7 @8 B) w$ J& s

    - q2 k( j. p$ o+ i0 Q  n2 ^下面是对应的xml文件。
    ! {4 [7 U* h' y8 J  S  A4 N' w3 ~

    1 G2 Z1 m, d8 E6 g7 M<annotation>
    8 j+ g4 }9 Z, c  @. N* y1 d6 M9 O        <folder>train</folder>6 \7 n  s: K' a1 m; X
            <filename>apple_30.jpg</filename>
    : N0 w' |) c7 w* O, \/ J        <path>C:\tensorflow1\models\research\object_detection\images\train\apple_30.jpg</path>
    - G. s5 u$ `! p        <source>0 L( u7 Q; i* u' d
                    <database>Unknown</database>7 v% M& E. ?! B" L1 K8 S# s+ w
            </source>
    - J/ ?! I% _5 x        <size>
    9 V! y) {; N: S                <width>800</width>! [) m' X1 E" H* k- H
                    <height>800</height>
    1 g0 g" H$ O+ p: }% n                <depth>3</depth>" [6 R( E/ }/ z7 k2 S5 k% p
            </size>9 x" Z  ?: z+ B/ J
            <segmented>0</segmented>+ c( v6 K/ f" ]+ B/ G3 B/ I
            <object>- \9 x& F8 _1 k$ V
                    <name>apple</name>) |, m( L- C, ?# q" M" ]$ v1 b! d
                    <pose>Unspecified</pose>! y/ B& l% f1 S4 i/ c; U7 N
                    <truncated>0</truncated>- T. K2 l. l3 \
                    <difficult>0</difficult>
    ) a5 l$ t- `& u1 w                <bndbox>7 k3 P& [/ b- A# P- e9 L
                            <xmin>254</xmin>
    * W" n6 t$ I2 E                        <ymin>163</ymin>
    , M6 f3 d; `' G; ^                        <xmax>582</xmax>
    5 X. `; A/ y9 b" o1 M6 H' O                        <ymax>487</ymax>$ S+ M0 Z+ q, _6 X1 a' J1 E7 v8 z
                    </bndbox>. m& U0 ~* m3 F: r' @5 M+ d
            </object>
    " F0 C( A( U- q8 `/ ^0 V        <object>0 O, m* l7 c3 L0 u- ~
                    <name>apple</name>, t( U9 T9 i" N2 X; }; u% A
                    <pose>Unspecified</pose>$ n5 b% l6 G, o! T9 k
                    <truncated>0</truncated>
    . a( h; D5 O1 ?2 f& [! G" X                <difficult>0</difficult>  J1 G" P- _: J) f
                    <bndbox>0 x* m# E: v+ {0 b3 J) R6 j
                            <xmin>217</xmin>6 w) ^  W3 k& v' P  I) K5 o; K  G4 ]
                            <ymin>448</ymin>
      L  O! Q1 X$ g7 B( U5 i7 O                        <xmax>535</xmax>
    5 [! h( G, h: y$ P                        <ymax>713</ymax>& M) n3 C! U' S. h
                    </bndbox>
    ( b- P/ n8 e5 C) Q% P        </object>! d7 H5 x2 Q$ z! {4 w
            <object>, q& \# W- ?5 x( R; i
                    <name>apple</name>
    " a5 C$ C/ L+ J8 |) T9 K                <pose>Unspecified</pose>
    0 G" t, Z4 \" V# [                <truncated>1</truncated>
    # B# Z& X) t5 b; y                <difficult>0</difficult>4 X  y& N: O/ o) W/ N) Z+ z
                    <bndbox>
    ' D4 `9 z, Z9 A0 O, @2 `* ^                        <xmin>603</xmin>
    ; q" W9 p- `9 }9 t                        <ymin>470</ymin>$ h% i2 K" z9 r9 i" N/ z& m9 @
                            <xmax>800</xmax>
    - d% g- ?% a; Y- V* D                        <ymax>716</ymax>. }6 [! O6 l# S
                    </bndbox>1 F1 ~" q/ F2 q4 ~3 N* m
            </object>0 W+ `- I; l  ], ~
            <object>
    ' m0 X7 X1 J. \5 `. Q                <name>apple</name>
    3 X/ p8 ?  @/ F/ ~1 a3 S                <pose>Unspecified</pose>
    7 L* Y2 I4 G5 z5 d( X  L                <truncated>0</truncated>, {4 r3 d$ T  a1 v% \* X/ d; G/ `
                    <difficult>0</difficult>
    ; u' V) v; l# Q) k" r5 n                <bndbox>
    ; y; i, M& x/ ^, B' `' R5 y0 T                        <xmin>468</xmin>, t" N6 K7 Y$ K% ]$ D) I
                            <ymin>179</ymin>9 _  m) O  s( k2 y9 C' k# F
                            <xmax>727</xmax>, _2 v! I% |6 r% p
                            <ymax>467</ymax>
      k5 a: Y5 j: O8 Q$ \                </bndbox>
    5 F4 l$ O+ O* Q% @5 }4 s6 U        </object>
    1 d& ~" b: N0 {; s2 L  H- w        <object>
    : B5 A: n$ X; g  |$ \1 ]: Z                <name>apple</name>
    1 B5 i2 Q- d& p: L, O2 ^6 S" t4 z9 `                <pose>Unspecified</pose>
    " f3 L& l/ U2 g- S1 d7 D( Q. K                <truncated>1</truncated>( u, i& Z. `) f5 `3 N1 I
                    <difficult>0</difficult># v8 N/ ]  R# K! P" A' O1 h4 V
                    <bndbox>9 l9 m1 \2 o- ]4 Z# x4 d
                            <xmin>1</xmin>
    : P2 |2 {- ?6 b                        <ymin>63</ymin>. E% t- N8 \9 y3 Z6 i4 U
                            <xmax>308</xmax>3 G. Y) e3 u& i0 B4 S
                            <ymax>414</ymax>
    , |& I8 ?* y0 m" k. {8 E5 ~                </bndbox>8 G/ [) K+ V# Q9 }; x  g
            </object>. ], \# p3 l; ^  l/ [* [3 I
    </annotation>6 w+ \9 u! D4 ^: P
    18 m7 M  I1 Q5 X" j% s8 Y! V* F
    2; v; d& w; o: G: d1 t: @
    3
    ! c* e+ S6 J' ^6 B! h0 k; x$ v4
    8 j: w/ D- `; h, A5 t2 s% r- F59 t& G  K! g& d9 G  n  t8 o
    6
    0 b2 {1 g# G( E0 h$ v, w, _1 k77 f7 T9 ]# J) ^+ k/ {' M1 g2 e' o
    8* r% T* |! ^# E1 j- ~) w: Q
    9
    ! M# i  p1 a+ T( I7 p1 w10  L8 `4 n7 E8 D8 S$ I& z
    11
    7 k) s# C% D! x) V3 {; y$ t12* i8 Y( d2 w3 c7 d0 j- a
    13/ Y: [# D) w" f7 L, K: ~
    14
    ! g( T. {2 U% v* k9 Z! T2 t15
      w7 y+ C! p* R" I: U6 H' H16  v5 z$ }3 V8 @
    178 g1 D$ g9 d; R( C
    18
    , @/ Q  w2 P4 N/ ?5 L+ h( z. i193 P! h$ x* R# P0 {
    20" S! Q$ B2 e% s: b+ _3 ?
    21
    ; G) w1 }$ F# L. |( n22
    4 D; l, X5 j# q' g239 H% M; v# n; w0 K
    24- Q4 `  H0 A6 R& ?9 J
    25' a0 S" y" V2 Y0 M: i
    26
    ) ?. j' `# x" f27
    * G, V0 @5 j, f' s28
    # y/ c& [2 g( U3 V# |29( G; S' P( x/ l. i
    30" D+ s# f4 O$ T0 U$ A: D
    31; q( R) J( A5 w# ?! a+ q5 |
    32  A' `3 h' d8 ]- [" f; Y$ Z4 p- @) T9 e
    33- \, P. l' e; G2 x
    340 a& k+ m9 _% ]: D; ?
    35
    5 i. I) Z2 y0 d! m$ V/ `36: z/ j8 D0 V" U1 l8 H& b3 A
    37
    7 g6 N! Q: n; j  E: J: ~38
    9 y+ [; d* ^- B5 h" w, t, H39- f3 G2 Y  v. d$ ^
    40! A& z3 V) ~5 s
    41: q* K- [1 m6 X  L9 S& N; Y& ^* U
    42! G. O" g) E( u  y+ I) j0 i: l
    439 p6 W8 \" e7 h6 N: ^  n7 z; b
    44) S* z4 H6 w/ K# y3 X# ?* b
    45% U5 u8 n+ A. z5 O# U2 M  ^
    46
    3 H# S" ^7 O: ^/ f2 A" x8 d47* U. Z, |1 G! j! d1 N. Y- t
    489 O+ O0 }. B, z
    49
    7 O; m6 X' g: \50
    ! u( d4 Y: B# w3 u5 [. G0 q' W51
    # t! i) K2 T3 z, C. |523 c& k/ x* ~3 R% e
    536 }9 P- S  \* ]& W6 Y! v; M, `
    54" p* c. T0 t- _$ ^* |+ F3 o) o
    55
    : {6 E2 y6 U, F2 J. |56
    . z# E2 M  o" @: I6 T57
    . M& A3 p. z  N4 c& s% X4 a( a- M58$ [6 E# k. x' T: j9 c7 \" t
    59
    : x4 o- h$ G) J1 [% l1 H60
    % R8 A! ~6 F6 L61# \: Y- R3 ]1 Z# P6 R% t+ X. j
    62$ P) N. n2 `0 _/ S& o* \( J
    63) j1 y9 j# s. ]! K1 A+ A! I) a
    64" t% S7 Z" l1 H2 c3 I
    65
    1 ~' s1 ?; E0 J1 \7 [( L' k66
    % n7 x) Q' u$ c& _5 E. X/ ^  X! m% {67
    4 N2 V) i9 R- Z+ ?- E68
    - p% K$ C; Z* ?- q69
    " r( Z% u, H- v: L1 \% z1 V70
    7 w3 R2 d* Y' B& B9 B71! P) l" s7 D  @
    722 Q' B1 H( N/ m& c% @/ W0 R
    73! t3 g6 f8 N* _' G
    74
    / {5 o7 x% o* i  z将xml文件提取图像信息,主要使用xml和opencv,基于torch提取,代码比较凌乱。/ H- U0 T& X9 y
    " f; E( W5 Q4 q
    , _: A6 f# R6 G4 N4 ?' R1 z; z
    import os
    + C& d7 C4 t, Zimport numpy as np
    - }1 [" E5 u$ t$ iimport cv2
    $ L1 U- G% [* {  C3 Aimport torch
    7 z+ \% h; h" Himport matplotlib.patches as patches. R. }( s, R# m- t% ^6 t
    import albumentations as A, N/ a) o  L" u) S
    from albumentations.pytorch.transforms import ToTensorV21 H! Q) C8 [1 k9 G/ \/ P
    from matplotlib import pyplot as plt8 a, V7 O6 _$ R8 m0 l9 e# S
    from torch.utils.data import Dataset5 L9 {! r9 R2 _8 K* H
    from xml.etree import ElementTree as et
    2 G3 E6 ]  Y/ R' u0 r" O% Qfrom torchvision import transforms as torchtrans
    0 H; E  A1 u3 X' [5 U+ E- h- H0 W# v
    & [4 `9 M8 a. W, Y/ l- T8 H

    # r& A9 n- g$ Z) `  N  O# defining the files directory and testing directory. ^2 z7 g2 J% d) E
    train_image_dir = 'train/train/image'
    ' g' J/ S9 S; X" }1 etrain_xml_dir = 'train/train/xml'
    # ?! O( ~" r) a* f# test_image_dir = 'test/test/image'5 Q# k, ]0 l* ~- V, L0 c
    # test_xml_dir = 'test/test/xml'
      \' S0 K2 k% W; h1 H) D7 H( }
    $ C; |" j; Q" T+ v- B

    ! L  k) t8 w2 yclass FruitImagesDataset(Dataset):+ |0 b$ ^% q& J/ x$ K! Z

    ; d+ Y- s1 k% j4 a1 [* V

    " j* v( j( a: }# D    def __init__(self, image_dir, xml_dir, width, height, transforms=None):, \" E4 W% L, Z; O& m
            self.transforms = transforms& N- ?& C4 J- a
            self.image_dir = image_dir
    ! ^& Q, {) c' }/ p# o/ @. Q7 h        self.xml_dir = xml_dir9 u) P8 ?$ [1 K% \- ~: o
            self.height = height
    - N8 {% x% K1 i( u# N5 A        self.width = width. p: L9 k  ]' T' o

    9 X. g% ~- f3 v: [, ~" U1 n

    1 h9 j' w# P! c7 y1 S        # sorting the images for consistency% O0 T3 ^* t; P6 g6 J" o
            # To get images, the extension of the filename is checked to be jpg
    ! o" G) x# u0 G% f) f. N        self.imgs = [image for image in os.listdir(self.image_dir)
    ( d4 n) ^! I5 `* D                     if image[-4:] == '.jpg']8 k+ D6 ^  ~, M* \
            self.xmls = [xml for xml in os.listdir(self.xml_dir)
    : A& @7 Z8 O4 L! C- A! t. j                     if xml[-4:] == '.xml']/ v4 L% q* t1 `

    " P, ]+ Z" B  D0 c! [
    & z% _6 O0 R9 F* _4 @* i
            # classes: 0 index is reserved for background
    8 s, ~; r, w  H% q, z  y$ m/ _, a        self.classes = ['apple', 'banana', 'orange']: Q, D* {' j2 |/ |- P$ v6 y* m% x
    " S: |: ]" ?& b4 Z2 a* m+ W

    # c* V" t6 f% [/ ?3 V    def __getitem__(self, idx):
    ' y9 ^/ n5 G) ^7 i
    + U! |# h0 R4 ]6 d8 z. x

    . i) n0 H* [) `7 ?/ h1 \! @        img_name = self.imgs[idx]
    4 ~7 G5 G; P' r0 c7 W/ m9 V7 R        image_path = os.path.join(self.image_dir, img_name). J# @* R% \/ z& P  N" ]

    , {% k9 J$ U* K5 Q5 C' B' o
    7 O* M) {7 p% h7 [- F  G
            # reading the images and converting them to correct size and color8 P1 V  n+ R2 T
            img = cv2.imread(image_path)- ]3 W8 Q5 [* d: B
            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)& }- v+ h# [, A4 Q0 T
            img_res = cv2.resize(img_rgb, (self.width, self.height), cv2.INTER_AREA)
      i4 ~  Z9 Q( S/ e. U9 [( n# b        # diving by 255& B3 p6 z+ {! ^+ h$ P+ v% {
            img_res /= 255.0& M9 n, Z8 `1 B' A* `

    * |$ Y( U1 }  x2 i1 e& o
    8 i, R& I. c9 s% j
            # annotation file$ R" q; a* b8 Y6 m0 m% F
            annot_filename = img_name[:-4] + '.xml'* o2 f  T/ k% W
            annot_file_path = os.path.join(self.xml_dir, annot_filename)
    # p" B8 J2 X/ g
    ( u  l1 ?/ A# ?9 [% ~6 k
    ! X6 H$ y4 I8 e1 _# Q- V; h4 [
            boxes = []5 l/ l) X* e# C& g  ?  {) f8 f7 C
            labels = []  O3 n  J& B6 L4 M* i0 e, C
            tree = et.parse(annot_file_path)
    / ?% y+ h) T7 H$ Q* Y% u* w6 x/ d& h        root = tree.getroot()* L8 m) r) n* [2 n1 ~5 x7 F9 V- m
    8 k- G$ ^# {0 U2 J

    ; @+ n7 h( b8 u        # cv2 image gives size as height x width- h0 f  k5 d9 h: `& z/ u
            wt = img.shape[1]
    3 L# m0 H5 _1 I6 ^4 r6 s: J$ I( y* \        ht = img.shape[0]
    0 o- O4 G, r. M( F2 b2 G5 O. u. o2 q0 O7 o; U9 w
    3 \( M/ z, e, \- ^5 u
            # box coordinates for xml files are extracted and corrected for image size given, n( G" v* w* x3 _) h3 G" s
            for member in root.findall('object'):- Z- c+ g# D5 V
                labels.append(self.classes.index(member.find('name').text))6 r" D. ^6 m' t! [2 E
    , D, h; a/ d5 {; C1 A% v
    8 v6 B/ G& @8 \8 b1 A" ]
                # bounding box
    7 d8 f2 q. S: C2 J6 B4 t            xmin = int(member.find('bndbox').find('xmin').text)
    - E. h$ @: X, ]- m4 B- ~7 o            xmax = int(member.find('bndbox').find('xmax').text)- G# A5 v3 c& D
    8 i  V$ D& w' z3 v

    & L0 ~5 _) i  I' Q+ g2 S& f2 i            ymin = int(member.find('bndbox').find('ymin').text)) Z. \) V  v" n- e2 _
                ymax = int(member.find('bndbox').find('ymax').text)
    , {) L* g1 L1 J5 A( s; u( ^, M: X( F8 v# l6 g  V
    1 f+ }% K6 K/ z( i5 B2 m
                xmin_corr = (xmin / wt) * self.width- r- C  l1 T6 f, A' W+ G
                xmax_corr = (xmax / wt) * self.width
    9 {$ P. g% `7 Z: R9 c            ymin_corr = (ymin / ht) * self.height
    ; [+ K- t; ]4 r. z( o' N            ymax_corr = (ymax / ht) * self.height  k6 a3 d5 ]3 I  \; |  I* \
                boxes.append([xmin_corr, ymin_corr, xmax_corr, ymax_corr])
    6 M7 U& j7 N2 {: r/ p" T6 e8 e
    ; K7 ]* Y1 W! W& U: O: G
    8 E6 F+ F# r, _7 _; v
            # convert boxes into a torch.Tensor
      w' Q8 J: ]4 \( L/ i! U/ p        boxes = torch.as_tensor(boxes, dtype=torch.float32)9 B! E4 P- e( `6 _, l1 S- l. k( P

    ( w; W- k2 K& r* f: D/ E5 m! W0 i

    : K2 `0 g3 c5 g  O+ p2 k0 i% u        # getting the areas of the boxes3 @8 I" x& R# {
            area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])' y% m2 H) ^. p8 G, d
      [( ^0 j! J" X8 u
    9 J  c! e0 X" P' h# m
            # suppose all instances are not crowd) J& P5 z* t4 j1 [
            iscrowd = torch.zeros((boxes.shape[0],), dtype=torch.int64)1 s/ d" W- o; h* q/ j( U
    % f6 n/ H$ Z- L) i5 W
    % B* y; U3 N; E% m  F
            labels = torch.as_tensor(labels, dtype=torch.int64)
    8 b" `) d' m" l6 b0 M
    6 u/ E. I/ f% G, U: `! e
    0 i' K9 R7 B! U2 \3 ]/ G& V
            target = {}* `( ~' |6 D- ?# V' m
            target["boxes"] = boxes
    ( u- h' ^8 ^- D4 Y+ E4 z& |  y        target["labels"] = labels- ~- ^7 G# y5 F: n
            target["area"] = area
    ( F1 Q8 K& u8 I$ P2 u; n        target["iscrowd"] = iscrowd# h$ T5 i1 N, d4 D/ j7 H' R" C
            # image_id1 {% Y5 s' _! g0 H) a
            image_id = torch.tensor([idx])
    # l0 p$ ?5 Y& H+ m, M% I        target["image_id"] = image_id
    ! K7 J) Y5 h+ n/ f! R, a4 E( Z" I0 Q4 f! G2 ?+ ]
    8 G+ [* j- Y( E' g5 C
            if self.transforms:0 e$ r' }  @( b$ s' X8 b
                sample = self.transforms(image=img_res,
    # ]( i) |3 j' e1 x                                     bboxes=target['boxes'],
      G; n' c7 Y0 r                                     labels=labels)
      }/ f, E7 V8 t* f5 x1 H% y# e! s# w+ T+ J$ |7 @$ d
    . n3 C" m) }" z* p4 A5 L! s2 K
                img_res = sample['image']
    2 K; ?3 I# @: J# V            target['boxes'] = torch.Tensor(sample['bboxes'])
    ! V: o( L* c: C9 r8 ?' O, C, E. |  H" ?

    & S' T3 }* J4 v' E- H        return img_res, target
    ' g& A7 w9 `4 q; {! T7 A/ u  v" C+ j) ?) X, y' V- |
    / j! D4 j' ~; b/ P0 M9 u; D
        def __len__(self):( v! Z3 X% R) `* l, S+ H
            return len(self.imgs); S( {. [  s0 m2 c% |4 _# l3 ?+ g

    * Z+ U2 _* C! I! Y  K

    ( R& i' Z5 _3 a' Z5 J' k! }* {0 T# function to convert a torchtensor back to PIL image
    , h" y1 H" m# O, p- vdef torch_to_pil(img):
    " Z& L3 p# P% ]2 C    return torchtrans.ToPILImage()(img).convert('RGB')
    8 Q4 X+ f3 G$ Z/ D- V/ V8 m' C( b6 k# k8 w* z; p
    + v" p, \% A4 _/ X- F/ I
    . K: {; ]" v/ v; c: j

    / m$ Q% h+ i' r% j1 hdef plot_img_bbox(img, target):. a" D; w' z6 X6 E$ D( e
        # plot the image and bboxes
    $ j" O. |( l6 S/ y1 g    fig, a = plt.subplots(1, 1)
    - v. \$ _$ O4 t; c8 k    fig.set_size_inches(5, 5)
    : B: H% E7 O* c9 z1 ^: |+ P2 Y1 u    a.imshow(img)
    ; M% G: e8 e# K; ?    for box in (target['boxes']):
    ; |; p) D% t6 U% `  p; p3 v        x, y, width, height = box[0], box[1], box[2] - box[0], box[3] - box[1]' d) V" O. V% }4 \+ h4 m
            rect = patches.Rectangle((x, y),1 E& [* t+ r4 F3 [* J0 s0 R
                                     width, height,
    8 i% ^' O$ B; w+ z                                 linewidth=2,- k8 j* G6 [. i
                                     edgecolor='r',3 y0 e: ]' Y+ w/ x
                                     facecolor='none')& f) N, p: b" R& l' {* i$ \

    3 K5 w' D# w+ A) }- y1 F
    ! ?% D% W4 D# c( m/ {
            # Draw the bounding box on top of the image1 I0 B% J$ [7 e% N+ ?/ s0 A" m' ?
            a.add_patch(rect)( K, ?1 s/ _1 w7 M7 t, x
        plt.show()
    ' M) K7 M* s+ F1 k$ Z4 f! Z3 A1 o. I. ^, w9 @% b& Y: o; m

    6 l" k4 F0 ^1 }2 V' E0 l  b/ {! @! o, }. L

    0 q, ~/ w$ d5 K- Y0 I) w" Ddef get_transform(train):( Y$ a9 ?7 o2 |. P1 o" Z1 \) o
        if train:
    7 T1 y( y7 j' j0 N        return A.Compose([; R. X8 C: x) p0 i- S
                A.HorizontalFlip(0.5),
    % {& T( {, v  ^            # ToTensorV2 converts image to pytorch tensor without div by 255
    ' Q* T* l/ R& Y' Q            ToTensorV2(p=1.0)
    6 U8 H: `9 M! S+ [7 m1 G1 B( j0 B        ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
    0 n! Y: v7 g$ t6 y( B    else:
    ' w7 Q3 J3 R5 R  I: b+ a. m        return A.Compose([1 X, h6 j* J. {
                ToTensorV2(p=1.0)
    1 l& S7 G% Z+ C/ Y        ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']})
      H. x. C) i1 G' I- Y8 U  B
    ; F, c9 }# f! g3 a
    ; Q- W- e! N: C" e

    ! R! r. }- \7 [  m+ O4 V
    5 q( C( E; V% s6 }; o% _
      F) c$ J! F  f+ z  m
    + A* A' ]$ p9 H2 u$ C6 Y
    dataset = FruitImagesDataset(train_image_dir,train_xml_dir, 480, 480, transforms= get_transform(train=True))8 t6 H8 f$ p( B1 |  d" Q1 A! j

    5 E/ S' T5 E$ ?# V' L8 ?

    - C! P  \) e; O, |  [. ^print(len(dataset))6 w' B: I8 g5 _7 n, b" j0 s4 B
    # getting the image and target for a test index.  Feel free to change the index.
    6 x- H9 P4 u7 _( f4 R' r- N7 Cimg, target = dataset[29]
    ( b2 j* f/ N8 ^& q3 Y6 n- hprint(img.shape, '\n', target)
    " a7 l+ d1 ?# ?( ~plot_img_bbox(torch_to_pil(img), target)
    " g' R/ M, y4 |1
      Q& m' R8 L' F9 N, }2
    ) O% C3 h- {5 X* G$ j37 j2 h; s4 n5 D1 X7 \3 R
    4. z( X: Y0 D3 f$ i6 i4 k, u& w) g
    5
    ' y: g9 u7 O* F4 o0 ?! g; U2 f6+ \8 u. u+ q1 m8 I9 r3 i! j
    7
    1 Z  M' p* z1 `' M( X6 d8
    9 v# O5 b; k+ @" {+ b5 i9' p9 k+ q5 W3 a$ V
    102 j( d0 m6 o4 I6 S, i# C
    116 L- e; O! K+ X5 H, C3 k
    12
    " R) f1 F, z$ }9 o& y136 ?3 }& J+ g4 G* x/ R( h
    14
    7 r) a5 V9 ~2 {  g8 ]  [, r15( `! }% E0 E$ }, T  ^+ w& u1 g
    16
    - H+ S9 n! @' n/ R8 Q! M6 T17+ ]" s: ~2 u9 J3 c
    184 j5 r5 C/ {8 C% I( y- O* l  }
    19# n. K) w9 |% C1 F
    20
    2 L, m# j( M0 x% E21
    . }& V& T, P& j. ~# b22; i4 h3 y" M+ C* E, ?% y. F. q
    234 ~$ z7 A; P' B
    247 D* c6 M' h5 m3 u+ Q
    25# L- @! P1 F4 b! y) w, t$ g/ \
    26
    6 o( h6 V) l7 m/ E- v270 o3 z$ ~) t7 m( {
    28
      [! z6 D; j( e4 f291 {+ K$ S& ?! l- V1 S! h
    30
    8 F6 G0 y: P- ~31
    ( [" Z! m/ r, t; L+ Z' v; L32( l3 B+ {* \5 I' L
    33
    . a- k% M% t0 d* b  {+ Y34& `+ r6 D. \6 P0 o3 k
    35  R& o8 s0 z: g  a, Y
    36
    ; }9 V! x- j0 l* k) [. R% g& S37' A+ G: R3 Q4 ?
    38
    + R8 B  g0 L: t: J9 o; i) [* Y39
    . @  ^1 X' W/ a* b; w  c40
    / F  R4 H: A" z41
    & e! {* A: ^6 W' p42; I" v3 _- F9 z, s' H! U+ z
    43
    ) C) I0 K/ L. x; ^" m* W44
    * g+ i! w# N% Q+ q7 t5 ]7 n456 ^. P( |) |8 |3 Q: d, e0 v
    46
    ( z9 h1 F9 U9 Q! p& t. f473 J; K6 r6 n, Q
    48* I" |9 X; A4 S5 `+ p$ H: \
    498 q2 V' u: R5 q3 @
    50
    : A7 l: |2 F4 X51
    ) z' V4 G! Q4 ^5 M1 C* `8 U527 Q- |& R7 ~; C# c
    53( t, ^8 m* y' L7 Z
    54: V: S5 G+ v5 n+ d
    55
    7 y+ C/ a) S+ C& @0 Y7 |+ U; r3 v56$ y! L" `5 X+ H0 d
    57
    . P/ d+ H; u  j& ]1 c+ _3 A58
    3 i+ D8 y- W1 X: c1 X59, M/ Z! o6 A3 ?
    60
    1 o# R! T. _4 g2 a; a: j+ U4 s61) _5 K& z( F/ x( ~
    62$ g0 B& M; j  B
    634 ^3 H; X* D! Z- g7 A
    647 [; P' q0 x- H. Q+ e
    65; q" G- P: ?% ]! I* W
    66# c9 D6 `3 c$ S; \) z: Q
    67' ~5 `1 k- q, n0 E
    68
      e6 @8 Y; K' b" f; [5 ]69
    : ]- U; I1 U- M' s* ]0 O70
    & k. D! _0 C# p5 c71, D+ ?; I  P" y2 g& w, g
    72# e2 y3 V7 ^2 `4 c# M8 m
    73
    2 t# ^+ R) n; O1 K  o7 X# O74! A9 z5 q0 Y. o: O' v
    75
    5 l" `7 y. z8 @" x76- Q6 W/ U8 n7 Y- ^3 X! d
    774 f' w9 w; O7 l) x( }) V5 ?, }; U
    78
    $ M3 X7 j& H; f5 m6 Q79' C! Q) V' T% c: N( M/ M- a
    80
    : z2 B; c# o- L; {9 ?7 y2 n81
    5 Z5 m( Q! {1 a1 g2 A/ L7 g822 P5 R6 ?. m7 Q. C- m7 b
    83# _( n& |; ~# Z" z/ Z, P. V3 Y
    84
    3 n8 H# Y2 h7 S+ R3 F- v85
    8 i! I* s5 l2 A86
    3 d6 e" z9 X: q/ c87
    + x5 v- P! l* w+ t: s7 W) s88
    - c# |5 k$ }; z# u: C893 a1 H2 \. d3 n, |: b
    90! `  ]2 f2 O$ b% W* a
    91- T3 E8 \8 d! c6 u' f: O9 l! e
    92( a7 ~0 _9 @% q- C8 U: ]
    93
    ; J" w2 ~+ i5 m  E94  n% C, G$ H; U* Y2 W. a
    95
    / j" V8 r' U! @96
      \( Q. R# o6 Z* Q97
    $ p9 [( _' B) t# N981 C5 l4 E5 A- \% X4 J2 J
    996 p2 G9 t' k6 U2 U6 @9 L
    1001 v  V1 \8 x8 z
    101/ N8 X1 o, z) ^8 Z0 w
    102
    5 m' T1 E# J4 F( {3 k2 j! v103+ t/ f: z2 Q; U& K0 i) D
    1040 r& f* q" x  A# K5 w. D( @/ o
    105$ W! z0 |8 E6 V2 w0 L
    106% I" b  n' ~0 |1 T4 l* ?: Y1 T$ W
    107: _2 H% u1 q! d. s6 ]
    108
    ) l" M9 ]: Q8 j3 D7 V, S" s109  ~+ |( }# I; \
    110
    " E: l5 W0 e5 ~8 t111* P/ m) a( U+ ~9 z# ^9 |8 f
    112$ e7 q7 J5 @( e7 |% m* o. D
    113: q2 o' y! ~1 F# V5 ?; d9 ]5 J
    1147 f4 a6 n& H+ a8 t8 O& b
    115
    + h, V* G. A. o116
    4 R2 w  ]$ ?1 S7 V! \, c- h117
    . F! _; s7 w% ~! V/ K6 L1 Z118' z( u0 e+ l; }4 n" h( z0 D# A
    119$ ~" O+ |+ {' @  F; M. ?
    1204 s0 w% y; C9 a) ?, I+ s8 Q3 J
    121
    0 `+ ^& L2 `6 d6 \, P7 R7 x1229 x$ ?$ v: d& `+ z" B5 F
    123
    - v+ ~- x2 o; |124
      B9 b# z/ d( I- `1252 `: i+ T( _  w8 s/ o
    126
    ' H& O4 ^, h& o$ B- F0 W4 }+ F( n1279 ?+ q( i! ?# ?  r- h
    128
    . Q7 k2 R4 ?  b  B5 @% [129
    $ F4 O, u3 r7 r9 A/ Y4 z2 Q130
    ' G) }  ~  K9 D8 ]131" O3 L9 j# t9 c/ B9 E( @+ u  ~$ `
    1329 u* \: r2 `  M5 c1 }- f
    133
      T& j! ^# D8 ?2 Q' Z1349 s; w8 a/ g8 h" N& t- Y6 @
    135
    % y2 z$ y" G" q' S8 X9 t1 X136
    7 A! n" ^& @; U" o, t# ~1378 p4 d* w/ P: E) N" j& b
    138
    0 y$ ?. }. ~- x, ~( B& ?1397 `/ y; O/ T8 b
    1400 Q" L6 U  l* L4 t& I) }/ T
    141
    % d9 `- l; n" L1423 H4 S+ e8 X' S
    143" A" n3 O2 y+ W; [6 l' r! b, I
    144( {; e+ a) S+ q8 `
    145- a( x6 V# \  p+ i7 N
    146
    & K* I2 e& u" G* [1475 e; T$ D2 z4 G/ |& l; K
    148
    5 t+ l9 \3 v# C5 |2 a8 a5 \. Y) ]( e149
    + ~! L+ u! J5 w1507 f$ `+ z, t; y. y# U
    151
    - N5 C# z9 l& e5 W( R152! B8 o  w7 e- @( [. v5 B
    153
    2 t2 S- ^  |4 j2 q( G% _4 V6 P/ b154$ Q/ I$ l$ r. C6 R
    155
    ! S4 j% U6 r. H# \9 U156
    ! m+ y0 B$ n! b: y! {7 l: }6 O输出如下:
    ( I5 P1 T" M+ X$ P. q4 f( a- C6 `2 b8 F9 D

    3 N. D* f2 \7 v: f- T3 z0 Ptorch.Size([3, 480, 480]) * h$ e1 f; C! {
    {'boxes': tensor([[130.8000,  97.8000, 327.6000, 292.2000],  l  r4 X6 |1 U) l! w1 i
            [159.0000, 268.8000, 349.8000, 427.8000],
    ( I* z5 s' y9 f& s- {. n        [  0.0000, 282.0000, 118.2000, 429.6000],
    , ^, `* s+ I. C        [ 43.8000, 107.4000, 199.2000, 280.2000],
    3 T# w  Q9 S- K  b  Z) ^        [295.2000,  37.8000, 479.4000, 248.4000]]), 'labels': tensor([0, 0, 0, 0, 0]), 'area': tensor([38257.9258, 30337.2012, 17446.3223, 26853.1270, 38792.5195]), 'iscrowd': tensor([0, 0, 0, 0, 0]), 'image_id': tensor([29])}$ G9 `* U9 B" \, f$ p% c
    1, R& U: U, @$ s" f. t& j
    2! f' u. q9 n$ C
    3
    + \6 u( J$ y# l% `* X42 D' p6 _/ l4 Q! ?( _5 `2 K
    5
    & U. L8 G- y, T2 }* I6
    - v/ i+ A/ P' W$ c: Q3 ?3 n% O1 T1 {8 ?! q$ \6 ]
    ; A$ R$ Z+ `. Y3 l! h; u8 F$ B
    4 e, L5 @% D( a+ O1 v* M$ ^! |

    5 W: c' j7 ^: V+ W; o7 h; j下载地址
    2 K* y. M7 t. U+ ?1 v+ c. m链接:https://pan.baidu.com/s/1QZDgeYTHyAlD2xhtJqZ-Yw
    % e4 J( }2 C. H- i. k1 y提取码:srjn8 z, x' U+ m0 i. p" z$ I5 G
    ————————————————
    3 o. j8 b" O. E: o, I" Z: \1 a版权声明:本文为CSDN博主「刘润森!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。2 Z3 n6 v" D4 }
    原文链接:https://blog.csdn.net/weixin_44510615/article/details/118496273
    + j6 e0 _6 g, _9 E+ V# h! n" `2 o  r) d; M" o' m+ x, p( n
    ! o! g5 c/ M4 E' N5 ]0 @
    zan
    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

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

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

    蒙公网安备 15010502000194号

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

    GMT+8, 2026-9-27 15:59 , Processed in 0.395439 second(s), 51 queries .

    回顶部