AI 实战:垃圾分类系列(二)之快速搭建垃圾分类模型后台服务

代码模型 kira ⋅ 于 1个月前 ⋅ 477 阅读
来源:作者:szZack CSDN

前言

前一篇文章 AI实战:上海垃圾分类系列之快速搭建垃圾分类模型 ,本文在其之上搭建了一个基于Django、REST的web服务,实现了从浏览器中上传图片做垃圾分类的功能。

2019上海市生活垃圾按照以下标准分类!:http://sh.bendibao.com/zffw/2019225/202535.shtm

上海生活垃圾分类标准及投放要求:https://www.sohu.com/a/163450869\_688983

模型介绍

垃圾分类模型详情请查看这里:AI实战:上海垃圾分类系列之快速搭建垃圾分类模型

环境

  • Python 3.5
  • Django 1.10.8
  • Django Rest Framework 3.5
  • Pillow

安装环境

pip install -r requirements.txt

垃圾分类识别模型

具体见:./refuse_recognize_service/refuse_recognize/readme.md

配置修改

  • 1、修改checkpoints

    ./refuse_recognize_service/refuse_recognize/runs/checkpoints
    把路径改为自己的全路径

  • 2、修改html样式:

    ./refuse_recognize_service/imageupload_frontend/static/index.html

核心代码

models.py :

import os
import uuid

from PIL import Image
from django.db import models
from django.conf import settings

import sys, time
sys.path.append('./refuse_recognize/')
sys.path.append('./refuse_recognize/textcnn/')
from refuse import *

refuse_classification = RefuseRecognize()#加载垃圾识别模型

def scramble_uploaded_filename(instance, filename):
    """
    Scramble / uglify the filename of the uploaded file, but keep the files extension (e.g., .jpg or .png)
    :param instance:
    :param filename:
    :return:
    """
    extension = filename.split(".")[-1]
    return "{}.{}".format(uuid.uuid4(), extension)

def create_thumbnail(input_image, thumbnail_size=(500, 500)):
    """
    Create a thumbnail of an existing image
    :param input_image:
    :param thumbnail_size:
    :return:
    """
    # make sure an image has been set
    if not input_image or input_image == "":
        return

    # open image
    image = Image.open(input_image)

    # use PILs thumbnail method; use anti aliasing to make the scaled picture look good
    image.thumbnail(thumbnail_size, Image.ANTIALIAS)

    # parse the filename and scramble it
    filename = scramble_uploaded_filename(None, os.path.basename(input_image.name))
    arrdata = filename.split(".")
    # extension is in the last element, pop it
    extension = arrdata.pop()
    basename = "".join(arrdata)
    # add _thumb to the filename
    new_filename = basename + "_thumb." + extension

    # save the image in MEDIA_ROOT and return the filename
    image.save(os.path.join(settings.MEDIA_ROOT, new_filename))

    return new_filename

class UploadedImage(models.Model):
    """
    Provides a Model which contains an uploaded image aswell as a thumbnail
    """
    image = models.ImageField("Uploaded image", upload_to=scramble_uploaded_filename)

    # thumbnail
    thumbnail = models.ImageField("Thumbnail of uploaded image", blank=True)

    # title and description
    title = models.CharField("Title of the uploaded image", max_length=800, default="Unknown Picture")
    description = models.TextField("Description of the uploaded image", default="")

    def __str__(self):
        return self.title

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        """
        On save, generate a new thumbnail
        :param force_insert:
        :param force_update:
        :param using:
        :param update_fields:
        :return:
        """
        # generate and set thumbnail or none
        self.thumbnail = create_thumbnail(self.image)

        # 垃圾类别识别
        image_data = Image.open(self.image)
        img_path = str(time.time) + '.png'
        while os.path.exists(img_path):
            img_path = str(time.time) + '.png'
        image_data.save(img_path, 'png')

        image_data = tf.gfile.FastGFile(img_path, 'rb').read()
        classify_res = refuse_classification.recognize_image(image_data)
        self.title = classify_res
        os.remove(img_path)

        # force update as we just changed something
        super(UploadedImage, self).save(force_update=force_update)

HTML显示样式

index.html :

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
    <!-- Include Angular and several angular libraries -->
    <script src="node_modules/angular/angular.js"></script>
    <script src="node_modules/angular-resource/angular-resource.min.js"></script>
    <script src="node_modules/ng-file-upload/dist/ng-file-upload.min.js"></script>
    <script src="node_modules/angular-animate/angular-animate.min.js"></script>
    <script src="node_modules/angularjs-toaster/toaster.js"></script>

    <!-- Include our app -->
    <script src="js/app.js"></script>
    <!-- Include our own controllers, factories, directives, etc... -->
    <script src="js/images.rest.js"></script>

    <!-- Include Bootstrap CSS -->
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">

    <!-- Include AngularJS Toaster CSS -->
    <link rel="stylesheet" href="node_modules/angularjs-toaster/toaster.min.css">
    <title>垃圾分类智能识别</title>
</head>
<body>

<div class="container-fluid">
    <div ng-app="imageuploadFrontendApp" ng-controller="MainCtrl">
        <toaster-container></toaster-container>
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form" name="form" ng-submit="uploadImage()">
                    <div class="form-group">
                        <h3 for="file">选择一张图片:</h3>
                        <input type="file" ngf-select ng-model="newImage.image" name="file"
                               class="form-control"
                               accept="image/*" ngf-max-size="10MB" required
                               ngf-model-invalid="errorFile">
                        <i ng-show="form.file.$error.maxSize">File too large
                            {{ errorFile.size / 1000000 | number:1 }} MB: max 10M</i>
                        <img ng-show="form.file.$valid" ngf-thumbnail="newImage.image" class="img-responsive" style="max-width: 50%">
                        <button class="btn btn-warning" ng-click="newImage.image = null" ng-show="newImage.image">Remove</button>
                    </div>
                    <button class="btn btn-primary" type="submit">
                        上传
                    </button>
                </form>
            </div>
        </div>

        <div ng-if="images.length == 0">
            请选择一张图片上传!
        </div>

        <!-- display the images -->
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-4" ng-repeat="image in images track by image.pk">
                <a href="{{ image.image }}">
                    <img class="img-responsive" ng-src="{{ image.thumbnail }}">
                    <p>{{ image.description }}</p>
                </a>
                <h3>垃圾类别:
                </h3>

                <textarea cols="50" rows="5" id="textareaid">{{ image.title }}</textarea>

                <h3>
                    <button class="btn btn-warning" ng-click="deleteImage(image)">删除</button>
                </h3>
            </div>
        </div>
    </div>
</div>
</body>
</html> 

开启服务

  • 服务

    cd refuse_recognize_service
    python manage.py migrate
    python manage.py runserver &  # 后台运行

效果展示

1- 上传效果图

2

  • 点击上传后,垃圾分类结果效果图

垃圾分类结果
45
效果展示大概就是这样了,整体比较粗糙,识别准确率就不再优化了。
还有很多细节工作需要做,主要是垃圾类别标注数据、imagenet的结果的1000类中文对照优化等工作。


相关推荐:
AI实战:上海垃圾分类系列(一)之快速搭建垃圾分类模型
AI实战:上海垃圾分类系列(三)之快速搭建垃圾分类智能问答机器人
基于 CNN 实现垃圾分类

成为第一个点赞的人吧 :bowtie:
回复数量: 0
暂无回复~
您需要登陆以后才能留下评论!