JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyNetOpenCV.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 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)
Simple DNN network invoked from OpenCV in python.
freeze(self, doit)
[Optional] Freeze some parameters that should not be changed at runtime
load(self)
[Required] Load the network from disk
init(self)
[Optional] JeVois parameters initialization
__init__(self)
[Optional] Constructor
process(self, blobs)
[Required] Main processing function: process input blobs through network and return output blobs blob...