@ -3,12 +3,19 @@
import numpy as np
import cv2 as cv
import os
import sys
import unittest
from tests_common import NewOpenCVTests
# Plaidml is an optional backend
pkgs = [
try :
if sys . version_info [ : 2 ] < ( 3 , 0 ) :
raise unittest . SkipTest ( ' Python 2.x is not supported ' )
# Plaidml is an optional backend
pkgs = [
( ' ocl ' , cv . gapi . core . ocl . kernels ( ) ) ,
( ' cpu ' , cv . gapi . core . cpu . kernels ( ) ) ,
( ' fluid ' , cv . gapi . core . fluid . kernels ( ) )
@ -16,7 +23,7 @@ pkgs = [
]
class gapi_core_test ( NewOpenCVTests ) :
class gapi_core_test ( NewOpenCVTests ) :
def test_add ( self ) :
# TODO: Extend to use any type and size here
@ -34,7 +41,7 @@ class gapi_core_test(NewOpenCVTests):
comp = cv . GComputation ( cv . GIn ( g_in1 , g_in2 ) , cv . GOut ( g_out ) )
for pkg_name , pkg in pkgs :
actual = comp . apply ( cv . gin ( in1 , in2 ) , args = cv . compile_args ( pkg ) )
actual = comp . apply ( cv . gin ( in1 , in2 ) , args = cv . gapi . compile_args ( pkg ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( expected , actual , cv . NORM_INF ) ,
' Failed on ' + pkg_name + ' backend ' )
@ -56,7 +63,7 @@ class gapi_core_test(NewOpenCVTests):
comp = cv . GComputation ( cv . GIn ( g_in1 , g_in2 ) , cv . GOut ( g_out ) )
for pkg_name , pkg in pkgs :
actual = comp . apply ( cv . gin ( in1 , in2 ) , args = cv . compile_args ( pkg ) )
actual = comp . apply ( cv . gin ( in1 , in2 ) , args = cv . gapi . compile_args ( pkg ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( expected , actual , cv . NORM_INF ) ,
' Failed on ' + pkg_name + ' backend ' )
@ -76,7 +83,7 @@ class gapi_core_test(NewOpenCVTests):
comp = cv . GComputation ( g_in , g_out )
for pkg_name , pkg in pkgs :
actual = comp . apply ( cv . gin ( in_mat ) , args = cv . compile_args ( pkg ) )
actual = comp . apply ( cv . gin ( in_mat ) , args = cv . gapi . compile_args ( pkg ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( expected , actual , cv . NORM_INF ) ,
' Failed on ' + pkg_name + ' backend ' )
@ -95,7 +102,7 @@ class gapi_core_test(NewOpenCVTests):
comp = cv . GComputation ( cv . GIn ( g_in ) , cv . GOut ( b , g , r ) )
for pkg_name , pkg in pkgs :
actual = comp . apply ( cv . gin ( in_mat ) , args = cv . compile_args ( pkg ) )
actual = comp . apply ( cv . gin ( in_mat ) , args = cv . gapi . compile_args ( pkg ) )
# Comparison
for e , a in zip ( expected , actual ) :
self . assertEqual ( 0.0 , cv . norm ( e , a , cv . NORM_INF ) ,
@ -118,7 +125,7 @@ class gapi_core_test(NewOpenCVTests):
comp = cv . GComputation ( cv . GIn ( g_in , g_sc ) , cv . GOut ( mat , threshold ) )
for pkg_name , pkg in pkgs :
actual_mat , actual_thresh = comp . apply ( cv . gin ( in_mat , maxv ) , args = cv . compile_args ( pkg ) )
actual_mat , actual_thresh = comp . apply ( cv . gin ( in_mat , maxv ) , args = cv . gapi . compile_args ( pkg ) )
# Comparison
self . assertEqual ( 0.0 , cv . norm ( expected_mat , actual_mat , cv . NORM_INF ) ,
' Failed on ' + pkg_name + ' backend ' )
@ -127,6 +134,7 @@ class gapi_core_test(NewOpenCVTests):
self . assertEqual ( expected_thresh , actual_thresh [ 0 ] ,
' Failed on ' + pkg_name + ' backend ' )
def test_kmeans ( self ) :
# K-means params
count = 100
@ -134,7 +142,7 @@ class gapi_core_test(NewOpenCVTests):
in_mat = np . random . random ( sz ) . astype ( np . float32 )
K = 5
flags = cv . KMEANS_RANDOM_CENTERS
attempts = 1 ;
attempts = 1
criteria = ( cv . TERM_CRITERIA_MAX_ITER + cv . TERM_CRITERIA_EPS , 30 , 0 )
# G-API
@ -149,9 +157,9 @@ class gapi_core_test(NewOpenCVTests):
self . assertEqual ( sz [ 0 ] , labels . shape [ 0 ] )
self . assertEqual ( 1 , labels . shape [ 1 ] )
self . assertTrue ( labels . size != 0 )
self . assertEqual ( centers . shape [ 1 ] , sz [ 1 ] ) ;
self . assertEqual ( centers . shape [ 0 ] , K ) ;
self . assertTrue ( centers . size != 0 ) ;
self . assertEqual ( centers . shape [ 1 ] , sz [ 1 ] )
self . assertEqual ( centers . shape [ 0 ] , K )
self . assertTrue ( centers . size != 0 )
def generate_random_points ( self , sz ) :
@ -166,8 +174,8 @@ class gapi_core_test(NewOpenCVTests):
amount = sz [ 0 ]
K = 5
flags = cv . KMEANS_RANDOM_CENTERS
attempts = 1 ;
criteria = ( cv . TERM_CRITERIA_MAX_ITER + cv . TERM_CRITERIA_EPS , 30 , 0 ) ;
attempts = 1
criteria = ( cv . TERM_CRITERIA_MAX_ITER + cv . TERM_CRITERIA_EPS , 30 , 0 )
in_vector = self . generate_random_points ( sz )
in_labels = [ ]
@ -175,10 +183,10 @@ class gapi_core_test(NewOpenCVTests):
data = cv . GArrayT ( cv . gapi . CV_POINT2F )
best_labels = cv . GArrayT ( cv . gapi . CV_INT )
compactness , out_labels , centers = cv . gapi . kmeans ( data , K , best_labels , criteria , attempts , flags ) ;
comp = cv . GComputation ( cv . GIn ( data , best_labels ) , cv . GOut ( compactness , out_labels , centers ) ) ;
compactness , out_labels , centers = cv . gapi . kmeans ( data , K , best_labels , criteria , attempts , flags )
comp = cv . GComputation ( cv . GIn ( data , best_labels ) , cv . GOut ( compactness , out_labels , centers ) )
compact , labels , centers = comp . apply ( cv . gin ( in_vector , in_labels ) ) ;
compact , labels , centers = comp . apply ( cv . gin ( in_vector , in_labels ) )
# Assert
self . assertTrue ( compact > = 0 )
@ -186,5 +194,19 @@ class gapi_core_test(NewOpenCVTests):
self . assertEqual ( K , len ( centers ) )
except unittest . SkipTest as e :
message = str ( e )
class TestSkip ( unittest . TestCase ) :
def setUp ( self ) :
self . skipTest ( ' Skip tests: ' + message )
def test_skip ( ) :
pass
pass
if __name__ == ' __main__ ' :
NewOpenCVTests . bootstrap ( )