|
import tensorflow.keras as keras |
|
import extract_bottleneck_features |
|
import cv2 |
|
import gradio as gr |
|
import numpy as np |
|
from glob import glob |
|
from keras.preprocessing import image |
|
InceptionV3_model = keras.models.load_model("weights.best.InceptionV3.hdf5",) |
|
|
|
|
|
|
|
dog_names= ['Affenpinscher', 'Afghan_hound', 'Airedale_terrier', 'Akita', 'Alaskan_malamute', 'American_eskimo_dog', 'American_foxhound', 'American_staffordshire_terrier', 'American_water_spaniel', 'Anatolian_shepherd_dog', 'Australian_cattle_dog', 'Australian_shepherd', 'Australian_terrier', 'Basenji', 'Basset_hound', 'Beagle', 'Bearded_collie', 'Beauceron', 'Bedlington_terrier', 'Belgian_malinois', 'Belgian_sheepdog', 'Belgian_tervuren', 'Bernese_mountain_dog', 'Bichon_frise', 'Black_and_tan_coonhound', 'Black_russian_terrier', 'Bloodhound', 'Bluetick_coonhound', 'Border_collie', 'Border_terrier', 'Borzoi', 'Boston_terrier', 'Bouvier_des_flandres', 'Boxer', 'Boykin_spaniel', 'Briard', 'Brittany', 'Brussels_griffon', 'Bull_terrier', 'Bulldog', 'Bullmastiff', 'Cairn_terrier', 'Canaan_dog', 'Cane_corso', 'Cardigan_welsh_corgi', 'Cavalier_king_charles_spaniel', 'Chesapeake_bay_retriever', 'Chihuahua', 'Chinese_crested', 'Chinese_shar-pei', 'Chow_chow', 'Clumber_spaniel', 'Cocker_spaniel', 'Collie', 'Curly-coated_retriever', 'Dachshund', 'Dalmatian', 'Dandie_dinmont_terrier', 'Doberman_pinscher', 'Dogue_de_bordeaux', 'English_cocker_spaniel', 'English_setter', 'English_springer_spaniel', 'English_toy_spaniel', 'Entlebucher_mountain_dog', 'Field_spaniel', 'Finnish_spitz', 'Flat-coated_retriever', 'French_bulldog', 'German_pinscher', 'German_shepherd_dog', 'German_shorthaired_pointer', 'German_wirehaired_pointer', 'Giant_schnauzer', 'Glen_of_imaal_terrier', 'Golden_retriever', 'Gordon_setter', 'Great_dane', 'Great_pyrenees', 'Greater_swiss_mountain_dog', 'Greyhound', 'Havanese', 'Ibizan_hound', 'Icelandic_sheepdog', 'Irish_red_and_white_setter', 'Irish_setter', 'Irish_terrier', 'Irish_water_spaniel', 'Irish_wolfhound', 'Italian_greyhound', 'Japanese_chin', 'Keeshond', 'Kerry_blue_terrier', 'Komondor', 'Kuvasz', 'Labrador_retriever', 'Lakeland_terrier', 'Leonberger', 'Lhasa_apso', 'Lowchen', 'Maltese', 'Manchester_terrier', 'Mastiff', 'Miniature_schnauzer', 'Neapolitan_mastiff', 'Newfoundland', 'Norfolk_terrier', 'Norwegian_buhund', 'Norwegian_elkhound', 'Norwegian_lundehund', 'Norwich_terrier', 'Nova_scotia_duck_tolling_retriever', 'Old_english_sheepdog', 'Otterhound', 'Papillon', 'Parson_russell_terrier', 'Pekingese', 'Pembroke_welsh_corgi', 'Petit_basset_griffon_vendeen', 'Pharaoh_hound', 'Plott', 'Pointer', 'Pomeranian', 'Poodle', 'Portuguese_water_dog', 'Saint_bernard', 'Silky_terrier', 'Smooth_fox_terrier', 'Tibetan_mastiff', 'Welsh_springer_spaniel', 'Wirehaired_pointing_griffon', 'Xoloitzcuintli', 'Yorkshire_terrier'] |
|
|
|
labels = dog_names |
|
|
|
def extract_InceptionV3(tensor): |
|
from keras.applications.inception_v3 import InceptionV3, preprocess_input |
|
return InceptionV3(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) |
|
|
|
|
|
def extract_Resnet50(tensor): |
|
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input |
|
return ResNet50(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) |
|
|
|
|
|
|
|
|
|
from tensorflow.keras.applications.resnet50 import preprocess_input |
|
|
|
|
|
|
|
import tensorflow as tf |
|
from keras.preprocessing import image |
|
from tqdm import tqdm |
|
|
|
|
|
|
|
from tensorflow.keras.applications.resnet50 import ResNet50 |
|
|
|
ResNet50_model = ResNet50(weights='imagenet') |
|
|
|
from keras.preprocessing import image |
|
from tqdm import tqdm |
|
|
|
|
|
|
|
from tensorflow.keras.applications.resnet50 import preprocess_input |
|
|
|
def ResNet50_predict_labels(img): |
|
|
|
img = np.expand_dims(img, axis=0) |
|
img = preprocess_input((img)) |
|
return np.argmax(ResNet50_model.predict(img)) |
|
|
|
|
|
def path_to_tensor(img_path): |
|
|
|
|
|
|
|
|
|
|
|
return np.expand_dims(img_path, axis=0) |
|
|
|
|
|
|
|
|
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') |
|
|
|
|
|
|
|
def face_detector(image): |
|
""" |
|
returns "True" if face is detected in image stored at image |
|
|
|
""" |
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
faces = face_cascade.detectMultiScale(gray) |
|
if len(faces) > 0: |
|
return "Number of human faces found in this image: {}". format(len(faces)) |
|
else: |
|
return "There are no human faces in this image" |
|
|
|
|
|
|
|
|
|
def InceptionV3_prediction_breed(img_path): |
|
""" |
|
Return: dog breed that is predicted by the model |
|
input: image |
|
""" |
|
|
|
|
|
bottleneck_feature = extract_InceptionV3(path_to_tensor(img_path)) |
|
|
|
predicted_vector = InceptionV3_model.predict(bottleneck_feature) |
|
|
|
return dog_names[np.argmax(predicted_vector)].split('.')[-1] |
|
|
|
|
|
|
|
def dog_detector(img): |
|
""" |
|
input: uploaded image by user |
|
return: "True" if a dog is detected in the image stored at img |
|
""" |
|
|
|
prediction = ResNet50_predict_labels(img) |
|
return ((prediction <= 268) & (prediction >= 151)) |
|
|
|
def identify_dog_app(img): |
|
"""This function predicts the breed of the human or dog" |
|
|
|
input: uploaded image by user |
|
Return: dog or human, and breed of the uploaded image |
|
""" |
|
|
|
breed = InceptionV3_prediction_breed(img) |
|
if dog_detector(img): |
|
return("This looks like a dog and its breed is:"),"{}".format(breed) |
|
elif face_detector(img): |
|
return("This looks like a human but might be classified as a dog of the following breed:"),"{}".format(breed) |
|
else: |
|
return("I have no idea what this might be. Please upload another image!"), ("Not applicable") |
|
|
|
|
|
|
|
|
|
image = gr.inputs.Image(shape=(224, 224), label="Image") |
|
label = gr.outputs.Label(num_top_classes=1) |
|
|
|
iface = gr.Interface( |
|
fn=identify_dog_app, |
|
inputs=image, |
|
outputs=[gr.outputs.Label(label="Human or Dog?"), gr.outputs.Label(label="Breed:")], |
|
title="Human or dog Identification - Breed Classification", |
|
|
|
article = |
|
'<b><span style="color: #ff9900;">Acknowledgement:</span></b><br/>' |
|
+'<p><span style="color: #ff9900;">I would like to express my special thanks of gratitude' |
|
+'to Misk & Sdaia for giving me the opportunity to enrol in "Data Scientist" Udacity nanodegree,' |
|
+' as well as to my mentor Mr. Haroon who was of great help during my learning journey.</span></p>' |
|
+'<p><span style="color: #ff9900;">This is my capstone project and herewith I finish this ND.</span></p>', |
|
|
|
theme="dark-huggingface" |
|
|
|
) |
|
|
|
iface.launch(share=False) |
|
|
|
|
|
|
|
|
|
|