a b/DataAugmentation/DatAug.py
1
dataPath = "../Data_postiveOnly/"
2
import os
3
print(os.getcwd())
4
import imgaug as ia
5
import skimage.io
6
import errno
7
import numpy as np
8
import skimage.color as color
9
#import matplotlib.pyplot as plt
10
import os
11
#from skimage.color import gray2rgb
12
13
14
def bboxSetupInImage(datapath, txtFile, img):
15
    """
16
    This is the function that reads in the bounding box files and then using imgaug to set up the bounding box on images
17
18
    :param txtFile: the txt file that store bounding box information
19
    :param img: the image file variable to represent the img to be plotted bounding box on it
20
    :return bbs: the image with bounding box in it
21
    """
22
    with open(datapath + 'bounding_boxes/' + txtFile, 'r') as f:
23
        content = [line.rstrip('\n') for line in f]
24
        iaBBoxList = []
25
        for bbline in content:
26
            bbox = bbline.strip().split()
27
            # print(bbox[1])
28
            if len(bbox) == 4:
29
                iaBBoxList.append(ia.BoundingBox(
30
                    x1=float(bbox[1]),
31
                    y1=float(bbox[0]),
32
                    x2=float(bbox[3]),
33
                    y2=float(bbox[2])))
34
        bbs = ia.BoundingBoxesOnImage(iaBBoxList, shape=img.shape)
35
        return bbs
36
37
38
def saveAugbbox2TXT(txtFile, bbs):
39
    """
40
    This is the function that save the augmented bounding box files into ChainerCV bbox format
41
42
    :param txtFile: the txt file that want to save
43
    :param bbs: bounding box lists
44
    """
45
    with open('' + txtFile, 'w') as f:
46
        for i in range(len(bbs.bounding_boxes)):
47
            bb = bbs_aug.bounding_boxes[i]
48
            # print("%s %.2f %.2f %.2f %.2f"%(bb.label,bb.y1,bb.x1,bb.y2,bb.x2))
49
            f.write("%.2f %.2f %.2f %.2f\n" % ( bb.y1, bb.x1, bb.y2, bb.x2))
50
51
def getImageList(imageTXT):
52
    """
53
    Function to loop the testing images for test
54
    :param imageTXT: the txt that stores the
55
    :return: imageFileList: the list contains all the original test image list
56
    """
57
    imageFileList = list()
58
    with open(imageTXT,'r') as f:
59
        lines = f.readlines()
60
        for line in lines:
61
            imageFileList.append(line.strip())
62
    return imageFileList
63
64
65
def createFolder(folderName):
66
    """
67
    Safely create folder when needed
68
69
    :param folderName : the directory that you  want to safely create
70
    :return: None
71
    """
72
    if not os.path.exists(folderName):
73
        try:
74
            os.makedirs(folderName)
75
        except OSError as exc:  # Guard against race condition
76
            if exc.errno != errno.EEXIST:
77
                raise
78
79
##################################################
80
# 1. Define data augmentation operations
81
##################################################
82
83
trainImageTxtFile = dataPath + "trainimages.txt"
84
imageList = getImageList(trainImageTxtFile)
85
86
current_operation = "GaussianNoise"
87
88
# Add gaussian noise.
89
# For 50% of all images, we sample the noise once per pixel.
90
# For the other 50% of all images, we sample the noise per pixel AND
91
# channel. This can change the color (not only brightness) of the
92
# pixels.
93
94
from imgaug import augmenters as iaa
95
96
ia.seed(1)
97
seq = iaa.Sequential([
98
    iaa.AdditiveGaussianNoise(loc=0,
99
                              scale=(0.0, 0.01 * 255),
100
                              per_channel=0.5)
101
])
102
103
# seq = iaa.Sequential([
104
#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.
105
#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.
106
#     iaa.GammaContrast((0.5, 1.5))
107
# ])
108
109
# Make our sequence deterministic.
110
# We can now apply it to the image and then to the BBs and it will
111
# lead to the same augmentations.
112
# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!
113
seq_det = seq.to_deterministic()
114
115
##################################################
116
# 2. loop through images
117
##################################################
118
119
for img in imageList:
120
    print(img)
121
    # Grayscale images must have shape (height, width, 1) each.
122
    # print(os.listdir(dataPath+'images/'))
123
    currentimage = skimage.io.imread(dataPath + 'images/' + img).astype(np.uint8)
