Examples >> Detecting scene boundaries
Fork me on GitHub

Detecting scene boundariesΒΆ

For this example, the luminance and edge-based scene detection algorithms are compared.

 1import matplotlib.pyplot as plt
 2import numpy as np
 3
 4import skvideo.measure
 5
 6try:
 7    xrange
 8except NameError:
 9    xrange = range
10
11def getPlot(edgelist1, edgelist2, t, w, h, T):
12    myDPI = 100
13    fig = plt.figure(figsize=(w/myDPI, h/myDPI), dpi=myDPI)
14    plt.subplot(211)
15    plt.title("histogram algorithm")
16    plt.plot(edgelist1)
17    plt.plot([t, t], [0, 1])
18    plt.xlim([0, T])
19    plt.ylim([0, 1])
20
21    plt.subplot(212)
22    plt.title("edges algorithm")
23    plt.plot(edgelist2)
24    plt.plot([t, t], [0, 1])
25    plt.xlim([0, T])
26    plt.ylim([0, 1])
27    
28    plt.tight_layout()
29    fig.canvas.draw()
30 
31    # Get the RGBA buffer from the figure
32    w,h = fig.canvas.get_width_height()
33    buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)
34    buf.shape = (h, w, 4)
35    plot = buf[:, :, 1:]
36    plt.close()
37
38    return plot
39
40filename = skvideo.datasets.bikes()
41
42videodata = skvideo.io.vread(filename)
43videometadata = skvideo.io.ffprobe(filename)
44frame_rate = videometadata['video']['@avg_frame_rate']
45num_frames = int(videometadata['video']['@nb_frames'])
46width = int(videometadata['video']['@width'])
47height = int(videometadata['video']['@height'])
48
49# using the "edge" algorithm
50scene_edge_idx = skvideo.measure.scenedet(videodata, method='edges')
51scene_edge = np.zeros((num_frames,))
52scene_edge[scene_edge_idx] = 1
53
54# using the "luminance" algorithm
55scene_lum_idx = skvideo.measure.scenedet(videodata, method='histogram', parameter1=1.0)
56scene_lum = np.zeros((num_frames,))
57scene_lum[scene_lum_idx] = 1
58
59# make a video that tracks the scene cuts as the video plays
60writer = skvideo.io.FFmpegWriter("scene_cuts.mp4", inputdict={
61    "-r": frame_rate,
62})
63
64for i in xrange(num_frames):
65    outputframe = np.zeros((height, width*2, 3))
66
67    chart = getPlot(scene_lum, scene_edge, i, width, height, num_frames)
68
69    outputframe[:, :width] = videodata[i]
70    outputframe[:, width:] = chart
71    writer.writeFrame(outputframe)
72
73writer.close()

Video output shows that the luminance scene detector captures all the scene changes in this particular video: