-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssd_ncs.py
68 lines (52 loc) · 2.02 KB
/
ssd_ncs.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# import mvnc
import mvnc.mvncapi as fx
import sys
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(BASE_DIR, 'caffe/python'))
import caffe
import numpy as np
def ncs_prepare():
print("[INFO] finding NCS devices...")
devices = fx.EnumerateDevices()
if len(devices) == 0:
print("[INFO] No devices found. Please plug in a NCS")
quit()
print("[INFO] found {} devices. device0 will be used. "
"opening device0...".format(len(devices)))
device = fx.Device(devices[0])
# try to open the device. this will throw an exception if someone else has it open already
try:
device.OpenDevice()
except:
print("Error - Could not open NCS device.")
quit()
return device
def graph_prepare(PATH_TO_CKPT, device):
print("[INFO] loading the graph file into RPi memory...")
with open(PATH_TO_CKPT, mode="rb") as f:
graph_in_memory = f.read()
# load the graph into the NCS
print("[INFO] allocating the graph on the NCS...")
detection_graph = device.AllocateGraph(graph_in_memory)
return detection_graph
transformer = caffe.io.Transformer({'data': (1, 3, 300, 300)})
transformer.set_transpose('data', (2, 0, 1))
transformer.set_mean('data', np.array([104,117,123])) # mean pixel
transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2,1,0)) # the reference model has channels in BGR order instead of RGB
def preprocess_image(PATH):
image = caffe.io.load_image(PATH)
return image
def ncs_clean(detection_graph, device):
detection_graph.DeallocateGraph()
device.CloseDevice()
exmaple_image = os.path.join(BASE_DIR, "pic/example.jpg")
graph_path = os.path.join(BASE_DIR, "models/ncs_ssd_graph")
image = preprocess_image(exmaple_image)
device = ncs_prepare()
graph = graph_prepare(graph_path, device)
graph.LoadTensor(image, None)
(output, _) = graph.GetResult()
print(output)
ncs_clean(graph, device)