def __match(self, sig, img_list): MAX_DIST = 0.20 A = list_to_tensor([sig] * self.batch_size) match_list = [] # a batch loop for i, x in enumerate(range(int(len(img_list) / self.batch_size) + 1)): curr_batch = img_list[i * self.batch_size:(i + 1) * self.batch_size] if len(curr_batch) != 0: X = list_to_tensor(curr_batch) S = A[:X.shape[0]] f_A, f_X = self.model.forward(S, X) dist = distance_metric(f_A, f_X).detach().numpy() match_list.append(dist <= MAX_DIST) return match_list
def verify(): if request.method == 'POST': file = request.files.get('signature') id = request.form.get('customer_id') X = Image.open(file.stream) X = convert_to_image_tensor(invert_image(X)).view(1, 1, 220, 155) anchor_img_file = get_file_from_db(id) A = Image.open(BytesIO(anchor_img_file)) A = convert_to_image_tensor(invert_image(A)).view(1, 1, 220, 155) model = load_model() f_A, f_X = model.forward(A, X) dist = distance_metric(f_A, f_X).detach().numpy() print('Dist={}'.format(dist)) if dist <= 0.145139: # Threshold obtained using Test.py return render_template('verify.html', result="<h6>Signatures are the same</h6>") else: return render_template('verify.html', result="<h6>Signatures are different</h6>") else: return render_template('verify.html', result='')
def verify(): try: customer_id = request.form['customerID'] input_image = Image.open(request.files['newSignature']) input_image_tensor = convert_to_image_tensor( invert_image(input_image)).view(1, 1, 220, 155) customer_sample_images = get_file_from_db(customer_id) if not customer_sample_images: return jsonify({'error': True}) anchor_images = [ Image.open(BytesIO(x)) for x in customer_sample_images ] anchor_image_tensors = [ convert_to_image_tensor(invert_image(x)).view(-1, 1, 220, 155) for x in anchor_images ] model = load_model() mindist = math.inf for anci in anchor_image_tensors: f_A, f_X = model.forward(anci, input_image_tensor) dist = float(distance_metric(f_A, f_X).detach().numpy()) mindist = min(mindist, dist) if dist <= 0.145139: # Threshold obtained using Test.py return jsonify({ "match": True, "error": False, "threshold": "%.6f" % (0.145139), "distance": "%.6f" % (mindist) }) return jsonify({ "match": False, "error": False, "threshold": 0.145139, "distance": round(mindist, 6) }) except Exception as e: print(e) return jsonify({"error": True})
def verify(): try: customer_id = request.form['customerID'] file = request.files['newSignature'] path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) file.save(path) input_image = Image.open(request.files['newSignature']) input_image_tensor = convert_to_image_tensor( invert_image(input_image)).view(1, 1, 220, 155) anchor_images = [ Image.open(BytesIO(x)) for x in get_file_from_db(customer_id) ] anchor_image_tensors = [ convert_to_image_tensor(invert_image(x)).view(-1, 1, 220, 155) for x in anchor_images ] model = load_model() mindist = math.inf for anci in anchor_image_tensors: f_A, f_X = model.forward(anci, input_image_tensor) dist = float(distance_metric(f_A, f_X).detach().numpy()) mindist = min(mindist, dist) if dist <= 0.145139: # Threshold obtained using Test.py return render_template("result.html", match="Yes", threshold=0.145139, distance=mindist, file=path) return render_template("result.html", match="No", threshold=0.145139, distance=mindist, file=path) except Exception as e: print(e)
def verify(): try: CREATE_TABLE = """CREATE TABLE IF NOT EXISTS signcout9 (id INTEGER PRIMARY KEY)""" cursor = connect_to_db().cursor() cursor.execute(CREATE_TABLE) cursor.connection.commit() cid = """SELECT id FROM signcout9 ORDER BY id DESC """ item = cursor.execute(cid) mainid = 1 print("q1") if item: print("q2") item1 = cursor.fetchone() # cursor.connection.commit() if item1 == None: mainid = 1 conn = connect_to_db() cursor = conn.cursor() query = """INSERT INTO signcout9 VALUES(?)""" cursor.execute(query, (1, )) conn.commit() cursor.close() else: print("adadad") print(item1[0]) mainid = int(item1[0]) + 1 print("added", mainid) cursor.close() conn = connect_to_db() cursor = conn.cursor() query = """INSERT INTO signcout9 VALUES(?)""" cursor.execute(query, (mainid, )) conn.commit() cursor.close() # test = request.json # print(test) # return test['title'] # customer_id = request.form['customerID'] customer_id = mainid # file1 = request.files['uploadedImage1'] # file2 = request.files['uploadedImage2'] # file3 = request.files['uploadedImage3'] # customer_id = request.form['customerID'] print(request.form) # print("sad") # s1=request.files['newSignature'] # print(type(s1)) # npimg = np.frombuffer(s1, np.uint8) # result=cv2.fastNlMeansDenoisingColored(s1, None, 10, 10, 7, 15) # print(type(result)) file1 = request.files['newSignature'] file2 = request.files['questioned'] input_image = Image.open(request.files['newSignature']) print("Asdsa") questioned = request.files['questioned'] input_image1 = Image.open(request.files['questioned']) # conn = connect_to_db() # cursor = conn.cursor() print("1") # query = """INSERT INTO signatures5 VALUES(?,?)""" # cursor.execute(query, (customer_id, questioned.read() # )) # conn.commit() print(customer_id) # path=os.path.join print(os.path) # path = r'D:\signature\signature69' # directory = r'D:\signature\signature69' # img = cv2.imread(path) # os.chdir(directory) # print("Before saving") # print(os.listdir(directory)) # filename = 'cat.jpg' # cv2.imwrite(filename, img) # print("After saving") # print(os.listdir(directory)) input_image.save('aaa' + str(customer_id) + '.png') input_image1.save('bbb' + str(customer_id) + '.png') # plt.imsave('abc'+str(customer_id)+'.png', input_image) # plt.imsave('xyz'+str(customer_id)+'.png', input_image1) Denoise.test('aaa' + str(customer_id) + '.png', 'bbb' + str(customer_id) + '.png') input_image = Image.open('aaa' + str(customer_id) + '.png') input_image1 = Image.open('bbb' + str(customer_id) + '.png') # img1 = extract_signature(request.files['newSignature']) # image = cv2.cvtColor(input_image, cv2.COLOR_BGR2HSV) # lower = np.array([90, 38, 0]) # upper = np.array([145, 255, 255]) # mask = cv2.inRange(image, lower, upper) # print(input_image) # kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations=1) # close = cv2.morphologyEx( # opening, cv2.MORPH_CLOSE, kernel, iterations=2) # cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, # cv2.CHAIN_APPROX_SIMPLE) # cnts = cnts[0] if len(cnts) == 2 else cnts[1] # boxes = [] # for c in cnts: # (x, y, w, h) = cv2.boundingRect(c) # boxes.append([x, y, x+w, y+h]) # boxes = np.asarray(boxes) # left = np.min(boxes[:, 0]) # top = np.min(boxes[:, 1]) # right = np.max(boxes[:, 2]) # bottom = np.max(boxes[:, 3]) # result[close == 0] = (255, 255, 255) # ROI = result[top:bottom, left:right].copy() # cv2.rectangle(result, (left, top), (right, bottom), (36, 255, 12), 2) # cv2.imshow('result', result) # cv2.imshow('ROI', ROI) # cv2.imshow('close', close) # cv2.imwrite('result.png', result) # cv2.imwrite('ROI.png', ROI) print(type(input_image)) input_image_tensor = convert_to_image_tensor( invert_image(input_image)).view(1, 1, 220, 155) customer_sample_images = get_file_from_db(customer_id) # print(customer_sample_images) plt.imsave('don.png', input_image_tensor) # if not customer_sample_images: # return jsonify({'error': "uyugy"}) # anchor_images = [Image.open(BytesIO(x)) # for x in customer_sample_images] anchor_image_tensors = convert_to_image_tensor( invert_image(input_image1)).view(-1, 1, 220, 155) # for x in anchor_images model = load_model() mindist = math.inf # for anci in anchor_image_tensors: f_A, f_X = model.forward(anchor_image_tensors, input_image_tensor) f1 = model.forward_once(anchor_image_tensors).detach().numpy() f2 = model.forward_once(input_image_tensor).detach().numpy() # plt.imshow(f1) # cv2.imshow('result.png', f1) plt.imsave('file1.png', f1) plt.imsave('file2.png', f1) # pq=ContrastiveLoss().forward(f1,f2,0.145139) # print("model1",f1) # print("model2",f2) out_arr = np.subtract(f1, f2) plt.imsave('file3.png', out_arr) print("Output array: ", out_arr) # print("bla",pq) dist = float(distance_metric(f_A, f_X).detach().numpy()) mindist = min(mindist, dist) # return send_file( # 'aaa'+str(customer_id)+'.png', # as_attachment=True, # attachment_filename='aaa'+str(customer_id)+'.png', # mimetype='image/jpeg', # # abcd=jsonify({"match": True, "error": False, "threshold": "%.6f" % (0.145139), "distance": "%.6f" % (mindist)}) # ) # os.remove( 'aaa'+str(customer_id)+'.png') # os.remove( 'bbb'+str(customer_id)+'.png') matcheddis = mindist * 100 percen = 100 - matcheddis print(percen) if dist <= 0.145139: # Threshold obtained using Test.py return jsonify({ "match": True, "error": False, "threshold": "%.6f" % (0.145139), "distance": "%.6f" % (mindist), "difference": str(out_arr), 'fileid': mainid, 'percentage': percen }) return jsonify({ "match": False, "error": False, "threshold": 0.145139, "distance": round(mindist, 6), "difference": str(out_arr), 'fileid': mainid, 'percentage': percen }) except Exception as e: print(e) return jsonify({"error": True, "adad": str(e)})