gstdl commited on
Commit
e1ffd20
1 Parent(s): 512dfa3

first commit

Browse files
app.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from model import run_app
3
+ from PIL import Image
4
+
5
+ ## streamlit app
6
+ st.set_option("deprecation.showfileUploaderEncoding", False)
7
+
8
+ st.title("Dog or Human Classifier")
9
+ st.text(
10
+ "Upload an Image for image classification as dog or human.\nI will also predict the dog breed in the image.\nIf you are human I will tell you which breed you look like"
11
+ )
12
+
13
+ uploaded_file = st.file_uploader(
14
+ "Upload your image here ...", type=["jpg", "jpeg", "png"]
15
+ )
16
+ if uploaded_file is not None:
17
+ image = Image.open(uploaded_file)
18
+ run_app(image)
19
+ st.image(uploaded_file, use_column_width=True)
model.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import json
3
+ import cv2
4
+ from torchvision import models, transforms
5
+ from PIL import Image, ImageOps
6
+ import numpy as np
7
+ import streamlit as st
8
+
9
+ ## face detector
10
+ face_cascade = cv2.CascadeClassifier("models/haarcascade_frontalface_alt.xml")
11
+
12
+
13
+ def face_detector(img):
14
+ img = np.asarray(img)
15
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
16
+ faces = face_cascade.detectMultiScale(gray)
17
+ return len(faces) > 0
18
+
19
+
20
+ ## preprocessing for pytorch models
21
+ def transform_img(img):
22
+ preprocess = transforms.Compose(
23
+ [
24
+ transforms.Resize([224, 224]),
25
+ transforms.ToTensor(),
26
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
27
+ ]
28
+ )
29
+
30
+ return preprocess(img).unsqueeze(0)
31
+
32
+
33
+ ## dog detector
34
+ VGG16 = models.vgg16(pretrained=True)
35
+ VGG16.eval()
36
+
37
+
38
+ def dog_detector(img):
39
+ pred_proba = VGG16(img).detach().numpy()
40
+ pred = np.argmax(pred_proba)
41
+ pred = 151 <= pred <= 268
42
+ return pred
43
+
44
+
45
+ ## breed
46
+ model_transfer = torch.load(
47
+ "models/model_transfer.pth", map_location=torch.device("cpu")
48
+ )
49
+ model_transfer.eval()
50
+ with open("models/classes.json", "r") as f:
51
+ class_names = json.load(f)
52
+
53
+
54
+ def predict_breed_transfer(img):
55
+ pred_proba = model_transfer(img)
56
+ _, pred = torch.topk(pred_proba, dim=1, k=1)
57
+ pred = str(pred.detach().numpy()[0][0])
58
+ pred = class_names[pred]
59
+ return pred
60
+
61
+
62
+ ## final predictor
63
+ def run_app(img):
64
+ human = face_detector(img)
65
+ img = transform_img(img)
66
+ dog = dog_detector(img)
67
+ if dog + human > 0:
68
+ dog_breed = predict_breed_transfer(img)
69
+ if dog:
70
+ st.header("hello, dog!")
71
+ else:
72
+ st.header("hello, human!")
73
+ st.header(f"You look like a {dog_breed}")
74
+ else:
75
+ st.header("um, what are you? Are you an alien!")
models/classes.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"0": "Affenpinscher", "1": "Afghan hound", "2": "Airedale terrier", "3": "Akita", "4": "Alaskan malamute", "5": "American eskimo dog", "6": "American foxhound", "7": "American staffordshire terrier", "8": "American water spaniel", "9": "Anatolian shepherd dog", "10": "Australian cattle dog", "11": "Australian shepherd", "12": "Australian terrier", "13": "Basenji", "14": "Basset hound", "15": "Beagle", "16": "Bearded collie", "17": "Beauceron", "18": "Bedlington terrier", "19": "Belgian malinois", "20": "Belgian sheepdog", "21": "Belgian tervuren", "22": "Bernese mountain dog", "23": "Bichon frise", "24": "Black and tan coonhound", "25": "Black russian terrier", "26": "Bloodhound", "27": "Bluetick coonhound", "28": "Border collie", "29": "Border terrier", "30": "Borzoi", "31": "Boston terrier", "32": "Bouvier des flandres", "33": "Boxer", "34": "Boykin spaniel", "35": "Briard", "36": "Brittany", "37": "Brussels griffon", "38": "Bull terrier", "39": "Bulldog", "40": "Bullmastiff", "41": "Cairn terrier", "42": "Canaan dog", "43": "Cane corso", "44": "Cardigan welsh corgi", "45": "Cavalier king charles spaniel", "46": "Chesapeake bay retriever", "47": "Chihuahua", "48": "Chinese crested", "49": "Chinese shar-pei", "50": "Chow chow", "51": "Clumber spaniel", "52": "Cocker spaniel", "53": "Collie", "54": "Curly-coated retriever", "55": "Dachshund", "56": "Dalmatian", "57": "Dandie dinmont terrier", "58": "Doberman pinscher", "59": "Dogue de bordeaux", "60": "English cocker spaniel", "61": "English setter", "62": "English springer spaniel", "63": "English toy spaniel", "64": "Entlebucher mountain dog", "65": "Field spaniel", "66": "Finnish spitz", "67": "Flat-coated retriever", "68": "French bulldog", "69": "German pinscher", "70": "German shepherd dog", "71": "German shorthaired pointer", "72": "German wirehaired pointer", "73": "Giant schnauzer", "74": "Glen of imaal terrier", "75": "Golden retriever", "76": "Gordon setter", "77": "Great dane", "78": "Great pyrenees", "79": "Greater swiss mountain dog", "80": "Greyhound", "81": "Havanese", "82": "Ibizan hound", "83": "Icelandic sheepdog", "84": "Irish red and white setter", "85": "Irish setter", "86": "Irish terrier", "87": "Irish water spaniel", "88": "Irish wolfhound", "89": "Italian greyhound", "90": "Japanese chin", "91": "Keeshond", "92": "Kerry blue terrier", "93": "Komondor", "94": "Kuvasz", "95": "Labrador retriever", "96": "Lakeland terrier", "97": "Leonberger", "98": "Lhasa apso", "99": "Lowchen", "100": "Maltese", "101": "Manchester terrier", "102": "Mastiff", "103": "Miniature schnauzer", "104": "Neapolitan mastiff", "105": "Newfoundland", "106": "Norfolk terrier", "107": "Norwegian buhund", "108": "Norwegian elkhound", "109": "Norwegian lundehund", "110": "Norwich terrier", "111": "Nova scotia duck tolling retriever", "112": "Old english sheepdog", "113": "Otterhound", "114": "Papillon", "115": "Parson russell terrier", "116": "Pekingese", "117": "Pembroke welsh corgi", "118": "Petit basset griffon vendeen", "119": "Pharaoh hound", "120": "Plott", "121": "Pointer", "122": "Pomeranian", "123": "Poodle", "124": "Portuguese water dog", "125": "Saint bernard", "126": "Silky terrier", "127": "Smooth fox terrier", "128": "Tibetan mastiff", "129": "Welsh springer spaniel", "130": "Wirehaired pointing griffon", "131": "Xoloitzcuintli", "132": "Yorkshire terrier"}
models/haarcascade_frontalface_alt.xml ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torchvision==0.6.0
2
+ streamlit==1.11.1
3
+ numpy==1.22.0
4
+ torch==1.8.1
5
+ opencv_python==4.5.1.48
6
+ Pillow==9.0.1
7
+ efficientnet-pytorch==0.6.3