随着6月结束,7月开始,最近上海最火的话题是垃圾分类无疑了。。上海人民是天天迟早俩小时定时定点扔垃圾。javascript
看完是否是要崩溃了?!别担忧,本人周末花一下午精心制做的看图识垃圾app,主要依赖 tensorflow coco-ssd 来识别照片中的多物体,而后找了个不知名的api,返回垃圾的分类。例如:html
首先,网上已经有不少能够输入文字查询垃圾分类的网站了,我灵光一闪:要是能够直接经过图像垃圾分类岂不更好。而后找到了tensorflow.js 的官方指南:java
<!-- Load TensorFlow.js. This is required to use coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>
<!-- Load the coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"> </script>
<img id="img" src="cat.jpg"/>
<script> // Notice there is no 'import' statement. 'cocoSsd' and 'tf' is // available on the index-page because of the script tag above. const img = document.getElementById('img'); // Load the model. 在浏览器里fetch和加载模型到内存可能要花1分钟以上 cocoSsd.load().then(model => { // detect objects in the image. model.detect(img).then(predictions => { console.log('Predictions: ', predictions); }); }); </script>
复制代码
可见,Google tensorflow 已经把经常使用的机器学习模型作到开箱即用的水平,很是方便。固然,这个多物体检测的函数返回的是个数组,包含了对象在图中的bbox,并且里面的分类标签都是英文的:node
[{
bbox: [x, y, width, height],
class: "person",
score: 0.8380282521247864
}, {
bbox: [x, y, width, height],
class: "kite",
score: 0.74644153267145157
}]
复制代码
那么问题来了:网上的垃圾分类api 都是要求输入中文的!!我第一时间想到了 Bing Translate API 把英文翻译成中文再去查询分类。因此又去申请了个Azure 的免费帐号,还好我有master card,付了一美金才搞定。具体的能够参考最后的官方文档连接git
通过一顿折腾,终于搞定,就是识别率很低。毕竟没有专门训练垃圾分类的模型,只是用现成的物体检测模型。github
因此有不少搞笑的结果 😂: npm
建议用电脑在线体验地址,加载模型就得花一分钟:api
感兴趣的能够看github 项目地址数组
也欢迎扫码体验: 浏览器