classify.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Aroz Photo
  3. Image Classification Script
  4. Generate classification for the given image path
  5. Require Data:
  6. imagepath (string)
  7. */
  8. requirelib("filelib");
  9. requirelib("imagelib");
  10. includes("imagedb.js");
  11. function returnError(msg){
  12. sendJSONResp(JSON.stringify({"error": msg}));
  13. }
  14. function main(){
  15. //Check if the imagepath exists
  16. if (!filelib.fileExists(imagepath)){
  17. returnError("file not exists");
  18. return
  19. }
  20. //Check if it is a supported image format
  21. var fileExt = imagepath.split(".").pop();
  22. if (fileExt == "jpg" || fileExt == "png" || fileExt == "jpeg"){
  23. //Get image classification, will take a bit time
  24. var results = imagelib.classify(imagepath, "darknet19");
  25. var responses = [];
  26. for (var i = 0; i < results.length; i++){
  27. responses.push({
  28. "object": results[i].Name,
  29. "confidence": results[i].Percentage,
  30. "position_x": results[i].Positions[0],
  31. "position_y": results[i].Positions[1],
  32. "width": results[i].Positions[2],
  33. "height": results[i].Positions[3]
  34. });
  35. }
  36. sendJSONResp(JSON.stringify(responses));
  37. }else{
  38. //Not supported format
  39. returnError("format not supported");
  40. return
  41. }
  42. }
  43. main();