Skip to content

Performance Improvement for ThresholdDetector #64

@nirabo

Description

@nirabo

Hi,

Frame averaging currently is done via

numpy.sum(frame[:,:,:]) / num_pixel_values

I've experimented a bit and found that a substantial improvement can be made by using OpenCV built-in methods for this purpose. For example:

cv2.mean(cv2.mean(frame))[0] / 3

The resulting compute_frame_average() method runs ~10% faster on the test video.

Example:

# Test with the proposed change
$ scenedetect -i goldeneye.mp4  -s output.csv -l
[PySceneDetect] Detecting scenes (threshold mode)...
[PySceneDetect] Parsing video goldeneye.mp4...
[PySceneDetect] Video Resolution / Framerate: 1280 x 544 / 23.976 FPS
Verify that the above parameters are correct (especially framerate, use --force-fps to correct if required).
[PySceneDetect] Processing complete, found 0 scenes in video.
[PySceneDetect] Processed 1980 / 1980 frames read in 7.8 secs (avg 254.5 FPS).
# Test with current method
$ scenedetect -i goldeneye.mp4  -s output.csv -l
[PySceneDetect] Detecting scenes (threshold mode)...
[PySceneDetect] Parsing video goldeneye.mp4...
[PySceneDetect] Video Resolution / Framerate: 1280 x 544 / 23.976 FPS
Verify that the above parameters are correct (especially framerate, use --force-fps to correct if required).
[PySceneDetect] Processing complete, found 0 scenes in video.
[PySceneDetect] Processed 1980 / 1980 frames read in 8.6 secs (avg 229.4 FPS).

Unit-Testing for performance of the proposed vs existing solution

print(frame.shape)
print("Proposed Solution results:")
%timeit compute_frame_average_PROPOSED(frame)
print("Existing Solution results:")
%timeit compute_frame_average_EXISTING(frame)

results in:

(1080, 1980, 3)
Proposed Solution results:
100 loops, best of 3: 4.92 ms per loop
Existing Solution results:
100 loops, best of 3: 6.32 ms per loop

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions