-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
74 lines (59 loc) · 1.91 KB
/
app.py
File metadata and controls
74 lines (59 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import io
import requests
import numpy as np
from PIL import Image
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from keras.api.models import load_model
from keras.api.preprocessing import image as keras_image
from keras.api.applications.vgg16 import preprocess_input
import cloudinary
import cloudinary.api
from dotenv import load_dotenv
app = FastAPI()
load_dotenv()
# Load the saved model
model = load_model("VGG16.h5")
cloud_name = os.getenv('CLOUD_NAME')
api_key = os.getenv('API_KEY')
api_secret = os.getenv('API_SECRET')
# Define the classes
class_names = ['healthy', 'unhealthy']
cloudinary.config(
cloud_name=cloud_name,
api_key=api_key,
api_secret=api_secret
)
def process_image(public_id):
image_url = cloudinary.api.resource(public_id)["url"]
response = requests.get(image_url)
image_data = response.content
img = Image.open(io.BytesIO(image_data))
img = img.resize((224, 224))
img_array = keras_image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
# Make a prediction
predictions = model.predict(img_array)
predicted_class_index = np.argmax(predictions[0])
predicted_class = class_names[predicted_class_index]
confidence = float(predictions[0][predicted_class_index]) * 100
# Return the results
results = {
'predicted_class': predicted_class,
'confidence': confidence
}
return results
@app.get("/result/{public_id}")
async def image_result(public_id: str):
analysis_result = process_image(public_id)
json_response = jsonable_encoder(analysis_result)
return JSONResponse(content=json_response, status_code=200)
@app.get("/health")
async def health_check():
return {"status": "alive-1"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)