JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PyLicensePlate.py
Go to the documentation of this file.
1 import pyjevois
2 if pyjevois.pro: import libjevoispro as jevois
3 else: import libjevois as jevois
4 import cv2
5 import numpy as np
6 
7 from 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 
70  # Get the next camera image at processing resolution (may block until it is captured):
71  frame = inframe.getCvBGRp()
72  h, w, _ = frame.shape
73 
74  # Start measuring image processing time:
75  self.timer.start()
76 
77  # Resize model if needed:
78  if self.w != w or self.h != h:
79  self.model.setInputSize([w, h])
80  self.h = h
81  self.w = w
82 
83  # Inference
84  dets = self.model.infer(frame)
85 
86  # Draw the detections:
87  for det in dets:
88  bbox = det[:-1]
89  x1, y1, x2, y2, x3, y3, x4, y4 = bbox
90 
91  # Draw the border of license plate
92  helper.drawLine(float(x1), float(y1), float(x2), float(y2), 0xff0000ff) # color format is ABGR
93  helper.drawLine(float(x2), float(y2), float(x3), float(y3), 0xff0000ff)
94  helper.drawLine(float(x3), float(y3), float(x4), float(y4), 0xff0000ff)
95  helper.drawLine(float(x4), float(y4), float(x1), float(y1), 0xff0000ff)
96 
97  # Write frames/s info from our timer:
98  fps = self.timer.stop()
99  helper.iinfo(inframe, fps, winw, winh);
100  helper.itext("JeVois-Pro - " + self.modelname, 0, -1)
101 
102  # End of frame:
103  helper.endFrame()
lpd_yunet.LPD_YuNet
Definition: lpd_yunet.py:6
PyLicensePlate.PyLicensePlate.modelname
modelname
Definition: PyLicensePlate.py:44
PyLicensePlate.PyLicensePlate
Detect license plates on NPU using YuNet TIM-VX.
Definition: PyLicensePlate.py:39
PyLicensePlate.PyLicensePlate.model
model
Definition: PyLicensePlate.py:46
PyLicensePlate.PyLicensePlate.h
h
Definition: PyLicensePlate.py:58
PyLicensePlate.PyLicensePlate.timer
timer
Definition: PyLicensePlate.py:55
PyLicensePlate.PyLicensePlate.__init__
def __init__(self)
Constructor.
Definition: PyLicensePlate.py:42
demo.float
float
Definition: demo.py:39
PyLicensePlate.PyLicensePlate.processGUI
def processGUI(self, inframe, helper)
Process function with GUI output.
Definition: PyLicensePlate.py:63
jevois::Timer
PyLicensePlate.PyLicensePlate.w
w
Definition: PyLicensePlate.py:59