-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain_tensorflow.py
More file actions
38 lines (28 loc) · 1.16 KB
/
main_tensorflow.py
File metadata and controls
38 lines (28 loc) · 1.16 KB
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
# Object Detection using SSD Inception arquitecture trained on COCO dataset
import cv2
import sys
FROZEN_GRAPH = "./tensorflow/ssd_inception_v2_coco.pb"
PB_TXT = "./tensorflow/ssd_inception_v2_coco.pbtxt"
SIZE = 300
from coco_labels import LABEL_MAP
def run(img):
cvNet = cv2.dnn.readNetFromTensorflow(FROZEN_GRAPH, PB_TXT)
rows = img.shape[0]
cols = img.shape[1]
cvNet.setInput(cv2.dnn.blobFromImage(img, 1.0/127.5, (SIZE, SIZE), (127.5, 127.5, 127.5), swapRB=True, crop=False))
cvOut = cvNet.forward()
for detection in cvOut[0,0,:,:]:
score = float(detection[2])
if score > 0.3:
left = detection[3] * cols
top = detection[4] * rows
right = detection[5] * cols
bottom = detection[6] * rows
cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (23, 230, 210), thickness=2)
cv2.putText(img, LABEL_MAP[int(detection[1])], (int(left), int(top)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
return img
if __name__ == "__main__":
image = cv2.imread(sys.argv[1])
image = run(image)
cv2.imshow("Image", image)
cv2.waitKey(0)