Skip to content

Commit

Permalink
bisa
Browse files Browse the repository at this point in the history
  • Loading branch information
satrio142 committed May 30, 2024
1 parent 33292ef commit df19839
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,59 +32,66 @@ async def predict_image(img: UploadFile, response: Response):
# img = load_img(file_like_object, target_size=(150, 150))

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

# Prepare the image for prediction
# image = img_to_array(img)
# image = np.expand_dims(image, axis=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)
print(f"Predictions: {prediction}, Confidence: {confidence}") # Debugging output
predicted_class_index = np.argmax(prediction)
if predicted_class_index >= len(class_labels) or confidence < confidence_threshold:
return "Cannot be predicted", confidence
return "0", confidence
predicted_label = class_labels[predicted_class_index]
return predicted_label, confidence

# Fungsi untuk prediksi kesegaran buah
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]
print(f"Kesegaran prediction: {prediction}") # Debugging output
predicted_label = 'Segar' if prediction[0] > 0.01 else 'Tidak Segar'
return predicted_label

# Mengunggah dan memprediksi gambar baru
uploaded = files.upload()
for fn in uploaded.keys():
file_path = fn
jenis_buah, confidence = predict_jenis_buah(combined_model, class_labels, file_path)
if jenis_buah == "Cannot be predicted":
kesegaran_buah = "Cannot be predicted"
else:
kesegaran_buah = predict_kesegaran(combined_model, file_path)

# Menampilkan hasil prediksi
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()

print(f'Gambar {fn} adalah {jenis_buah}, dan buah tersebut {kesegaran_buah}')
# 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)

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

# 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":predicted_label}
return {"result":json_output}
except Exception as e:
traceback.print_exc()
response.status_code = 500
Expand Down

0 comments on commit df19839

Please sign in to comment.