-
Notifications
You must be signed in to change notification settings - Fork 0
/
iscat.py
53 lines (38 loc) · 1.17 KB
/
iscat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""isCat.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1FKoEyoMkIAxj5ihj30-oRoOfskXg7sfi
"""
import tensorflow as tf
import cv2
import numpy as np
print("tensorflow version: " + tf.__version__)
# pretrained model from imagenet
model = tf.keras.applications.MobileNetV2(weights='imagenet')
# store image path in src variable
src = 'cat.png'
# store image
image = cv2.imread(src)
#resize image
image = cv2.resize(image, (224,224))
# convert color of image
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# expand dimensions of image array
image = np.expand_dims(image, axis=0)
#preprocess image
image = tf.keras.applications.mobilenet_v2.preprocess_input(image)
# get predictions from model
predict = model.predict(image)
#decode predictions, get 5
decode = tf.keras.applications.mobilenet_v2.decode_predictions(predict, top=5)
# iterate through first 5 predictions, check if any of them contain 'cat'
isCat = False
for tup in decode[0]:
if 'cat' in tup[1]:
isCat = True
# print message to user
if isCat:
print('There is a cat in the image')
else:
print('There is no cat in the image')