138 lines
3.8 KiB
Python
138 lines
3.8 KiB
Python
|
|
|
|
from PIL import Image
|
|
from sanic import Sanic
|
|
from sanic.response import json as json_answer, text
|
|
import numpy as np
|
|
from tensorflow import keras
|
|
from tensorflow.keras.utils import img_to_array
|
|
import io
|
|
import os
|
|
import json
|
|
import requests
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
|
|
|
|
app = Sanic("Ai")
|
|
model_name = "../beerd_imagenet_02_05_2023.keras"
|
|
test_model_imagenet = keras.models.load_model(model_name)
|
|
|
|
model_name = "../beerd_25_04_2023.keras"
|
|
test_model = keras.models.load_model(model_name)
|
|
|
|
dict_names = {}
|
|
with open("beerds.json", "r") as f:
|
|
dict_names = json.loads(f.read())
|
|
for key in dict_names:
|
|
dict_names[key] = dict_names[key].replace("_", " ")
|
|
app.static("/", "index.html", name="main")
|
|
app.static("/static/", "static/", name="static")
|
|
|
|
VK_URL = "https://api.vk.com/method/"
|
|
TOKEN = "vk1.a.2VJFQn9oTIqfpVNcgk7OvxXU8TZPomCH4biRvZEZp8-tQTi8IdKajlXCY5vJbLFjjPGrRWpsM8wbG1mek2pVpktwqi1MGAFJQfQafg68buH7YiE3GtClgWhZNuDUX5PwQuANLRVh6Ao-DcN0Z-72AmWmsIKhf9A4yuE8q3O6Asn_miGvO9gUY_JpctKEVtAYIEhbJtQK7hxW8qpud8J5Vg"
|
|
headers = {"Authorization": f"Bearer {TOKEN}"}
|
|
group_id = 220240483
|
|
postfix = "?v=5.131"
|
|
IMAGES = {}
|
|
|
|
|
|
def get_images():
|
|
global IMAGES
|
|
|
|
r = requests.get(
|
|
f"{VK_URL}photos.getAll{postfix}&access_token={TOKEN}&owner_id=-{group_id}&count=200")
|
|
items = r.json().get("response").get("items")
|
|
for item in items:
|
|
for s in item.get("sizes"):
|
|
if s.get("type") != "x":
|
|
continue
|
|
IMAGES[item.get("text")] = s.get("url")
|
|
break
|
|
|
|
|
|
get_images()
|
|
|
|
|
|
@app.post("/beeds")
|
|
async def beeds(request):
|
|
body = request.files.get("f").body
|
|
|
|
img = Image.open(io.BytesIO(body))
|
|
img = img.convert('RGB')
|
|
|
|
img_net = img.resize((180, 180, ), Image.BILINEAR)
|
|
img_array = img_to_array(img_net)
|
|
test_loss_image_net = test_model_imagenet.predict(
|
|
np.expand_dims(img_array, 0))
|
|
|
|
img = img.resize((200, 200, ), Image.BILINEAR)
|
|
img_array = img_to_array(img)
|
|
test_loss = test_model.predict(np.expand_dims(img_array, 0))
|
|
|
|
result = {}
|
|
for i, val in enumerate(test_loss[0]):
|
|
if val <= 0.09:
|
|
continue
|
|
result[val] = dict_names[str(i)]
|
|
|
|
result_net = {}
|
|
for i, val in enumerate(test_loss_image_net[0]):
|
|
if val <= 0.09:
|
|
continue
|
|
result_net[val] = dict_names[str(i)]
|
|
items_one = dict(sorted(result.items(), reverse=True))
|
|
items_two = dict(sorted(result_net.items(), reverse=True))
|
|
images = []
|
|
for item in items_one:
|
|
name = items_one[item].replace("_", " ")
|
|
if name not in IMAGES:
|
|
continue
|
|
images.append({"name": name, "url": IMAGES[name]})
|
|
for item in items_two:
|
|
name = items_two[item].replace("_", " ")
|
|
if name not in IMAGES:
|
|
continue
|
|
images.append({"name": name, "url": IMAGES[name]})
|
|
return json_answer({
|
|
"results": items_one,
|
|
"results_net": items_two,
|
|
"images": images,
|
|
})
|
|
|
|
|
|
sitemap_data = '''<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset
|
|
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
|
|
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
|
|
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
|
|
|
|
|
|
<url>
|
|
<loc>https://xn-----6kcp3cadbabfh8a0a.xn--p1ai/</loc>
|
|
<lastmod>2023-05-01T19:01:03+00:00</lastmod>
|
|
</url>
|
|
|
|
|
|
</urlset>
|
|
'''
|
|
|
|
@app.get("/sitemap.xml")
|
|
async def sitemaps(request):
|
|
return text(sitemap_data, content_type="application/xml")
|
|
|
|
robots_data = '''
|
|
User-agent: *
|
|
Allow: /
|
|
|
|
Sitemap: https://xn-----6kcp3cadbabfh8a0a.xn--p1ai/sitemap.xml
|
|
'''
|
|
|
|
@app.get("/robots.txt")
|
|
async def robots(request):
|
|
return text(robots_data)
|
|
|
|
if __name__ == "__main__":
|
|
app.run(auto_reload=True, port=4003, host="0.0.0.0")
|