1.opencv保存视频文件很小,没有视频
cap = cv2.VideoCapture(args.video_path)
cv2.namedWindow("SSD", 0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH),cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#**这里的size必须与最后保存的图片的size一致,否则保存的文件很小,没有视频**
size=(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = 15 # 视频每秒24帧
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#保存视频文件的名称
name=os.path.basename(args.video_path)[0:-4]+"_test.avi"
outVideoName=os.path.join(os.path.dirname(args.video_path),name)
VideoOut = cv2.VideoWriter(outVideoName, fourcc, fps, size)
2.opencv imread读入的图片转torch.tensor()
有两种方法
1.经过PIL Image转tensor
from PIL import Image
ret, ori_img = cap.read()#h w c bgr
image = Image.fromarray(cv2.cvtColor(ori_img, cv2.COLOR_BGR2RGB))
cls_img = data_transforms['val'](image)
data_transforms =
{
'train': transforms.Compose([
transforms.Resize((img_size,img_size),3),
#transforms.RandomCrop(img_size),
transforms.RandomHorizontalFlip(),#随机水平翻转
transforms.RandomRotation(10), #随机旋转45度
transforms.ColorJitter(brightness=1,contrast=1,hue=0.5),# 随机从 0 ~ 2 之间亮度变化,1 表示原图
transforms.RandomGrayscale(p=0.5), # 以0.5的概率进行灰度化
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize((img_size,img_size),3),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
2. 利用numpy
transforms.Resize((img_size,img_size),3)不能对numpy.narray进行处理,所以在外部进行cv2.resize(),同时To.Tensor()会将 hwc===>>>chw所以外部不要进行np.transpose()
#读取视频帧
ret, ori_img = cap.read()#h w c bgr
image=ori_img[...,[2,1,0]]#h w c bgr to hwc rgb
image=cv2.resize(image,(img_size,img_size))
#image=np.transpose(image,[2,0,1])#hwc rgb totensor 会换成 chw rgb
cls_img = data_transforms['val'](image)
data_transforms =
{
'train': transforms.Compose([
transforms.Resize((img_size,img_size),3),
#transforms.RandomCrop(img_size),
transforms.RandomHorizontalFlip(),#随机水平翻转
transforms.RandomRotation(10), #随机旋转45度
transforms.ColorJitter(brightness=1,contrast=1,hue=0.5),# 随机从 0 ~ 2 之间亮度变化,1 表示原图
transforms.RandomGrayscale(p=0.5), # 以0.5的概率进行灰度化
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
#transforms.Resize((img_size,img_size),3),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}