- h2 r# t6 b! u$ \3 }. ? & O0 J m* l. @& w # sorting the images for consistency ' {# r$ m- _( K# ^) r! N" M # To get images, the extension of the filename is checked to be jpg 9 G, K! j$ ^1 G6 f! L3 O2 S self.imgs = [image for image in os.listdir(self.image_dir) W, A8 b2 E) q7 O, G- e7 _8 y if image[-4:] == '.jpg']; p, t! u( R1 s( k/ K& c8 r2 K
self.xmls = [xml for xml in os.listdir(self.xml_dir)" m" h% w% R. b/ C5 ~1 G8 v" p
if xml[-4:] == '.xml']2 k4 j' J3 G# g z: b1 c" C
3 q# z4 f, _; ~" f f4 ~3 G
6 U8 @1 `( G# D& ]. a+ i @ # classes: 0 index is reserved for background 3 L% [% G: Y; o. r! e% ~& { self.classes = ['apple', 'banana', 'orange'] & T& M3 g1 g* ^, [ 3 D. F1 N0 }- d/ U5 U, ^, N. Q4 V% K9 J; R5 U5 i7 G. E
def __getitem__(self, idx): ) H6 h/ ]5 c1 ?4 |' u" Q' w2 H c$ x2 k
& G' w6 K5 H: g$ N: o4 ~2 K img_name = self.imgs[idx] 5 f o1 y$ [2 R: C: o+ B image_path = os.path.join(self.image_dir, img_name)3 p3 B5 a- ^ r- h& ]; ?- H4 s
- }, X6 B" Z. @, t4 Y& w
/ h4 Z: e C7 C. c; `
# reading the images and converting them to correct size and color $ L- I! K3 {: X; l; [: N- ^ img = cv2.imread(image_path) ~) E5 M+ K9 _# c" H img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32): ^5 C: Q/ f4 U+ k( _$ x
img_res = cv2.resize(img_rgb, (self.width, self.height), cv2.INTER_AREA) 0 C- s1 d0 @, U. h$ X. S/ U # diving by 255 9 Z2 \$ s" ]( C# P img_res /= 255.0 * G& ^) C$ v6 d4 J7 G, E) ]3 e+ k; J; ?/ T: s7 j$ g+ N