JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PyPostStub.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 
5 import numpy as np
6 
7 ## Stub DNN post-processor written in python
8 #
9 # When importing a network for which you are not sure about the exact output tensors and how to interpret them,
10 # you can use this stub first to see what outputs are produced by your network. You can then copy this stub to a
11 # new file name and edit it to actually decode the outputs of your network.
12 #
13 # You would create an entry like this in your network's YAML file:
14 #
15 # MyNet:
16 # [...]
17 # postproc: Python
18 # pypost: "pydnn/post/PyPostStub.py"
19 # [...]
20 #
21 # @author Laurent Itti
22 #
23 # @email itti\@usc.edu
24 # @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
25 # @copyright Copyright (C) 2023 by Laurent Itti, iLab and the University of Southern California
26 # @mainurl http://jevois.org
27 # @supporturl http://jevois.org/doc
28 # @otherurl http://iLab.usc.edu
29 # @license GPL v3
30 # @distribution Unrestricted
31 # @restrictions None
32 # @ingroup pydnn
33 class PyPostStub: # When copying to a new file, REMEMBER to change class name to match file name
34  # ###################################################################################################
35  ## [Optional] Constructor
36  def __init__(self):
37  self.results = [] # use a persistent variable to store results in process() and then draw them in report()
38 
39  # ###################################################################################################
40  ## [Optional] JeVois parameters initialization
41  def init(self):
42  # Could define parameters here that will be accessible to users in the GUI
43  #pc = jevois.ParameterCategory("DNN Post-Processing Options", "")
44 
45  #self.classes = jevois.Parameter(self, 'classes', 'str',
46  # "Path to text file with names of object classes",
47  # '', pc)
48  #self.classes.setCallback(self.loadClasses)
49 
50  # See other python post-processors for examples.
51  pass
52 
53  # ###################################################################################################
54  ## [Optional] Freeze some parameters that should not be changed at runtime.
55  ## The JeVois core will call this with doit being either True or False
56  def freeze(self, doit):
57  #self.classes.freeze(doit)
58  pass
59 
60  # ###################################################################################################
61  ## [Optional] Parameter callback: Load class names when 'classes' parameter value is changed,
62  ## when a pipeline is selected from the model zoo
63  #def loadClasses(self, filename):
64  # if filename:
65  # jevois.LINFO(f"Loading {filename}...")
66  # f = open(pyjevois.share + '/' + filename, 'rt') # will throw if file not found
67  # self.classmap = f.read().rstrip('\n').split('\n')
68 
69  # ###################################################################################################
70  ## [Required] Main processing function: parse network output blobs and store resulting labels and scores locally.
71  ## outs is a list of numpy arrays for the network's outputs.
72  ## preproc is a handle to the pre-processor that was used, useful to recover transforms from original image
73  ## to cropped/resized network inputs (not used here).
74  def process(self, outs, preproc):
75  # Clear any old results:
76  self.results.clear()
77 
78  # Process the newly received network outputs:
79  for o in outs:
80  # Remove all dimensions that are 1, e.g., from 1x1x1000 to 1000
81  o = np.squeeze(o)
82 
83  # [INSERT YOUR OWN CODE HERE]
84  # Get any reportable results and store them in self.results or similar class variables,
85  # those will then be rendered on screen and to serial ports in report()
86 
87  # ###################################################################################################
88  ## [Optional] Report the latest results obtained by process() by drawing them
89  ## outimg is None or a RawImage to draw into when in Legacy mode (drawing to an image sent to USB)
90  ## helper is None or a GUIhelper to do OpenGL drawings when in JeVois-Pro GUI mode
91  ## overlay is True if user wishes to see overlay text
92  ## idle is true if keyboard/mouse have been idle for a while, which typically would reduce what is displayed
93  ##
94  ## Note that report() is called on every frame even though the network may run slower or take some time to load and
95  ## initialize, thus you should be prepared for report() being called even before process() has ever been called
96  ## (i.e., create some class member variables to hold the reported results, initialize them to some defaults in your
97  ## constructor, report their current values here, and update their values in process()).
98  def report(self, outimg, helper, overlay, idle):
99  # Legacy JeVois mode: Write results into YUYV RawImage to send over USB:
100  if outimg is not None:
101  if overlay:
102  jevois.writeText(outimg, 'Legacy mode', 50, 100, jevois.YUYV.White, jevois.Font.Font6x10)
103 
104  # JeVois-Pro mode: Write the results as OpenGL overlay text on top of the video:
105  if helper is not None:
106  if overlay:
107  # itext writes one line of text and keeps track of incrementing the ordinate:
108  helper.itext('Pro mode', 0, -1)
PyPostStub.PyPostStub
Stub DNN post-processor written in python.
Definition: PyPostStub.py:33
PyPostStub.PyPostStub.freeze
def freeze(self, doit)
[Optional] Freeze some parameters that should not be changed at runtime.
Definition: PyPostStub.py:56
PyPostStub.PyPostStub.process
def process(self, outs, preproc)
[Optional] Parameter callback: Load class names when 'classes' parameter value is changed,...
Definition: PyPostStub.py:74
PyPostStub.PyPostStub.__init__
def __init__(self)
[Optional] Constructor
Definition: PyPostStub.py:36
PyPostStub.PyPostStub.init
def init(self)
[Optional] JeVois parameters initialization
Definition: PyPostStub.py:41
PyPostStub.PyPostStub.report
def report(self, outimg, helper, overlay, idle)
[Optional] Report the latest results obtained by process() by drawing them outimg is None or a RawIma...
Definition: PyPostStub.py:98
PyPostStub.PyPostStub.results
results
Definition: PyPostStub.py:37