JeVoisBase  1.10
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PyClassificationDNN.py
Go to the documentation of this file.
1 import libjevois as jevois
2 import cv2 as cv
3 import numpy as np
4 import sys
5 
6 ## Object recognition using OpenCV Deep Neural Networks (DNN)
7 #
8 # This module runs an object classification deep neural network using the OpenCV DNN library. Classification
9 # (recognition) networks analyze a central portion of the whole scene and produce identity labels and confidence scores
10 # about what the object in the field of view might be.
11 #
12 # This module supports detection networks implemented in TensorFlow, Caffe,
13 # Darknet, Torch, ONNX, etc as supported by the OpenCV DNN module.
14 #
15 # Included with the standard JeVois distribution are:
16 #
17 # - SqueezeNet v1.1, Caffe model
18 # - more to come, please contribute!
19 #
20 # See the module's constructor (__init__) code and select a value for \b model to switch network.
21 #
22 # Object category names for models trained on ImageNet are at
23 # https://github.com/jevois/jevoisbase/blob/master/share/opencv-dnn/classification/synset_words.txt
24 #
25 # Sometimes it will make mistakes! The performance of SqueezeNet v1.1 is about 56.1% correct (mean average precision,
26 # top-1) on the ImageNet test set.
27 #
28 # This module is adapted from the sample OpenCV code:
29 # https://github.com/opencv/opencv/blob/master/samples/dnn/classification.py
30 #
31 # More pre-trained models are available on github in opencv_extra
32 #
33 #
34 # @author Laurent Itti
35 #
36 # @videomapping YUYV 320 264 30.0 YUYV 320 240 30.0 JeVois PyClassificationDNN
37 # @email itti@usc.edu
38 # @address 880 W 1st St Suite 807, Los Angeles CA 90012, USA
39 # @copyright Copyright (C) 2018 by Laurent Itti
40 # @mainurl http://jevois.org
41 # @supporturl http://jevois.org
42 # @otherurl http://jevois.org
43 # @license GPL v3
44 # @distribution Unrestricted
45 # @restrictions None
46 # @ingroup modules
48  # ####################################################################################################
49  ## Constructor
50  def __init__(self):
51  self.confThreshold = 0.2 # Confidence threshold (0..1), higher for stricter confidence.
52  self.inpWidth = 227 # Resized image width passed to network
53  self.inpHeight = 227 # Resized image height passed to network
54  self.scale = 1.0 # Value scaling factor applied to input pixels
55  self.mean = [104, 117, 123] # Mean BGR value subtracted from input image
56  self.rgb = True # True if model expects RGB inputs, otherwise it expects BGR
57 
58  # Select one of the models:
59  model = 'SqueezeNet' # SqueezeNet v1.1, Caffe model
60 
61  # You should not have to edit anything beyond this point.
62  backend = cv.dnn.DNN_BACKEND_DEFAULT
63  target = cv.dnn.DNN_TARGET_CPU
64  self.classes = None
65  classnames = None
66  if (model == 'SqueezeNet'):
67  classnames = '/jevois/share/opencv-dnn/classification/synset_words.txt'
68  modelname = '/jevois/share/opencv-dnn/classification/squeezenet_v1.1.caffemodel'
69  configname = '/jevois/share/opencv-dnn/classification/squeezenet_v1.1.prototxt'
70 
71  # Load names of classes
72  if classnames:
73  with open(classnames, 'rt') as f:
74  self.classes = f.read().rstrip('\n').split('\n')
75 
76  # Load a network
77  self.net = cv.dnn.readNet(modelname, configname)
78  self.net.setPreferableBackend(backend)
79  self.net.setPreferableTarget(target)
80  self.timer = jevois.Timer('Neural classification', 10, jevois.LOG_DEBUG)
81  self.model = model
82 
83  # ####################################################################################################
84  ## JeVois main processing function
85  def process(self, inframe, outframe):
86  frame = inframe.getCvBGR()
87  self.timer.start()
88 
89  frameHeight = frame.shape[0]
90  frameWidth = frame.shape[1]
91 
92  # Create a 4D blob from a frame.
93  blob = cv.dnn.blobFromImage(frame, self.scale, (self.inpWidth, self.inpHeight), self.mean, self.rgb, crop=True)
94 
95  # Run a model
96  self.net.setInput(blob)
97  out = self.net.forward()
98 
99  # Get a class with a highest score:
100  out = out.flatten()
101  classId = np.argmax(out)
102  confidence = out[classId]
103 
104  # Create dark-gray (value 80) image for the bottom panel, 24 pixels tall and show top-1 class:
105  msgbox = np.zeros((24, frame.shape[1], 3), dtype = np.uint8) + 80
106  rlabel = ' '
107  if (confidence > self.confThreshold):
108  rlabel = '%s: %.2f' % (self.classes[classId] if self.classes else 'Class #%d' % classId, confidence*100)
109 
110  cv.putText(msgbox, rlabel, (3, 15), cv.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv.LINE_AA)
111 
112  # Put efficiency information.
113  cv.putText(frame, 'JeVois Classification DNN - ' + self.model, (3, 15),
114  cv.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv.LINE_AA)
115  t, _ = self.net.getPerfProfile()
116  fps = self.timer.stop()
117  label = fps + ', %dms' % (t * 1000.0 / cv.getTickFrequency())
118  cv.putText(frame, label, (3, frameHeight-5), cv.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv.LINE_AA)
119 
120  # Stack bottom panel below main image:
121  frame = np.vstack((frame, msgbox))
122 
123  # Send output frame to host:
124  outframe.sendCv(frame)
Object recognition using OpenCV Deep Neural Networks (DNN)
std::vector< std::string > split(std::string const &input, std::string const &regex="\")
def process(self, inframe, outframe)
JeVois main processing function.