JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PyNetOpenCV.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 import cv2
7 
8 ## Simple DNN network invoked from OpenCV in python
9 #
10 # @author Laurent Itti
11 #
12 # @email itti\@usc.edu
13 # @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
14 # @copyright Copyright (C) 2022 by Laurent Itti, iLab and the University of Southern California
15 # @mainurl http://jevois.org
16 # @supporturl http://jevois.org/doc
17 # @otherurl http://iLab.usc.edu
18 # @license GPL v3
19 # @distribution Unrestricted
20 # @restrictions None
21 # @ingroup pydnn
23  # ###################################################################################################
24  ## [Optional] Constructor
25  def __init__(self):
26  self.net = None # we do not have a network yet, we will load it after parameters have been set
27  self.gflops = None # need a network and an image to compute number of operations in net
28 
29  # ###################################################################################################
30  ## [Optional] JeVois parameters initialization
31  def init(self):
32  pc = jevois.ParameterCategory("DNN Network Options", "")
33 
34  self.dataroot = jevois.Parameter(self, 'dataroot', 'str',
35  "Root directory to use when config or model parameters are relative paths.",
36  pyjevois.share, pc) # pyjevois.share contains '/jevois[pro]/share'
37 
38  self.intensors = jevois.Parameter(self, 'intensors', 'str',
39  "Specification of input tensors",
40  '', pc)
41 
42  self.outtensors = jevois.Parameter(self, 'outtensors', 'str',
43  "Specification of output tensors (optional)",
44  '', pc)
45 
46  self.config = jevois.Parameter(self, 'config', 'str',
47  "Path to a text file that contains network configuration. " +
48  "Can have extension .prototxt (Caffe), .pbtxt (TensorFlow), or .cfg (Darknet). " +
49  "If path is relative, it will be prefixed by dataroot.",
50  '', pc);
51 
52  self.model = jevois.Parameter(self, 'model', 'str',
53  "Path to a binary file of model contains trained weights. " +
54  "Can have extension .caffemodel (Caffe), .pb (TensorFlow), .t7 or .net (Torch), " +
55  ".tflite (TensorFlow Lite), or .weights (Darknet). If path is relative, it will be " +
56  "prefixed by dataroot.",
57  "", pc);
58 
59  # ###################################################################################################
60  ## [Optional] Freeze some parameters that should not be changed at runtime
61  def freeze(self, doit):
62  self.dataroot.freeze(doit)
63  self.intensors.freeze(doit)
64  self.outtensors.freeze(doit)
65  self.config.freeze(doit)
66  self.model.freeze(doit)
67 
68  # ###################################################################################################
69  ## [Required] Load the network from disk
70  def load(self):
71  self.net = cv2.dnn.readNet(self.dataroot.get() + '/' + self.model.get(),
72  self.dataroot.get() + '/' + self.config.get())
73  self.net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
74  self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
75 
76  # ###################################################################################################
77  ## [Required] Main processing function: process input blobs through network and return output blobs
78  ## blobs is a list of numpy arrays for the network's outputs
79  ## Should return a tuple with (list of output blobs, list of info strings), where the info strings
80  ## could contain some information about the network
81  def process(self, blobs):
82  if self.net is None: raise RuntimeError("Cannot process because no loaded network")
83  if len(blobs) != 1: raise ValueError("Only one input blob is supported")
84 
85  # Run the network:
86  self.net.setInput(blobs[0])
87  outs = self.net.forward()
88 
89  # Some simple info strings that will be shown along with preproc/postproc info:
90  if self.gflops is None: self.gflops = int(self.net.getFLOPS(blobs[0].shape) * 1.0e-9)
91 
92  info = [
93  "* Network",
94  "{}GFLOPS".format(self.gflops),
95  ]
96 
97  # Return outs and info:
98  return (outs, info)
PyNetOpenCV.PyNetOpenCV.process
def process(self, blobs)
[Required] Main processing function: process input blobs through network and return output blobs blob...
Definition: PyNetOpenCV.py:81
PyNetOpenCV.PyNetOpenCV.gflops
gflops
Definition: PyNetOpenCV.py:27
PyNetOpenCV.PyNetOpenCV.intensors
intensors
Definition: PyNetOpenCV.py:38
demo.int
int
Definition: demo.py:37
PyNetOpenCV.PyNetOpenCV
Simple DNN network invoked from OpenCV in python.
Definition: PyNetOpenCV.py:22
jevois::ParameterCategory
PyNetOpenCV.PyNetOpenCV.outtensors
outtensors
Definition: PyNetOpenCV.py:42
PyNetOpenCV.PyNetOpenCV.load
def load(self)
[Required] Load the network from disk
Definition: PyNetOpenCV.py:70
PyNetOpenCV.PyNetOpenCV.model
model
Definition: PyNetOpenCV.py:52
PyNetOpenCV.PyNetOpenCV.freeze
def freeze(self, doit)
[Optional] Freeze some parameters that should not be changed at runtime
Definition: PyNetOpenCV.py:61
PyNetOpenCV.PyNetOpenCV.__init__
def __init__(self)
[Optional] Constructor
Definition: PyNetOpenCV.py:25
PyNetOpenCV.PyNetOpenCV.init
def init(self)
[Optional] JeVois parameters initialization
Definition: PyNetOpenCV.py:31
PyNetOpenCV.PyNetOpenCV.dataroot
dataroot
Definition: PyNetOpenCV.py:34
PyNetOpenCV.PyNetOpenCV.net
net
Definition: PyNetOpenCV.py:26
PyNetOpenCV.PyNetOpenCV.config
config
Definition: PyNetOpenCV.py:46