66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
|
|
|
|
from PIL import Image
|
|
from sanic import Sanic
|
|
from sanic.response import json as json_answer
|
|
import numpy as np
|
|
from tensorflow import keras
|
|
from tensorflow.keras.utils import img_to_array
|
|
import io
|
|
import os
|
|
import json
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
|
|
|
|
|
app = Sanic("Ai")
|
|
model_name = "../beerd_imagenet_25_04_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)
|
|
|
|
list_labels = [fname for fname in os.listdir("../assets/dog")]
|
|
list_labels.sort()
|
|
dict_names = {}
|
|
with open("beerds.json", "r") as f:
|
|
dict_names = json.loads(f.read())
|
|
app.static("/", "index.html", name="main")
|
|
app.static("/static/", "static/", name="static")
|
|
|
|
|
|
@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)]
|
|
|
|
return json_answer({
|
|
"results": dict(sorted(result.items(), reverse=True)),
|
|
"results_net": dict(sorted(result_net.items(), reverse=True)),
|
|
})
|
|
|
|
if __name__ == "__main__":
|
|
app.run(auto_reload=True)
|