Spaces:
Runtime error
Runtime error
Update app.py
Browse files13 unmodified lines1415161718192021222324252627282930313233343536373839404142434445464748495051525354555613 unmodified linesresnet_model = tf.keras.models.load_model(resnet_model_path)efficientnet_model = tf.keras.models.load_model(efficientnet_model_path)class_folders = ['Umm Qais', 'Jeresh', 'Petra',' Ajloun Castle', 'Wadi Rum', 'Roman amphitheater']def classify_selected_model(image, model_name):img = cv2.imread(image.name)img = cv2.resize(img, (150, 150))img = np.expand_dims(img, axis=0)img = img / 255.0if model_name.lower() == 'inceptionv3':predictions = inception_model.predict(img)elif model_name.lower() == 'vggnet':predictions = vggnet_model.predict(img)elif model_name.lower() == 'resnet':predictions = resnet_model.predict(img)elif model_name.lower() == 'efficientnet':predictions = efficientnet_model.predict(img)else:return {"error": "Invalid model selection"}class_index = np.argmax(predictions)class_name = class_folders[class_index]probability = predictions[0, class_index]print(f"Class Index: {class_index}")print(f"Class Name: {class_name}")print(f"Probability: {probability}")return {class_name: probability}image_input = gr.Image()model_selection_input = gr.Dropdown(["InceptionV3", "VGGNet", "ResNet", "EfficientNet"], value="InceptionV3")gr.Interface(fn=classify_selected_model,inputs=[image_input, model_selection_input],outputs="label",live=True).launch(debug=True)13 unmodified lines141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717213 unmodified linesresnet_model = tf.keras.models.load_model(resnet_model_path)efficientnet_model = tf.keras.models.load_model(efficientnet_model_path)# Define class names corresponding to your datasetclass_names = ["Ajloun Castle", "Jeresh", "Petra", "Roman amphitheater", "Umm_Qais", "Wadi_Rum"] # Replace with your actual class names# Define the classify_image functiondef classify_image(img, model_choice):# Resize the input image based on the selected model's expected input shapeif model_choice == "ResNet":img = tf.image.resize(img, (150, 150)) # ResNet50 input sizeimg = image.img_to_array(img)img = img.copy() # Make a copy of the image arrayimg = resnet_preprocess_input(img)elif model_choice == "EfficientNet":img = tf.image.resize(img, (150, 150))img = image.img_to_array(img)img = preprocess_input(img)elif model_choice == "VGGNet":img = tf.image.resize(img, (150, 150))img = image.img_to_array(img)img = preprocess_input(img)elif model_choice == "InceptionV3":img = tf.image.resize(img, (150, 150))img = image.img_to_array(img)img = preprocess_input(img)else:return "Invalid model choice"img = tf.expand_dims(img, 0) # Add batch dimension# Make predictions based on the selected modelif model_choice == "ResNet":predictions = resnet_model.predict(img)elif model_choice == "EfficientNet":predictions = efficientnet_model.predict(img)elif model_choice == "VGGNet":predictions = VGGNet_model.predict(img)elif model_choice == "InceptionV3":predictions = InceptionV3_model.predict(img)class_idx = tf.argmax(predictions[0])# Map class index to class nameclass_name = class_names[class_idx]# Return the predicted class name as a stringreturn f"Predicted class using {model_choice}: {class_name}"# Create a Gradio interface with a dropdown for model selectioniface = gr.Interface(fn=classify_image,inputs=["image", gr.Dropdown(["ResNet", "EfficientNet","VGGNet","InceptionV3"], label="Select Model")],outputs="text",live=True)# Launch the Gradio interfaceiface.launch(debug=True,share=True)
app.py+45-29