124
    # gray2rgb() simply duplicates the gray values over the three color channels.
125
    currentimage = color.gray2rgb(currentimage)
126
    bbs = bboxSetupInImage(dataPath, img.rstrip('.jpg') + '.txt', currentimage)
127
    # Augment BBs and images.
128
    # As we only have one image and list of BBs, we use
129
    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your
130
    # variables would likely already be lists.
131
    image_aug = seq_det.augment_images([currentimage])[0]
132
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
133
    print(bbs_aug)
134
    augImgFolder = current_operation + "Images"
135
    augTxTFolder = current_operation + "TXT"
136
    createFolder(augImgFolder)
137
    createFolder(augTxTFolder)
138
    # Save aug images and bboxes
139
    skimage.io.imsave(augImgFolder + '/' +
140
                      img.rstrip('.jpg') +
141
                      '_' + current_operation +
142
                      '.jpg'
143
                      , image_aug)
144
    saveAugbbox2TXT(augTxTFolder + '/' +
145
                    img.rstrip('.jpg') +
146
                    '_' + current_operation +
147
                    '.txt', bbs_aug)
148
    # image with BBs before/after augmentation (shown below)
149
    # image_before = bbs.draw_on_image(currentimage, thickness=2)
150
    # image_after = bbs_aug.draw_on_image(image_aug,
151
    # thickness=2, color=[0, 0, 255])
152
    # image with BBs before/after augmentation (shown below)
153
    # plot and save figures before and after data augmentations
154
    # skimage.io.imshow(image_before)
155
    # skimage.io.imshow(image_after)
156
    # for i in range(len(bbs.bounding_boxes)):
157
    #     before = bbs.bounding_boxes[i]
158
    #     after = bbs_aug.bounding_boxes[i]
159
    #     print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
160
    #         i,
161
    #         before.x1, before.y1, before.x2, before.y2,
162
    #         after.x1, after.y1, after.x2, after.y2)
163
    #     )
164
165
##################################################
166
# 1. Define data augmentation operations
167
##################################################
168
169
trainImageTxtFile = dataPath + "trainimages.txt"
170
imageList = getImageList(trainImageTxtFile)
171
172
current_operation = "GaussianBlur"
173
174
# blur images with a sigma of 0 to 3.0
175
from imgaug import augmenters as iaa
176
ia.seed(1)
177
seq = iaa.Sequential([
178
  iaa.GaussianBlur(sigma=(0, 3))
179
])
180
181
# seq = iaa.Sequential([
182
#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.
183
#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.
184
#     iaa.GammaContrast((0.5, 1.5))
185
# ])
186
187
# Make our sequence deterministic.
188
# We can now apply it to the image and then to the BBs and it will
189
# lead to the same augmentations.
190
# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!
191
seq_det = seq.to_deterministic()
192
193
##################################################
194
# 2. loop through images
195
##################################################
196
197
for img in imageList:
198
    print(img)
199
    # Grayscale images must have shape (height, width, 1) each.
200
    #print(os.listdir(dataPath+'images/'))
201
    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)
202
    # gray2rgb() simply duplicates the gray values over the three color channels.
203
    currentimage = color.gray2rgb(currentimage)
204
    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)
205
    # Augment BBs and images.
206
    # As we only have one image and list of BBs, we use
207
    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your
208
    # variables would likely already be lists.
209
    image_aug = seq_det.augment_images([currentimage])[0]
210
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
211
    augImgFolder = current_operation + "Images"
212
    augTxTFolder = current_operation + "TXT"
213
    createFolder(augImgFolder)
214
    createFolder(augTxTFolder)
215
    # Save aug images and bboxes
216
    skimage.io.imsave(augImgFolder + '/'+
217
                      img.rstrip('.jpg') +
218
                      '_' + current_operation +
219
                      '.jpg'
220
                      ,image_aug)
221
    saveAugbbox2TXT(augTxTFolder+ '/'+
222
                    img.rstrip('.jpg') +
223
                    '_'+ current_operation +
224
                    '.txt',bbs_aug)
225
    # image with BBs before/after augmentation (shown below)
226
    # image_before = bbs.draw_on_image(currentimage, thickness=2)
227
    # image_after = bbs_aug.draw_on_image(image_aug,
228
    # thickness=2, color=[0, 0, 255])
229
    # image with BBs before/after augmentation (shown below)
230
    # plot and save figures before and after data augmentations
