Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-029
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2023-029
2023-029
Commits
14426ce0
Commit
14426ce0
authored
Oct 29, 2023
by
Janith Gamage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: update
parent
08c7f16f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
1 deletion
+76
-1
Project/Backend/Server_Node/controllers/marksCalculator.controller.js
...end/Server_Node/controllers/marksCalculator.controller.js
+73
-0
Project/Backend/Server_Node/routes/marksCalculator.routes.js
Project/Backend/Server_Node/routes/marksCalculator.routes.js
+3
-1
No files found.
Project/Backend/Server_Node/controllers/marksCalculator.controller.js
View file @
14426ce0
import
{
exec
}
from
"
child_process
"
;
import
{
exec
}
from
"
child_process
"
;
import
fs
from
"
fs
"
;
export
const
marksCalculator
=
async
(
req
,
res
)
=>
{
export
const
marksCalculator
=
async
(
req
,
res
)
=>
{
try
{
try
{
...
@@ -53,3 +54,75 @@ export const marksCalculator = async (req, res) => {
...
@@ -53,3 +54,75 @@ export const marksCalculator = async (req, res) => {
res
.
status
(
500
).
json
({
code
:
"
00
"
,
message
:
"
Something went wrong
"
});
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
Project/Backend/Server_Node/routes/marksCalculator.routes.js
View file @
14426ce0
import
express
from
"
express
"
;
import
express
from
"
express
"
;
import
multer
from
"
multer
"
;
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
// Set up storage for uploaded images
const
storage
=
multer
.
memoryStorage
();
const
storage
=
multer
.
memoryStorage
();
...
@@ -9,5 +9,7 @@ const upload = multer({ storage: storage });
...
@@ -9,5 +9,7 @@ const upload = multer({ storage: storage });
const
router
=
express
.
Router
();
const
router
=
express
.
Router
();
router
.
post
(
'
/curriculum/:curriculumIndex/tutorial/:tutorialIndex
'
,
upload
.
single
(
'
image
'
),
marksCalculator
)
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
;
export
default
router
;
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment