I have a nodeJS script, which calls a py script:
const { spawn } = require('child_process');
const callPythonFunctionRequest = async (functionName, argument1, argument2) => {
return new Promise((resolve, reject) => {
const pythonProcess = spawn('py', ['pathgetNmatch.py', functionName, argument1, argument2]);
pythonProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
pythonProcess.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
reject(new Error(`Error running Python script: ${data}`));
});
pythonProcess.on('exit', (code, data) => {
console.log(`Python script exited with code ${code}`);
if (code === 0) {
resolve();
} else {
reject(new Error(`Python script exited with code ${code}`));
}
});
pythonProcess.on('error', (err) => {
console.log(`Error spawning Python process: ${err}`);
reject(err);
});
pythonProcess.on('close', (code) => {
console.log(`Python script closed with code ${code}`);
});
});
};
exports.callPythonFunctionRequest = callPythonFunctionRequest;
as arguments i give a filenames of files, that it need to compare
import cv2
import base64
import numpy as np
import json
def decode(b64string):
decoded_data = base64.b64decode(b64string)
np_data = np.frombuffer(decoded_data, np.uint8)
img = cv2.imdecode(np_data, cv2.IMREAD_COLOR)
return img
def match(content_sample, content_sql, id, arr_keys, req_id):
sift = cv2.SIFT_create()
decoded_sample = decode(content_sample)
bf = cv2.BFMatcher()
best_score = 0
best_decoded_sql = None
kp1, kp2, mp = None, None, None
result = None
for element in content_sql:
for key in arr_keys:
decoded_sql = decode(element[f'{key}'])
decoded_sample = cv2.resize(decoded_sample, (decoded_sql.shape[1],
decoded_sql.shape[0]))
# cv2.imshow('1', decoded_sample)
# cv2.imshow('2', decoded_sql)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
keypoints_1, descriptors_1 = sift.detectAndCompute(decoded_sample, None)
keypoints_2, descriptors_2 = sift.detectAndCompute(decoded_sql, None)
matches = bf.knnMatch(descriptors_1, descriptors_2, k=2)
match_points = []
for p, q in matches:
if p.distance < 0.1 * q.distance:
match_points.append(p)
keypoints = min(len(keypoints_1), len(keypoints_2))
if len(match_points) / keypoints * 100 > best_score:
best_score = len(match_points) / keypoints * 100
kp1, kp2, mp = keypoints_1, keypoints_2, match_points
best_decoded_sql = decoded_sql
best_key = key
result = cv2.drawMatches(decoded_sample, kp1, best_decoded_sql, kp2, mp,
None)
_, img_encoded = cv2.imencode('.png', result)
base64_string = base64.b64encode(img_encoded)
matched_data = {'res_score': best_score, 'res_key': best_key, 'res_id': id}
return matched_data
def createFile(data, id):
with open(f"path\result_req_{id}.json", "w") as outfile:
json.dump(data, outfile)
def divide(sample, giant_arr):
print('called py fu')
req_id = sample.split("_")
req_id = sample.split(".")
arr_keys = []
with open(f'path\request\{sample}', 'r') as file:
sample = file.read()
with open(f'path\request\{giant_arr}', 'r') as file:
content_sql = json.load(file)
for content in content_sql:
id = content['id']
for item in content:
if item != 'id':
arr_keys.append(item)
result = match(sample, content_sql, id, arr_keys, req_id[0])
createFile(result, req_id)
print(result)
return result
and this code must create a json file with the result, but it not happens, what’s the problem and how to solve it?
i call a callPythonFunctionRequest this way:
await callTestPyFunc(file_name_req, file_name_db)
I just don’t know what I can do, I’ve tested a huge number of options including AI hints to solve this problem but it doesn’t work…