Skip to content

Instantly share code, notes, and snippets.

@tinesubic
Created April 4, 2018 13:36
Show Gist options
  • Select an option

  • Save tinesubic/0eab6f6847fdb7565cf325065df08bee to your computer and use it in GitHub Desktop.

Select an option

Save tinesubic/0eab6f6847fdb7565cf325065df08bee to your computer and use it in GitHub Desktop.
def cnn_detect_object(path):
args = { "prototxt": "cnn/bvlc_googlenet.prototxt",
"model": "cnn/bvlc_googlenet.caffemodel",
"labels": "cnn/synset_words.txt"}
# load the input image from disk
image = cv2.imread(path)
# load the class labels from disk
rows = open(args["labels"]).read().strip().split("\n")
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]
# our CNN requires fixed spatial dimensions for our input image(s)
# so we need to ensure it is resized to 224x224 pixels while
# performing mean subtraction (104, 117, 123) to normalize the input;
# after executing this command our "blob" now has the shape:
# (1, 3, 224, 224)
blob = cv2.dnn.blobFromImage(image, 1, (224, 224), (104, 117, 123))
# load our serialized model from disk
# print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
# set the blob as input to the network and perform a forward-pass to
# obtain our output classification
net.setInput(blob)
preds = net.forward()
# sort the indexes of the probabilities in descending order (higher
# probabilitiy first) and grab the top-5 predictions
idxs = np.argsort(preds[0])[::-1][:5]
# loop over the top-5 predictions and display them
result = []
for (i, idx) in enumerate(idxs):
# draw the top prediction on the input image
result.append({"label": classes[idx], "probability": preds[0][idx]*100})
# display the output image
return result
def detect_objects(root_path, group, num):
for i in range(1, num + 1):
filename = "{}_{}_402_{}.jpg".format(root_path, group, i)
result = rv.cnn_detect_object(filename)
print('=====================')
print("Image {} - most probable: {} : {}%".format(filename, result[0]["label"], result[0]["probability"]))
print('Other options: ', end='')
for item in result[1::]:
print("{} ({:3.3}%), ".format(item["label"], item["probability"]), end='')
print()
root_path = "images/cnn_object_detection"
print("Detecting successful images: ")
detect_objects(root_path, "success", 5)
print()
print("Detecting failed images: ")
detect_objects(root_path, "failed", 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment