detection

……

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
# StrictVersion:版本号比较
# A version number consists of two or three dot-separated numeric components, with an optional "pre-release" tag on the end. The pre-release tag consists of the letter 'a' or 'b' followed by a number.
# If the numeric components of two version numbers are equal, then one with a pre-release tag will always be deemed earlier (lesser) than one without.
# 由小到大的版本号:
0.4 0.4.0 (these two are equivalent)
0.4.1
0.5a1
0.5b3
0.5
0.9.6
1.0
1.0.4a3
1.0.4b1
1.0.4
>>> from distutils.version import StrictVersion
>>> import tensorflow as tf
>>> StrictVersion(tf.__version__) < StrictVersion('1.9.0') # True/False

# sys.path:a list of strings that determines the interpreter’s search path for modules.
>>> import sys
>>> sys.path.append("..") # 将上一级目录添加至搜索路径

# urllib & tarfile
>>> import six.moves.urllib as urllib
>>> import tarfile
>>> import os
>>> opener = urllib.request.URLopener()
>>> opnner.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
>>> tar_file = tarfile.open(MODEL_FILE)
>>> for file in tar_file.getmembers():
file_name = os.path.basename(file.name)
if 'frozen_inference_graph.pb' in file_name:
tar_file.extract(file, os.getcwd()) # 当前工作路径
# os.path.split(path) >>> (head, tail)
# os.path.split(home/wenny/aaa) >>> (home/wenny, aaa)
# os.path.split(home/wenny/aaa/) >>> (home/wenny/aaa, '')
# os.path.dirname(path) >>> os.path.split(path)的第一个部分head
# os.path.basename(path) >>> os.path.split(path)的第二个部分tail

# GraphDef:how the other languages transform the Graph to C++? They use a tool called protobuf which can generate specific language stubs, that's where the GraphDef come from. It's a serialized version of Graph.
>>> detection_graph = tf.Graph()
>>> with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')

# numpy.expand_dims(a, axis) 增加一维
# Insert a new axis, corresponding to a given position in the array shape.
>>> x = np.array([1,2])
>>> x.shape
(2,)
>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1,2]])
>>> y.shape
(1,2)

# PIL.Image
>>> from PIL import Image
>>> import numpy as np
>>> image = Image.open(image_path)
>>> (im_width, im_height) = image.size
>>> if image.format == 'PNG':
image = image.convert('RGB')
>>> image_np = np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)
>>> image_np_expanded = np.expand_dims(image_np, axis=0)