File size: 2,676 Bytes
a5d7af1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# SPDX-License-Identifier: MIT
# Copyright (C) Intel Corporation
#
# License Plate Recognition: DLStreamer pipeline sample.
# See README.md for prerequisites and inputs. Run: python3 dlstreamer_sample.py

import os

import gi

gi.require_version("Gst", "1.0")
gi.require_version("GstAnalytics", "1.0")
gi.require_version("DLStreamerWatermarkMeta", "1.0")
from gi.repository import Gst, GLib, GstAnalytics, DLStreamerWatermarkMeta

Gst.init([])

MODELS_DIR = os.path.abspath("./models/yolov8_license_plate_detector")
DETECTOR_XML = (
    f"{MODELS_DIR}/license-plate-reader/models/"
    "yolov8n/yolov8n_retrained.xml"
)
OCR_XML = (
    f"{MODELS_DIR}/license-plate-reader/models/"
    "ch_PP-OCRv4_rec_infer/ch_PP-OCRv4_rec_infer.xml"
)
INPUT_VIDEO = "ParkingVideo.mp4"
DEVICE = "GPU"

pipeline_str = (
    f"filesrc location={INPUT_VIDEO} ! decodebin3 ! "
    f"videoconvert ! queue ! "
    f"gvadetect model={DETECTOR_XML} device={DEVICE} ! queue ! "
    f"videoconvert ! "
    f"gvaclassify model={OCR_XML} device={DEVICE} ! queue ! "
    f"identity name=probe ! "
    f"gvawatermark displ-cfg=show-labels=false ! videoconvert ! video/x-raw,format=I420 ! "
    f"openh264enc ! h264parse ! "
    f"mp4mux ! filesink location=output_dlstreamer.mp4"
)

pipeline = Gst.parse_launch(pipeline_str)


def on_buffer(pad, info):
    buf = info.get_buffer()
    new_buf = buf.copy_deep()
    rmeta = GstAnalytics.buffer_get_analytics_relation_meta(new_buf)
    if not rmeta:
        info.set_buffer(new_buf)
        return Gst.PadProbeReturn.OK
    for od in rmeta.iter_on_type(GstAnalytics.ODMtd):
        _, x, y, w, h, _ = od.get_location()
        # OCR result is attached as ClsMtd with CONTAIN relation
        text = ""
        for cls in od.iter_direct_related(
            GstAnalytics.RelTypes.CONTAIN, GstAnalytics.ClsMtd
        ):
            if cls.get_length() > 0:
                q = cls.get_quark(0)
                text = GLib.quark_to_string(q) if q else ""
            break
        if text:
            DLStreamerWatermarkMeta.text_meta_add(
                new_buf, x=int(x), y=max(0, int(y) - 10),
                text=text, font_scale=0.6, font_type=0,
                r=0, g=255, b=0, thickness=1, draw_bg=True)
            print(f"Plate: {text}  bbox=({x},{y})", flush=True)
    info.set_buffer(new_buf)
    return Gst.PadProbeReturn.OK


probe = pipeline.get_by_name("probe")
probe.get_static_pad("src").add_probe(
    Gst.PadProbeType.BUFFER, on_buffer
)

pipeline.set_state(Gst.State.PLAYING)
bus = pipeline.get_bus()
bus.timed_pop_filtered(
    Gst.CLOCK_TIME_NONE,
    Gst.MessageType.EOS | Gst.MessageType.ERROR,
)
pipeline.set_state(Gst.State.NULL)