-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnn_single_image.py
44 lines (33 loc) · 1.45 KB
/
dnn_single_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import cv2 as cv
path = "text\\"
model_bin = (path + "MobileNetSSD_deploy.caffemodel")
config_text = (path + "MobileNetSSD_deploy.prototxt")
objName = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog",
"horse", "motorbike", "person", "sheep", "pottedplant", "sofa", "train",
"tv", "monitor"]
net = cv.dnn.readNetFromCaffe(config_text, model_bin)
path1 = "img\\"
image = cv.imread(path1 + "dogs.jpg")
h = image.shape[0]
w = image.shape[1]
layerNames = net.getLayerNames()
lastLayerId = net.getLayerId(layerNames[-1])
lastLayer = net.getLayer(lastLayerId)
blobImage = cv.dnn.blobFromImage(image, 0.008444, (300, 300), (127.5, 127.5, 127.5), True, False)
net.setInput(blobImage)
cvOut = net.forward()
for detection in cvOut[0, 0, :, :]:
score = float(detection[2])
objIndex = int(detection[1])
if score > 0.5:
left = detection[3] * w
top = detection[4] * h
right = detection[5] * w
bottom = detection[6] * h
cv.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
cv.putText(image, "score: %2.f, %s" % (score, objName[objIndex]),
(int(left) - 10, int(top) - 5), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, 8)
cv.imshow("mobile-ssd-demo", image)
cv.imwrite(path1 + "result.png", image)
cv.waitKey(1000)