From c02b9e342a68b81c366ffa268f86f1e8f274bf71 Mon Sep 17 00:00:00 2001 From: Fam Zheng Date: Tue, 25 Mar 2025 13:19:39 +0800 Subject: [PATCH] api: Add use_roi_verify --- api/products/views.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/api/products/views.py b/api/products/views.py index 2dc5e54..41166a8 100644 --- a/api/products/views.py +++ b/api/products/views.py @@ -673,6 +673,29 @@ class QrVerifyView(BaseView): messages.append(f"All angle check api failed") raise Exception("Angle detection failed") + def roi_verify_check(self, sc, batch, 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=img_fn + ".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") + 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("../research/roi-verify.py") + roi_verify_model = os.path.abspath("../research/models/resnet18_20250325_114510_94.56.pth") + cmd = [roi_verify_py, '--model', roi_verify_model, '--image', side_by_side_fn] + messages.append(" ".join(cmd)) + r = subprocess.call(cmd, cwd=cwd) + os.unlink(side_by_side_fn) + if r != 0: + raise Exception("roi_verify check failed, exit code not 0") + 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 @@ -717,6 +740,7 @@ 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) @@ -761,7 +785,10 @@ class QrVerifyView(BaseView): sd.tenant = tenant sd.batch = sc.batch self.dot_angle_check(sc.batch, tf.name, messages, request.data.get("angle")) - self.feature_comparison_check(sd, sc.batch, tf.name, messages, threshold) + if use_roi_verify: + self.roi_verify_check(sd, sc.batch, tf.name, messages) + else: + self.feature_comparison_check(sd, sc.batch, tf.name, messages, threshold) sd.succeeded = True article_id = None if product.article: