Use v5 qr_verify api

This commit is contained in:
Fam Zheng 2025-04-17 13:20:11 +01:00
parent 86f195109c
commit d711b326bb

View File

@ -673,30 +673,6 @@ class QrVerifyView(BaseView):
messages.append(f"All angle check api failed") messages.append(f"All angle check api failed")
raise Exception("Angle detection 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): def feature_comparison_check(self, sc, batch, img_fn, messages, threshold):
if not threshold: if not threshold:
threshold = batch.feature_comparison_threshold threshold = batch.feature_comparison_threshold
@ -734,6 +710,21 @@ class QrVerifyView(BaseView):
raise Exception(f"feature comparison check failed: {j}") raise Exception(f"feature comparison check failed: {j}")
messages.append(f"Feature comparison succeeded") 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): def post(self, request):
image_name = '' image_name = ''
tenant = None tenant = None
@ -741,7 +732,6 @@ class QrVerifyView(BaseView):
sd.message = '' sd.message = ''
sd.phone_model = request.data.get("phonemodel") sd.phone_model = request.data.get("phonemodel")
sd.client_log = request.data.get("log") sd.client_log = request.data.get("log")
use_roi_verify = request.data.get("use_roi_verify")
messages = [] messages = []
try: try:
filebody, filename = get_image(request) filebody, filename = get_image(request)
@ -786,8 +776,8 @@ class QrVerifyView(BaseView):
sd.tenant = tenant sd.tenant = tenant
sd.batch = sc.batch sd.batch = sc.batch
self.dot_angle_check(sc.batch, tf.name, messages, request.data.get("angle")) self.dot_angle_check(sc.batch, tf.name, messages, request.data.get("angle"))
if use_roi_verify: if self.do_v5_qr_verify(tf.name, messages):
self.roi_verify_check(sd, tf.name, messages) pass
else: else:
self.feature_comparison_check(sd, sc.batch, tf.name, messages, threshold) self.feature_comparison_check(sd, sc.batch, tf.name, messages, threshold)
sd.succeeded = True sd.succeeded = True