JeVoisBase  1.20
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  helper.itext('JeVois-Pro License Plate Detection', 0, -1)
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, 0, -1)
102 
103  # End of frame:
104  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