classification / app.py
AbdullahAj2000's picture
Update app.py
a4d00c4 verified
import onnx
import onnxruntime as ort
import numpy as np
import cv2
import gradio as gr
def location(place):
if place == "Ajloun Castle":
map_link = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d387195.9481044243!2d35.7274499!3d32.3252097!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x151c8888296393d7%3A0xad15bd4bdbfa4a2c!2sAjloun%20Castle!5e0!3m2!1sen!2sus!4v1645602498168!5m2!1sen!2sus"
elif place == "Jerash":
map_link = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d403636.1090892695!2d35.8898159!3d32.2756771!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x151c802ff49ac08d%3A0x876c24b1cbded302!2sJerash!5e0!3m2!1sen!2sus!4v1645603957293!5m2!1sen!2sus"
elif place == "Petra":
map_link = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d403636.1090892695!2d35.4665137!3d30.3200019!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x15016930021ab6b1%3A0xe43a697bc3a3e0b9!2sWadi+Musa!5e0!3m2!1sen!2sus!4v1645604690535!5m2!1sen!2sus"
elif place == "Roman amphitheater":
map_link = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d403636.1090892695!2d35.9368315!3d31.9517165!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x151ca07139c70a21%3A0xc5b2f8903093b109!2sRoman+Theater!5e0!3m2!1sen!2sus!4v1645605382663!5m2!1sen!2sus"
elif place == "Umm Qais":
map_link = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d403636.1090892695!2d35.6698552!3d32.6625169!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x151c6d07287b7199%3A0x96d0d420e01bf0b1!2sUmm+Qais!5e0!3m2!1sen!2sus!4v1645606064411!5m2!1sen!2sus"
elif place == "Wadi Rum":
map_link = "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d403636.1090892695!2d35.3964418!3d29.556416!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0:0x0!2sWadi+Rum!5e0!3m2!1sen!2sus!4v1645606782015!5m2!1sen!2sus"
return map_link
def information(place):
if place == "Ajloun Castle":
info = "Ajloun Castle, located in northern Jordan, is a medieval fortress built in the 12th century by the Ayyubids. It played a strategic role in defending against Crusader invasions and offers panoramic views of the surrounding landscape. The castle showcases impressive architecture and is a key historical site in the Ajloun region."
elif place == "Jerash":
info = "Jerash, situated in northern Jordan, is renowned for its well-preserved ancient Roman ruins, including a magnificent oval-shaped forum, triumphal arch, and grand colonnaded street. With origins dating back to the Hellenistic period, Jerash flourished as a Roman city and is now one of the best-preserved provincial Roman cities in the world, attracting visitors with its rich history and architectural splendor. The site provides a captivating glimpse into daily life during the Roman Empire."
elif place == "Petra":
info = "Petra, located in southern Jordan, is a UNESCO World Heritage site famous for its rose-red rock-cut architecture and intricate archaeological wonders. Carved into the cliffs by the Nabataeans around 300 BCE, the ancient city features iconic structures such as Al-Khazneh (The Treasury) and the Monastery, showcasing a unique blend of Hellenistic, Roman, and Arabian influences. Petra's historical significance and breathtaking beauty make it a must-visit destination for travelers and history enthusiasts alike."
elif place == "Roman amphitheater":
info = "Roman Amphitheater, situated in Amman, Jordan, is a well-preserved ancient structure dating back to the 2nd century CE. This impressive amphitheater, capable of seating thousands, was a venue for various performances, including gladiator contests and theatrical productions. With its commanding presence in downtown Amman, the Roman Amphitheater stands as a testament to the city rich history and Roman influence, offering visitors a glimpse into the cultural life of the ancient past."
elif place == "Umm Qais":
info = "Umm Qais, located in northern Jordan, is an archaeological site known for its Greco-Roman ruins. Once part of the Decapolis league, the city features well-preserved remnants, including a theater, basilica, and colonnaded streets. Offering panoramic views of the Sea of Galilee and the Golan Heights, Umm Qais provides a fascinating journey through its historical layers, showcasing the influence of various civilizations, including the Romans and Byzantines."
elif place == "Wadi Rum":
info = "Wadi Rum, also known as the Valley of the Moon, is a vast desert wilderness in southern Jordan characterized by stunning red sandstone mountains and unique rock formations. Inhabited by Bedouins for centuries, Wadi Rum offers a captivating landscape featured in various films. Visitors can explore the surreal desert terrain, go on jeep tours, or experience traditional Bedouin hospitality in a tranquil setting. Wadi Rum stands as a natural wonder, inviting travelers to immerse themselves in its awe-inspiring beauty and cultural richness."
return info
def links_info(place):
if place == "Ajloun Castle":
link = ["https://www.tripadvisor.com/RestaurantsNear-g293988-d1549575-Ajloun_Castle-Jerash_Jerash_Governorate.html","https://www.booking.com/hotel/jo/al-jabal-castle-ajloun.html","https://www.gettyimages.ae/photos/ajloun-castle?assettype=image&sort=mostpopular&phrase=Ajloun%20Castle&license=rf%2Crm"]
elif place == "Jerash":
link = ["https://www.tripadvisor.com/Restaurants-g293988-Jerash_Jerash_Governorate.html","https://www.booking.com/city/jo/jarash.html","https://www.gettyimages.ae/photos/jerash?assettype=image&phrase=Jerash&sort=mostpopular&license=rf%2Crm"]
elif place == "Petra":
link = ["https://www.tripadvisor.com/Restaurants-g318895-Petra_Wadi_Musa_Ma_an_Governorate.html","https://www.booking.com/region/jo/petra.html","https://www.gettyimages.ae/photos/petra?assettype=image&phrase=Petra&sort=mostpopular&license=rf%2Crm"]
elif place == "Roman amphitheater":
link = ["https://www.tripadvisor.com/RestaurantsNear-g293986-d319450-Roman_Theatre-Amman_Amman_Governorate.html","https://www.tripadvisor.com/HotelsNear-g293986-d319450-Roman_Theatre-Amman_Amman_Governorate.html","https://www.gettyimages.ae/photos/roman-amphitheater-amman?assettype=image&phrase=Roman%20amphitheater%20Amman&sort=mostpopular&license=rf%2Crm"]
elif place == "Umm Qais":
link = ["https://www.tripadvisor.com/Restaurants-g318874-Umm_Qais_Irbid_Governorate.html","https://www.booking.com/city/jo/umm-qays.html","https://www.gettyimages.ae/photos/umm-qais?assettype=image&phrase=Umm%20Qais&sort=mostpopular&license=rf%2Crm"]
elif place == "Wadi Rum":
link = ["https://www.tripadvisor.com/Restaurants-g303973-Wadi_Rum_Al_Aqabah_Governorate.html","https://www.booking.com/city/jo/wadi-rum.html","https://www.gettyimages.ae/photos/wadi-rum?assettype=image&phrase=Wadi%20Rum&sort=mostpopular&license=rf%2Crm"]
return link
def load_image_with_cv2(image_path):
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (224, 224))
img_array = img.astype(np.float32) / 255.0
img_array = np.expand_dims(img_array, axis=0)
return img_array
def open_model(model_selection):
if model_selection == "VGG19":
return './models/VGG19_32_onnx.onnx'
elif model_selection == "ResNet50":
return './models/ResNet50_32_onnx.onnx'
elif model_selection == "Efficientnet":
return './models/efficientnet_32_onnx.onnx'
elif model_selection == "InceptionV3":
return './models/last_inceptionV3.onnx'
def classify_image(input_image,model_selection):
img_array = load_image_with_cv2(input_image)
ort_sess = ort.InferenceSession(open_model(model_selection))
input_name = ort_sess.get_inputs()[0].name
output_name = ort_sess.get_outputs()[0].name
outputs = ort_sess.run(None, {input_name: img_array})
predicted_class_index = np.argmax(outputs[0][0])
classes = ['Ajloun Castle', 'Jerash', 'Jerash', 'Petra', 'Roman amphitheater', 'Umm Qais', 'Wadi Rum']
predicted_class = classes[predicted_class_index]
confidence = max(outputs[0][0])
return predicted_class, confidence
def show_preds_image(image_path,model_selection):
mux_value = str(classify_image(image_path,model_selection)[0])
if float(classify_image(image_path,model_selection)[1])>0.85:
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (224, 224))
height, width, _ = img.shape
#text_x = int((width - len(mux_value) * 10) / 2)
#text_y = int(height / 2)
font_scale = 1
text_size = cv2.getTextSize(mux_value, cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, thickness=2)[0]
text_x = 10
text_y = text_size[1] + 10
cv2.putText(img, mux_value, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 0, 0), 2, cv2.LINE_AA)
conf = (classify_image(image_path,model_selection))
informations = information(mux_value)
map_link = location(mux_value)
links = links_info(mux_value)
google_map = f'''
<!DOCTYPE html>
<html>
<head>
<title>Map Display</title>
</head>
<body>
<h1>Map Display</h1>
<iframe width="600" height="450" frameborder="0" style="border:0" src="{map_link}" allowfullscreen></iframe>
</body>
</html>
'''
link_info = f'''
<!DOCTYPE html>
<html>
<head>
<title>Links</title>
</head>
<body>
<h1>Useful Links</h1>
<ul>
<li>Restaurants : <a href="{links[0]}" target="_blank">{links[0]}</a></li>
<li>Hotels : <a href="{links[1]}" target="_blank">{links[1]}</a></li>
<li>Images : <a href="{links[2]}" target="_blank">{links[2]}</a></li>
</ul>
</body>
</html>
'''
return img,conf,informations,link_info,google_map
elif 0.3 <float(classify_image(image_path,model_selection)[1])<0.85:
mux_value = str(classify_image(image_path,model_selection)[0])
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (224, 224))
conf = (classify_image(image_path,model_selection))
informations = "I'm not sure pls try another input it may be true"
map_link = location(mux_value)
links = links_info(mux_value)
google_map = f'''
<!DOCTYPE html>
<html>
<head>
<title>Map Display</title>
</head>
<body>
<h1>Map Display</h1>
<iframe width="600" height="450" frameborder="0" style="border:0" src="{map_link}" allowfullscreen></iframe>
</body>
</html>
'''
link_info = f'''
<!DOCTYPE html>
<html>
<head>
<title>Links</title>
</head>
<body>
<h1>Useful Links</h1>
<ul>
<li>Restaurants : <a href="{links[0]}" target="_blank">{links[0]}</a></li>
<li>Hotels : <a href="{links[1]}" target="_blank">{links[1]}</a></li>
<li>Images : <a href="{links[2]}" target="_blank">{links[2]}</a></li>
</ul>
</body>
</html>
'''
return img,conf,informations,link_info,google_map
elif 0.0 <float(classify_image(image_path,model_selection)[1])<0.3:
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (224, 224))
conf = (classify_image(image_path,model_selection))
informations = "Wronge input pls try another input"
link_info = f'''
<!DOCTYPE html>
<html>
<head>
<title>Links</title>
</head>
<body>
<h1>NO link provide pls try another input</h1>
</body>
</html>
'''
google_map = f'''
<!DOCTYPE html>
<html>
<head>
<title>Map Display pls try another input</title>
</head>
<body>
<h1>No location pls try another input</h1>
</body>
</html>
'''
return img,conf,informations,link_info,google_map
inputs_image = gr.Image(type="filepath", label="Input Image")
model_selection = gr.Dropdown(["VGG19","InceptionV3","ResNet50","Efficientnet"],label="Model selection")
outputs_image = gr.Image(type="numpy", label="Output Image")
output_text = gr.Textbox(label="prediction & confidence")
inform = gr.Textbox(label="Information about the place")
g_map = gr.HTML(label="Place location")
info_html = gr.HTML()
interface_image = gr.Interface(
fn=show_preds_image,
inputs=[inputs_image,model_selection],
outputs=[outputs_image,output_text,inform,info_html,g_map],
title="image classification",
)
interface_image.launch()
#Abdullah jarrah.