def data_augmentation(self, image_paths, labels, mode='train',
resize=False, jitter=0.2, flip=False, whiten=False):
new_images, new_labels = [], []
for image_path, label in zip(image_paths, labels):
image = cv2.imread(image_path)
# ??????
if resize:
image, label = self.image_resize(
image, label, jitter=jitter, mode=mode)
# ????
if flip:
image, label = self.image_flip(image, label, mode=mode)
# ????
if whiten:
image = self.image_whitening(image)
new_images.append(image)
new_labels.append(label)
new_images = numpy.array(new_images, dtype='uint8')
new_labels = numpy.array(new_labels, dtype='float32')
return new_images, new_labels
python类flip()的实例源码
def image_flip(self, image, label, mode='train'):
# ????
if mode == 'train':
old_image = image
if numpy.random.random() < 0.5:
new_image = cv2.flip(old_image, 1)
else:
new_image = old_image
# ????box label
for j in range(len(label)):
if sum(label[j]) == 0:
break
right = 1.0 - label[j][0]
left = 1.0 - label[j][1]
label[j][0] = left
label[j][1] = right
else:
new_image = image
return new_image, label
def data_augmentation(self, images, labels,
mode='train',
flip=False,
crop=False, padding=20,
whiten=False,
noise=False, noise_mean=0, noise_std=0.01,
resize=False, jitter=0.2):
# ??????
if resize:
images, labels = self.image_resize(images, labels, jitter=jitter, mode=mode)
# ????
if crop:
images = self.image_crop(images, padding=padding)
# ????
if flip:
images, labels = self.image_flip(images, labels)
# ????
if whiten:
images = self.image_whitening(images)
# ????
if noise:
images = self.image_noise(images, mean=noise_mean, std=noise_std)
return numpy.array(images, dtype='uint8'), numpy.array(labels, dtype='float32')
def image_flip(self, images, labels):
# ????
for i in range(len(images)):
old_image = images[i]
if numpy.random.random() < 0.5:
new_image = cv2.flip(old_image, 1)
else:
new_image = old_image
images[i] = new_image
# ????box label
for i in range(len(labels)):
for j in range(len(labels[i])):
if sum(labels[i][j]) == 0:
break
right = 1.0 - labels[i][j][0]
left = 1.0 - labels[i][j][1]
labels[i][j][0] = left
labels[i][j][1] = right
return images, labels
def augmentate(self):
angles = [45, 90, 135, 180, 225, 270, 315]
scale = 1.0
for img in self.images:
print "image shape : ", img.shape
w = img.shape[1]
h = img.shape[0]
img_vmirror = cv2.flip(img,1)
skimage.io.imsave("testv"+".jpg", img_vmirror )
for angle in angles:
#rangle = np.deg2rad(angle)
# nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
# nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
rot_mat = cv2.getRotationMatrix2D((w*0.5, h*0.5), angle, scale)
# rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0]))
# rot_mat[0,2] += rot_move[0]
# rot_mat[1,2] += rot_move[1]
new_img = cv2.warpAffine(img, rot_mat, (int(math.ceil(w)), int(math.ceil(h))), flags=cv2.INTER_LANCZOS4)
skimage.io.imsave("test"+str(angle)+".jpg", new_img)
new_img_vmirror = cv2.flip(new_img, 1)
skimage.io.imsave("testv"+str(angle)+".jpg", new_img_vmirror)
# img_rmirror = cv2.flip(new_img, 0)
# skimage.io.imsave("testh"+str(angle)+".jpg", img_rmirror)
def load_and_augmentate(self, root):
angles = [45, 90, 135, 180, 225, 270, 315]
scale = 1.0
for img_dir in os.listdir(root):
img_dir_path = os.path.join(root, img_dir)
for img in os.listdir(img_dir_path):
img_path = os.path.join(img_dir_path, img)
image = caffe.io.load_image(img_path,color=True)
w = image.shape[1]
h = image.shape[0]
img_name = img.split(".")[0]
img_type = img.split(".")[-1]
img_vmirror = cv2.flip(image,1)
img_vmirror_path = os.path.join(img_dir_path,img_name+"_v."+img_type)
skimage.io.imsave(img_vmirror_path, img_vmirror )
for angle in angles:
rot_mat = cv2.getRotationMatrix2D((w*0.5, h*0.5), angle, scale)
new_img = cv2.warpAffine(image, rot_mat, (int(math.ceil(w)), int(math.ceil(h))), flags=cv2.INTER_LANCZOS4)
new_img_path = os.path.join(img_dir_path,img_name+"_"+str(angle)+"."+img_type)
skimage.io.imsave(new_img_path, new_img)
new_img_vmirror = cv2.flip(new_img, 1)
new_img_vmirror_path = os.path.join(img_dir_path, img_name+"_"+str(angle)+"_v."+img_type)
skimage.io.imsave(new_img_vmirror_path, new_img_vmirror)
def get_test_aug(factor):
if not factor or factor == 1:
return [
[False, False, False]]
elif factor == 4:
# transpose, v-flip, h-flip
return [
[False, False, False],
[False, False, True],
[False, True, False],
[True, True, True]]
elif factor == 8:
# return list of all combinations of flips and transpose
return ((1 & np.arange(0, 8)[:, np.newaxis] // 2**np.arange(2, -1, -1)) > 0).tolist()
else:
print('Invalid augmentation factor')
return [
[False, False, False]]
def catchOwner():
global frame_num
cap = cv2.VideoCapture(0)
while(True):
ret, img = cap.read()
img = cv2.flip(img, 1) # flip the image????????
show_image = face_detector(img, face_cascade)
cv2.imshow('image',show_image)
k = cv2.waitKey(2)
if k == ord('s'):
cv2.imwrite('/Users/gushixin/Desktop/OwnerSensor/data/owner/catch%s.jpg' % frame_num,img)
frame_num += 1
elif k == 27:
break
cap.release()
cv2.destroyAllWindows()
#??py??????????????????py??????????????
def shape_points(img, nsteps, mirrow=False, only_upper=False):
"""
Simple formatting the shape_df output to be passed to the ShapeContext class
"""
if mirrow:
im = cv2.flip(img, 2)
else:
im = img.copy()
df_y = shape_df(im, 'y', nsteps)
df_x = shape_df(im, 'x', nsteps)
if (not df_y.empty) and (not df_x.empty):
y_init = [(df_y.init[i], df_y.coord[i]) for i in xrange(df_y.shape[0])]
y_end = [(df_y.end[i], df_y.coord[i]) for i in xrange(df_y.shape[0])]
x_init = [(df_x.coord[i], df_x.init[i]) for i in xrange(df_x.shape[0])]
x_end = [(df_x.coord[i], df_x.end[i]) for i in xrange(df_x.shape[0])]
if only_upper: return x_init
return y_init+y_end+x_init+x_end
else:
return []
def shape_points(img, nsteps, mirrow=False, only_upper=False):
"""
Simple formatting the shape_df output to be passed to the ShapeContext class
"""
if mirrow:
im = cv2.flip(img, 2)
else:
im = img.copy()
df_y = shape_df(im, 'y', nsteps)
df_x = shape_df(im, 'x', nsteps)
if (not df_y.empty) and (not df_x.empty):
y_init = [(df_y.init[i], df_y.coord[i]) for i in xrange(df_y.shape[0])]
y_end = [(df_y.end[i], df_y.coord[i]) for i in xrange(df_y.shape[0])]
x_init = [(df_x.coord[i], df_x.init[i]) for i in xrange(df_x.shape[0])]
x_end = [(df_x.coord[i], df_x.end[i]) for i in xrange(df_x.shape[0])]
if only_upper: return x_init
return y_init+y_end+x_init+x_end
else:
return []
def setCameraProperties():
name_window = 'Press esc after finish'
cv2.namedWindow(name_window)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_SETTINGS, 0);
ret, frame_from = cap.read()
while(cap.isOpened()):
ret, frame_from = cap.read()
frame = cv2.flip(frame_from, -1)
if ret==True:
cv2.imshow(name_window,frame)
if cv2.waitKey(1) & 0xFF == 27:
break
else:
break
# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()
def getFrameFromCamera():
name_window = 'camera window'
cv2.namedWindow(name_window)
cap = cv2.VideoCapture(0)
ret, frame_from = cap.read()
output = []
while(cap.isOpened()):
ret, frame = cap.read()
frame = cv2.flip(frame, -1)
if ret==True:
cv2.imshow(name_window,frame)
cur_key = cv2.waitKey(1)
if cur_key == 27:
break
if cur_key == ord('s'):
output = frame
break
else:
break
# Release everything if job is finished
cap.release()
#out.release()
cv2.destroyAllWindows()
return output
def augmentation(image, org_width=160,org_height=224, width=190, height=262):
max_angle=20
image=resize(image,(width,height))
angle=np.random.randint(max_angle)
if np.random.randint(2):
angle=-angle
image=rotate(image,angle,resize=True)
xstart=np.random.randint(width-org_width)
ystart=np.random.randint(height-org_height)
image=image[xstart:xstart+org_width,ystart:ystart+org_height]
if np.random.randint(2):
image=cv2.flip(image,1)
if np.random.randint(2):
image=cv2.flip(image,0)
# image=resize(image,(org_width,org_height))
print(image.shape)
plt.imshow(image)
plt.show()
def render(self, markers):
for marker in markers:
rvecs, tvecs, marker_rotation, marker_name = marker
# build view matrix
rmtx = cv2.Rodrigues(rvecs)[0]
view_matrix = np.array([[rmtx[0][0],rmtx[0][1],rmtx[0][2],tvecs[0]],
[rmtx[1][0],rmtx[1][1],rmtx[1][2],tvecs[1]],
[rmtx[2][0],rmtx[2][1],rmtx[2][2],tvecs[2]],
[0.0 ,0.0 ,0.0 ,1.0 ]])
view_matrix = view_matrix * self.INVERSE_MATRIX
view_matrix = np.transpose(view_matrix)
# load view matrix and draw cube
glPushMatrix()
glLoadMatrixd(view_matrix)
if marker_name == MARKER_ONE:
self.marker_one_textures[TEXTURE_FRONT] = cv2.flip(self._get_video_frame(), 0)
self._draw_cube(marker_rotation, self.marker_one_textures)
elif marker_name == MARKER_TWO:
self._draw_cube(marker_rotation, self.marker_two_textures)
glColor3f(1.0, 1.0, 1.0)
glPopMatrix()
def crop_image(image, contours, min_aspect_ratio=0.5):
ratio = image.shape[0] / float(scale_factor)
warped = four_point_transform(image, contours.reshape(4, 2) * ratio)
# test to see if the box ratio is correct
height, width, channels = warped.shape
if height > width:
aspect_ratio = width / height
else:
aspect_ratio = height / width
if aspect_ratio < min_aspect_ratio:
raise ImageNotReadable()
# test to see if the orientation is correct, if not flip it
if height > width:
warped = cv2.transpose(warped)
warped = cv2.flip(warped, 0)
return warped
def load_next_image(self,loss_task):
if loss_task == 0:
if self.cls_cur == len(self.cls_list):
self.cls_cur = 0
random.shuffle(self.cls_list)
cur_data = self.cls_list[self.cls_cur] # Get the image index
im = cur_data[0]
label = cur_data[1]
roi = [-1,-1,-1,-1]
pts = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
if random.choice([0,1])==1:
im = cv2.flip(im,random.choice([-1,0,1]))
self.cls_cur += 1
return im, label, roi, pts
if loss_task == 1:
if self.roi_cur == len(self.roi_list):
self.roi_cur = 0
random.shuffle(self.roi_list)
cur_data = self.roi_list[self.roi_cur] # Get the image index
im = cur_data[0]
label = -1
roi = cur_data[2]
pts = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
self.roi_cur += 1
return im, label, roi, pts
if loss_task == 2:
if self.pts_cur == len(self.pts_list):
self.pts_cur = 0
random.shuffle(self.pts_list)
cur_data = self.pts_list[self.pts_cur] # Get the image index
im = cur_data[0]
label = -1
roi = [-1,-1,-1,-1]
pts = cur_data[3]
self.pts_cur += 1
return im, label, roi, pts
################################################################################
######################Regression Loss Layer By Python###########################
################################################################################
def data_augmentation(self, images, mode='train', flip=False,
crop=False, crop_shape=(24,24,3), whiten=False,
noise=False, noise_mean=0, noise_std=0.01):
# ????
if crop:
if mode == 'train':
images = self._image_crop(images, shape=crop_shape)
elif mode == 'test':
images = self._image_crop_test(images, shape=crop_shape)
# ????
if flip:
images = self._image_flip(images)
# ????
if whiten:
images = self._image_whitening(images)
# ????
if noise:
images = self._image_noise(images, mean=noise_mean, std=noise_std)
return images
step2_train_mass_segmenter.py 文件源码
项目:kaggle_ndsb2017
作者: juliandewit
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def random_flip_img(img, horizontal_chance=0, vertical_chance=0):
import cv2
flip_horizontal = False
if random.random() < horizontal_chance:
flip_horizontal = True
flip_vertical = False
if random.random() < vertical_chance:
flip_vertical = True
if not flip_horizontal and not flip_vertical:
return img
flip_val = 1
if flip_vertical:
flip_val = -1 if flip_horizontal else 0
if not isinstance(img, list):
res = cv2.flip(img, flip_val) # 0 = X axis, 1 = Y axis, -1 = both
else:
res = []
for img_item in img:
img_flip = cv2.flip(img_item, flip_val)
res.append(img_flip)
return res
def display_video_stream(self):
r , frame = self.capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(40, 40),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
frame = cv2.cvtColor(frame, cv2.cv.CV_BGR2RGB)
frame = cv2.flip(frame, 1)
image = QImage(frame, frame.shape[1], frame.shape[0],
frame.strides[0], QImage.Format_RGB888)
self.imageLabel.setPixmap(QPixmap.fromImage(image))
def __init__(self, horiz=False, vert=False, prob=0.5):
"""
Only one of horiz, vert can be set.
:param horiz: whether or not apply horizontal flip.
:param vert: whether or not apply vertical flip.
:param prob: probability of flip.
"""
super(Flip, self).__init__()
if horiz and vert:
raise ValueError("Please use two Flip instead.")
elif horiz:
self.code = 1
elif vert:
self.code = 0
else:
raise ValueError("Are you kidding?")
self.prob = prob
self._init()