JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyPreBlob.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: import libjevois as jevois
4
5import numpy as np
6import cv2
7
8## Simple DNN pre-processor written in python
9#
10# This version is mainly for tutorial purposes and does not support as many features as the C++ PreProcessorBlob
11#
12# Compare this code to the C++ PreProcessorBlob (which has more functionality than here):
13# - Abstract base: https://github.com/jevois/jevois/blob/master/include/jevois/DNN/PreProcessor.H
14# - Header: https://github.com/jevois/jevois/blob/master/include/jevois/DNN/PreProcessorBlob.H
15# - Implementation: https://github.com/jevois/jevois/blob/master/src/jevois/DNN/PreProcessorBlob.C
16#
17# @author Laurent Itti
18#
19# @email itti\@usc.edu
20# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
21# @copyright Copyright (C) 2022 by Laurent Itti, iLab and the University of Southern California
22# @mainurl http://jevois.org
23# @supporturl http://jevois.org/doc
24# @otherurl http://iLab.usc.edu
25# @license GPL v3
26# @distribution Unrestricted
27# @restrictions None
28# @ingroup pydnn
30 # ###################################################################################################
31 ## [Optional] Constructor
32 def __init__(self):
33 self.results = [] # list of strings containing our latest results to report
34
35 # ###################################################################################################
36 ## [Optional] JeVois parameters initialization
37 def init(self):
38 pc = jevois.ParameterCategory("DNN Pre-Processing Options", "")
39
40 self.scale = jevois.Parameter(self, 'scale', 'float',
41 "Value scaling factor applied to input pixels, or 0.0 to extract a " +
42 "UINT8 blob, typically for use with quantized networks",
43 2.0/255.0, pc)
44
45 self.mean = jevois.Parameter(self, 'mean', 'fscalar',
46 "Mean value subtracted from input image",
47 (127.5, 127.5, 127.5), pc)
48
49 # ###################################################################################################
50 ## [Required] Main processing function: extract one or more 4D blobs from an image
51 ## img is the input image from the camera sensor
52 ## swaprb is true if we should swap red/blue channels (based on camera grab order vs. network desired order)
53 ## attrs is a list of string blob specifiers, one per desired blob (see below for format and parsing)
54 ## We return a tuple with 2 lists: list of blobs, and list of crop rectangles that were used to extract the blobs.
55 ## The Network will use the blobs, and a PostProcessor may use the crops to, e.g., rescale object detection boxes
56 ## back to the original image.
57 def process(self, img, swaprb, attrs):
58 # Clear any old results:
59 self.results.clear()
60
61 # We want to return a tuple containing two lists: list of 4D blobs (numpy arrays), and list of crop rectangles
62 blobs = []
63 crops = []
64
65 # Create one blob per received size attribute:
66 for a in attrs:
67 # attr format is: [NCHW:|NHWC:|NA:|AUTO:]Type:[NxCxHxW|NxHxWxC|...][:QNT[:fl|:scale:zero]]
68 alist = a.split(':')
69
70 # In this simple example, we will use cv2.blobFromImage() which returns NCHW blobs. Hence require that:
71 if alist[0] != 'NCHW': raise ValueError("Only NCHW blobs supported. Try the C++ preprocessor for more.")
72 typ = alist[1]
73 dims = alist[2]
74
75 # We only support uint8 or float32 in this example:
76 if typ == '8U': typ = cv2.CV_8U
77 elif typ == '32F': typ = cv2.CV_32F
78 else: raise ValueError("Only 8U or 32F blob types supported. Try the C++ preprocessor for more.")
79
80 # Split the dims from NxCxHxW string:
81 dlist = dims.split('x')
82 if len(dlist) != 4: raise ValueError("Only 4D blobs supported. Try the C++ preprocessor for more.")
83
84 # We just want the width and height:
85 siz = (int(dlist[3]), int(dlist[2]))
86
87 # Ok, hand it over to blobFromImage(). To keep it simple here, we do not do cropping, so that we can easily
88 # recover the crop rectangles from the original image:
89 b = cv2.dnn.blobFromImage(img, self.scale.get(), siz, self.mean.get(), swaprb, crop = False, ddepth = typ)
90 blobs.append(b)
91
92 # We did not crop, so the recangles are just the original image. We send rectangles as tuples of format:
93 # ( (x,y), (w, h) )
94 crops.append( ( (0, 0), (img.shape[1], img.shape[0] ) ) )
95
96 return (blobs, crops)
97
98 # ###################################################################################################
99 ## [Optional] Report the latest results obtained by process() by drawing them
100 ## outimg is None or a RawImage to draw into when in Legacy mode (drawing to an image send to USB)
101 ## helper is None or a GUIhelper to do OpenGL drawings when in JeVois-Pro mode
102 ## overlay is True if users wishes to see overlay text
103 ## idle is true if keyboard/mouse have been idle for a while, which typically would reduce what is displayed.
104 ##
105 ## Note that report() is called on every frame even though the network may run slower or take some time to load and
106 ## initialize, thus you should be prepared for report() being called even before process() has ever been called
107 ## (i.e., create some class member variables to hold the reported results, initialize them to some defaults in your
108 ## constructor, report their current values here, and update their values in process()).
109 def report(self, outimg, helper, overlay, idle):
110 # Nothing to report here. Note that the base C++ PreProcessor class will do some reporting for us.
111 # You could delete that whole method, kept here for tutorial purposes.
112 pass
113
Simple DNN pre-processor written in python.
Definition PyPreBlob.py:29
init(self)
[Optional] JeVois parameters initialization
Definition PyPreBlob.py:37
process(self, img, swaprb, attrs)
[Required] Main processing function: extract one or more 4D blobs from an image img is the input imag...
Definition PyPreBlob.py:57
__init__(self)
[Optional] Constructor
Definition PyPreBlob.py:32
report(self, outimg, helper, overlay, idle)
[Optional] Report the latest results obtained by process() by drawing them outimg is None or a RawIma...
Definition PyPreBlob.py:109