下面我定义读取的视频类型包括两个-------------avi和mp4.
但是我电脑里只有mp4类型的视频,所以就只演示一个了,两种格式运行都是一样的道理。
1定义read_pic()来读取路径下全部图片
def read_pic(path):
if os.path.exists(path):
print(1)
else:
print(2)
dirnames = sorted(os.listdir(path))
# print(dirname)
n = len(dirnames)
print(n)
Srcimg= [] # 创建一个集合用于保存所有图片
for dirname in dirnames:
# print("正在读取第%d张图片" % i)
# fromfile()函数读回数据时需要用户指定元素类型,并对数组的形状进行适当的修改,indecode下中文路径也可以运行
img = cv2.imdecode(np.fromfile(path + dirname, dtype=np.uint8), -1)
#####保存图片#########
# cv2.imwrite(output_path + "/" + dirname, img)
#img_path = os.path.join(input_path, dirname)
# f.write(img_path+'\n')
Srcimg.append(img)
print(img_path)
Srcimg = np.array(Srcimg)
return Srcimg
也可以用glob来读取该文件夹下全部图片,非常简洁好记,代码如下:
import glob
import os
import cv2
IMAGE_PATH='D:/pycharm/4kinds_detectface_module/test'
filenames = glob.glob(os.path.join(IMAGE_PATH,'*.jpg'))
nums = len(filenames)
print(filenames)
print(nums)
for i in range(nums):
img=cv2.imread(filenames[i])
cv2.imshow('img',img)
cv2.waitKey(0)
2定义读取mp4类型的视频,25帧读取一张图片
def splitFrames_mp4(sourceFileName):
# 在这里把后缀接上
video_path = os.path.join('E:/qq/MobileFile/', sourceFileName + '.mp4')
times = 0
# 提取视频的频率,每25帧提取一个,也就是一秒一张图
frameFrequency = 25
# 输出图片到当前目录vedio文件夹下
outPutDirName = 'D:/pycharm/facenet/data/lfw_5/' + sourceFileName + '/'
# 如果文件目录不存在则创建目录
Datas = []
if not os.path.exists(outPutDirName):
os.makedirs(outPutDirName)
camera = cv2.VideoCapture(video_path)
while True:
times+=1
res, image = camera.read()
if not res:
# print('not res , not image')
break
if times%frameFrequency==0:
# cv2.imwrite(outPutDirName + str(times)+'.jpg', image)
print(outPutDirName + str(times)+'.jpg')
Datas.append(image)
# cv2.imwrite(outPutDirName + str(times) + '.jpg', image)
# print(times)
print('图片提取结束')
# print(Datas)
camera.release()
return Datas
3定义读取avi类型的视频,25帧读取一张图片
def splitFrames(sourceFileName):
# 在这里把后缀接上
video_path = os.path.join(im_file, sourceFileName + '.avi')
outPutDirName = 'D:/pycharm/facenet/data/lfw_5/' + sourceFileName + '/'
if not os.path.exists(outPutDirName):
#如果文件目录不存在则创建目录
os.makedirs(outPutDirName)
datas = []
cap = cv2.VideoCapture(video_path) # 打开视频文件
num = 1
while True:
num = num + 1
# success 表示是否成功,data是当前帧的图像数据;.read读取一帧图像,移动到下一帧
success, data = cap.read()
if not success:
break
if num % 25 == 0:
print(outPutDirName + str(num)+'.jpg')
# cv2.imwrite( outPutDirName +str(num)+".jpg", data)
datas.append(data)
print('图片提取结束')
cap.release()
return datas
4完整代码:
import os
import cv2
import numpy as np
def read_pic(path):
if os.path.exists(path):
print(1)
else:
print(2)
dirnames = sorted(os.listdir(path))
# print(dirname)
n = len(dirnames)
print(n)
# f = open('neg.txt', 'w')
Srcimg= []
for dirname in dirnames:
# print("正在读取第%d张图片" % i)
# fromfile()函数读回数据时需要用户指定元素类型,并对数组的形状进行适当的修改,indecode下中文路径也可以运行
img = cv2.imdecode(np.fromfile(path + dirname, dtype=np.uint8), -1)
#####保存图片#########
# cv2.imwrite(output_path + "/" + dirname, img)
img_path = os.path.join(input_path, dirname)
# f.write(img_path+'\n')
Srcimg.append(img)
print(img_path)
Srcimg = np.array(Srcimg)
return Srcimg
def splitFrames_mp4(sourceFileName):
# 在这里把后缀接上
video_path = os.path.join('E:/qq/MobileFile/', sourceFileName + '.mp4')
times = 0
num = 0
# 提取视频的频率,每25帧提取一个,也就是一秒一张图
frameFrequency = 25
# 输出图片到当前目录vedio文件夹下
outPutDirName = 'D:/pycharm/arithmetic/' + sourceFileName + '/'
# 如果文件目录不存在则创建目录
Datas = []
if not os.path.exists(outPutDirName):
os.makedirs(outPutDirName)
camera = cv2.VideoCapture(video_path)
while True:
times+=1
res, image = camera.read()
if not res:
# print('not res , not image')
break
if times%frameFrequency==0:
num+=1
# cv2.imwrite(outPutDirName + str(times)+'.jpg', image)
print(outPutDirName + str(times)+'.jpg')
cv2.imwrite(outPutDirName+str(times)+'.jpg',image)
Datas.append(image)
# cv2.imwrite(outPutDirName + str(times) + '.jpg', image)
# print(times)
print('--------一共有{}-------'.format(num))
print('图片提取结束')
# print(Datas)
camera.release()
return Datas
# 从.avi 类型的视频中提取图像
def splitFrames(sourceFileName):
# 在这里把后缀接上
video_path = os.path.join(im_file, sourceFileName + '.avi')
outPutDirName = 'D:/pycharm/facenet/data/lfw_5/' + sourceFileName + '/'
if not os.path.exists(outPutDirName):
#如果文件目录不存在则创建目录
os.makedirs(outPutDirName)
datas = []
cap = cv2.VideoCapture(video_path) # 打开视频文件
num = 1
while True:
num = num + 1
# success 表示是否成功,data是当前帧的图像数据;.read读取一帧图像,移动到下一帧
success, data = cap.read()
if not success:
break
# im = Image.fromarray(data, mode='RGB') # 重建图像
# im.save('C:/Users/Taozi/Desktop/2019.04.30/' +str(num)+".jpg") # 保存当前帧的静态图像
# cv2.imwrite( outPutDirName +str(num)+".jpg", data)
# data.append(data)
if num % 25 == 0:
print(outPutDirName + str(num)+'.jpg')
datas.append(data)
print('图片提取结束')
cap.release()
return datas
if __name__ == '__main__':
im_file = 'E:/qq/MobileFile/'
# input_path = 'D:/pycharm/10kinds-light-face-detector-align-recognition-master/lfw_5/test/'
for im_name in os.listdir(im_file):
C_file = os.path.splitext(im_name)[-1]
if C_file == '.mp4':
print('~~~~~~~~~~ 从.mp4 视频提取图像 ~~~~~~~~~~~~~~~')
#
sourceFileName = os.path.splitext(im_name)[0]
splitFrames_mp4(sourceFileName)
if C_file == '.avi':
print('~~~~~~~~~~ 从.avi 视频提取图像 ~~~~~~~~~~~~~~~')
#
sourceFileName = os.path.splitext(im_name)[0]
splitFrames(sourceFileName)
e)
5运行结果:
6图片裁剪
裁剪部分主要是根据下面这一行代码进行的,这里要记住(我被这里坑了一下午),
参数tr[1]:左上角或右上角的纵坐标值
参数bl[1]:左下角或右下角的纵坐标值
参数tl[0]:左上角或左下角的横坐标值
参数br[0]:右上角或右下角的横坐标值
crop = img[int(tr[1]):int(bl[1]), int(tl[0]):int(br[0]) ]