-
Notifications
You must be signed in to change notification settings - Fork 154
/
run.py
91 lines (72 loc) · 2.46 KB
/
run.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /usr/bin/env python3
# Copyright (c) 2017-2018 Intel Corporation. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mvnc import mvncapi as mvnc
import sys
import numpy
import cv2
path_to_networks = './'
path_to_images = '../../data/images/'
graph_filename = 'graph'
image_filename = path_to_images + 'nps_electric_guitar.png'
#mvnc.SetGlobalOption(mvnc.GlobalOption.LOGLEVEL, 2)
devices = mvnc.EnumerateDevices()
if len(devices) == 0:
print('No devices found')
quit()
device = mvnc.Device(devices[0])
device.OpenDevice()
#Load graph
with open(path_to_networks + graph_filename, mode='rb') as f:
graphfile = f.read()
#Load preprocessing data
mean = 128
std = 1/128
#Load categories
categories = []
with open(path_to_networks + 'categories.txt', 'r') as f:
for line in f:
cat = line.split('\n')[0]
if cat != 'classes':
categories.append(cat)
f.close()
print('Number of categories:', len(categories))
#Load image size
with open(path_to_networks + 'inputsize.txt', 'r') as f:
reqsize = int(f.readline().split('\n')[0])
graph = device.AllocateGraph(graphfile)
img = cv2.imread(image_filename).astype(numpy.float32)
dx,dy,dz= img.shape
delta=float(abs(dy-dx))
if dx > dy: #crop the x dimension
img=img[int(0.5*delta):dx-int(0.5*delta),0:dy]
else:
img=img[0:dx,int(0.5*delta):dy-int(0.5*delta)]
img = cv2.resize(img, (reqsize, reqsize))
img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
for i in range(3):
img[:,:,i] = (img[:,:,i] - mean) * std
print('Start download to NCS...')
graph.LoadTensor(img.astype(numpy.float16), 'user object')
output, userobj = graph.GetResult()
top_inds = output.argsort()[::-1][:5]
print(''.join(['*' for i in range(79)]))
print('inception-v1 on NCS')
print(''.join(['*' for i in range(79)]))
for i in range(5):
print(top_inds[i], categories[top_inds[i]], output[top_inds[i]])
print(''.join(['*' for i in range(79)]))
graph.DeallocateGraph()
device.CloseDevice()
print('Finished')