231
    #skimage.io.imshow(image_before)
232
    #skimage.io.imshow(image_after)
233
    # for i in range(len(bbs.bounding_boxes)):
234
    #     before = bbs.bounding_boxes[i]
235
    #     after = bbs_aug.bounding_boxes[i]
236
    #     print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
237
    #         i,
238
    #         before.x1, before.y1, before.x2, before.y2,
239
    #         after.x1, after.y1, after.x2, after.y2)
240
    #     )
241
242
##################################################
243
# 1. Define data augmentation operations
244
##################################################
245
246
trainImageTxtFile = dataPath + "trainimages.txt"
247
imageList = getImageList(trainImageTxtFile)
248
249
current_operation = "Brightness"
250
251
# Strengthen or weaken the contrast in each image.
252
from imgaug import augmenters as iaa
253
ia.seed(1)
254
seq = iaa.Sequential([
255
    iaa.Multiply((1.2, 1.5))
256
])
257
258
# seq = iaa.Sequential([
259
#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.
260
#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.
261
#     iaa.GammaContrast((0.5, 1.5))
262
# ])
263
264
# Make our sequence deterministic.
265
# We can now apply it to the image and then to the BBs and it will
266
# lead to the same augmentations.
267
# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!
268
seq_det = seq.to_deterministic()
269
270
##################################################
271
# 2. loop through images
272
##################################################
273
274
for img in imageList:
275
    print(img)
276
    # Grayscale images must have shape (height, width, 1) each.
277
    #print(os.listdir(dataPath+'images/'))
278
    currentimage = skimage.io.imread(dataPath+'images/'+img).astype(np.uint8)
279
    # gray2rgb() simply duplicates the gray values over the three color channels.
280
    currentimage = color.gray2rgb(currentimage)
281
    bbs = bboxSetupInImage(dataPath , img.rstrip('.jpg') + '.txt',currentimage)
282
    # Augment BBs and images.
283
    # As we only have one image and list of BBs, we use
284
    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your
285
    # variables would likely already be lists.
286
    image_aug = seq_det.augment_images([currentimage])[0]
287
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
288
    augImgFolder = current_operation + "Images"
289
    augTxTFolder = current_operation + "TXT"
290
    createFolder(augImgFolder)
291
    createFolder(augTxTFolder)
292
    # Save aug images and bboxes
293
    skimage.io.imsave(augImgFolder + '/'+
294
                      img.rstrip('.jpg') +
295
                      '_' + current_operation +
296
                      '.jpg'
297
                      ,image_aug)
298
    saveAugbbox2TXT(augTxTFolder+ '/'+
299
                    img.rstrip('.jpg') +
300
                    '_'+ current_operation +
301
                    '.txt',bbs_aug)
302
    # image with BBs before/after augmentation (shown below)
303
    # image_before = bbs.draw_on_image(currentimage, thickness=2)
304
    # image_after = bbs_aug.draw_on_image(image_aug,
305
    # thickness=2, color=[0, 0, 255])
306
    # image with BBs before/after augmentation (shown below)
307
    # plot and save figures before and after data augmentations
308
    #skimage.io.imshow(image_before)
309
    #skimage.io.imshow(image_after)
310
    # for i in range(len(bbs.bounding_boxes)):
311
    #     before = bbs.bounding_boxes[i]
312
    #     after = bbs_aug.bounding_boxes[i]
313
    #     print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
314
    #         i,
315
    #         before.x1, before.y1, before.x2, before.y2,
316
    #         after.x1, after.y1, after.x2, after.y2)
317
    #     )
318
319
##################################################
320
# 1. Define data augmentation operations
321
##################################################
322
323
trainImageTxtFile = dataPath + "trainimages.txt"
324
imageList = getImageList(trainImageTxtFile)
325
326
current_operation = "Fliplr"
327
328
# Flip/mirror input images horizontally.
329
330
from imgaug import augmenters as iaa
331
332
ia.seed(1)
333
seq = iaa.Sequential([
334
    iaa.Fliplr(1.0)
335
])
336
337
# seq = iaa.Sequential([
338
#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.
339
#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.
340
#     iaa.GammaContrast((0.5, 1.5))
341
# ])
342
343
# Make our sequence deterministic.
344
# We can now apply it to the image and then to the BBs and it will
345
# lead to the same augmentations.
346
# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!
347
seq_det = seq.to_deterministic()
348
349
##################################################
350
# 2. loop through images
351
##################################################
352
353
for img in imageList:
354
    print(img)
