#!/usr/bin/env python3 import time import unittest import atexit import cv2 import requests import subprocess import random import os import json BASE_DIR = os.path.abspath(os.path.dirname(__file__) + "/..") class Testing(unittest.TestCase): def setUp(self): self.server = self.start_server() def tearDown(self): self.server.terminate() self.server.wait() def start_server(self): port = random.randint(40000, 60000) cmd = ['python3', 'app.py', '-l', '127.0.0.1', '-p', str(port)] p = subprocess.Popen(cmd, cwd=BASE_DIR) start = time.time() while p.poll() == None and time.time() - start < 60: try: url = 'http://localhost:%d' % port if 'emblem' in requests.get(url, timeout=1).text: self.base_url = url return p except: time.sleep(1) raise Exception("Failed to start server") def do_test_roi_cloud_comparison(self, std, ter): std_file_Path = os.path.join(BASE_DIR, 'tests/data/', std) ter_file_Path = os.path.join(BASE_DIR, 'tests/data/', ter) std_img = cv2.imread(std_file_Path) ter_img = cv2.imread(ter_file_Path) std_img_encode = cv2.imencode('.jpg', std_img)[1] ter_img_encode = cv2.imencode('.jpg', ter_img)[1] files = {"std_file": ('std_file.jpg', std_img_encode, 'image/jpg'), "ter_file": ('ter_file.jpg', ter_img_encode, 'image/jpg')} # form = {"threshold":'50',"angle":'45'} form = {"threshold": '50'} begin = time.time() api = '/qr_roi_cloud_comparison' r = requests.post(self.base_url + api, files=files, data=form).json() print("std file", std_file_Path) print("ter file", ter_file_Path) print(r) print("%.3fs processed %s" % (time.time() - begin, r)) self.assertEqual(r['data']['status'], 'OK') def test_roi_cmp(self): self.do_test_roi_cloud_comparison('11173059793161.jpg', '11173059793161_pos_M2012K11AC_fix.jpg') #std_file_Path =os.path.join(BASE_DIR, 'tests/data/','11173059793161.jpg') #ter_file_Path =os.path.join(BASE_DIR, 'tests/data/','11173059793161_pos_M2012K11AC_fix.jpg') #self.do_test_roi_cloud_comparison('0079080983780.jpg', '0079080983780_pos_iPhone14Pro_1706842853.5876188.jpg') if __name__ == '__main__': unittest.main()