您的位置:首页 > 大数据

京东金融大数据竞赛猪脸识别(1)-从视频提取图像

2018-03-13 10:35 656 查看
2017年11月的京东金融大数据竞赛参与人数最多的是猪脸识别的算法比赛,参加整个大数据比赛的有四千多人,而猪脸识别算法组就有一千多人。可见,搞图像识别的的人很多啊。想要提升自己价值的小伙伴们,向语音、文本、机器人等领域进发吧,有了机器学习的基础,入门这些领域应该都不是太难。
比赛给的数据是30头猪的视频,做识别的第一步还是从视频中去图像吧。本想用以前写过的视频取帧程序。看Matlab示例的时候发现用Matlab取帧更简便易行,那就用它吧。这样又省了很多时间。代码如下:

%exam1.m extract frame from video
clc;
close all;
imtool close all;
clear;
%30段视频在本代码同级目录video文件夹下,格式为mp4
fpath = [pwd,'\video\*.mp4'];
fname = dir(fpath);
fnum = numel(fname);
vidpath = [pwd,'\video'];
try
for i=1:fnum
videoname = fullfile(vidpath,fname(i).name);
%读取视频数据
videoObject = VideoReader(videoname);
% 获取视频中帧数.
numberOfFrames = videoObject.NumberOfFrames;
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
numberOfFramesWritten = 0;

figure;
writeToDisk = true;
%对视频文件全路径进行分离.
[folder, baseFileName, extentions] = fileparts(videoname);
%获取当前路径
folder = pwd;
%在当前路径下建立子文件夹image,并在image下为各视频建立以文件名命名的子文件夹,用以存放该视频提取的帧
outputFolder = sprintf('%s/image/%s', folder, baseFileName);
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
%每分钟约为30帧图像,每隔15帧取一帧
for frame = 1 :15: numberOfFrames
%读取帧数据
thisFrame = read(videoObject, frame);
image(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow; % Force it to refresh the window.
%将图像帧数据写入文件
if writeToDisk
% 创建图像名,格式为“视频名-帧序号”
imgName = sprintf('%s-%04d.jpg',baseFileName, frame);
outputFullFileName = fullfile(outputFolder, imgName);
text(5, 15, imgName, 'FontSize', 20);
% Extract the image with the text "burned into" it.
frameWithText = getframe(gca);  .
imwrite(frameWithText.cdata, outputFullFileName, 'jpg');
end

% 输出提取信息.
if writeToDisk
progressIndication = sprintf('Wrote frame %4d of %d.', frame, numberOfFrames);
else
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
end
disp(progressIndication);
numberOfFramesWritten = numberOfFramesWritten + 1;
end
end
catch
warning('Problem using function.  Assigning a value of 0.');
a = 0;
end

该程序运行完后,就得到了30头猪的图像信息。若需增加数据量,可将提取帧的间隔减小。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息