From 16a9526d24fbdad7bbc544aeae9fd7ba067b3d91 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Sat, 28 Jun 2014 21:06:40 +0200 Subject: [PATCH] save figures from matplotlib --- imago_pack/intrsc.py | 15 ++++++++-- imago_pack/linef.py | 17 +----------- test/html_results.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/test.py | 9 +++++- 4 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 test/html_results.py diff --git a/imago_pack/intrsc.py b/imago_pack/intrsc.py index 9fabcd5..1d4dd67 100644 --- a/imago_pack/intrsc.py +++ b/imago_pack/intrsc.py @@ -55,6 +55,8 @@ def board(image, lines, show_all, do_something): if show_all: import matplotlib.pyplot as pyplot + import Image + fig = pyplot.figure(figsize=(8, 6)) pyplot.scatter(luma, saturation, color=[(s[2][0]/255., s[2][1]/255., @@ -62,13 +64,18 @@ def board(image, lines, show_all, do_something): for s in board_raw]) pyplot.xlim(0,1) pyplot.ylim(0,1) - pyplot.show() + fig.canvas.draw() + size = fig.canvas.get_width_height() + buff = fig.canvas.tostring_rgb() + image_p = Image.fromstring('RGB', size, buff, 'raw') + do_something(image_p, "color distribution") clusters = k_means.cluster(3, 2,zip(zip(luma, saturation), range(len(luma))), [[0., 0.5], [0.5, 0.5], [1., 0.5]]) #clusters.sort(key=mean_luma) if show_all: + fig = pyplot.figure(figsize=(8, 6)) pyplot.scatter([d[0][0] for d in clusters[0]], [d[0][1] for d in clusters[0]], color=(1,0,0,1)) pyplot.scatter([d[0][0] for d in clusters[1]], [d[0][1] for d in clusters[1]], @@ -77,7 +84,11 @@ def board(image, lines, show_all, do_something): color=(0,0,1,1)) pyplot.xlim(0,1) pyplot.ylim(0,1) - pyplot.show() + fig.canvas.draw() + size = fig.canvas.get_width_height() + buff = fig.canvas.tostring_rgb() + image_p = Image.fromstring('RGB', size, buff, 'raw') + do_something(image_p, "color clustering") clusters[0] = [(p[1], 'B') for p in clusters[0]] clusters[1] = [(p[1], '.') for p in clusters[1]] diff --git a/imago_pack/linef.py b/imago_pack/linef.py index 2e40fca..ab7c95d 100644 --- a/imago_pack/linef.py +++ b/imago_pack/linef.py @@ -66,11 +66,8 @@ def run_ransac(image): line2 = [line_to_points(line2, 0), line_to_points(line2, width - 1)] return [sorted(points), sorted(points2)], line, line2 - - def find_lines(image, show_image, logger): """Find lines in the *image*.""" - # TODO refactor into smaller functions logger("preprocessing") show_image(image, "original image") @@ -100,20 +97,8 @@ def find_lines(image, show_image, logger): draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120)) show_image(image_g, "lines") - return lines, l1, l2, bounds, hough # TODO - -def combine(image1, image2): - """Return a list of points that are present in both images.""" - im_l1 = image1.load() - im_l2 = image2.load() - - on_both = [] - for x in xrange(image1.size[0]): - for y in xrange(image1.size[1]): - if im_l1[x, y] and im_l2[x, y]: - on_both.append((x, y)) - return on_both + return lines, l1, l2, bounds, hough # TODO def line_from_angl_dist((angle, distance), size): """Take *angle* and *distance* (from the center of the image) of a line and diff --git a/test/html_results.py b/test/html_results.py new file mode 100644 index 0000000..a89c8af --- /dev/null +++ b/test/html_results.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python + +import sys +import pickle + +print "" +print "" +print "" +print "" + +print "" + +print "

Imago test

" + +print "
"
+
+res = pickle.load(open(sys.argv[1], 'r'))
+
+total = len(res)
+
+failed = [(name,error) for (name, time, output, error, r_code) in res
+          if r_code > 0]
+print "failed tests:", str(len(failed)) + '/' + str(total)
+
+for (name, error) in failed:
+    print "    ", name
+
+print ""
+
+n = 0
+t = 0
+correct = 0
+
+for (name, time, output, error, r_code) in res:
+    if r_code > 0:
+        continue
+    n += 1
+    t += time
+    ref = open(name.split('.')[0] + '.txt').read()
+    if output == ref:
+        correct += 1
+    else:
+        #TODO find and record the error
+        pass
+
+print "average runtime:", (t/n)
+print "correct:", str(correct) + '/' + str(n)
+
+print "
" + +for (name, time, output, error, r_code) in res: + print "
" + print "

" + name + "

" + print "" + print "" + output = output.split('\n') + correct = open(name[:-4]+".txt").readlines() + output = map(lambda s: s.split(), output) + correct = map(lambda s: s.split(), correct) + for i in range(len(output)): + print "" + for j in range(len(output[i])): + if output[i][j] == correct[i][j]: + print "".format(output[i][j]) + else: + print "".format(output[i][j]) + print "" + print "
{}
{}
" + print "
" + print "
" + +print "" +print "" diff --git a/test/test.py b/test/test.py index da0a1f4..e6b5b6d 100755 --- a/test/test.py +++ b/test/test.py @@ -22,11 +22,18 @@ res_name = "res" + datetime.datetime.now().strftime("%Y%m%d%H%M") + "-" + git_ha test_results = [] n = 1 +args = ["-w", "640"] +if len(sys.argv) > 1 and sys.argv[1] == "debug": + args = ["-vds"] + args + +args = ["../imago"] + args + for fl in test_files: print "testing file", fl, str(n) + "/" + str(len(test_files)) + print " ".join(args + [fl]) n += 1 t0 = time.time() - proc = subprocess.Popen(["../imago", "-w", "640", fl], + proc = subprocess.Popen(args + [fl], stdout=subprocess.PIPE, stderr=subprocess.PIPE) [output, err] = proc.communicate() -- 2.4.2