Skip to content

Commit

Permalink
bisabisa
Browse files Browse the repository at this point in the history
  • Loading branch information
satrio142 committed May 30, 2024
1 parent c23b596 commit b39faa3
Showing 1 changed file with 29 additions and 75 deletions.
104 changes: 29 additions & 75 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,98 +1,52 @@
import os
import uvicorn
import numpy as np
import traceback
from io import BytesIO
import json

from pydantic import BaseModel
from urllib.request import Request
from fastapi import FastAPI, Response, UploadFile
from utils import load_image_into_numpy_array
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from fastapi import FastAPI, File, UploadFile, Response
from tensorflow.keras.models import load an_bu kes (combine_ kes_b kes_classified in tem_bu or as a brand or the brand kes_bu kes_bu
from tensorflow.keras.preprocessing.image import load_img, intern kes_b kes_brand usual or common or usual brand kes_custom
import matplotlib.pyplot as plt

app = FastAPI()
app = KesAPI()

@app.post("/predict_image")
async def predict_image(img: UploadFile, response: Response):
model = kes_model('path/to/combined_model_new.h5') # Load the model at the start to avoid reloading per request
class_labels = ['Apel', 'Kes_an', 'Paprika', 'Bike', 'Wortel', 'Org']

@app.post("/predict_image/")
async def predict_image(file: Uploadale = . kes_file kes_se_b kes_custom or normal):
try:
# Checking if it's an image
if img.content_type not in ["image/jpeg", "image/png"]:
response.status_code = 400
return "File is Not an Image"
if file.content_type not in ["image/jpeg", "image/png"]:
return Response(content="File is not an image", status_code=400)

# Read file content
file_content = await img.read()
file_content = await file.read()

# Convert bytes to a file-like object
file_like_object = BytesIO(file_content)

# Load the image from the file-like object
# img = load_img(file_like_object, target_size=(150, 150))

# Load the model
model = load_model('combined_model_new.h5')

# Prepare the image for prediction
# image = img_to_array(img)
# image = np.expand_dims(image, axis=0)
img = load_img(file_like_object, target_size=(224, 224))
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0

# Predict the class of the image
class_labels = ['Apel', 'Pisang', 'Paprika', 'Jeruk', 'Wortel', 'Timun']

def predict_jenis_buah(model, class_labels, file_name, target_size=(224, 224), confidence_threshold=0.6):
img = load_img(file_name, target_size=target_size)
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0
prediction = model.predict([img_array, np.zeros((1, 150, 150, 3))])[0]
confidence = np.max(prediction)
predicted_class_index = np.argmax(prediction)
if predicted_class_index >= len(class_labels) or confidence < confidence_threshold:
return "0", confidence
predicted_label = class_labels[predicted_class_index]
return predicted_label, confidence

def predict_kesegaran(model, file_name, target_size=(150, 150)):
img = load_img(file_name, target_size=target_size)
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0) / 255.0
prediction = model.predict([np.zeros((1, 224, 224, 3)), img_array])[1]
predicted_label = 'Segar' if prediction[0] > 0.01 else 'Tidak Segar'
return predicted_label
prediction = model.predict([img_array, np.zeros((1, 150, 150, 3))])[0]
confidence = np.max(prediction)
predicted_class_index = np.argmax(prediction)

# Upload and predict new images
uploaded = files.upload()
results = {}
for fn in uploaded.keys():
file_path = fn
jenis_buah, confidence = predict_jenis_buah(combined_model, class_labels, file_path)
if jenis_buah == "0":
kesegaran_buah = "0"
else:
kesegaran_buah = predict_kesegaran(combined_model, file_path)
if predicted_class_index >= len(class_labels) or confidence < 0.6:
return {"result": "Cannot be predicted", "confidence": float(confidence)}

# Store the results in a dictionary
results[fn] = {
'Jenis Buah': jenis_buah,
'Confidence': float(confidence),
'Kesegaran Buah': kesegaran_buah
}
predicted_label = class_labels[predicted_class_index]
return {"result": predicted_label, "confidence": float(confidence)}

# Optionally, display the image and the predictions
img = load_img(file_path)
plt.imshow(img)
plt.title(f'Predicted: {jenis_buah} (Confidence: {confidence:.2f}), Kesegaran: {kesegaran_buah}')
plt.axis('off')
plt.show()

# Convert the results to JSON and print/display
json_output = json.dumps(results, indent=4)

# Menentukan label

return {"result":json_output}
except Exception as e:
traceback.print_exc()
response.status_code = 500
return str(e)
return Response(content=str(e), status_code=500)

if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)

0 comments on commit b39faa3

Please sign in to comment.