Skip to content

Commit d51a6bc

Browse files
authored
Update dataloader.py
1 parent 2af997c commit d51a6bc

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

utils/dataloader.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from utils.utils import bbox_iou, merge_bboxes
1212
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
1313
from nets.yolo_training import Generator
14-
14+
import cv2
1515

1616
class YoloDataset(Dataset):
1717
def __init__(self, train_lines, image_size, mosaic=True):
@@ -65,15 +65,16 @@ def get_random_data(self, annotation_line, input_shape, jitter=.3, hue=.1, sat=1
6565
hue = self.rand(-hue, hue)
6666
sat = self.rand(1, sat) if self.rand() < .5 else 1 / self.rand(1, sat)
6767
val = self.rand(1, val) if self.rand() < .5 else 1 / self.rand(1, val)
68-
x = rgb_to_hsv(np.array(image) / 255.)
69-
x[..., 0] += hue
70-
x[..., 0][x[..., 0] > 1] -= 1
71-
x[..., 0][x[..., 0] < 0] += 1
68+
x = cv2.cvtColor(np.array(image,np.float32)/255, cv2.COLOR_RGB2HSV)
69+
x[..., 0] += hue*360
70+
x[..., 0][x[..., 0]>1] -= 1
71+
x[..., 0][x[..., 0]<0] += 1
7272
x[..., 1] *= sat
7373
x[..., 2] *= val
74-
x[x > 1] = 1
75-
x[x < 0] = 0
76-
image_data = hsv_to_rgb(x) * 255 # numpy array, 0 to 1
74+
x[x[:,:, 0]>360, 0] = 360
75+
x[:, :, 1:][x[:, :, 1:]>1] = 1
76+
x[x<0] = 0
77+
image_data = cv2.cvtColor(x, cv2.COLOR_HSV2RGB)*255
7778

7879
# 调整目标框坐标
7980
box_data = np.zeros((len(box), 5))
@@ -144,15 +145,16 @@ def get_random_data_with_Mosaic(self, annotation_line, input_shape, hue=.1, sat=
144145
hue = self.rand(-hue, hue)
145146
sat = self.rand(1, sat) if self.rand() < .5 else 1 / self.rand(1, sat)
146147
val = self.rand(1, val) if self.rand() < .5 else 1 / self.rand(1, val)
147-
x = rgb_to_hsv(np.array(image) / 255.)
148-
x[..., 0] += hue
149-
x[..., 0][x[..., 0] > 1] -= 1
150-
x[..., 0][x[..., 0] < 0] += 1
148+
x = cv2.cvtColor(np.array(image,np.float32)/255, cv2.COLOR_RGB2HSV)
149+
x[..., 0] += hue*360
150+
x[..., 0][x[..., 0]>1] -= 1
151+
x[..., 0][x[..., 0]<0] += 1
151152
x[..., 1] *= sat
152153
x[..., 2] *= val
153-
x[x > 1] = 1
154-
x[x < 0] = 0
155-
image = hsv_to_rgb(x)
154+
x[x[:,:, 0]>360, 0] = 360
155+
x[:, :, 1:][x[:, :, 1:]>1] = 1
156+
x[x<0] = 0
157+
image = cv2.cvtColor(x, cv2.COLOR_HSV2RGB) # numpy array, 0 to 1
156158

157159
image = Image.fromarray((image * 255).astype(np.uint8))
158160
# 将图片进行放置,分别对应四张分割图片的位置

0 commit comments

Comments
 (0)