0 g+ A( b; B4 Z9 h. q. Q2 O0 `下面是对应的xml文件。0 A" d6 H7 Q# s: O& b% q
k7 x' }" _8 L6 z2 U* G+ d V
" f9 q9 t9 D/ x- i5 [0 D4 A<annotation>" M( ]0 w6 M' ]( W+ i5 Z2 O' c
<folder>train</folder> 5 M* r( @9 K m! V <filename>apple_30.jpg</filename>: J6 N( D( J* l7 f: V9 i9 D1 X7 W
<path>C:\tensorflow1\models\research\object_detection\images\train\apple_30.jpg</path> - d6 g$ L, r( k; f; B6 d4 k" A <source> 2 b0 F/ u( W5 k5 ]( k <database>Unknown</database> + |4 m8 ?, V4 i: G) G A, t* A </source>8 q" n! c( ^) D" }& [
<size>2 |( Q p) M/ a* _
<width>800</width>5 X, Q' [) W5 a. G4 A. r9 h1 d7 M
<height>800</height> 6 x. F8 S1 b/ `9 a <depth>3</depth> - e: [5 j5 H4 R </size>+ U: ?' T3 N0 n" ?, n' X
<segmented>0</segmented> ) L" r( a; G2 I6 R. a. o Z3 t <object>/ Z0 I/ a4 N% q* M7 ^
<name>apple</name> , M, P- c$ `% r6 P0 U <pose>Unspecified</pose>/ t1 |. B7 `. _: y& F
<truncated>0</truncated>2 k5 t8 `) O% a9 w$ F5 S
<difficult>0</difficult>2 S. S0 N" k* G" e; u, y
<bndbox> : y& H' F! [7 F3 s6 i2 S; ? <xmin>254</xmin>- l3 @( k) t7 S6 L& u( W9 [
<ymin>163</ymin> % j2 v3 ?% S- g! ~* b <xmax>582</xmax>' a) \+ ^1 {0 N2 T# D* y \5 k
<ymax>487</ymax>9 Y' _3 S; E( c6 s
</bndbox>- w4 ^# h5 P. U: Y2 m
</object>% v* Q+ D+ b3 G
<object> ! h9 K1 W+ D% W& ^" S. d- d <name>apple</name>3 Z6 C( k q3 S2 l6 ~* q" u
<pose>Unspecified</pose>$ P2 ^) E# ]5 M- A* H4 s
<truncated>0</truncated> ! I$ E; l$ F" i+ t1 _ <difficult>0</difficult>6 ^7 J8 U( V, r$ m0 J: _% K+ f
<bndbox> + `& K. i4 x4 I. l& { <xmin>217</xmin> ; h+ O' e4 \) O$ O: x. j: a4 Z <ymin>448</ymin> 4 W T# C4 M$ ?; q <xmax>535</xmax> 0 n, X& s) w5 B& I; d" }0 E. M* ^ <ymax>713</ymax> 0 I9 G8 ]( t$ Q7 L( K2 G! Y </bndbox>1 G& a+ C! E! v' T
</object> ) K( k8 w! X, w6 t <object>- h0 U Y' z6 F0 j: B1 y1 }
<name>apple</name> & c j8 k" B/ ]$ N, z <pose>Unspecified</pose> 8 z* f& h5 i3 E! s6 _8 V <truncated>1</truncated>( g8 y1 D$ ~8 n0 q- Q6 [6 [1 W2 Z
<difficult>0</difficult> ) G+ M7 Q8 N2 m$ I <bndbox> ! a3 n# k: K2 [ q! f! p- Q8 @9 i+ y <xmin>603</xmin> 2 U! a5 a! Q5 ?& R" r1 U- N <ymin>470</ymin> ; M7 u3 i5 h+ D# L- `, F <xmax>800</xmax> 6 p) R# m$ m+ a; j0 j <ymax>716</ymax>. `& r! t* ~ A# L; F
</bndbox>1 y7 N. x; V% k* b" c$ v' }
</object> 2 B$ R5 L! v: @4 w6 `9 k <object> , j: F, I6 B% ^: l6 N! ^1 N, P <name>apple</name> : \ H3 n6 u' Y' X R- n" P <pose>Unspecified</pose>9 N7 g& q1 H) k- I' ~$ A* f7 z
<truncated>0</truncated> 0 m' C* f3 Z9 r( o6 P <difficult>0</difficult>& Z$ ^ g/ q9 P( e* b
<bndbox> 3 ^4 k1 s% O6 V( { <xmin>468</xmin>3 C4 I6 ?" Y4 V
<ymin>179</ymin>0 g' ]9 d. r g6 W
<xmax>727</xmax> * ]3 j( ?3 Z2 p% x I <ymax>467</ymax> + B! m$ Q- Y" @' [* b4 E </bndbox> 2 h5 e; k: d6 G& r- F. f p2 B </object> 0 ?4 F/ F# e6 b+ B2 S8 {; f; W <object>7 [- N% u% x; A# P* J8 n. T& k4 k2 m
<name>apple</name> 3 c$ D; Y" J1 n. r <pose>Unspecified</pose> * M9 Q3 y" D& Z <truncated>1</truncated> ; I9 h% y I- z- ^- f2 U <difficult>0</difficult> $ E( U. n( k- N <bndbox> 6 k7 a5 \8 |% ^ <xmin>1</xmin> $ n8 ]1 I, I, D; X' o <ymin>63</ymin>1 s8 k% t# h+ W3 k: k
<xmax>308</xmax> 0 M2 ]( r1 E7 D2 [. G <ymax>414</ymax>5 ^# g* v6 k" |2 |) n; l% P! N
</bndbox> + a u& f, T: R* E7 P' O5 s </object>+ s6 m1 F& z' O) e
</annotation>0 x5 \( Q8 x# H+ u# @
13 n. s k: o# X" v; G
2 D @$ s" w: }$ n$ H* `, e- ^. h3 + i5 B5 s+ g Z6 Q) X$ M" O4% h7 Z' \* ~ H* X; k
5 l6 J' ?( Q8 J M6 $ m% l0 e2 }! F% m7 - F i( }: u- @( I8 2 }$ ]3 e% a2 [( w9- s3 a2 o) q5 t! Y
10 , @7 C: |- Y. T" Y: b2 b* s( b/ A11# p4 Z0 D( x9 j7 ~6 l9 |2 N
121 y; M0 u# k' l
13 ; R3 ~- h) B' H14 3 y: j; a" S7 U; L15 % F/ J& f& |" z, f ^. d; \) k16( m, Q" i# x7 _1 a$ u! V
17 ( ^! V3 J- f4 K3 S% ~189 Y& y5 z, T" o' L# k; S
19 : R% J- k. L2 ~3 h# p20. E7 p2 Y+ \( t) [* t$ `' l
21+ B" t$ O3 S7 ^9 Y' b0 {
225 V7 U- `% g8 n( I
23+ c3 ^5 Q. z0 h. C3 i$ A* D+ T
242 @* m: D3 V* q1 v- d! t0 K
25 ; C2 W# }! P4 l26 + `) l( l4 _7 x, J( p$ W27" j7 [1 P7 i: {0 a2 p* k
28 9 l }5 |( i8 T- v4 _# B29# T6 G( q. z* Y, E: f6 j
30 G/ {+ g' _0 ]6 M- V" q31 4 ^5 A, W9 s) j: B6 ^% ]) @( n32: T! m% b) |% e6 v2 k: @7 e
33 . V! ~- l+ Z* {, }5 _34 ( [1 k5 C( ^. ?9 m4 h5 |( P4 `35: U: X( K6 c% h( T$ O
36% y' v r, F$ N4 o) k
37 3 j: k; O" i/ Z, W38/ h9 V* m- C! b" ^# y
39. A. p, F. x, S0 W d2 G; X6 H; W
40+ ^1 m% [: \. K1 F$ i+ _
41+ k L! L9 G+ H' u! N# G
42 ' D- }5 e. G, B. r43# ?- ]7 y$ C/ y
449 p$ v) S# w6 Q W7 G9 O4 F, F( T- j
45 % r5 j/ c( U6 D# E, v0 l46! t8 U) ]3 W; y/ c" _+ Z; Q1 J* s
470 J, a9 Y7 U2 s" W2 q/ P
486 f) g+ q: H3 g0 x- F1 Y
49 & d S) G& }5 \50 : d8 I! l' w$ r3 ]51$ o) C3 P7 r) l+ s4 b' ~
52 5 j. x9 @% T6 h y( m: q53 6 C7 _' W8 v& f* [9 g54% A. f" p) z- B, X6 m: h) F
55 8 s2 R, H+ q8 R6 `# p+ Z) e/ D- P56 * |# d. q% C# P574 B/ m7 g; S5 n r! _
583 W7 j2 z J# T% J5 C
59 7 @% w2 [" s" a k; z6 L' C60 7 k1 t4 c4 c7 {61 4 R+ `3 X/ r2 X, ?; O62 ! z+ @3 l: X' I: Z9 H" m63 # N; m4 v! Z3 K1 W+ z64; p! [, F4 Z$ A/ v
65 : q( ~; o7 t5 j8 M' J" X2 s$ k668 R" q! B; x- D' e. E
67. z7 W @' O4 a- n
680 c$ u4 J+ v2 ?; Y% b" \2 X. U n1 o! y
69 ; z: V% L$ ~3 j9 T70# _( j4 o3 w5 K$ n
71. A3 w4 e$ d. p- @/ o5 P
72+ E0 i. F& u2 l3 B
734 z. U! v6 x9 z( |' w
74 , I4 e; f) ]# w; u% u- o7 n) m' Z将xml文件提取图像信息,主要使用xml和opencv,基于torch提取,代码比较凌乱。 * L- g d6 ^3 q# i# g9 _ ! y N# r( k6 E3 o, X! X* }6 @' k9 M0 y% m' G `
import os$ _/ O8 N& I8 A- f/ @/ K
import numpy as np2 }/ E+ g/ Y* T/ j! Q; z" Q
import cv29 U( V9 R6 Q$ ^' E" I* d$ h
import torch! N7 c) N1 s. }- y
import matplotlib.patches as patches / [, I0 I7 ^: I% Z% N+ ^import albumentations as A3 ]: y) O& D, f; D0 a3 q9 I! {
from albumentations.pytorch.transforms import ToTensorV2( D5 u# p# Y: C2 e1 v* z- B- O
from matplotlib import pyplot as plt 5 a) o+ W7 g2 xfrom torch.utils.data import Dataset/ b/ N" t) K7 ?
from xml.etree import ElementTree as et ; {0 ]- ]1 S. Z* q8 [from torchvision import transforms as torchtrans . X2 Z+ r% R; {* f& `/ {5 O4 S # ?% Z8 k1 \- ~* y1 [/ F- \4 Q1 ^
# defining the files directory and testing directory$ ?& w$ H5 z+ o( F) S
train_image_dir = 'train/train/image'6 S* c$ {0 Z9 m+ `( G) ?
train_xml_dir = 'train/train/xml' ' H0 C7 n+ z: a+ O4 x, o4 d8 r# test_image_dir = 'test/test/image'& |; z& t) F5 Q, P3 u9 H
# test_xml_dir = 'test/test/xml' 1 B& a5 V7 a/ U+ y# z2 \+ @ & G" Q+ `4 E* h( E3 h# f6 }2 `$ v9 [. Q( a' A, R
class FruitImagesDataset(Dataset): / X! q7 V' k9 L9 N & I$ g/ L! f$ W0 f 2 b6 R* g& e7 c& Q5 d def __init__(self, image_dir, xml_dir, width, height, transforms=None): Z8 P* `. p% k9 g, m1 @ self.transforms = transforms 2 F& v" O% ~5 ~8 m self.image_dir = image_dir / B. j5 L1 v8 N9 g self.xml_dir = xml_dir ) {1 b* O6 S$ l self.height = height; I. Y$ _3 K1 v
self.width = width $ |, K7 `# J% k 0 D1 N: H3 U2 ]- r3 \ _ - b- Z7 J* Z7 U: W # sorting the images for consistency" W3 F8 C4 i! Z4 N2 n: [) ^
# To get images, the extension of the filename is checked to be jpg t7 U) O7 U. ]4 X: g2 B
self.imgs = [image for image in os.listdir(self.image_dir) ' r. S0 a7 y9 T( ^8 G1 ?6 j if image[-4:] == '.jpg']. [5 Y. ^2 r; q5 ~6 e
self.xmls = [xml for xml in os.listdir(self.xml_dir)# n. g! a% O8 t6 Y. p; I, }
if xml[-4:] == '.xml'] " w/ }8 E$ h N o W* y1 K! H" ~) M7 u* [" @& H
+ v$ d: s5 t; z5 L* T+ Y # classes: 0 index is reserved for background' W( l5 Y6 p& p/ U! L
self.classes = ['apple', 'banana', 'orange'] 3 {* i @& b" w/ R1 | 5 E9 z: k9 _. @: V0 `) u [: A( K7 ^2 J9 |
def __getitem__(self, idx): 1 A) c2 \- p4 D+ _0 u+ J5 d; I) P% e+ s _7 X6 V' v
4 V2 i) H" V% O% e/ n img_name = self.imgs[idx]6 `8 Y' X4 K' w% s0 S. N) m( W9 l
image_path = os.path.join(self.image_dir, img_name)1 {& y% |0 U) ]1 Z7 C3 k
# b1 b2 V5 s: s" [
9 `& e3 d. z' I& c0 t7 L0 ]2 t7 R4 z0 G # reading the images and converting them to correct size and color / u, K* H' R) c5 {# `8 y6 z$ L/ [' R img = cv2.imread(image_path): W E* o u# G5 Q- r: i- V
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)' C, v4 y6 p5 P- j/ G' `
img_res = cv2.resize(img_rgb, (self.width, self.height), cv2.INTER_AREA) + E Z% B" E5 P _3 V # diving by 255 - e3 X V# E4 T5 j( K% I img_res /= 255.0 7 h% S: B- t3 r- V1 Q 6 m( y) }; e# q' W+ S0 o5 I9 G # Y9 N! s0 \8 H! l # annotation file % q$ j# W u/ A annot_filename = img_name[:-4] + '.xml') L d, _8 X- ~+ O, Z9 \
annot_file_path = os.path.join(self.xml_dir, annot_filename) % D F$ T7 p! g+ n8 P- W6 w& k5 ^0 f, c! p6 E L0 K
' G" O) A9 @* p1 C& g
boxes = []7 [! Y( j: a( U0 u" z7 x6 M
labels = []2 }& y. _' [" P7 s; x1 u' s$ Q
tree = et.parse(annot_file_path) ) f3 c3 v2 a G4 u, h root = tree.getroot()' Z) c) I' z: e0 b7 O
- V+ l& K7 k$ Q6 X" s0 [3 F
- i' z9 C0 U8 {" {* ] # cv2 image gives size as height x width * O5 b* U4 {/ {8 o wt = img.shape[1]! x5 @% r: Q& t0 C% X5 D3 W2 g9 a
ht = img.shape[0]' |% M5 [: i5 o: M5 K: O* I
8 x) ^% A( ]/ d4 a 4 I# k( |4 z. D4 Q # box coordinates for xml files are extracted and corrected for image size given) l k7 y* x( y9 s$ y
for member in root.findall('object'):4 i9 t9 I8 t% L, @
labels.append(self.classes.index(member.find('name').text))! [0 {, o, z/ J3 B7 S# b
; `) f" O/ G; y8 T/ K0 a
" U4 m! e2 I" K. d, \2 V" q* n8 u # bounding box% ^, y z9 a, R! M' i
xmin = int(member.find('bndbox').find('xmin').text) # w" ?6 }9 ~( v _ xmax = int(member.find('bndbox').find('xmax').text)) [, E" w6 i3 M( a( O3 o F+ p) T