Commit 14426ce0 authored by Janith Gamage's avatar Janith Gamage

fix: update

parent 08c7f16f
import { exec } from "child_process";
import fs from "fs";
export const marksCalculator = async (req, res) => {
try {
......@@ -53,3 +54,75 @@ export const marksCalculator = async (req, res) => {
res.status(500).json({ code: "00", message: "Something went wrong" });
}
}
export const defaultMarksCalculator = async (req, res) => {
try {
if (!req.files || !req.files.original_image || !req.files.user_input_image) {
return res.status(400).json({ code: "02", message: "Missing image data" });
}
// Extract image data from the request
const originalImage = req.files.original_image[0].buffer.toString('base64');
const userInputImage = req.files.user_input_image[0].buffer.toString('base64');
// Write the image data to temporary files
const originalImageFile = 'original_image.jpeg';
const userInputImageFile = 'user_input_image.jpeg';
fs.writeFileSync(originalImageFile, originalImage, 'base64');
fs.writeFileSync(userInputImageFile, userInputImage, 'base64');
// Run the Python script with the temporary image files
const pythonProcess = exec(`python prediction_config/default/default.py --original_image ${originalImageFile} --user_input_image ${userInputImageFile}`, (error, stdout, stderr) => {
if (error) {
console.error(error);
return res.status(500).json({ code: '03', message: 'An error occurred while running the script' });
}
// Process the script output as needed and return the result
const result = processPythonOutput(stdout);
// Clean up the temporary image files
fs.unlinkSync(originalImageFile);
fs.unlinkSync(userInputImageFile);
res.status(200).json(result);
});
} catch (error) {
console.error(error);
res.status(500).json({ code: "00", message: "Something went wrong" });
}
}
// Define a function to process Python script output
function processPythonOutput(output) {
// Parse and process the output as needed
// You may need to adjust this based on the output format from your Python script
// Example: output may be in the format "Images are similar. Similarity: 70.00%"
const similarityPercentage = parseFloat(output.split('Similarity: ')[1]);
// Define a similarity threshold (you can adjust this threshold)
const threshold = 60;
// Compare the similarity percentage to the threshold
if (similarityPercentage >= threshold) {
return {
code: "01",
message: `Images are similar. Similarity: ${similarityPercentage.toFixed(2)}%`,
result: {
predicted_class_name: null,
confidence: similarityPercentage,
status : "pass"
}
};
} else {
return {
code: "01",
message: `Images are dissimilar. Similarity: ${similarityPercentage.toFixed(2)}%`,
result: {
predicted_class_name : null,
confidence: similarityPercentage,
status : "fail"
}
};
}
}
\ No newline at end of file
import express from "express";
import multer from "multer";
import { marksCalculator } from "../controllers/marksCalculator.controller.js";
import { defaultMarksCalculator, marksCalculator } from "../controllers/marksCalculator.controller.js";
// Set up storage for uploaded images
const storage = multer.memoryStorage();
......@@ -9,5 +9,7 @@ const upload = multer({ storage: storage });
const router = express.Router();
router.post('/curriculum/:curriculumIndex/tutorial/:tutorialIndex', upload.single('image'), marksCalculator)
router.post('/default', upload.fields([{ name: 'original_image', maxCount: 1 }, { name: 'user_input_image', maxCount: 1 }]), defaultMarksCalculator);
export default router;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment