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