-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathaugmentation.py
78 lines (73 loc) · 2.64 KB
/
augmentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from PIL import Image
from PIL import ImageEnhance
import PIL
import random
import numpy as np
class Brightness(object):
def __init__(self, min=1, max=1) -> None:
self.min = min
self.max = max
def __call__(self, clip):
factor = random.uniform(self.min, self.max)
if isinstance(clip[0], PIL.Image.Image):
im_w, im_h = clip[0].size
else:
raise TypeError('Expected numpy.ndarray or PIL.Image' +
'but got list of {0}'.format(type(clip[0])))
new_clip = []
for img in clip:
enh_bri = ImageEnhance.Brightness(img)
new_img = enh_bri.enhance(factor=factor)
new_clip.append(new_img)
return new_clip
class Color(object):
def __init__(self, min=1, max=1) -> None:
self.min = min
self.max = max
def __call__(self, clip):
factor = random.uniform(self.min, self.max)
if isinstance(clip[0], PIL.Image.Image):
im_w, im_h = clip[0].size
else:
raise TypeError('Expected numpy.ndarray or PIL.Image' +
'but got list of {0}'.format(type(clip[0])))
new_clip = []
for img in clip:
enh_col = ImageEnhance.Color(img)
new_img = enh_col.enhance(factor=factor)
new_clip.append(new_img)
return new_clip
class Contrast(object):
def __init__(self, min=1, max=1) -> None:
self.min = min
self.max = max
def __call__(self, clip):
factor = random.uniform(self.min, self.max)
if isinstance(clip[0], PIL.Image.Image):
im_w, im_h = clip[0].size
else:
raise TypeError('Expected numpy.ndarray or PIL.Image' +
'but got list of {0}'.format(type(clip[0])))
new_clip = []
for img in clip:
enh_con = ImageEnhance.Contrast(img)
new_img = enh_con.enhance(factor=factor)
new_clip.append(new_img)
return new_clip
class Sharpness(object):
def __init__(self, min=1, max=1) -> None:
self.min = min
self.max = max
def __call__(self, clip):
factor = random.uniform(self.min, self.max)
if isinstance(clip[0], PIL.Image.Image):
im_w, im_h = clip[0].size
else:
raise TypeError('Expected numpy.ndarray or PIL.Image' +
'but got list of {0}'.format(type(clip[0])))
new_clip = []
for img in clip:
enh_sha = ImageEnhance.Sharpness(img)
new_img = enh_sha.enhance(factor=1.5)
new_clip.append(new_img)
return new_clip