Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,39 @@
|
|
| 1 |
-
#import tensorflow as tf
|
| 2 |
import onnx
|
| 3 |
import onnxruntime as ort
|
| 4 |
import numpy as np
|
| 5 |
-
#from keras.preprocessing.image import img_to_array, load_img
|
| 6 |
import cv2
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
|
| 10 |
-
#model = tf.keras.models.load_model("./models/last_Vgg19.h5")\
|
| 11 |
model = ort.InferenceSession('./models/VGG19_32_onnx.onnx')
|
| 12 |
|
| 13 |
-
def
|
| 14 |
img = cv2.imread(image_path)
|
| 15 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 16 |
img = cv2.resize(img, (224, 224))
|
| 17 |
-
img_array = img / 255.0
|
| 18 |
-
img_array =
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
classes = ['Ajloun Castle', 'Jerash', 'Jerash', 'Petra', 'Roman amphitheater', 'Roman amphitheater', 'Wadi Rum']
|
| 29 |
predicted_class = classes[predicted_class_index]
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
height, width, _ = img.shape
|
| 33 |
text_x = int((width - len(mux_value) * 10) / 2)
|
| 34 |
text_y = int(height / 2)
|
|
|
|
|
|
|
| 1 |
import onnx
|
| 2 |
import onnxruntime as ort
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import cv2
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
|
|
|
| 7 |
model = ort.InferenceSession('./models/VGG19_32_onnx.onnx')
|
| 8 |
|
| 9 |
+
def load_image_with_cv2(image_path):
|
| 10 |
img = cv2.imread(image_path)
|
| 11 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 12 |
img = cv2.resize(img, (224, 224))
|
| 13 |
+
img_array = img.astype(np.float32) / 255.0
|
| 14 |
+
img_array = np.expand_dims(img_array, axis=0)
|
| 15 |
+
return img_array
|
| 16 |
+
|
| 17 |
+
def classify_image(input_image):
|
| 18 |
+
img_array = load_image_with_cv2(input_image)
|
| 19 |
+
onnx32=onnx.load("./models/VGG19_32_onnx.onnx")
|
| 20 |
+
onnx.checker.check_model(onnx32)
|
| 21 |
+
ort_sess = ort.InferenceSession('./models/VGG19_32_onnx.onnx')
|
| 22 |
+
input_name = ort_sess.get_inputs()[0].name
|
| 23 |
+
output_name = ort_sess.get_outputs()[0].name
|
| 24 |
+
outputs = ort_sess.run(None, {input_name: img_array})
|
| 25 |
+
predicted_class_index = np.argmax(outputs[0][0])
|
| 26 |
classes = ['Ajloun Castle', 'Jerash', 'Jerash', 'Petra', 'Roman amphitheater', 'Roman amphitheater', 'Wadi Rum']
|
| 27 |
predicted_class = classes[predicted_class_index]
|
| 28 |
+
confidence = max(outputs[0][0])
|
| 29 |
+
return predicted_class, confidence
|
| 30 |
|
| 31 |
+
def show_preds_image(image_path):
|
| 32 |
+
mux_value = str(classify_image(image_path))
|
| 33 |
+
print(mux_value)
|
| 34 |
+
img = cv2.imread(image_path)
|
| 35 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 36 |
+
|
| 37 |
height, width, _ = img.shape
|
| 38 |
text_x = int((width - len(mux_value) * 10) / 2)
|
| 39 |
text_y = int(height / 2)
|