mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2024-06-10 02:09:22 +00:00
b423febfbe
This new video filter is able to detect the dominant color in a video frame. When the color has changed from the previous frame the filter posts an Element message on the bus, the associated structure is named `colordetect` and has two fields: * a string field named `dominant-color` * a list field containing the whole color palette, stored as uint values, sorted by dominance, with more dominant colors first
67 lines
2.2 KiB
Rust
67 lines
2.2 KiB
Rust
// Copyright (C) 2022 Philippe Normand <philn@igalia.com>
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
// <https://mozilla.org/MPL/2.0/>.
|
|
//
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
use gst::prelude::*;
|
|
|
|
fn init() {
|
|
use std::sync::Once;
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
|
gst::init().unwrap();
|
|
gstvideofx::plugin_register_static().expect("Failed to register videofx plugin");
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn test_red_color() {
|
|
init();
|
|
let pipeline = gst::Pipeline::new(None);
|
|
|
|
let src = gst::ElementFactory::make("videotestsrc", None).unwrap();
|
|
src.set_property_from_str("pattern", "red");
|
|
src.set_property("num-buffers", &2i32);
|
|
|
|
let filter = gst::ElementFactory::make("colordetect", None).unwrap();
|
|
let sink = gst::ElementFactory::make("fakevideosink", None).unwrap();
|
|
|
|
pipeline
|
|
.add_many(&[&src, &filter, &sink])
|
|
.expect("failed to add elements to the pipeline");
|
|
gst::Element::link_many(&[&src, &filter, &sink]).expect("failed to link the elements");
|
|
|
|
pipeline
|
|
.set_state(gst::State::Playing)
|
|
.expect("Unable to set the pipeline to the `Playing` state");
|
|
|
|
let mut detected_color: Option<String> = None;
|
|
let bus = pipeline.bus().unwrap();
|
|
for msg in bus.iter_timed(gst::ClockTime::NONE) {
|
|
use gst::MessageView;
|
|
match msg.view() {
|
|
MessageView::Element(elt) => {
|
|
if let Some(s) = elt.structure() {
|
|
if s.name() == "colordetect" {
|
|
// The video source emits 2 red frames, but we should
|
|
// receive only one message because the dominant color
|
|
// doesn't change.
|
|
assert_eq!(detected_color.as_deref(), None);
|
|
detected_color = Some(s.get::<String>("dominant-color").unwrap());
|
|
}
|
|
}
|
|
}
|
|
MessageView::Eos(..) => break,
|
|
_ => (),
|
|
}
|
|
}
|
|
|
|
pipeline.set_state(gst::State::Null).unwrap();
|
|
|
|
assert_eq!(detected_color.as_deref(), Some("red"));
|
|
}
|