2 Example of using Cocotb to send image files in to the opencores JPEG Encoder 3 and check that the output is sufficiently similar to the input. 5 NB Limited to 96x96 images since we're using a static JPEG header. 10 from itertools
import izip
14 from cocotb.result
import TestFailure
15 from cocotb.regression
import TestFactory
16 from cocotb.clock
import Clock
18 from interfaces
import ImageDriver, JpegMonitor
22 Compare the similarity of two images 24 From http://rosettacode.org/wiki/Percentage_difference_between_images 26 assert i1.mode == i2.mode,
"Different kinds of images." 27 assert i1.size == i2.size,
"Different sizes." 29 pairs = izip(i1.getdata(), i2.getdata())
30 dif = sum(abs(c1-c2)
for p1,p2
in pairs
for c1,c2
in zip(p1,p2))
31 ncomponents = i1.size[0] * i1.size[1] * 3
32 return (dif / 255.0 * 100) / ncomponents
37 """Run an image file through the jpeg encoder and compare the result""" 38 cocotb.fork(Clock(dut.clk, 100).start())
40 driver = ImageDriver(dut)
41 monitor = JpegMonitor(dut)
44 driver.log.setLevel(logging.DEBUG)
45 monitor.log.setLevel(logging.DEBUG)
47 stimulus = Image.open(filename)
48 yield driver.send(stimulus)
49 output =
yield monitor.wait_for_recv()
52 output.save(filename +
"_process.jpg")
54 difference =
compare(stimulus, output)
56 dut.log.info(
"Compressed image differs to original by %f%%" % (difference))
58 if difference > threshold:
59 raise TestFailure(
"Resulting image file was too different (%f > %f)" %
60 (difference, threshold))
62 tf = TestFactory(process_image)
63 tf.add_option(
"filename", [os.path.join(
'test_images', f)
64 for f
in os.listdir(
'test_images')])
def process_image( dut, filename="", debug=False, threshold=0.22)