From d711b326bbd96469873cfa43e64e3dbbac50716a Mon Sep 17 00:00:00 2001 From: Fam Zheng Date: Thu, 17 Apr 2025 13:20:11 +0100 Subject: [PATCH] Use v5 qr_verify api --- api/products/views.py | 44 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/api/products/views.py b/api/products/views.py index f4b273d..5531889 100644 --- a/api/products/views.py +++ b/api/products/views.py @@ -673,30 +673,6 @@ class QrVerifyView(BaseView): messages.append(f"All angle check api failed") raise Exception("Angle detection failed") - def roi_verify_check(self, sc, img_fn, messages): - feature_roi = self.get_feature_roi(sc.code) - if not feature_roi: - messages.append(f"feature roi not found for code {sc.code}, skiping") - return - with tempfile.NamedTemporaryFile(dir="/tmp/", suffix=".roi.jpg") as f: - f.write(feature_roi) - f.flush() - side_by_side_fn = img_fn + ".side_by_side.jpg" - qrtool_path = os.path.abspath("../alg/qrtool") - cwd = os.path.dirname(qrtool_path) - cmd = [qrtool_path, 'side_by_side', img_fn, f.name] - messages.append("creating side by side image...") - subprocess.check_call(cmd, cwd=cwd) - roi_verify_py = os.path.abspath("../alg/roi-verify.py") - roi_verify_model = os.path.abspath("../alg/models/resnet18_20250326_063838_96.34.pth") - cmd = [roi_verify_py, '--model', roi_verify_model, '--image', side_by_side_fn] - messages.append(" ".join(cmd)) - r = subprocess.call(cmd) - os.unlink(side_by_side_fn) - if r != 0: - raise Exception("roi_verify check failed") - messages.append(f"roi_verify check succeeded") - def feature_comparison_check(self, sc, batch, img_fn, messages, threshold): if not threshold: threshold = batch.feature_comparison_threshold @@ -734,6 +710,21 @@ class QrVerifyView(BaseView): raise Exception(f"feature comparison check failed: {j}") messages.append(f"Feature comparison succeeded") + def do_v5_qr_verify(self, img_fn, messages): + try: + resp = requests.post( + "https://themblem.com/api/v5/qr_verify", + files={ + 'frame': open(img_fn, 'rb'), + }, + ) + rd = resp.json() + messages.append(f"v5 qr_verify response: {rd}") + return rd.get("result", {}).get("predicted_class", 0) == 1 + except Exception as e: + messages.append(f"v5 qr verify failed: {e}") + return False + def post(self, request): image_name = '' tenant = None @@ -741,7 +732,6 @@ class QrVerifyView(BaseView): sd.message = '' sd.phone_model = request.data.get("phonemodel") sd.client_log = request.data.get("log") - use_roi_verify = request.data.get("use_roi_verify") messages = [] try: filebody, filename = get_image(request) @@ -786,8 +776,8 @@ class QrVerifyView(BaseView): sd.tenant = tenant sd.batch = sc.batch self.dot_angle_check(sc.batch, tf.name, messages, request.data.get("angle")) - if use_roi_verify: - self.roi_verify_check(sd, tf.name, messages) + if self.do_v5_qr_verify(tf.name, messages): + pass else: self.feature_comparison_check(sd, sc.batch, tf.name, messages, threshold) sd.succeeded = True