Skip to content

Instantly share code, notes, and snippets.

View SeaOfOcean's full-sized avatar

Xianyan Jia SeaOfOcean

View GitHub Profile
__background__
messi
# load model and apply quantization
model = Model.loadModel("bigdl_messi.model").quantize()
# load dataset
myclip = VideoFileClip("messi_clip.mp4")
video_rdd = sc.parallelize(myclip.iter_frames(fps=5))
image_frame = DistributedImageFrame(video_rdd)
# Define Predictor Configure
preprocess = Pipeline([Resize(300, 300), ChannelNormalize(123.0, 117.0, 104.0), MatToTensor(), ImageFrameToSample()])
import os
from os.path import isfile
import random
annots = os.listdir("annotations")
random.shuffle(annots)
test_len = int(len(annots) * 0.1)
train_data = annots[test_len:]
test_data = annots[:test_len]
f = open('ImageSets/train_messi.txt', 'w')
<annotation>
<folder>messi</folder>
<filename>100.jpg</filename>
<path>100.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>1280</width>
<height>720</height>
from scipy.misc import imsave
path = 'test.mp4'
myclip = VideoFileClip(path)
iter = myclip.iter_frames(fps=1)
i = 0
for img in iter:
i = i + 1
imsave('output/' + str(i) + ".png", img)
import io
import base64
from IPython.display import HTML
video = io.open(output_path, 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''<video alt="test" controls>
<source src="data:video/mp4;base64,{0}" type="video/mp4" />
</video>'''.format(encoded.decode('ascii')))
# Import everything needed to edit/save/watch video clips
from moviepy.editor import *
from IPython.display import HTML
# Prepare input rdd
myclip = VideoFileClip("Dog imitates baby.mp4").subclip(0, 10)
video_rdd = sc.parallelize(myclip.iter_frames(fps=5))
(data, sizes) = preprocess(video_rdd, means, scale, resolution)
# Do detection
# preprocess data
(data, sizes) = preprocess(img_rdd, means, scale, resolution)
# image prediction
output = model.predict(data)
# scale normalized results according to original image sizes
result = output.zip(sizes).map(lambda (output_i, img_size):
scaleOutput(output_i, img_size)
).collect()
from scipy import misc
# data preprocess
def preprocess(imgRdd):
means = np.array([127.5, 127.5, 127.5]) # mean value in BGR order
sizes = imgRdd.map(lambda img: img.shape)
data = imgRdd.map(lambda original_img: misc.imresize(original_img, (300, 300))) \
.map(lambda resized_img: (resized_img[:, :] - means) * 0.007843) \
.map(lambda normalized_img: normalized_img.transpose(2, 0, 1)) \
from bigdl.nn.layer import Model
# load model
model = Model.load("bigdl_mobilenet.model")
print 'load model done'