11-cnn-detect.agi 766 B

123456789101112131415161718192021222324252627282930
  1. /*---
  2. title: Detect objects in a photo
  3. group: Services
  4. summary: cnn.detect and cnn.classify with answers from the CNN window.
  5. ---*/
  6. /*
  7. The CNN window holds the JSON each function returns. The image must
  8. exist on the drive; the mock does not look at its pixels.
  9. */
  10. requirelib("cnn");
  11. var photo = "user:/Photo/sunset.jpg";
  12. var found = cnn.detect(photo, { conf: 0.5 });
  13. var labels = found.data.map(function (d) {
  14. return d.label + " (" + Math.round(d.score * 100) + "%)";
  15. });
  16. var kind = cnn.classify(photo, { top_k: 1 });
  17. try {
  18. cnn.classify("user:/Photo/missing.jpg");
  19. } catch (e) {
  20. console.log("Expected failure: " + e.name + ": " + e.message);
  21. }
  22. sendJSONResp({
  23. objects: labels,
  24. looksLike: kind.data[0].label,
  25. model: found.model
  26. });