视频转换成图片
import cv2
from datetime import datetime
def video_to_frames(path):
videoCapture = cv2.VideoCapture()
videoCapture.open(path)
# 帧率
fps = videoCapture.get(cv2.CAP_PROP_FPS)
# 总帧数
frames = videoCapture.get(cv2.CAP_PROP_FRAME_COUNT)
print("fps=", int(fps), "frames=", int(frames))
for i in range(int(frames)):
ret, frame = videoCapture.read()
cv2.imwrite("E:/train/picture/%d.jpg" % (i), frame)
return
if __name__ == '__main__':
t1 = datetime.now()
video_to_frames(r"E:\train\video\001.mp4")
t2 = datetime.now()
print("Time cost = ", (t2 - t1))
print("SUCCEED !!!")
数据增强
from PIL import Image
from PIL import ImageEnhance
import os
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from PIL import Image
from keras.preprocessing import image as imageGenerator
import glob
# imageDir="D:/fphotos/flower_photos/train/roses" #要改变的图片的路径文件夹
# E:/flower_world-master/flphotos/train/daisy
imageDir=r"E:\train\old" #要改变的图片的路径文件夹
# { 'roses','tulips','dandelion','sunflowers','daisy'}
saveDir=r"E:\train\new" #数据增强生成图片的路径文件夹
def brightnessEnhancement(root_path,img_name):#亮度增强
image = Image.open(os.path.join(root_path, img_name))
enh_bri = ImageEnhance.Brightness(image)
# brightness = 1.1+0.4*np.random.random()#取值范围1.1-1.5
brightness = 1.5
image_brightened = enh_bri.enhance(brightness)
return image_brightened
def contrastEnhancement(root_path, img_name): # 对比度增强
image = Image.open(os.path.join(root_path, img_name))
enh_con = ImageEnhance.Contrast(image)
# contrast = 1.1+0.4*np.random.random()#取值范围1.1-1.5
contrast = 1.5
image_contrasted = enh_con.enhance(contrast)
return image_contrasted
def rotation(root_path, img_name):
img = Image.open(os.path.join(root_path, img_name))
random_angle = np.random.randint(-2, 2)*90
if random_angle==0:
rotation_img = img.rotate(-90) #旋转角度
else:
rotation_img = img.rotate( random_angle) # 旋转角度
# rotation_img.save(os.path.join(root_path,img_name.split('.')[0] + '_rotation.jpg'))
return rotation_img
def flip(root_path,img_name): #翻转图像
img = Image.open(os.path.join(root_path, img_name))
filp_img = img.transpose(Image.FLIP_LEFT_RIGHT)
# filp_img.save(os.path.join(root_path,img_name.split('.')[0] + '_flip.jpg'))
return filp_img
i=0
for name in os.listdir(imageDir):
i=i+1
saveName="cesun"+str(i)+".jpg"
saveImage=contrastEnhancement(imageDir,name)
saveImage.save(os.path.join(saveDir,saveName))
saveName1 = "flip" + str(i) + ".jpg"
saveImage1 = flip(imageDir,name)
saveImage1.save(os.path.join(saveDir, saveName1))
saveName2 = "brightnessE" + str(i) + ".jpg"
saveImage2 = brightnessEnhancement(imageDir, name)
saveImage2.save(os.path.join(saveDir, saveName2))