7 {! M4 \# W* a & D s' ]9 N' G. F# E+ J“打开目录”打开图片文件夹,选择第一张图片开始标注,用“创建矩形框”或“Ctrl+N”启动框,点击结束框,双击选择类别。完成一张图片点击“保存”保存后,XML文件已经保存到本地了。单击“下一张图片”转到下一张图片。 - E# F* g; T6 L3 _" g4 n5 u; T: x# W& ]$ K" z
3 g3 S: l! c6 e \+ b, r8 G- B
贴标过程可以随时返回修改,保存的文件会覆盖上一个。 $ K. K) A7 _! `& v& o+ A& A* Y+ s. f
$ R- h* d9 `; H
完成注解后,打开XML文件,发现和PASCAL VOC格式一样。 3 R, [+ \/ f2 t8 g ) n: |& b; r w g( ~ 3 E% S$ M% l, F3 a( L将xml文件提取图像信息 7 W! ~2 m _$ E3 @: t L2 f下面列举如何将xml文件提取图像信息,图片保存到image文件夹,xml保存标注内容。图片和标注的文件名字一样的。 " L9 f1 w$ S v# ]/ }8 j4 @, A3 s6 y7 e4 Y9 [; p
9 ^& D% o4 s6 M ! U" o" E M7 c# O* |4 x; m3 U+ y+ N# [4 x4 Z
下面是images图片中的一个。+ Q; P2 u# [3 p, V
+ H) ~. L0 [# C7 w5 v+ N4 q- M8 w$ [ W; j) ^
下面是对应的xml文件。 " Q7 q1 o; k, R: B4 `8 }8 _ . Z+ c9 F9 e8 `$ q; n) B D L; f7 l& f
<annotation> , g4 t5 ?/ D: G: L+ p <folder>train</folder>; ?. w* B2 \. N4 V
<filename>apple_30.jpg</filename> \' @6 Y! r) O5 m6 l
<path>C:\tensorflow1\models\research\object_detection\images\train\apple_30.jpg</path> " [8 r4 @3 c6 G* h) C6 b <source> . y0 h& E' d x3 O0 F <database>Unknown</database> + A' U v" W1 i; j0 U- j5 U </source>, z$ s: w- O: J- P5 C) Q F
<size> # X1 u7 J3 P1 L, v, M% A <width>800</width> / \% L/ X; {; Z1 B' I <height>800</height> ( O! P5 g: E2 q3 l' y <depth>3</depth>: B& i7 P) H% ^' B B% f, e
</size> ) k5 j j6 H0 O, H0 \ <segmented>0</segmented> 6 i7 D3 N! o' j. x A" g# i; x- [ <object>0 X ?# d$ q. h8 B
<name>apple</name>+ _2 v% o( t& U8 G' }
<pose>Unspecified</pose> + l; @1 {8 Q+ r! K5 T* F6 M <truncated>0</truncated>8 \* K. s* d0 a. F% W9 _
<difficult>0</difficult>! c+ ^7 i4 s& b- o8 C R+ C% q. u3 {
<bndbox> : l! T$ {, _- D3 e <xmin>254</xmin> ^ X# i* ]& R <ymin>163</ymin> $ O$ x3 @0 U3 J$ l7 s% L8 }: Y <xmax>582</xmax>& |" v- q) M) P' |' W
<ymax>487</ymax> b# q, M3 o( f6 i1 p# [5 l
</bndbox>+ {) i+ g5 b4 K
</object> 5 {/ D6 F8 H$ S, N; ]0 ` <object> ' r( Z3 |" [: e9 \' M <name>apple</name>6 v8 C' u2 J+ C9 s" L$ Z) S/ C
<pose>Unspecified</pose> " R ]# p3 B P6 L- B* c& p( [ <truncated>0</truncated>" K+ d5 }" R( P' Z1 s/ L
<difficult>0</difficult> 8 p9 Q- h) H! q- a <bndbox>) k4 w2 x# F; I# _0 q* T) c) r$ q
<xmin>217</xmin>" h# F4 W4 d3 J# }! i9 n# f/ m
<ymin>448</ymin> V+ y" M; @6 h9 B. z
<xmax>535</xmax> 1 n6 F( a5 |( {& Q! c* G/ p& O <ymax>713</ymax> # b g- w& g: M/ p! e( z </bndbox> 9 m: k6 I, n+ V6 P! _' f </object> 8 L" j( K7 r; V$ c- p <object>; a6 p9 N# L/ i% X
<name>apple</name>6 B' r& p/ A, r* b' p; q2 n. g
<pose>Unspecified</pose>1 L' F- @$ E3 j; }& O& `
<truncated>1</truncated> ! U9 S. c/ ~4 `0 }+ Y5 o <difficult>0</difficult>+ R3 U& n' w2 B+ R( E" |; S
<bndbox>6 r9 R/ p, {. o+ ~; x; O: ]
<xmin>603</xmin> 5 h0 m4 Z% l5 h! T) n1 M <ymin>470</ymin> ! ]6 j0 {1 o5 O8 s5 c <xmax>800</xmax>( t$ E2 u- @% N
<ymax>716</ymax>) f2 E; H, C% h. T! a8 _! ]) b' D
</bndbox>0 m: S% ?7 H5 b
</object>( Q/ C9 u3 t9 ^5 A2 Z7 d$ {
<object> : p+ X6 E" T' E <name>apple</name> ( f V6 }$ r! A8 k" A6 G <pose>Unspecified</pose> ^1 U4 c9 p, C/ r1 f8 r4 B2 p8 z <truncated>0</truncated> 5 I& g$ p* l0 @3 d" M1 M <difficult>0</difficult> 5 c2 [! L4 X9 q+ d; f <bndbox> $ Y- |" a) k- t; a, P% r: r, V! G1 |3 K <xmin>468</xmin>/ |1 ]& f' V5 R& \! e/ @
<ymin>179</ymin>" r* Q/ x# O" W7 D
<xmax>727</xmax> ( ?3 w& u. D ^ <ymax>467</ymax> . c- u d- y$ v8 O( H% J9 @ g8 I B z( v </bndbox> 8 P/ R8 u) C) @" G* ~8 O </object>) f* h: M- q# e! z4 s
<object>- q6 t% ?: l4 H
<name>apple</name> ! }3 |- j2 L2 C+ B2 f; w2 n' E <pose>Unspecified</pose># X' F- D# T2 c
<truncated>1</truncated> ' Y+ Y. v0 }- U4 j$ c <difficult>0</difficult> 9 F1 d, w$ }, e* G0 Y <bndbox>' l. S% L, D* l5 |2 q. T
<xmin>1</xmin>& V4 U, X( ]3 {1 a- t% j
<ymin>63</ymin> ( C% ^$ h! o* ?0 s2 j <xmax>308</xmax># t) M1 X2 w: F6 a7 l; |
<ymax>414</ymax>0 V: q$ l1 B3 g/ r$ k
</bndbox> ! I4 t8 q4 ]6 K( y+ e </object>9 G. k! V- T' A# R3 U/ f! i \# D
</annotation> ! C$ B, M4 D% k P! Z4 Q4 S8 p) z1* q" ^9 d# f4 [- b; o
21 n5 ~: `5 [, O# i- d! [6 e$ {; q; e3 V
3 ]9 f+ |/ @4 A. M4 7 j3 t* R" t, A7 @, [" _5 9 l8 V8 E' Y, T+ Q+ @3 p- s6 N, r1 d3 {& e; @: c7 2 {# \2 d0 g' Q% k) r8) M# O* f7 ]% c6 y6 x( y v
9" Z) N& @# }+ I% w5 y3 N* I
10 ! i2 F5 @+ y l& o( V1 ^11 . R9 P2 J7 E: B: ~( j12" X& Y* H/ j- @6 N4 m7 ~ ]5 q! t4 f
13 + H% n0 \' h; @14, ~( s2 a( r1 @
15 8 Q3 ?" @" |; T$ ]* V/ m0 v! ^8 ~/ W/ V16 6 q. O% k, ~4 ~* k6 e/ v17 3 T0 O0 o9 ^& U3 u18 ( p0 b" O/ `6 e8 \& U% c- S19 2 p! d/ _! `' [1 n% v s20& j! }1 W' |9 P1 o! l. e
21 ' u3 l. _* ~4 q" o9 ~- y22 - Q7 y$ [$ Q4 s. @. f8 }% ]& n" |23 9 ]- q# O8 { K# t* Z* I; {" k24+ c( x( [! Z# Y, F9 i
25 $ I8 e0 @/ O/ g% ]* T" r& v2 c j26, z" U4 S9 `7 t+ V8 Z1 C/ ?
27$ @* v1 ]. y7 }0 X& ~1 q0 U7 I
28 5 z% r# B% Z6 T1 Y3 t$ [294 k6 c S' K9 G& j/ b# X1 p
305 A' X2 h4 X2 j' X9 [% |
31 / m4 r: H4 f3 ^3 ^* \1 M; r+ M+ I, ^ A32' O( L6 B4 p3 G
33: e7 j" w6 z7 u
346 Z' D& Q6 K5 m- ]
35$ C& E% y/ k8 O: W' l# z$ n; V
36% ~' Y. @( T1 |; f* s5 p: a1 ]* A
37 * [4 b1 F: |7 X5 x- p' _* g4 k38! V x8 J! }- W' i1 `9 ]
39# w2 P* _/ j J9 c+ {
40 . V1 d- L, r R41# I4 E/ Y# d8 c" [
42, m) H! a) g: ]1 k1 U
43, J; X' N8 p. R
44: m/ ?( x& X- h% i( ^
45 . z% ~, O0 a9 x1 ^) o5 [! N46- H/ d2 K+ ~7 V+ S+ w$ Q% ~
471 S4 p/ ]; g2 F' _1 [: ^% T
48 6 M( {3 K, R1 j& u9 V7 k" z49 ) x! x* X4 I0 E50 4 L) u$ x& Y8 h, b# B- T- x517 E+ ~( I& W+ f7 }9 d- n
52 7 j. s2 k% [, h$ F53/ l; ^ t- a7 D/ X
54* R0 E2 v; ~) a9 u- `
55+ N0 T4 Q, [/ H' k3 @: ^
56 8 t: I! e7 ?; a0 g8 |; m) v57; }: J. ]- E! X0 [3 G
58- X. k! p5 @. G( u0 z8 a
59 ) q; Q. V8 G( r60% O+ _6 ?1 Q9 E" @" R
612 f% ~$ R9 ~9 Y# Z
62 . t. `; a# d M" q* B! n9 C63: O& i0 k1 j$ V$ v
64 # A3 e: `/ t* `0 D3 C65. e0 L0 Y$ L" r" s1 r
66 $ A1 [1 b! t1 J+ U- A; I4 G67, m1 f$ j/ K0 r( A
68 % r$ A4 f- T% k3 G* }, @3 r6 ]69 & g" Y/ u* E# y7 ` O) d) H70& x3 D. |! e+ H0 l0 L! e' h l
71 0 ]' k) E! O, a# ~* A72 4 A% d+ X, ~4 H D3 |5 k73( R" `) B( J: d8 C9 }2 J6 [
74$ O. c! r* y$ o# ?% z$ T. O
将xml文件提取图像信息,主要使用xml和opencv,基于torch提取,代码比较凌乱。 , F2 g: n/ ?1 n1 i9 r& Y( [! S' p3 y/ m7 I) h' \
: C: x6 e' k1 s" j1 }) _$ K" e" mimport os + q. |4 @# } r) g+ Jimport numpy as np6 L: x, p. @4 I0 G3 A
import cv21 H+ q& h" ~4 N; x; ^+ X- a
import torch( I, F4 R/ A3 c5 G9 W
import matplotlib.patches as patches 2 W, F& D: }/ P% w5 Y3 a& simport albumentations as A |8 x) I b. M) l5 L7 w* Y2 `& N
from albumentations.pytorch.transforms import ToTensorV2 2 G+ t# j/ N, W6 g" Mfrom matplotlib import pyplot as plt) V% t5 ^0 g( e* V4 d
from torch.utils.data import Dataset4 n" ?3 H1 x& D2 z3 I/ t0 w# [
from xml.etree import ElementTree as et + Q' _# ]4 d0 q* Ofrom torchvision import transforms as torchtrans) y' d* H- V/ M" i9 O
) G- t% \& q. U2 i; M 9 _' a; v# W2 L x( l3 b# defining the files directory and testing directory( f+ y' h: Z& C( Q
train_image_dir = 'train/train/image' 1 e. @7 z1 ^3 R/ u$ Y8 Vtrain_xml_dir = 'train/train/xml' 9 ^1 F, p8 M5 f; g3 A, X$ j# test_image_dir = 'test/test/image'+ z( I5 j1 E+ ^) z6 z
# test_xml_dir = 'test/test/xml'; _1 x2 O& N/ j
6 N5 j1 Q1 b- k
' X% s; }. ^% M; C8 K
class FruitImagesDataset(Dataset): & y" j% t" Y" r, P; m# P" |0 T5 j8 e, ^8 W/ h1 N& ~. G
" H8 {: ]) f. t: B def __init__(self, image_dir, xml_dir, width, height, transforms=None): B7 `" u7 \2 R7 z0 ~: [2 o- N self.transforms = transforms; c8 {, g7 b' c& v
self.image_dir = image_dir0 m a$ @6 }0 n P# \4 a7 ^3 {
self.xml_dir = xml_dir ! M4 r$ N6 \$ U9 e self.height = height 8 r* q5 f5 W, H" o$ j6 u self.width = width ! B, v2 b w6 V/ @" B1 ] , e% t* b. S0 C5 j+ \+ O2 l 4 _( h9 J( a2 b( E6 @ # sorting the images for consistency : o/ B2 @6 T/ I7 N: n% g # To get images, the extension of the filename is checked to be jpg+ o! ^0 R' b0 ]$ O f
self.imgs = [image for image in os.listdir(self.image_dir) , U# H4 H! ?4 P% J& b if image[-4:] == '.jpg']) k' r( V3 k$ y0 o) w
self.xmls = [xml for xml in os.listdir(self.xml_dir) 3 e2 Z6 t3 {- n$ L' h n m if xml[-4:] == '.xml'] ' [1 ], W8 q$ k* t6 g; c8 n . @" U; p$ P) E5 y% P* i6 X' K) c3 b' }0 v' G4 @) n) F
# classes: 0 index is reserved for background 3 |2 [# q- d& A self.classes = ['apple', 'banana', 'orange'] : \; l1 }7 n* r! C9 o 3 ^/ \( j g. T4 t! K3 W0 X# @9 e% e$ y9 G3 A( `
def __getitem__(self, idx):; ~) K$ `" d+ R2 W7 P* l! q8 e/ \2 ?
3 N) f2 d! r/ x0 [6 b+ R1 z4 Q/ ]
# t( y, c$ T: t, }) \- {- U img_name = self.imgs[idx], V3 L' I% u6 V
image_path = os.path.join(self.image_dir, img_name), \. Q3 c* ]8 F. A8 u1 c6 G
7 N! _) X/ e$ W8 U3 `
. R* ?/ [! {/ _1 @* v; o% \
# reading the images and converting them to correct size and color 9 P) W5 f* F. ^/ p+ o. Z) |; U img = cv2.imread(image_path) ! E0 N. E: i: r$ l img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)* E& v. S" i3 }6 F
img_res = cv2.resize(img_rgb, (self.width, self.height), cv2.INTER_AREA)! O. U, c9 s$ m: f! Z
# diving by 255+ u# C! a: Y2 h, I! K F
img_res /= 255.0 ( N ]+ m" @( I: @) e1 C 8 y4 H6 m2 P/ U ( Y/ }: ^$ M5 N, V& b* n # annotation file& O8 g$ T" L& W) [$ U
annot_filename = img_name[:-4] + '.xml': D$ `, M8 e' m- y& y( r
annot_file_path = os.path.join(self.xml_dir, annot_filename) " Q/ J3 X. G' I 1 Z; z/ |2 b0 |6 z- R+ O% p* q) I% ?' ~; Y/ s6 [# ` s( r: ?) e
boxes = []) M' Z: f: |" B$ w2 i7 h) S
labels = []% z& @, ~8 \- D1 x& x- N _
tree = et.parse(annot_file_path) ; l/ c6 P- r! B' z1 | L% P3 S, j root = tree.getroot() ) R# z3 D* Q( z4 \7 z( z8 l1 d( g( _ Y
) u# a9 {& S& L' H8 M # cv2 image gives size as height x width / e% x: a0 h& z wt = img.shape[1] 2 V- c% {1 m/ ]4 o! x) Y# E* s ht = img.shape[0] 6 ]8 _0 `& O) ]' a# x: @& y2 a: u$ m- X. t, H* u$ _4 o( P
, N2 @4 p0 d: [% l* W1 ?; | # box coordinates for xml files are extracted and corrected for image size given " w) ~# Y" `+ I: q9 r0 X+ }0 ] for member in root.findall('object'): , Y c2 h" m) P7 \& l. y% E/ I labels.append(self.classes.index(member.find('name').text))9 N. Y8 V F0 T) ~* T3 Z) s1 f* ]
+ c4 | m5 ^' T& J" S- l
$ ~: s) ?5 F, k0 k # bounding box% w# Z3 l! w' Y2 u
xmin = int(member.find('bndbox').find('xmin').text)2 A/ o/ \% `, }: f1 M
xmax = int(member.find('bndbox').find('xmax').text) ; y, D' f" {3 m6 N' B6 @3 D 5 o' H1 x& k& l" { ~9 U4 X. y! o: U6 B' o( V9 p
ymin = int(member.find('bndbox').find('ymin').text) [" J) ]+ `% F: _/ l" R+ E/ d7 n ymax = int(member.find('bndbox').find('ymax').text)) L& r6 |( v: K5 N, [
5 v, u8 Q( X, R" r! R7 P- b' N
, X. `% b/ C7 m4 D5 ^, m6 j: i# b xmin_corr = (xmin / wt) * self.width0 b& ]6 g1 Z$ o+ q2 g
xmax_corr = (xmax / wt) * self.width 2 A D& Z5 m; B, H9 ], w ymin_corr = (ymin / ht) * self.height, v$ R _; I' E0 n5 C, f# }
ymax_corr = (ymax / ht) * self.height ( U! {$ m2 s4 ?2 J+ F! B boxes.append([xmin_corr, ymin_corr, xmax_corr, ymax_corr])8 m( H$ N7 x3 [1 f: r& ]% g
( i9 p4 k% S( M, k, P
; o! B* u4 K1 P5 o2 J& q # convert boxes into a torch.Tensor2 A3 o1 G0 j5 N4 Z5 H
boxes = torch.as_tensor(boxes, dtype=torch.float32)% H. Q% C: Z# g; i
" S$ V0 D7 U8 j/ ~& c" `) g
+ s" s c; |5 A! R! l6 }, ~ # getting the areas of the boxes 8 h% Z1 N2 ], _# v; J) o area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])1 Z% K& u$ N! g6 u5 S
+ t( g8 b% D Z* ?
$ G# f: W) A% w3 D& i # suppose all instances are not crowd4 ~* e& w- H- y/ `
iscrowd = torch.zeros((boxes.shape[0],), dtype=torch.int64)% x# N5 g1 O0 d$ k$ b
4 ~. P, o. ^. C9 S0 i0 i' H4 ~% P4 ?% B* D; `* A* V9 a
labels = torch.as_tensor(labels, dtype=torch.int64) R' Y/ l) \3 g$ s" N' F
$ c2 s, A' e" ~6 H0 C8 y ?: H9 b2 p' I- j: ~. m
target = {}$ t" ^' o9 x+ f$ _: k
target["boxes"] = boxes . t% o/ l2 P! ]2 p z3 ?2 @ target["labels"] = labels% U' J+ j5 S; y" E# S
target["area"] = area + i% V! f U$ O$ y/ s target["iscrowd"] = iscrowd . @; \ X4 i$ Y! k. B% U( k) X$ X # image_id + L3 `/ f; K8 ^+ h2 w% C image_id = torch.tensor([idx]) ; C5 J) h5 Y s0 Y, u3 o& y target["image_id"] = image_id ( Q0 s+ f7 n3 k0 x! B- p% I : ]1 e, O/ ^* P& l. {' B% u# P$ a4 u2 D, |# `4 V
if self.transforms:6 V) p& y- }# }( q! x, [/ V& D
sample = self.transforms(image=img_res,$ p) `* Z. y; x; V. N
bboxes=target['boxes'],) d: {/ U+ j/ ?4 u3 a% s
labels=labels)" i0 B4 M ~. e' N2 y+ f% E3 |* \
: d. Y# c, C7 m" G4 `4 o5 a & @ y2 M6 y7 P! g8 Q3 { img_res = sample['image']( N# @! V h4 ~& E y
target['boxes'] = torch.Tensor(sample['bboxes'])( R5 B8 ~0 n' r. [! L
: Y( Q3 c9 Y. P, K
1 ?. H2 b8 r; f- u) z) G/ D' W
return img_res, target 5 _$ y, H$ V6 x- N3 s$ G0 L" V+ ]# D% O( T" J9 t2 `
% a/ T' _. H" p1 x
def __len__(self): . }. V! }7 j" L) P9 i return len(self.imgs) # c! o2 Q. |# `" Q8 o" R* @) i& p" v& h; h3 q0 {! o& w
7 ]* P( e: u- U R# function to convert a torchtensor back to PIL image 6 F, d/ @5 N( E+ Adef torch_to_pil(img): 7 _2 G! ], h6 n" D) T1 Z; }$ w return torchtrans.ToPILImage()(img).convert('RGB'): f* u5 P& z% i! J; u
$ \! `/ S3 v' N+ f ( q; {- D8 c7 o _& v! J) M. x5 R6 B# J% s+ R$ v( I4 p8 X1 B
% \0 k. U s) S. [def plot_img_bbox(img, target): . ]7 f9 \% A- K k- ^/ l7 U# B- M # plot the image and bboxes' R$ k9 D+ d7 e$ Y
fig, a = plt.subplots(1, 1)2 x5 _4 c0 z, a
fig.set_size_inches(5, 5), T# `% B @# G: N/ F& d+ P
a.imshow(img) ) I6 I2 P6 G$ l& b/ A- t for box in (target['boxes']): 2 }! Q% V; o% g6 R* {% e8 o x, y, width, height = box[0], box[1], box[2] - box[0], box[3] - box[1] - a* O* U3 ~/ b1 B J" C1 f2 u rect = patches.Rectangle((x, y),3 p; l$ K. }& \* Y
width, height,) K8 ~3 I9 \+ V0 S! l
linewidth=2,4 e( N+ W' p- C
edgecolor='r',& O [# ~- k0 A+ ~6 \: m2 c
facecolor='none') 2 _; m% I+ H% D5 y; D/ n! a! i0 a8 Y. j$ ~' S5 b& W
. }7 f! o' z3 Y$ x+ Y1 s
# Draw the bounding box on top of the image. S' s: ^9 |8 f3 E( ]
a.add_patch(rect)' z; |- K6 K2 _
plt.show()6 ^ \6 A$ R$ _$ r1 f S
) Q) H* z f" v7 c* e. r" J4 i" v& r0 j' r' D p
6 S' B$ N; D Z2 l( Q7 [5 v7 F, h3 _' `1 N" o! ]0 B
def get_transform(train):4 n1 ?: W9 X4 I5 n: M( o- F: W, C
if train: 3 C- V) W1 ?/ \, c n return A.Compose([5 d) V! z4 `- Z; i/ Y0 z
A.HorizontalFlip(0.5), & w% A# N1 R& x, C4 K # ToTensorV2 converts image to pytorch tensor without div by 255. b( ?1 @8 Z' @4 R0 [
ToTensorV2(p=1.0)% K& ^' l1 q3 Y8 @3 f g
], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']}) 9 O i) W( [6 ]8 Z! }- C else:8 n' x# L" l- X* g' }: p8 G6 ~) |
return A.Compose([5 L8 C2 L7 O! ?% |4 O: O
ToTensorV2(p=1.0) % z$ ?, D: V: } ], bbox_params={'format': 'pascal_voc', 'label_fields': ['labels']}) 3 K8 D# h6 K& H* m ( w* h: @: p1 o m$ M9 ]- }# b# K6 b
& s' f3 ]) E0 d0 ^2 a _" _; ^7 P
: W- J0 r5 j. E5 x