def test(): import eyegrade.imageproc image = cv.LoadImage('../captures/test-001-processed.png') image_proc = imageproc.pre_process(image) bitmap = image_to_bitmap(image_proc) image2 = bitmap_to_image(image_proc.width, image_proc.height, bitmap) cv.SaveImage('/tmp/test.png', image2)
def test_server(host, image_filename, preprocess=True): """Sends a valid request to the server in order to test it.""" import httplib, urllib import sys headers = {} # First, send a config file with open('../doc/sample-files/exam.eye') as f: data = f.read() conn = httplib.HTTPConnection(host) headers['Content-type'] = 'application/x-eyegrade-exam-config' conn.request('POST', '/init', data, headers) response = conn.getresponse() if response.status == 200: print 'Exam config file succesfully sent' session_id = read_session_cookie(response) conn.close() else: print >> sys.stderr, response.status, response.reason print response.read() conn.close() sys.exit(1) if session_id is not None: headers['Cookie'] = 'session_id=' + session_id # Send a student list headers['Content-type'] = 'application/x-eyegrade-student-list' data='100099999\tJohn Doe\n100099998\tJane Doe\n' conn = httplib.HTTPConnection(host) conn.request('POST', '/students', data, headers) response = conn.getresponse() if response.status == 200: print 'Student list succesfully sent' conn.close() else: print >> sys.stderr, response.status, response.reason print response.read() conn.close() sys.exit(1) # Then, send an image image = cv.LoadImage(image_filename) if preprocess: bitmap = image_to_bitmap(imageproc.pre_process(image)) else: bitmap = image_to_bitmap(imageproc.rgb_to_gray(image)) headers['Content-type'] = 'application/x-eyegrade-bitmap' success = False counter = 0 while not success and counter < 32: conn = httplib.HTTPConnection(host) conn.request('POST', '/process', bitmap, headers) response = conn.getresponse() if response.status == 200: result = response.read() print result if result.strip() != '<output><ok>false</ok></output>': success = True else: print >> sys.stderr, response.status, response.reason print response.read() break conn.close() counter += 1 # Close session del headers['Content-type'] headers['Content-length'] = '0' conn = httplib.HTTPConnection(host) conn.request('POST', '/close', '', headers=headers) response = conn.getresponse() if response.status == 200: print 'Session succesfully closed' conn.close() else: print >> sys.stderr, response.status, response.reason print response.read() conn.close() sys.exit(1)