355
    # Grayscale images must have shape (height, width, 1) each.
356
    # print(os.listdir(dataPath+'images/'))
357
    currentimage = skimage.io.imread(dataPath + 'images/' + img).astype(np.uint8)
358
    # gray2rgb() simply duplicates the gray values over the three color channels.
359
    currentimage = color.gray2rgb(currentimage)
360
    bbs = bboxSetupInImage(dataPath, img.rstrip('.jpg') + '.txt', currentimage)
361
    # Augment BBs and images.
362
    # As we only have one image and list of BBs, we use
363
    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your
364
    # variables would likely already be lists.
365
    image_aug = seq_det.augment_images([currentimage])[0]
366
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
367
    augImgFolder = current_operation + "Images"
368
    augTxTFolder = current_operation + "TXT"
369
    createFolder(augImgFolder)
370
    createFolder(augTxTFolder)
371
    # Save aug images and bboxes
372
    skimage.io.imsave(augImgFolder + '/' +
373
                      img.rstrip('.jpg') +
374
                      '_' + current_operation +
375
                      '.jpg'
376
                      , image_aug)
377
    saveAugbbox2TXT(augTxTFolder + '/' +
378
                    img.rstrip('.jpg') +
379
                    '_' + current_operation +
380
                    '.txt', bbs_aug)
381
    # image with BBs before/after augmentation (shown below)
382
    # image_before = bbs.draw_on_image(currentimage, thickness=2)
383
    # image_after = bbs_aug.draw_on_image(image_aug,
384
    # thickness=2, color=[0, 0, 255])
385
    # image with BBs before/after augmentation (shown below)
386
    # plot and save figures before and after data augmentations
387
    # skimage.io.imshow(image_before)
388
    # skimage.io.imshow(image_after)
389
    # for i in range(len(bbs.bounding_boxes)):
390
    #     before = bbs.bounding_boxes[i]
391
    #     after = bbs_aug.bounding_boxes[i]
392
    #     print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
393
    #         i,
394
    #         before.x1, before.y1, before.x2, before.y2,
395
    #         after.x1, after.y1, after.x2, after.y2)
396
    #     )
397
398
##################################################
399
# 1. Define data augmentation operations
400
##################################################
401
402
trainImageTxtFile = dataPath + "trainimages.txt"
403
imageList = getImageList(trainImageTxtFile)
404
405
current_operation = "Flipud"
406
407
# Flip/mirror input images vertically.
408
409
from imgaug import augmenters as iaa
410
411
ia.seed(1)
412
seq = iaa.Sequential([
413
    iaa.Flipud(1.0)
414
])
415
416
# seq = iaa.Sequential([
417
#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.
418
#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.
419
#     iaa.GammaContrast((0.5, 1.5))
420
# ])
421
422
# Make our sequence deterministic.
423
# We can now apply it to the image and then to the BBs and it will
424
# lead to the same augmentations.
425
# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!
426
seq_det = seq.to_deterministic()
427
428
##################################################
429
# 2. loop through images
430
##################################################
431
432
for img in imageList:
433
    print(img)
434
    # Grayscale images must have shape (height, width, 1) each.
435
    # print(os.listdir(dataPath+'images/'))
436
    currentimage = skimage.io.imread(dataPath + 'images/' + img).astype(np.uint8)
437
    # gray2rgb() simply duplicates the gray values over the three color channels.
438
    currentimage = color.gray2rgb(currentimage)
439
    bbs = bboxSetupInImage(dataPath, img.rstrip('.jpg') + '.txt', currentimage)
440
    # Augment BBs and images.
441
    # As we only have one image and list of BBs, we use
442
    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your
443
    # variables would likely already be lists.
444
    image_aug = seq_det.augment_images([currentimage])[0]
445
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
446
    augImgFolder = current_operation + "Images"
447
    augTxTFolder = current_operation + "TXT"
448
    createFolder(augImgFolder)
449
    createFolder(augTxTFolder)
450
    # Save aug images and bboxes
451
    skimage.io.imsave(augImgFolder + '/' +
452
                      img.rstrip('.jpg') +
453
                      '_' + current_operation +
454
                      '.jpg'
455
                      , image_aug)
456
    saveAugbbox2TXT(augTxTFolder + '/' +
457
                    img.rstrip('.jpg') +
458
                    '_' + current_operation +
459
                    '.txt', bbs_aug)
