caffe:自己的数据训练模型 用于视频检索(一)

一、数据准备:

(1)原始数据

11类 包括目标和场景

目标:plane, oiltank, ship, warship, vehicel

场景:building, desert, residential, water

车辆:包括完整的和不完整的车辆

(2)扩充数据

 ①在原始框进行缩放平移,利用numpy

from __future__ import division
import scipy.io as scio
from  PIL import Image
import numpy as np

# import matplotlib.pyplot as plt 

dataFile = "C:/Users/liesmars/Desktop/QSData/MidData/mat/VIRAT1_2.mat"
ImgPath = "C:/Users/liesmars/Desktop/QSData/MidData/VIRAT1/"

fileName = scio.loadmat(dataFile)['filenames'][0]
# print "fileName:",fileName
XYdata = scio.loadmat(dataFile)['rs'][0]
# print "XYdata:", XYdata

fileNumb = len(XYdata)
# print fileNumb

N = 0
for i, rois in enumerate(XYdata):

	Name = fileName[i]
	Name = str(Name[0])
	print "Name:", Name
	FilePath = ImgPath + Name
	ImageFile = Image.open(FilePath)
	width = ImageFile.size[0]
	print "width:", width

	height = ImageFile.size[1]
	print "height:", height

	for box in rois:
		x1 = box[0]
		y1 = box[1]
		x2 = box[2]         
		y2 = box[3]

		w = x2 - x1
		h = y2 - y1
		
		obj =  np.array([x1,y1,x2,y2])
		# zoom  = np.array([[1.2],[1.1]])
		# zoom = np.tile(zoom,(1,4))
		# print zoom

		shift = np.array([[0.8,0.8,1.2,1.2],[0.9,0.9,1.1,1.1],[1,1,1,1],[0.8,0.8,1,1],[1,1,1.1,1.1],\
				[0.8,1,1,1.1],[1,0.8,1.1,1],[(x1+w*1/3)/x1,(y1+h*1/3)/y1,(x2+w*1/3)/x2,(y2+h*1/3)/y2],\
				[(x1-w*1/3)/x1,(y1-h*1/3)/y1,(x2-w*1/3)/x2,(y2-h*1/3)/y2]])

		# print shift

		XYmatrix = np.tile(obj,(9,1))
		# print XYmatrix

		objects = XYmatrix * shift
		print objects

		for item in objects:
			# print item[0];
			x1=np.maximum(item[0],0)
			y1=np.maximum(item[1],0)
			x2=np.minimum(item[2],width)
			y2=np.minimum(item[3],height)

			# with open("C:/Users/liesmars/Desktop/annotation/VIRAT1.txt",'a') as f:
			# 	f.write(Name, + '\t' + 'vehicle' + '\t' + x1 + '\t' + y1 + '\t' + x2
			# 		+ '\t' + y2 + '\n')
			NewBox = (x1,y1,x2,y2)
			print NewBox
			patch = ImageFile.crop(NewBox)
			patch.save("C:/Users/liesmars/Desktop/vehicle/" + "VIRAT1_" + str(N) + ".jpg")
			N += 1
			print N

②扩充:镜像、旋转、仿射变换、放大

import os
import cv2
from  PIL import Image
import copy
import numpy as np

TrainDataPath = "C:/Users/liesmars/Desktop/QSData/TrainDataOrig/"
ResizedPath = "C:/Users/liesmars/Desktop/QSData/TrainDataExpd/"

