JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyLicensePlate.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: import libjevois as jevois
4import cv2
5import numpy as np
6
7from lpd_yunet import LPD_YuNet
8
9## Detect license plates on NPU using YuNet TIM-VX
10#
11# This module runs on the JeVois-Pro NPU using a quantized deep neural network. It is derived from
12# https://github.com/opencv/opencv_zoo/tree/master/models/license_plate_detection_yunet
13# See LICENSE for license information.
14#
15# Please note that the model is trained with Chinese license plates, so the detection results of other license plates
16# with this model may be limited. See the screenshots of this module for examples, or search for "Chinese license plate"
17# on the web for more images.
18#
19# This module is mainly intended as a tutorial for how to run quantized int8 DNNs on the NPU using OpenCV and TIM-VX,
20# here achieved through only small modifications of code from https://github.com/opencv/opencv_zoo - in particular,
21# the core class for this model, LPD_YuNet, was not modified at all, and only the demo code was edited to use the
22# JeVois GUI Helper for fast OpenGL drawing as opposed to slow drawings into OpenCV images.
23#
24# @author Laurent Itti
25#
26# @displayname PyLicensePlate
27# @videomapping JVUI 0 0 30.0 CropScale=RGB24@512x288:YUYV 1920 1080 30.0 JeVois PyLicensePlate
28# @videomapping JVUI 0 0 30.0 CropScale=RGB24@256x144:YUYV 1920 1080 30.0 JeVois PyLicensePlate
29# @email itti\@usc.edu
30# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
31# @copyright Copyright (C) 2022 by Laurent Itti, iLab and the University of Southern California
32# @mainurl http://jevois.org
33# @supporturl http://jevois.org/doc
34# @otherurl http://iLab.usc.edu
35# @license GPL v3
36# @distribution Unrestricted
37# @restrictions None
38# @ingroup modules
40 # ###################################################################################################
41 ## Constructor
42 def __init__(self):
43 # Instantiate the model
44 self.modelname = "license_plate_detection_lpd_yunet_2022may-int8-quantized.onnx"
45
46 self.model = LPD_YuNet(modelPath = "/jevoispro/share/npu/other/" + self.modelname,
47 confThreshold = 0.9,
48 nmsThreshold = 0.3,
49 topK = 5000,
50 keepTopK = 750,
51 backendId = cv2.dnn.DNN_BACKEND_TIMVX,
52 targetId = cv2.dnn.DNN_TARGET_NPU)
53
54 # Instantiate a timer for framerate:
55 self.timer = jevois.Timer('PyLicensePlate', 30, jevois.LOG_DEBUG)
56
57 # Keep track of frame size:
58 self.h = 0
59 self.w = 0
60
61 # ####################################################################################################
62 ## Process function with GUI output
63 def processGUI(self, inframe, helper):
64 # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
65 idle, winw, winh = helper.startFrame()
66
67 # Draw full-resolution input frame from camera:
68 dx, dy, dw, dh = helper.drawInputFrame("c", inframe, False, False)
69 helper.itext('JeVois-Pro License Plate Detection')
70
71 # Get the next camera image at processing resolution (may block until it is captured):
72 frame = inframe.getCvBGRp()
73 h, w, _ = frame.shape
74
75 # Start measuring image processing time:
76 self.timer.start()
77
78 # Resize model if needed:
79 if self.w != w or self.h != h:
80 self.model.setInputSize([w, h])
81 self.h = h
82 self.w = w
83
84 # Inference
85 dets = self.model.infer(frame)
86
87 # Draw the detections:
88 for det in dets:
89 bbox = det[:-1]
90 x1, y1, x2, y2, x3, y3, x4, y4 = bbox
91
92 # Draw the border of license plate
93 helper.drawLine(float(x1), float(y1), float(x2), float(y2), 0xff0000ff) # color format is ABGR
94 helper.drawLine(float(x2), float(y2), float(x3), float(y3), 0xff0000ff)
95 helper.drawLine(float(x3), float(y3), float(x4), float(y4), 0xff0000ff)
96 helper.drawLine(float(x4), float(y4), float(x1), float(y1), 0xff0000ff)
97
98 # Write frames/s info from our timer:
99 fps = self.timer.stop()
100 helper.iinfo(inframe, fps, winw, winh);
101 helper.itext("JeVois-Pro - " + self.modelname)
102
103 # End of frame:
104 helper.endFrame()
Detect license plates on NPU using YuNet TIM-VX.
processGUI(self, inframe, helper)
Process function with GUI output.