{"id":765,"date":"2021-02-24T14:20:48","date_gmt":"2021-02-24T06:20:48","guid":{"rendered":"https:\/\/blog.sukasuka.cn\/?p=765"},"modified":"2021-08-21T18:13:05","modified_gmt":"2021-08-21T10:13:05","slug":"%e4%bd%bf%e7%94%a8python%e7%9a%84bottle%e6%9c%8d%e5%8a%a1%e7%ab%af","status":"publish","type":"post","link":"https:\/\/blog.sukasuka.cn\/?p=765","title":{"rendered":"\u4f7f\u7528python\u7684bottle\u670d\u52a1\u7aef"},"content":{"rendered":"<p>\u4e3a\u4e86\u8c03\u7528deepdanbooru\u800c\u642d\u5efa\u7684\u670d\u52a1\u5668<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>#!\/usr\/bin\/env python\r\n# -*- coding: utf-8 -*-\r\n\r\nimport os,time,json\r\nfrom bottle import *\r\nfrom typing import Any, Iterable, List, Tuple, Union\r\nimport six\r\nimport tensorflow as tf\r\nimport deepdanbooru as dd\r\n\r\nHTML = \"\"\"\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt; \r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt; \r\n&lt;title&gt;DeepDanbooru&lt;\/title&gt;\r\n&lt;style type=\"text\/css\"&gt;\r\nbody{\r\nfont-size:14px;\r\nalign-text:center;\r\n}\r\ninput{ \r\nvertical-align:middle;\r\nmargin:0;\r\npadding:0\r\n}\r\n.file-box{\r\nposition:relative;\r\nwidth:340px;\r\nmargin:0px;\r\n}\r\n.txt{\r\nheight:22px;\r\nborder:1px solid #cdcdcd;\r\nwidth:180px;\r\n}\r\n.btn{\r\nbackground-color:#FFF;\r\nborder:1px solid #CDCDCD;\r\nheight:24px;\r\nwidth:70px;\r\n}\r\n.file{\r\nposition:absolute;\r\ntop:0;\r\nright:80px;\r\nheight:24px;\r\nfilter:alpha(opacity:0);\r\nopacity: 0;\r\nwidth:260px\r\n}\r\n&lt;\/style&gt; \r\n&lt;\/head&gt; \r\n&lt;body&gt; \r\n&lt;h1&gt;Deep Danbooru&lt;\/h1&gt;\r\n&lt;h3&gt;Upload image&lt;\/h3&gt; \r\n&lt;div class=\"file-box\"&gt; \r\n&lt;form action=\"\/upload\" method=\"post\" enctype=\"multipart\/form-data\"&gt; \r\n&lt;input type=\"text\" name=\"textfield\" id=\"textfield\" class=\"txt\" \/&gt; \r\n&lt;input type=\"button\" class=\"btn\" value=\"\u6d4f\u89c8...\" \/&gt; \r\n&lt;input type=\"file\" name=\"fileField\" class=\"file\" id=\"fileField\" size=\"28\" onchange=\"document.getElementById('textfield').value=this.value\" \/&gt; \r\n&lt;input type=\"submit\" name=\"submit\" class=\"btn\" value=\"\u4e0a\u4f20\" onclick=\"\" \/&gt; \r\n&lt;\/form&gt; \r\n&lt;\/div&gt; \r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\"\"\"\r\n \r\nmodel_path = \"\/root\/model\/model-resnet_custom_v4.h5\"\r\ntags_path = \"\/root\/model\/tags.txt\"\r\nos.environ['CUDA_VISIBLE_DEVICES'] = '-1'\r\nmodel = tf.keras.models.load_model(model_path, compile=True)\r\ntags = dd.data.load_tags(tags_path)\r\ndef evaluate_image(\r\n    image_input: Union[str, six.BytesIO], model: Any, tags: List[str], threshold: float\r\n) -&gt; Iterable[Tuple[str, float]]:\r\n    width = model.input_shape[2]\r\n    height = model.input_shape[1]\r\n\r\n    image = dd.data.load_image_for_evaluate(\r\n        image_input, width=width, height=height)\r\n\r\n    image_shape = image.shape\r\n    image = image.reshape(\r\n        (1, image_shape[0], image_shape[1], image_shape[2]))\r\n    y = model.predict(image)[0]\r\n\r\n    result_dict = {}\r\n\r\n    for i, tag in enumerate(tags):\r\n        result_dict[tag] = y[i]\r\n\r\n    for tag in tags:\r\n        if result_dict[tag] &gt;= threshold:\r\n            yield tag, result_dict[tag]\r\n\r\n\r\ndef evaluate(file_path):\r\n    resp = []\r\n    for tag, score in evaluate_image(file_path, model, tags, 0.5):\r\n        resp.append({\"tag\":tag,\"socre\":str(score)})\r\n    os.remove(file_path)\r\n    return json.dumps(resp)\r\n\r\n\r\nbase_path = os.path.dirname(os.path.realpath(__file__))  # \u83b7\u53d6\u811a\u672c\u8def\u5f84\r\n \r\nupload_path = os.path.join(base_path, 'upload')   # \u4e0a\u4f20\u6587\u4ef6\u76ee\u5f55\r\nif not os.path.exists(upload_path):\r\n    os.makedirs(upload_path)\r\n    \r\n\r\n@route('\/', method='GET')\r\n@route('\/upload', method='GET')\r\n@route('\/index.html', method='GET')\r\n@route('\/upload.html', method='GET')\r\ndef index():\r\n    \"\"\"\u663e\u793a\u4e0a\u4f20\u9875\"\"\"\r\n    return HTML\r\n \r\n \r\n@route('\/upload', method='POST')\r\ndef do_upload():\r\n    \"\"\"\u5904\u7406\u4e0a\u4f20\u6587\u4ef6\"\"\"\r\n    filedata = request.files.get('fileField')\r\n     \r\n    if filedata.file:\r\n        file_name = os.path.join(upload_path, str(hash(time.time()))+os.path.splitext(filedata.filename)[1])\r\n        try:\r\n            filedata.save(file_name)  # \u4e0a\u4f20\u6587\u4ef6\u5199\u5165\r\n        except IOError:\r\n            return '\u4e0a\u4f20\u6587\u4ef6\u5931\u8d25'\r\n        return evaluate(file_name)\r\n    else:\r\n        return '\u4e0a\u4f20\u6587\u4ef6\u5931\u8d25'\r\n \r\n@route('\/favicon.ico', method='GET')\r\ndef server_static():\r\n    \"\"\"\u5904\u7406\u7f51\u7ad9\u56fe\u6807\u6587\u4ef6, \u627e\u4e2a\u56fe\u6807\u6587\u4ef6\u653e\u5728\u811a\u672c\u76ee\u5f55\u91cc\"\"\"\r\n    return static_file('favicon.ico', root=base_path)\r\n \r\n \r\n@error(404)\r\ndef error404(error):\r\n    \"\"\"\u5904\u7406\u9519\u8bef\u4fe1\u606f\"\"\"\r\n    return '404 Not Found'\r\n@error(500)\r\ndef error500(error):\r\n    return '500 Internal Error'\r\n\r\nrun(host='0.0.0.0', port=80, reloader=True) #\u8fd9\u91cc\u6ce8\u610fhost\u4e0d\u80fd\u586b\"127.0.0.1\"\u5426\u5219\u5bf9\u5916\u65e0\u6cd5\u8bbf\u95ee\r\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4e86\u8c03\u7528deepdanbooru\u800c\u642d\u5efa\u7684\u670d\u52a1\u5668 #!\/usr\/bin\/env python # -*- cod [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,15,26],"tags":[],"class_list":["post-765","post","type-post","status-publish","format-standard","hentry","category-backend-web","category-frontend","category-python"],"_links":{"self":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts\/765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=765"}],"version-history":[{"count":2,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":768,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts\/765\/revisions\/768"}],"wp:attachment":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}