@ -3,318 +3,338 @@
import numpy as np
import numpy as np
import cv2 as cv
import cv2 as cv
import os
import os
import sys
import unittest
from tests_common import NewOpenCVTests
from tests_common import NewOpenCVTests
class test_gapi_infer ( NewOpenCVTests ) :
try :
def infer_reference_network ( self , model_path , weights_path , img ) :
if sys . version_info [ : 2 ] < ( 3 , 0 ) :
net = cv . dnn . readNetFromModelOptimizer ( model_path , weights_path )
raise unittest . SkipTest ( ' Python 2.x is not supported ' )
net . setPreferableBackend ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE )
net . setPreferableTarget ( cv . dnn . DNN_TARGET_CPU )
blob = cv . dnn . blobFromImage ( img )
net . setInput ( blob )
class test_gapi_infer ( NewOpenCVTests ) :
return net . forward ( net . getUnconnectedOutLayersNames ( ) )
def infer_reference_network ( self , model_path , weights_path , img ) :
net = cv . dnn . readNetFromModelOptimizer ( model_path , weights_path )
net . setPreferableBackend ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE )
net . setPreferableTarget ( cv . dnn . DNN_TARGET_CPU )
def make_roi ( self , img , roi ) :
blob = cv . dnn . blobFromImage ( img )
return img [ roi [ 1 ] : roi [ 1 ] + roi [ 3 ] , roi [ 0 ] : roi [ 0 ] + roi [ 2 ] , . . . ]
net . setInput ( blob )
return net . forward ( net . getUnconnectedOutLayersNames ( ) )
def test_age_gender_infer ( self ) :
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
def make_roi ( self , img , roi ) :
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
return img [ roi [ 1 ] : roi [ 1 ] + roi [ 3 ] , roi [ 0 ] : roi [ 0 ] + roi [ 2 ] , . . . ]
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
img = cv . resize ( cv . imread ( img_path ) , ( 62 , 62 ) )
# OpenCV DNN
def test_age_gender_infer ( self ) :
dnn_age , dnn_gender = self . infer_reference_network ( model_path , weights_path , img )
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
# OpenCV G-API
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
g_in = cv . GMat ( )
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
inputs = cv . GInferInputs ( )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
inputs . setInput ( ' data ' , g_in )
device_id = ' CPU '
outputs = cv . gapi . infer ( " net " , inputs )
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
age_g = outputs . at ( " age_conv3 " )
img = cv . resize ( cv . imread ( img_path ) , ( 62 , 62 ) )
gender_g = outputs . at ( " prob " )
comp = cv . GComputation ( cv . GIn ( g_in ) , cv . GOut ( age_g , gender_g ) )
# OpenCV DNN
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
dnn_age , dnn_gender = self . infer_reference_network ( model_path , weights_path , img )
gapi_age , gapi_gender = comp . apply ( cv . gin ( img ) , args = cv . compile_args ( cv . gapi . networks ( pp ) ) )
# OpenCV G-API
g_in = cv . GMat ( )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
# Check
outputs = cv . gapi . infer ( " net " , inputs )
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
age_g = outputs . at ( " age_conv3 " )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
gender_g = outputs . at ( " prob " )
comp = cv . GComputation ( cv . GIn ( g_in ) , cv . GOut ( age_g , gender_g ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
def test_age_gender_infer_roi ( self ) :
gapi_age , gapi_gender = comp . apply ( cv . gin ( img ) , args = cv . gapi . compile_args ( cv . gapi . networks ( pp ) ) )
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
# Check
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
device_id = ' CPU '
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
img = cv . imread ( img_path )
roi = ( 10 , 10 , 62 , 62 )
# OpenCV DNN
def test_age_gender_infer_roi ( self ) :
dnn_age , dnn_gender = self . infer_reference_network ( model_path ,
# NB: Check IE
weights_path ,
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
self . make_roi ( img , roi ) )
return
# OpenCV G-API
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
g_in = cv . GMat ( )
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
g_roi = cv . GOpaqueT ( cv . gapi . CV_RECT )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
inputs = cv . GInferInputs ( )
device_id = ' CPU '
inputs . setInput ( ' data ' , g_in )
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
outputs = cv . gapi . infer ( " net " , g_roi , inputs )
img = cv . imread ( img_path )
age_g = outputs . at ( " age_conv3 " )
roi = ( 10 , 10 , 62 , 62 )
gender_g = outputs . at ( " prob " )
# OpenCV DNN
comp = cv . GComputation ( cv . GIn ( g_in , g_roi ) , cv . GOut ( age_g , gender_g ) )
dnn_age , dnn_gender = self . infer_reference_network ( model_path ,
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
weights_path ,
self . make_roi ( img , roi ) )
gapi_age , gapi_gender = comp . apply ( cv . gin ( img , roi ) , args = cv . compile_args ( cv . gapi . networks ( pp ) ) )
# OpenCV G-API
# Check
g_in = cv . GMat ( )
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
g_roi = cv . GOpaqueT ( cv . gapi . CV_RECT )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
def test_age_gender_infer_roi_list ( self ) :
outputs = cv . gapi . infer ( " net " , g_roi , inputs )
# NB: Check IE
age_g = outputs . at ( " age_conv3 " )
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
gender_g = outputs . at ( " prob " )
return
comp = cv . GComputation ( cv . GIn ( g_in , g_roi ) , cv . GOut ( age_g , gender_g ) )
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
rois = [ ( 10 , 15 , 62 , 62 ) , ( 23 , 50 , 62 , 62 ) , ( 14 , 100 , 62 , 62 ) , ( 80 , 50 , 62 , 62 ) ]
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
img = cv . imread ( img_path )
# OpenCV DNN
dnn_age_list = [ ]
dnn_gender_list = [ ]
for roi in rois :
age , gender = self . infer_reference_network ( model_path ,
weights_path ,
self . make_roi ( img , roi ) )
dnn_age_list . append ( age )
dnn_gender_list . append ( gender )
# OpenCV G-API
g_in = cv . GMat ( )
g_rois = cv . GArrayT ( cv . gapi . CV_RECT )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
outputs = cv . gapi . infer ( " net " , g_rois , inputs )
age_g = outputs . at ( " age_conv3 " )
gender_g = outputs . at ( " prob " )
comp = cv . GComputation ( cv . GIn ( g_in , g_rois ) , cv . GOut ( age_g , gender_g ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_age_list , gapi_gender_list = comp . apply ( cv . gin ( img , rois ) ,
args = cv . compile_args ( cv . gapi . networks ( pp ) ) )
# Check
for gapi_age , gapi_gender , dnn_age , dnn_gender in zip ( gapi_age_list ,
gapi_gender_list ,
dnn_age_list ,
dnn_gender_list ) :
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
gapi_age , gapi_gender = comp . apply ( cv . gin ( img , roi ) , args = cv . gapi . compile_args ( cv . gapi . networks ( pp ) ) )
def test_age_gender_infer2_roi ( self ) :
# Check
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
rois = [ ( 10 , 15 , 62 , 62 ) , ( 23 , 50 , 62 , 62 ) , ( 14 , 100 , 62 , 62 ) , ( 80 , 50 , 62 , 62 ) ]
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
img = cv . imread ( img_path )
# OpenCV DNN
dnn_age_list = [ ]
dnn_gender_list = [ ]
for roi in rois :
age , gender = self . infer_reference_network ( model_path ,
weights_path ,
self . make_roi ( img , roi ) )
dnn_age_list . append ( age )
dnn_gender_list . append ( gender )
# OpenCV G-API
g_in = cv . GMat ( )
g_rois = cv . GArrayT ( cv . gapi . CV_RECT )
inputs = cv . GInferListInputs ( )
inputs . setInput ( ' data ' , g_rois )
outputs = cv . gapi . infer2 ( " net " , g_in , inputs )
age_g = outputs . at ( " age_conv3 " )
gender_g = outputs . at ( " prob " )
comp = cv . GComputation ( cv . GIn ( g_in , g_rois ) , cv . GOut ( age_g , gender_g ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_age_list , gapi_gender_list = comp . apply ( cv . gin ( img , rois ) ,
args = cv . compile_args ( cv . gapi . networks ( pp ) ) )
# Check
for gapi_age , gapi_gender , dnn_age , dnn_gender in zip ( gapi_age_list ,
gapi_gender_list ,
dnn_age_list ,
dnn_gender_list ) :
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
def test_age_gender_infer_roi_list ( self ) :
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
rois = [ ( 10 , 15 , 62 , 62 ) , ( 23 , 50 , 62 , 62 ) , ( 14 , 100 , 62 , 62 ) , ( 80 , 50 , 62 , 62 ) ]
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
img = cv . imread ( img_path )
# OpenCV DNN
dnn_age_list = [ ]
dnn_gender_list = [ ]
for roi in rois :
age , gender = self . infer_reference_network ( model_path ,
weights_path ,
self . make_roi ( img , roi ) )
dnn_age_list . append ( age )
dnn_gender_list . append ( gender )
# OpenCV G-API
g_in = cv . GMat ( )
g_rois = cv . GArrayT ( cv . gapi . CV_RECT )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
outputs = cv . gapi . infer ( " net " , g_rois , inputs )
age_g = outputs . at ( " age_conv3 " )
gender_g = outputs . at ( " prob " )
comp = cv . GComputation ( cv . GIn ( g_in , g_rois ) , cv . GOut ( age_g , gender_g ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_age_list , gapi_gender_list = comp . apply ( cv . gin ( img , rois ) ,
args = cv . gapi . compile_args ( cv . gapi . networks ( pp ) ) )
# Check
for gapi_age , gapi_gender , dnn_age , dnn_gender in zip ( gapi_age_list ,
gapi_gender_list ,
dnn_age_list ,
dnn_gender_list ) :
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
def test_age_gender_infer2_roi ( self ) :
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013 '
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
rois = [ ( 10 , 15 , 62 , 62 ) , ( 23 , 50 , 62 , 62 ) , ( 14 , 100 , 62 , 62 ) , ( 80 , 50 , 62 , 62 ) ]
img_path = self . find_file ( ' cv/face/david2.jpg ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
img = cv . imread ( img_path )
# OpenCV DNN
dnn_age_list = [ ]
dnn_gender_list = [ ]
for roi in rois :
age , gender = self . infer_reference_network ( model_path ,
weights_path ,
self . make_roi ( img , roi ) )
dnn_age_list . append ( age )
dnn_gender_list . append ( gender )
# OpenCV G-API
g_in = cv . GMat ( )
g_rois = cv . GArrayT ( cv . gapi . CV_RECT )
inputs = cv . GInferListInputs ( )
inputs . setInput ( ' data ' , g_rois )
outputs = cv . gapi . infer2 ( " net " , g_in , inputs )
age_g = outputs . at ( " age_conv3 " )
gender_g = outputs . at ( " prob " )
comp = cv . GComputation ( cv . GIn ( g_in , g_rois ) , cv . GOut ( age_g , gender_g ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_age_list , gapi_gender_list = comp . apply ( cv . gin ( img , rois ) ,
args = cv . gapi . compile_args ( cv . gapi . networks ( pp ) ) )
# Check
for gapi_age , gapi_gender , dnn_age , dnn_gender in zip ( gapi_age_list ,
gapi_gender_list ,
dnn_age_list ,
dnn_gender_list ) :
self . assertEqual ( 0.0 , cv . norm ( dnn_gender , gapi_gender , cv . NORM_INF ) )
self . assertEqual ( 0.0 , cv . norm ( dnn_age , gapi_age , cv . NORM_INF ) )
def test_person_detection_retail_0013 ( self ) :
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013 '
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
img_path = self . find_file ( ' gpu/lbpcascade/er.png ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
img = cv . resize ( cv . imread ( img_path ) , ( 544 , 320 ) )
# OpenCV DNN
net = cv . dnn . readNetFromModelOptimizer ( model_path , weights_path )
net . setPreferableBackend ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE )
net . setPreferableTarget ( cv . dnn . DNN_TARGET_CPU )
blob = cv . dnn . blobFromImage ( img )
def parseSSD ( detections , size ) :
h , w = size
bboxes = [ ]
detections = detections . reshape ( - 1 , 7 )
for sample_id , class_id , confidence , xmin , ymin , xmax , ymax in detections :
if confidence > = 0.5 :
x = int ( xmin * w )
y = int ( ymin * h )
width = int ( xmax * w - x )
height = int ( ymax * h - y )
bboxes . append ( ( x , y , width , height ) )
return bboxes
net . setInput ( blob )
dnn_detections = net . forward ( )
dnn_boxes = parseSSD ( np . array ( dnn_detections ) , img . shape [ : 2 ] )
# OpenCV G-API
g_in = cv . GMat ( )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
g_sz = cv . gapi . streaming . size ( g_in )
outputs = cv . gapi . infer ( " net " , inputs )
detections = outputs . at ( " detection_out " )
bboxes = cv . gapi . parseSSD ( detections , g_sz , 0.5 , False , False )
comp = cv . GComputation ( cv . GIn ( g_in ) , cv . GOut ( bboxes ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_boxes = comp . apply ( cv . gin ( img . astype ( np . float32 ) ) ,
args = cv . gapi . compile_args ( cv . gapi . networks ( pp ) ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( np . array ( dnn_boxes ) . flatten ( ) ,
np . array ( gapi_boxes ) . flatten ( ) ,
cv . NORM_INF ) )
def test_person_detection_retail_0013 ( self ) :
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013 '
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
img_path = self . find_file ( ' gpu/lbpcascade/er.png ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
img = cv . resize ( cv . imread ( img_path ) , ( 544 , 320 ) )
# OpenCV DNN
net = cv . dnn . readNetFromModelOptimizer ( model_path , weights_path )
net . setPreferableBackend ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE )
net . setPreferableTarget ( cv . dnn . DNN_TARGET_CPU )
blob = cv . dnn . blobFromImage ( img )
def parseSSD ( detections , size ) :
h , w = size
bboxes = [ ]
detections = detections . reshape ( - 1 , 7 )
for sample_id , class_id , confidence , xmin , ymin , xmax , ymax in detections :
if confidence > = 0.5 :
x = int ( xmin * w )
y = int ( ymin * h )
width = int ( xmax * w - x )
height = int ( ymax * h - y )
bboxes . append ( ( x , y , width , height ) )
return bboxes
net . setInput ( blob )
dnn_detections = net . forward ( )
dnn_boxes = parseSSD ( np . array ( dnn_detections ) , img . shape [ : 2 ] )
# OpenCV G-API
g_in = cv . GMat ( )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
g_sz = cv . gapi . streaming . size ( g_in )
outputs = cv . gapi . infer ( " net " , inputs )
detections = outputs . at ( " detection_out " )
bboxes = cv . gapi . parseSSD ( detections , g_sz , 0.5 , False , False )
comp = cv . GComputation ( cv . GIn ( g_in ) , cv . GOut ( bboxes ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_boxes = comp . apply ( cv . gin ( img . astype ( np . float32 ) ) ,
args = cv . gapi . compile_args ( cv . gapi . networks ( pp ) ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( np . array ( dnn_boxes ) . flatten ( ) ,
np . array ( gapi_boxes ) . flatten ( ) ,
cv . NORM_INF ) )
except unittest . SkipTest as e :
message = str ( e )
class TestSkip ( unittest . TestCase ) :
def setUp ( self ) :
self . skipTest ( ' Skip tests: ' + message )
def test_skip ( ) :
pass
def test_person_detection_retail_0013 ( self ) :
pass
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013 '
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
img_path = self . find_file ( ' gpu/lbpcascade/er.png ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
img = cv . resize ( cv . imread ( img_path ) , ( 544 , 320 ) )
# OpenCV DNN
net = cv . dnn . readNetFromModelOptimizer ( model_path , weights_path )
net . setPreferableBackend ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE )
net . setPreferableTarget ( cv . dnn . DNN_TARGET_CPU )
blob = cv . dnn . blobFromImage ( img )
def parseSSD ( detections , size ) :
h , w = size
bboxes = [ ]
detections = detections . reshape ( - 1 , 7 )
for sample_id , class_id , confidence , xmin , ymin , xmax , ymax in detections :
if confidence > = 0.5 :
x = int ( xmin * w )
y = int ( ymin * h )
width = int ( xmax * w - x )
height = int ( ymax * h - y )
bboxes . append ( ( x , y , width , height ) )
return bboxes
net . setInput ( blob )
dnn_detections = net . forward ( )
dnn_boxes = parseSSD ( np . array ( dnn_detections ) , img . shape [ : 2 ] )
# OpenCV G-API
g_in = cv . GMat ( )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
g_sz = cv . gapi . streaming . size ( g_in )
outputs = cv . gapi . infer ( " net " , inputs )
detections = outputs . at ( " detection_out " )
bboxes = cv . gapi . parseSSD ( detections , g_sz , 0.5 , False , False )
comp = cv . GComputation ( cv . GIn ( g_in ) , cv . GOut ( bboxes ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_age , gapi_gender = comp . apply ( cv . gin ( img ) , args = cv . compile_args ( cv . gapi . networks ( pp ) ) )
gapi_boxes = comp . apply ( cv . gin ( img . astype ( np . float32 ) ) ,
args = cv . compile_args ( cv . gapi . networks ( pp ) ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( np . array ( dnn_boxes ) . flatten ( ) ,
np . array ( gapi_boxes ) . flatten ( ) ,
cv . NORM_INF ) )
def test_person_detection_retail_0013 ( self ) :
# NB: Check IE
if not cv . dnn . DNN_TARGET_CPU in cv . dnn . getAvailableTargets ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE ) :
return
root_path = ' /omz_intel_models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013 '
model_path = self . find_file ( root_path + ' .xml ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
weights_path = self . find_file ( root_path + ' .bin ' , [ os . environ . get ( ' OPENCV_DNN_TEST_DATA_PATH ' ) ] )
img_path = self . find_file ( ' gpu/lbpcascade/er.png ' , [ os . environ . get ( ' OPENCV_TEST_DATA_PATH ' ) ] )
device_id = ' CPU '
img = cv . resize ( cv . imread ( img_path ) , ( 544 , 320 ) )
# OpenCV DNN
net = cv . dnn . readNetFromModelOptimizer ( model_path , weights_path )
net . setPreferableBackend ( cv . dnn . DNN_BACKEND_INFERENCE_ENGINE )
net . setPreferableTarget ( cv . dnn . DNN_TARGET_CPU )
blob = cv . dnn . blobFromImage ( img )
def parseSSD ( detections , size ) :
h , w = size
bboxes = [ ]
detections = detections . reshape ( - 1 , 7 )
for sample_id , class_id , confidence , xmin , ymin , xmax , ymax in detections :
if confidence > = 0.5 :
x = int ( xmin * w )
y = int ( ymin * h )
width = int ( xmax * w - x )
height = int ( ymax * h - y )
bboxes . append ( ( x , y , width , height ) )
return bboxes
net . setInput ( blob )
dnn_detections = net . forward ( )
dnn_boxes = parseSSD ( np . array ( dnn_detections ) , img . shape [ : 2 ] )
# OpenCV G-API
g_in = cv . GMat ( )
inputs = cv . GInferInputs ( )
inputs . setInput ( ' data ' , g_in )
g_sz = cv . gapi . streaming . size ( g_in )
outputs = cv . gapi . infer ( " net " , inputs )
detections = outputs . at ( " detection_out " )
bboxes = cv . gapi . parseSSD ( detections , g_sz , 0.5 , False , False )
comp = cv . GComputation ( cv . GIn ( g_in ) , cv . GOut ( bboxes ) )
pp = cv . gapi . ie . params ( " net " , model_path , weights_path , device_id )
gapi_boxes = comp . apply ( cv . gin ( img . astype ( np . float32 ) ) ,
args = cv . compile_args ( cv . gapi . networks ( pp ) ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( np . array ( dnn_boxes ) . flatten ( ) ,
np . array ( gapi_boxes ) . flatten ( ) ,
cv . NORM_INF ) )
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :