155 lines
4.2 KiB
Python
155 lines
4.2 KiB
Python
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
from starlite import Starlite, Controller, StaticFilesConfig, get, post, Body, MediaType, RequestEncodingType, Starlite, UploadFile, Template, TemplateConfig
|
|
from starlite.contrib.jinja import JinjaTemplateEngine
|
|
import numpy as np
|
|
import io
|
|
import os
|
|
import json
|
|
import requests
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
|
|
|
|
model_name = "models/beerd_imagenet_02_05_2023.keras"
|
|
test_model_imagenet = keras.models.load_model(model_name)
|
|
|
|
model_name = "./models/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("_", " ")
|
|
|
|
VK_URL = "https://api.vk.com/method/"
|
|
TOKEN = ""
|
|
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()
|
|
|
|
|
|
class BeerdsController(Controller):
|
|
path = "/breeds"
|
|
|
|
@post("/", media_type=MediaType.TEXT)
|
|
async def beeds(self, data: UploadFile = Body(media_type=RequestEncodingType.MULTI_PART)) -> dict:
|
|
body = await data.read()
|
|
|
|
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 {
|
|
"results": items_one,
|
|
"results_net": items_two,
|
|
"images": images,
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BaseController(Controller):
|
|
path = "/"
|
|
|
|
@get("/")
|
|
async def main(self) -> Template:
|
|
return Template(name="index.html")
|
|
|
|
@get("/sitemap.xml", media_type=MediaType.XML)
|
|
async def sitemaps(self) -> bytes:
|
|
return '''<?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>
|
|
'''.encode()
|
|
|
|
|
|
@get("/robots.txt", media_type=MediaType.TEXT)
|
|
async def robots(self) -> str:
|
|
return '''
|
|
User-agent: *
|
|
Allow: /
|
|
|
|
Sitemap: https://xn-----6kcp3cadbabfh8a0a.xn--p1ai/sitemap.xml
|
|
'''
|
|
|
|
|
|
app = Starlite(
|
|
route_handlers=[BeerdsController, BaseController],
|
|
static_files_config=[
|
|
StaticFilesConfig(directories=["static"], path="/static"),
|
|
|
|
],
|
|
template_config=TemplateConfig(
|
|
directory=Path("templates"),
|
|
engine=JinjaTemplateEngine,
|
|
),
|
|
)
|