from keras.applications.mobilenet_v2 import MobileNetV2
from keras.preprocessing import image
#import keras.utils as image
from keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
import numpy as np
model = MobileNetV2(weights='imagenet')
def inference(x):
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
return decode_predictions(preds, top=1)[0][0][1]
img_path = './image/elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
inference(x)
'African_elephant'
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, (224, 224))
predicted = inference(frame[...,::-1])
cv2.putText(frame, predicted, (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), lineType=cv2.LINE_AA)
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) == 13: #13 is the Enter Key
break
cap.release()
cv2.destroyAllWindows()