I am working on realtime object detection using my laptop's camera with Yolo and Keras. I have trained a model and the resulting output is a .h5 file containing (from my understanding) the model and the weights. When trying to test the model with my webcam I get the following error: "NameError: name 'yolo_head' is not defined"

Here's my code:

import time
import cv2
import numpy as np
from keras import backend as K
from keras.models import load_model
from yad2k.models.keras_yolo import yolo_head, yolo_eval
from yad2k.yolo_utils import read_classes, read_anchors, preprocess_webcam_image, draw_boxes, \
#from yolo3.model import yolo_head

stream = cv2.VideoCapture(0)

class_names = read_classes("4_CLASS_test_classes.txt")
anchors = read_anchors("model_data/yolo_anchors.txt")
image_shape = (480., 640.)
yolo_model = load_model("logs/second_final_model.h5")
yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))
scores, boxes, classes = yolo_eval(yolo_outputs, image_shape)

def predict(sess, frame):
    # Preprocess your image
    image, image_data = preprocess_webcam_image(frame, model_image_size=(608, 608))
    # Run the session with the correct tensors and choose the correct placeholders in the feed_dict.
    # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0})
    out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input: image_data,
                                                                                   K.learning_phase(): 0})
    # Print predictions info
    print('Found {} boxes'.format(len(out_boxes)))
    # Generate colors for drawing bounding boxes.
    colors = generate_colors(class_names)
    # Draw bounding boxes on the image file
    draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)
    return np.array(image)

sess = K.get_session()

while True:
    # Capture frame-by-frame
    grabbed, frame = stream.read()
    if not grabbed:
    # Run detection
    start = time.time()
    output_image = predict(sess, frame)
    end = time.time()
    print("Inference time: {:.2f}s".format(end - start))
    # Display the resulting frame
    cv2.imshow('', output_image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
# When everything done, release the capture

I am fairly new to deep learning I just need this for a school project nearing its due date. Any help would be greatly appreciated please.

  • 1
    $\begingroup$ this not (or at least the error is not telling you that it is) a h5 issue. i assume the error line is this one: yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) and that the import do not raise anything? (please always add the line of the error) $\endgroup$ – Frayal Nov 13 '19 at 13:28
  • $\begingroup$ yes that is indeed the line that contains the error, but there is no problem with yolo_head as it has been compiled in a previous line in the beginning of the code. do you any idea what the problem is the second time yolo_head is reached please? $\endgroup$ – Yasmine Guemouria Nov 13 '19 at 17:36
  • $\begingroup$ Please check whether you are in the same folder or directory or not. $\endgroup$ – Joy Kumar Chakraborty Feb 24 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.