Commit 4be103f3 authored by Kavindu Randika's avatar Kavindu Randika

Model integration

parent 841928ba
from fastapi import APIRouter, File, UploadFile
import tensorflow as tf
from tensorflow import keras
from typing import List
from fastapi import APIRouter
from os import error
import pandas as pd
import numpy as np
import json
import os
from keras.preprocessing import image
prediction = APIRouter()
batch_size = 32
img_height = 180
img_width = 180
class_names = ['Acne and Rosacea Photos', 'Actinic Keratosis Basal Cell Carcinoma and other Malignant Lesions', 'Atopic Dermatitis Photos', 'Bullous Disease Photos', 'Cellulitis Impetigo and other Bacterial Infections', 'Eczema Photos', 'Exanthems and Drug Eruptions', 'Hair Loss Photos Alopecia and other Hair Diseases', 'Herpes HPV and other STDs Photos', 'Light Diseases and Disorders of Pigmentation', 'Lupus and other Connective Tissue diseases', 'Melanoma Skin Cancer Nevi and Moles', 'Nail Fungus and other Nail Disease', 'Poison Ivy Photos and other Contact Dermatitis', 'Psoriasis pictures Lichen Planus and related diseases', 'Scabies Lyme Disease and other Infestations and Bites', 'Seborrheic Keratoses and other Benign Tumors', 'Systemic Disease', 'Tinea Ringworm Candidiasis and other Fungal Infections', 'Urticaria Hives', 'Vascular Tumors', 'Vasculitis Photos', 'Warts Molluscum and other Viral Infections']
def load_data_json(path=''):
with open(path, 'r') as fp:
data = json.load(fp)
fp.close()
return data
def is_image(filename: str)->bool:
valid = (".png", ".jpg", "jpeg")
return filename.endswith(valid)
def read_imagefile( image:UploadFile ):
if is_image(image.filename):
directory = './temp_images/temp_image.jpg'
with open(directory, 'wb+') as image_upload:
image_upload.write(image.file.read())
return directory
return None
@prediction.post('/api/predictions/symptoms')
async def predict_symptoms(symptoms: List[int]):
try:
......@@ -33,5 +55,37 @@ async def predict_symptoms(symptoms: List[int]):
except error:
return {"success": False, "msg": error}
@prediction.post('/api/predictions/picture')
async def predict_symptoms(req_file: UploadFile = File(...)):
try:
img = read_imagefile(req_file)
if img is None:
return {"success": False, "msg": 'Not a valid data format'}
model_path = './xception'
image_path = "./temp_images/temp_image.jpg"
#load model
model = keras.models.load_model(model_path)
#load data
load_img = image.load_img(image_path, target_size=(img_height, img_width))
img_array = image.img_to_array(load_img)
img_array = tf.expand_dims(img_array, 0)
#predict
predictions = model.predict(img_array, batch_size)
score = tf.nn.softmax(predictions[0])
#return
prediction = class_names[np.argmax(score)]
return {"success": True, "result": prediction}
except error:
return {"success": False, "msg": error}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment