如何在 python 中对图像作为输入和输出进行单元测试?



我正在对Python中的对象检测代码(接受的答案)进行单元测试。我知道在单元测试中,我们基本上将测试参数放入我们在程序中定义的函数中,然后输入预期的结果。如果输出预期结果,我们得到 OK,否则,我们将得到一个错误。

所以我的问题是我的输入是图像,我的输出也是一个图像(即在图像中检测到的对象),后来,结果使用条形图和带滑块的直方图表示。如何对此类数据进行单元测试?

到目前为止,这是我尝试过的(此代码保存为 cirCode)

from unittest import TestCase
import unittest
from unittest import TestCase
import cirCode
class TestFind_circles(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
#def test_circle(self):
#   self.fail()
def test_find_circles(self):
Negative_circles, Positive_circles, out_filepath, circles, threshold = cirCode.find_circles('blobs.jpg')
self.assertEqual(Negative_circles, 20)
self.assertEqual(Positive_circles, 8)
if __name__ == '__main__':
unittest.main()

现在,我不知道如何测试def circle函数。另外,我不确定这是否是测试find_circles功能的正确方法。

你们有更好的主意对此代码进行单元测试吗,以及如何对circle函数进行单元测试?

不确定你在问什么,我假设你要求 opencv 函数和变量的单元测试方法。

遵循opencv python测试示例怎么样?他们做了类似的工作,你已经尝试了。他们有几十个示例测试用例。

https://github.com/opencv/opencv/tree/master/modules/python/test

例如

import cv2 as cv
import numpy as np
import sys
from numpy import pi, sin, cos
from tests_common import NewOpenCVTests
def circleApproximation(circle):
nPoints = 30
dPhi = 2*pi / nPoints
contour = []
for i in range(nPoints):
contour.append(([circle[0] + circle[2]*cos(i*dPhi),
circle[1] + circle[2]*sin(i*dPhi)]))
return np.array(contour).astype(int)

最新更新