欢迎关注我的微信公众号“人小路远”哦,在这里我将会记录自己日常学习的点滴收获与大家分享,以后也可能会定期记录一下自己在外读博的所见所闻,希望大家喜欢,感谢支持!
以监控摄像头视频为例,帧率为15fps,分辨率为1920*1080,要求每秒提取一张JPG,并按照x hour/x minutes的文件夹目录进行存储,然后把所有的照片拉伸至2256*256。
from PIL import Image # 需要pillow库
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
im = Image.open(path)
w, h = im.size
im = im.resize((int(w * 256/1920), int(h * 256/1080)))
im.save(path2)
c和t为1-15帧,即每15帧抽一张图片。name为图片命名,从1开始,依次递增。file为分钟,从1开始。hour为小时,从1开始。每60分钟即3600张图,hour进1,file从1开始
import cv2
import os
from PIL import Image
import glob
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
# 图片保存的路径
path = '/home/lgy/桌面/picture/'
if not os.path.exists(path):
os.mkdir(path)
for i in range(1,11,1):
os.mkdir(path+str(i)+'hour/')
for x in range(1,61,1):
os.mkdir(path+str(i)+'hour/'+str(x)+'minutes/')
# 从路径加载视频
vc = cv2.VideoCapture('/home/lgy/桌面/infrared.mp4')
c = 1
# set interval
t = 15
name = 1
file = 1
hour = 1
rval = vc.isOpened()
while rval:
rval, frame = vc.read()
if (c % t == 1):
if (name % 60 == 0):
file = file + 1
if(file==61):
file == 1
if (name % 3600 == 0):
hour = hour + 2
file = 1
path2 = path + str(hour) + 'hour/'+ str(file) + 'minutes/'
print('path====' + str(path2) + str(name))
cv2.imwrite(path2 + str(name) + 's.jpg', frame)
# 图片拉伸1920*1080至256*256
im = Image.open(path2 + str(name) + 's.jpg')
w, h = im.size
im = im.resize((int(w * 256/1920), int(h * 256/1080)))
im.save(path2 + str(name) + 's.jpg')
name = name + 1
c = c + 1
vc.release()
欢迎关注我的微信公众号“人小路远”哦,在这里我将会记录自己日常学习的点滴收获与大家分享,以后也可能会定期记录一下自己在外读博的所见所闻,希望大家喜欢,感谢支持!