This notebook shows how to use a pretrained model in customized dataset to detect Lionel Messi.
We used one of the Messi videos in Youtube for demo, which is a scene of football game. We use a pretrained model (downloadable from link which is finetuned based on a SSD with VGG16 model for detection. It is able to detect Messi in the game. In the video Messi is identified with boxes and class scores are shown on top.
In [1]:
import os
from IPython.display import Image, display
%pylab inline
from moviepy.editor import *
In [2]:
%matplotlib inline
from zoo.common.nncontext import *
from zoo.feature.image import *
from zoo.feature.common import ChainedPreprocessing
from zoo.models.image.common.image_config import ImageConfigure
from zoo.models.image.objectdetection import *
In [3]:
sc = init_nncontext("Detect Messi Example")
Download model
Download Video Clip
Move the model and video to the same directory with this notebook
In [4]:
model=ObjectDetector.load_model("bigdl_messi_quantize.model")
print 'load model done'
In [5]:
path = "messi_clip.mp4"
myclip = VideoFileClip(path)
video_rdd = sc.parallelize(myclip.iter_frames(fps=5))
image_set = DistributedImageSet(video_rdd)
In [6]:
preprocess = ChainedPreprocessing([ImageResize(300, 300), ImageChannelNormalize(123.0, 117.0, 104.0), ImageMatToTensor(), ImageSetToSample()])
postprocess = ScaleDetection()
label_map = {0: '__background__', 1: 'messi'}
config = ImageConfigure(preprocess, postprocess, 2, label_map)
In [7]:
output = model.predict_image_set(image_set, config)
visualizer = Visualizer(config.label_map())
visualized = visualizer(output).get_image(to_chw=False).collect()
In [8]:
clip = ImageSequenceClip(visualized, fps=5)
output_path = '/tmp/out.mp4'
%time clip.write_videofile(output_path, audio=False)
clip.write_gif("messi.gif")
In [9]:
with open("messi.gif",'rb') as f:
display(Image(f.read()))