def ExpandImage(TrainDataPath,ResizedPath):
	for root, dirs, files, in os.walk(TrainDataPath):
		N = 0
		for name in files:
			folder = root.split('/')[-1]
			SavePath = ResizedPath + folder 
			SavePath = SavePath.replace('\\','/')

			if not os.path.exists(SavePath):
				os.mkdir(SavePath)

			ImgPath = os.path.join(root, name)
			ImgPath = ImgPath.replace('\\','/')

			# ImgFile =  Image.open(ImgPath)
			# ImgFile = ImgFile.convert('RGB')
			ImgFile = cv2.imread(ImgPath)

			ResizePath = SavePath + '/' + folder + '_r' + str(N) + '.jpg'
			ResizedImage = Resize(ImgFile)
			cv2.imwrite(ResizePath, ResizedImage)

			MirrorPath = SavePath + '/' +folder + '_m' + str(N) + '.jpg'
			MirrorIamge =  Mirror(ImgFile)
			cv2.imwrite(MirrorPath, MirrorIamge)

			RotatePath = SavePath + '/' +folder + '_t' + str(N)
			angle =[90,180,270]
			
			for i in angle:
				RotateImage = Rotate(ImgFile,i)
				cv2.imwrite(RotatePath + '_' + str(i) + '.jpg', RotateImage)

			# angle1 = 90
			# angle2 = 180
			# angle3 = 270

			# RotateImage90 = Rotate(ImgFile,angle1)
			# cv2.imwrite(RotatePath + '_' + '90' + '.jpg', RotateImage90)
			# RotateImage180 = Rotate(ImgFile,angle2)
			# cv2.imwrite(RotatePath + '_' + '180' + '.jpg', RotateImage180)
			# RotateImage270 = Rotate(ImgFile,angle3)
			# cv2.imwrite(RotatePath + '_' + '270' + '.jpg', RotateImage270)

			# AffinePath = SavePath + '/' +folder + '_a' + str(N) + '.jpg'
			# AffineIamge = Affine(ImgFile)
			# cv2.imwrite(AffinePath, AffineIamge)

			ZoomInPath = SavePath + '/' +folder + '_z' + str(N) + '.jpg'
			ZoomInImage = ZoomIn(ImgPath)
			ZoomInImage.save(ZoomInPath)
		# cv2.imwrite(ZoomInPath, ZoomInImage)
			N += 1
			print N


def Resize(ImgFile):
	# ResizedImg = ImgFile.resize((224,224))
	ResizedImg = cv2.resize(ImgFile,(224,224))
	return ResizedImg

def Mirror(ImgFile):
	size = ImgFile.shape
	CopyImg = copy.deepcopy(ImgFile)
	# RotateImg = cv.CreateImage(size, ImgFile.depth, ImgFile.nChannels)
	h = size[0]
	w = size[1]
	for i in range(h):
		for j in range(w):
			CopyImg[i,w-1-j] = ImgFile[i,j]

	CopyResizeImg = cv2.resize(CopyImg,(224,224))
	return CopyResizeImg	
	# imwrite(path, CopyImg)

def Rotate(ImgFile, angle):
	size = ImgFile.shape
	height = size[0]
	width = size[1]
	# RotateImg = cv.CreateImage(size, ImgFile.depthm, ImgFile.nChannels)
	# for i in angle:
	RotateImg = cv2.getRotationMatrix2D((height/2,width/2),angle,1)
	RotateWarpImg = cv2.warpAffine(ImgFile, RotateImg, (224,224))
	RotateResizeImg = cv2.resize(RotateWarpImg,(224,224))
	return RotateResizeImg

def ZoomIn(ImgPath):

	ImgFile = Image.open(ImgPath)
	ImgFile = ImgFile.convert('RGB')
	# size = ImgFile.shape
	height = ImgFile.size[0]
	width = ImgFile.size[1]

	box = [width*1/6,height*1/6,width*5/6,height*5/6]
	ZoomInImg = ImgFile.crop(box)
	ZoomInImg.resize((224,224))
	return ZoomInImg
#Affine

# def Affine(ImgFile):
# 	size = ImgFile.shape
# 	height = size[0]
# 	width = size[1]

# 	position1 = np.float32([[5,5],[20,5],[5,20]])
# 	position2 = np.float32([[2,10],[20,5],[10,25]])

# 	AffineImg = cv2.getAffineTransform(position1,position2)
# 	ResizeAffineImg = cv2.warpAffine(ImgFile, AffineImg,(224,224))
# 	# cv2.imwrite(path, ResizeAffine)
# 	return ResizeAffineImg

ExpandImage(TrainDataPath,ResizedPath)


自己的网络,要求图片大小为224:统一将图片大小resize为224*224





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值