460
    # image with BBs before/after augmentation (shown below)
461
    # image_before = bbs.draw_on_image(currentimage, thickness=2)
462
    # image_after = bbs_aug.draw_on_image(image_aug,
463
    # thickness=2, color=[0, 0, 255])
464
    # image with BBs before/after augmentation (shown below)
465
    # plot and save figures before and after data augmentations
466
    # skimage.io.imshow(image_before)
467
    # skimage.io.imshow(image_after)
468
    # for i in range(len(bbs.bounding_boxes)):
469
    #     before = bbs.bounding_boxes[i]
470
    #     after = bbs_aug.bounding_boxes[i]
471
    #     print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
472
    #         i,
473
    #         before.x1, before.y1, before.x2, before.y2,
474
    #         after.x1, after.y1, after.x2, after.y2)
475
    #     )
476
477
##################################################
478
# 1. Define data augmentation operations
479
##################################################
480
481
trainImageTxtFile = dataPath + "trainimages.txt"
482
imageList = getImageList(trainImageTxtFile)
483
484
current_operation = "Rot90or270Degree"
485
486
# Rotates all images by 90 or 270 degrees.
487
488
from imgaug import augmenters as iaa
489
490
ia.seed(1)
491
seq = iaa.Sequential([
492
    iaa.Rot90([1, 3])
493
])
494
495
# seq = iaa.Sequential([
496
#     # Adjust contrast by scaling each pixel value to (I_ij/255.0)**gamma.
497
#     # Values in the range gamma=(0.5, 2.0) seem to be sensible.
498
#     iaa.GammaContrast((0.5, 1.5))
499
# ])
500
501
# Make our sequence deterministic.
502
# We can now apply it to the image and then to the BBs and it will
503
# lead to the same augmentations.
504
# IMPORTANT: Call this once PER BATCH, otherwise you will always get the exactly same augmentations for every batch!
505
seq_det = seq.to_deterministic()
506
507
##################################################
508
# 2. loop through images
509
##################################################
510
511
for img in imageList:
512
    print(img)
513
    # Grayscale images must have shape (height, width, 1) each.
514
    # print(os.listdir(dataPath+'images/'))
515
    currentimage = skimage.io.imread(dataPath + 'images/' + img).astype(np.uint8)
516
    # gray2rgb() simply duplicates the gray values over the three color channels.
517
    currentimage = color.gray2rgb(currentimage)
518
    bbs = bboxSetupInImage(dataPath, img.rstrip('.jpg') + '.txt', currentimage)
519
    # Augment BBs and images.
520
    # As we only have one image and list of BBs, we use
521
    # [image] and [bbs] to turn both into lists (batches) for the# functions and then [0] to reverse that. In a real experiment, your
522
    # variables would likely already be lists.
523
    image_aug = seq_det.augment_images([currentimage])[0]
524
    bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
525
    augImgFolder = current_operation + "Images"
526
    augTxTFolder = current_operation + "TXT"
527
    createFolder(augImgFolder)
528
    createFolder(augTxTFolder)
529
    # Save aug images and bboxes
530
    skimage.io.imsave(augImgFolder + '/' +
531
                      img.rstrip('.jpg') +
532
                      '_' + current_operation +
533
                      '.jpg'
534
                      , image_aug)
535
    saveAugbbox2TXT(augTxTFolder + '/' +
536
                    img.rstrip('.jpg') +
537
                    '_' + current_operation +
538
                    '.txt', bbs_aug)
539
    # image with BBs before/after augmentation (shown below)
540
    # image_before = bbs.draw_on_image(currentimage, thickness=2)
541
    # image_after = bbs_aug.draw_on_image(image_aug,
542
    # thickness=2, color=[0, 0, 255])
543
    # image with BBs before/after augmentation (shown below)
544
    # plot and save figures before and after data augmentations
545
    # skimage.io.imshow(image_before)
546
    # skimage.io.imshow(image_after)
547
    # for i in range(len(bbs.bounding_boxes)):
548
    #     before = bbs.bounding_boxes[i]
549
    #     after = bbs_aug.bounding_boxes[i]
550
    #     print("BB %d: (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (
551
    #         i,
552
    #         before.x1, before.y1, before.x2, before.y2,
553
    #         after.x1, after.y1, after.x2, after.y2)
554
    #     )
555