Skip to content

Commit a6839fe

Browse files
authored
Add files via upload
1 parent 6216792 commit a6839fe

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

deep_learning_with_opencv.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import numpy as np
2+
import argparse
3+
import time
4+
import cv2
5+
6+
ap = argparse.ArgumentParser()
7+
ap.add_argument("-i", "--image", required=True,
8+
help="path to input image")
9+
ap.add_argument("-p", "--prototxt", required=True,
10+
help="path to Caffe 'deploy' prototxt file")
11+
ap.add_argument("-m", "--model", required=True,
12+
help="path to Caffe pre-trained model")
13+
ap.add_argument("-l", "--labels", required=True,
14+
help="path to ImageNet labels (i.e., syn-sets)")
15+
args = vars(ap.parse_args())
16+
17+
image = cv2.imread(args["image"])
18+
rows = open(args["labels"]).read().strip().split("\n")
19+
classes = [r[r.find(" ") + 1:].split(",")[0] for r in rows]
20+
21+
blob = cv2.dnn.blobFromImage(image, 1, (224, 224), (104, 117, 123))
22+
print("[INFO] loading model...")
23+
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
24+
25+
net.setInput(blob)
26+
start = time.time()
27+
preds = net.forward()
28+
end = time.time()
29+
print("[INFO] classification took {:.5} seconds".format(end - start))
30+
31+
idxs = np.argsort(preds[0])[::-1][:5]
32+
33+
for (i, idx) in enumerate(idxs):
34+
if i == 0:
35+
text = "Label: {}, {:.2f}%".format(classes[idx],
36+
preds[0][idx] * 100)
37+
cv2.putText(image, text, (5, 25), cv2.FONT_HERSHEY_SIMPLEX,
38+
0.7, (0, 0, 255), 2)
39+
40+
print("[INFO] {}. label: {}, probability: {:.5}".format(i + 1,
41+
classes[idx], preds[0][idx]))
42+
43+
cv2.imshow("Image", image)
44+
cv2.waitKey(0)

0 commit comments

Comments
 (0)