Skip to content

Commit 99e3372

Browse files
committed
深度学习
1 parent 977b5c6 commit 99e3372

8 files changed

Lines changed: 3235 additions & 0 deletions

File tree

Binary file not shown.

Notebook/Blog/deep-learning-opencv深度学习dnn/bvlc_googlenet.prototxt

Lines changed: 2157 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# USAGE
2+
'''
3+
python deep_learning_with_opencv.py --image images/jemma.png \
4+
--prototxt bvlc_googlenet.prototxt \
5+
--model bvlc_googlenet.caffemodel --labels synset_words.txt
6+
7+
python deep_learning_with_opencv.py --image images/traffic_light.png \
8+
--prototxt bvlc_googlenet.prototxt \
9+
--model bvlc_googlenet.caffemodel --labels synset_words.txt
10+
11+
12+
python deep_learning_with_opencv.py --image images/eagle.png \
13+
--prototxt bvlc_googlenet.prototxt \
14+
--model bvlc_googlenet.caffemodel --labels synset_words.txt
15+
'''
16+
17+
# import the necessary packages
18+
import numpy as np
19+
import argparse
20+
import time
21+
import cv2
22+
23+
# construct the argument parse and parse the arguments
24+
ap = argparse.ArgumentParser()
25+
ap.add_argument("-i", "--image", required=True,
26+
help="path to input image")
27+
ap.add_argument("-p", "--prototxt", required=True,
28+
help="path to Caffe 'deploy' prototxt file")
29+
ap.add_argument("-m", "--model", required=True,
30+
help="path to Caffe pre-trained model")
31+
ap.add_argument("-l", "--labels", required=True,
32+
help="path to ImageNet labels (i.e., syn-sets)")
33+
args = vars(ap.parse_args())
34+
35+
# load the input image from disk
36+
image = cv2.imread(args["image"])
37+
38+
# load the class labels from disk
39+
rows = open(args["labels"]).read().strip().split("\n")
40+
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]
41+
42+
# our CNN requires fixed spatial dimensions for our input image(s)
43+
# so we need to ensure it is resized to 224x224 pixels while
44+
# performing mean subtraction (104, 117, 123) to normalize the input;
45+
# after executing this command our "blob" now has the shape:
46+
# (1, 3, 224, 224)
47+
blob = cv2.dnn.blobFromImage(image, 1, (224, 224), (104, 117, 123))
48+
49+
# load our serialized model from disk
50+
print("[INFO] loading model...")
51+
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
52+
53+
# set the blob as input to the network and perform a forward-pass to
54+
# obtain our output classification
55+
net.setInput(blob)
56+
start = time.time()
57+
preds = net.forward()
58+
end = time.time()
59+
print("[INFO] classification took {:.5} seconds".format(end - start))
60+
61+
# sort the indexes of the probabilities in descending order (higher
62+
# probabilitiy first) and grab the top-5 predictions
63+
idxs = np.argsort(preds[0])[::-1][:5]
64+
65+
# loop over the top-5 predictions and display them
66+
for (i, idx) in enumerate(idxs):
67+
# draw the top prediction on the input image
68+
if i == 0:
69+
text = "Label: {}, {:.2f}%".format(classes[idx], preds[0][idx] * 100)
70+
cv2.putText(image, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
71+
72+
# display the predicted label + associated probability to the
73+
# console
74+
print("[INFO] {}. label: {}, probability: {:.5}".format(i + 1, classes[idx], preds[0][idx]))
75+
76+
# display the output image
77+
cv2.imshow("Image", image)
78+
cv2.waitKey(0)
317 KB
Loading
263 KB
Loading
306 KB
Loading
348 KB
Loading

0 commit comments

Comments
 (0)