Skip to content

Instantly share code, notes, and snippets.

@Merwanski
Created January 24, 2022 00:13
Show Gist options
  • Save Merwanski/22c3c2be3c1e8332bb1bf5749ffdaafd to your computer and use it in GitHub Desktop.
Save Merwanski/22c3c2be3c1e8332bb1bf5749ffdaafd to your computer and use it in GitHub Desktop.
yolo image using keras
# import necessary packages
from numpy import expand_dims
from keras.models import load_model
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
import time
import pdb
# load and prepare an image
def load_image_pixels(filename, shape):
image = load_img(filename, target_size=shape)
width, height = image.size
# convert to numpy array
image = img_to_array(image)
# scale pixel values to [0, 1]
image = image.astype('float32')
image /= 255.0
# add a dimension so that we have one sample
image = expand_dims(image, 0)
return image, width, height
if __name__ == "__main__":
model = load_model('yolov3.h5')
input_w, input_h = 608, 608
photo_filename = 'image_sample.png'
image, image_w, image_h = load_image_pixels(photo_filename, (input_w, input_h))
while(1):
# make prediction
start = time.time()
yhat = model.predict(image)
end = time.time()
# show timing information on YOLO
print("[INFO] YOLO took {:.6f} seconds".format(end - start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment