├── README.md ├── camera_detection.py ├── cv_test.py ├── location_output_detection.py ├── video_detection.py ├── 在树莓派上安装OpenCV-Python └── 安装指引-Guide of install /README.md: -------------------------------------------------------------------------------- 1 | # -TensorFlow-Object-Detection 2 | Inatall TensorFlow Object Detection on raspberrypi(3B) 3 | 4 | 用摄像机来完成实时检测 5 | camera_detection.py 6 | 7 | 检测视频并输出 8 | video_detection.py 9 | 10 | 输出图片中检测到的对象的坐标和分数 11 | location_output_detection.py 12 | 13 | 树莓派相关视频: 14 | https://space.bilibili.com/275177832/#/channel/detail?cid=52029 15 | 16 | 我的B站空间: 17 | https://space.bilibili.com/275177832 18 | 19 | 安卓配置api的视频: 20 | https://space.bilibili.com/275177832/#/channel/detail?cid=52347 21 | -------------------------------------------------------------------------------- /camera_detection.py: -------------------------------------------------------------------------------- 1 | # By Bend_Function 2 | # https://space.bilibili.com/275177832 3 | # 可以放在任何文件夹下运行(前提正确配置API[环境变量]) 4 | # 退出 按q键 5 | 6 | import numpy as np 7 | import tensorflow as tf 8 | import cv2 9 | import os 10 | 11 | from object_detection.utils import visualization_utils as vis_util 12 | from object_detection.utils import label_map_util 13 | 14 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 15 | cv2.setUseOptimized(True) # 加速cv 16 | 17 | # 要改的内容 18 | ############################################### 19 | PATH_TO_CKPT = 'model\\ssd_mobilenet_v1_graph.pb' # 模型及标签地址 20 | PATH_TO_LABELS = 'model\\mscoco_label_map.pbtxt' 21 | 22 | NUM_CLASSES = 90 # 检测对象个数 23 | 24 | camera_num = 1 # 要打开的摄像头编号,可能是0或1 25 | width, height = 1280,720 # 视频分辨率 26 | ############################################### 27 | 28 | # Load a (frozen) Tensorflow model into memory. 29 | detection_graph = tf.Graph() 30 | with detection_graph.as_default(): 31 | od_graph_def = tf.GraphDef() 32 | with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: 33 | serialized_graph = fid.read() 34 | od_graph_def.ParseFromString(serialized_graph) 35 | tf.import_graph_def(od_graph_def, name='') 36 | 37 | # Loading label map 38 | label_map = label_map_util.load_labelmap(PATH_TO_LABELS) 39 | categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) 40 | category_index = label_map_util.create_category_index(categories) 41 | 42 | 43 | mv = cv2.VideoCapture(camera_num) # 打开摄像头 44 | 45 | mv.set(3, width) # 设置分辨率 46 | mv.set(4, height) 47 | 48 | 49 | config = tf.ConfigProto() 50 | config.gpu_options.allow_growth = True 51 | with detection_graph.as_default(): 52 | with tf.Session(graph=detection_graph, config=config) as sess: 53 | image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') 54 | detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') 55 | detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') 56 | detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') 57 | num_detections = detection_graph.get_tensor_by_name('num_detections:0') 58 | 59 | while True: 60 | ret, image_source = mv.read() # 读取视频帧 61 | image_source = cv2.cvtColor(image_source, cv2.COLOR_BGR2RGB) 62 | image_np = cv2.resize(image_source , (width, height), interpolation=cv2.INTER_CUBIC) 63 | image_np_expanded = np.expand_dims(image_np, axis=0) 64 | # Actual detection. 65 | (boxes, scores, classes, num) = sess.run( 66 | [detection_boxes, detection_scores, detection_classes, num_detections], 67 | feed_dict={image_tensor: image_np_expanded}) 68 | # Visualization of the results of a detection. 69 | vis_util.visualize_boxes_and_labels_on_image_array( 70 | image_np, 71 | np.squeeze(boxes), 72 | np.squeeze(classes).astype(np.int32), 73 | np.squeeze(scores), 74 | category_index, 75 | use_normalized_coordinates=True, 76 | line_thickness=4) 77 | image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) 78 | cv2.imshow("video", image_np) 79 | if cv2.waitKey(1) & 0xFF == ord('q'): # 按q退出 80 | break 81 | -------------------------------------------------------------------------------- /cv_test.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | cap = cv2.VideoCapture(0) # 打开摄像头 4 | 5 | while True: 6 | 7 | ret, frame = cap.read() # 读摄像头 8 | cv2.imshow("video", frame) 9 | 10 | if cv2.waitKey(1) & 0xFF == ord('q'): # 按q退出 11 | break 12 | 13 | cap.release() 14 | cv2.destroyAllWindows() # 基本操作 15 | -------------------------------------------------------------------------------- /location_output_detection.py: -------------------------------------------------------------------------------- 1 | # By Bend_Function 2 | # https://space.bilibili.com/275177832 3 | # 可以放在任何文件夹下运行(前提正确配置API[环境变量]) 4 | 5 | import time 6 | start = time.time() 7 | import numpy as np 8 | import os 9 | import tensorflow as tf 10 | 11 | from PIL import Image 12 | from object_detection.utils import label_map_util 13 | 14 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 15 | 16 | # 要改的内容 17 | ############################################### 18 | PATH_TO_CKPT = 'model\\ssd_mobilenet_v1_graph.pb' # 模型及标签地址 19 | PATH_TO_LABELS = 'model\\mscoco_label_map.pbtxt' 20 | 21 | NUM_CLASSES = 90 # 检测对象个数 22 | 23 | PATH_TO_TEST_IMAGES_DIR = os.getcwd()+'\\test_images' # 测试图片路径 24 | 25 | confident = 0.5 # 置信度,即scores>confident的目标才被输出 26 | ############################################### 27 | 28 | # Load a (frozen) Tensorflow model into memory. 29 | detection_graph = tf.Graph() 30 | with detection_graph.as_default(): 31 | od_graph_def = tf.GraphDef() 32 | with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: 33 | serialized_graph = fid.read() 34 | od_graph_def.ParseFromString(serialized_graph) 35 | tf.import_graph_def(od_graph_def, name='') 36 | 37 | 38 | # Loading label map 39 | label_map = label_map_util.load_labelmap(PATH_TO_LABELS) 40 | categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) 41 | category_index = label_map_util.create_category_index(categories) 42 | 43 | 44 | os.chdir(PATH_TO_TEST_IMAGES_DIR) 45 | TEST_IMAGE_PATHS = os.listdir(PATH_TO_TEST_IMAGES_DIR) 46 | 47 | config = tf.ConfigProto() 48 | config.gpu_options.allow_growth = True 49 | with detection_graph.as_default(): 50 | with tf.Session(graph=detection_graph, config=config) as sess: 51 | image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') 52 | detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') 53 | detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') 54 | detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') 55 | num_detections = detection_graph.get_tensor_by_name('num_detections:0') 56 | 57 | for image_path in TEST_IMAGE_PATHS: # 开始检测 58 | image = Image.open(image_path) # 读图片 59 | width, height = image.size 60 | image_np = np.array(image) 61 | image_np_expanded = np.expand_dims(image_np, axis=0) 62 | # Actual detection. 63 | (boxes, scores, classes, num) = sess.run( 64 | [detection_boxes, detection_scores, detection_classes, num_detections], 65 | feed_dict={image_tensor: image_np_expanded}) 66 | 67 | s_boxes = boxes[scores > confident] 68 | s_classes = classes[scores > confident] 69 | s_scores = scores[scores > confident] 70 | 71 | for i in range(len(s_classes)): 72 | name = image_path.split("\\")[-1] 73 | # name = image_path.split("\\")[-1].split('.')[0] # 不带后缀 74 | ymin = s_boxes[i][0] * height # ymin 75 | xmin = s_boxes[i][1] * width # xmin 76 | ymax = s_boxes[i][2] * height # ymax 77 | xmax = s_boxes[i][3] * width # xmax 78 | score = s_scores[i] 79 | if s_classes[i] in category_index.keys(): 80 | class_name = category_index[s_classes[i]]['name'] # 得到英文class名称 81 | 82 | print("name:", name) 83 | print("ymin:", ymin) 84 | print("xmin:", xmin) 85 | print("ymax:", ymax) 86 | print("xmax:", xmax) 87 | print("score:", score) 88 | print("class:", class_name) 89 | print("################") 90 | 91 | 92 | end = time.time() 93 | print("Execution Time: ", end - start) 94 | -------------------------------------------------------------------------------- /video_detection.py: -------------------------------------------------------------------------------- 1 | # By Bend_Function 2 | # https://space.bilibili.com/275177832 3 | # 可以放在任何文件夹下运行(前提正确配置API[环境变量]) 4 | # 输出视频没有声音,pr可解决一切 5 | 6 | import numpy as np 7 | import os 8 | import sys 9 | import tensorflow as tf 10 | import cv2 11 | import time 12 | 13 | from object_detection.utils import label_map_util 14 | from object_detection.utils import visualization_utils as vis_util 15 | 16 | start = time.time() 17 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 18 | cv2.setUseOptimized(True) # 加速cv 19 | 20 | # This is needed since the notebook is stored in the object_detection folder. 21 | sys.path.append("..") 22 | 23 | # 可能要改的内容 24 | ###################################################### 25 | PATH_TO_CKPT = 'model\\ssd_mobilenet_v1_graph.pb' # 模型及标签地址 26 | PATH_TO_LABELS = 'model\\mscoco_label_map.pbtxt' 27 | 28 | video_PATH = "test_video\\cycling.mp4" # 要检测的视频 29 | out_PATH = "OUTPUT\\out_cycling1.mp4" # 输出地址 30 | 31 | NUM_CLASSES = 90 # 检测对象个数 32 | 33 | fourcc = cv2.VideoWriter_fourcc(*'MJPG') # 编码器类型(可选) 34 | # 编码器: DIVX , XVID , MJPG , X264 , WMV1 , WMV2 35 | 36 | ###################################################### 37 | 38 | # Load a (frozen) Tensorflow model into memory. 39 | detection_graph = tf.Graph() 40 | with detection_graph.as_default(): 41 | od_graph_def = tf.GraphDef() 42 | with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: 43 | serialized_graph = fid.read() 44 | od_graph_def.ParseFromString(serialized_graph) 45 | tf.import_graph_def(od_graph_def, name='') 46 | 47 | 48 | # Loading label map 49 | label_map = label_map_util.load_labelmap(PATH_TO_LABELS) 50 | categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) 51 | category_index = label_map_util.create_category_index(categories) 52 | 53 | 54 | # 读取视频 55 | video_cap = cv2.VideoCapture(video_PATH) 56 | fps = int(video_cap.get(cv2.CAP_PROP_FPS)) # 帧率 57 | 58 | 59 | width = int(video_cap.get(3)) # 视频长,宽 60 | hight = int(video_cap.get(4)) 61 | 62 | 63 | videoWriter = cv2.VideoWriter(out_PATH, fourcc, fps, (width, hight)) 64 | 65 | config = tf.ConfigProto() 66 | config.gpu_options.allow_growth = True # 减小显存占用 67 | with detection_graph.as_default(): 68 | with tf.Session(graph=detection_graph, config=config) as sess: 69 | # Definite input and output Tensors for detection_graph 70 | image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') 71 | # Each box represents a part of the image where a particular object was detected. 72 | detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') 73 | # Each score represent how level of confidence for each of the objects. 74 | # Score is shown on the result image, together with the class label. 75 | detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') 76 | detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') 77 | num_detections = detection_graph.get_tensor_by_name('num_detections:0') 78 | num = 0 79 | while True: 80 | ret, frame = video_cap.read() 81 | if ret == False: # 没检测到就跳出 82 | break 83 | num += 1 84 | print(num) # 输出检测到第几帧了 85 | # print(num/fps) # 检测到第几秒了 86 | 87 | image_np = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 88 | 89 | image_np_expanded = np.expand_dims(image_np, axis=0) 90 | image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') 91 | boxes = detection_graph.get_tensor_by_name('detection_boxes:0') 92 | scores = detection_graph.get_tensor_by_name('detection_scores:0') 93 | classes = detection_graph.get_tensor_by_name('detection_classes:0') 94 | num_detections = detection_graph.get_tensor_by_name('num_detections:0') 95 | 96 | # Actual detection. 97 | (boxes, scores, classes, num_detections) = sess.run( 98 | [boxes, scores, classes, num_detections], 99 | feed_dict={image_tensor: image_np_expanded}) 100 | 101 | # Visualization of the results of a detection. 102 | vis_util.visualize_boxes_and_labels_on_image_array( 103 | image_np, 104 | np.squeeze(boxes), 105 | np.squeeze(classes).astype(np.int32), 106 | np.squeeze(scores), 107 | category_index, 108 | use_normalized_coordinates=True, 109 | line_thickness=4) 110 | 111 | # 写视频 112 | videoWriter.write(cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)) 113 | 114 | videoWriter.release() 115 | end = time.time() 116 | print("Execution Time: ", end - start) 117 | -------------------------------------------------------------------------------- /在树莓派上安装OpenCV-Python: -------------------------------------------------------------------------------- 1 | 先决条件:安装完毕树莓派系统 2 | 参考https://github.com/Bend-Function/-TensorFlow-Object-Detection/blob/master/%E5%AE%89%E8%A3%85%E6%8C%87%E5%BC%95-Guide%20of%20install 3 | 第一步 4 | 5 | 1、用pip安装cv 6 | 7 | sudo pip3 install opencv-python 8 | 9 | 10 | 11 | 2、安装各种依赖库 12 | 13 | sudo apt-get install libjpeg8-dev 14 | 15 | sudo apt-get install libtiff5-dev 16 | 17 | sudo apt-get install libjasper-dev 18 | 19 | sudo apt-get install libpng12-dev 20 | 21 | sudo apt-get install libqt4-test 22 | 23 | sudo apt-get install libqtgui4 24 | 25 | 26 | 27 | 一个很好的网站,如果程序提示什么东西找不到或不存在,他或许可以帮到你: 28 | 29 | https://packages.ubuntu.com/ 30 | 31 | 32 | 33 | 3、测试 34 | 35 | 程序源码地址: 36 | 37 | https://github.com/Bend-Function/-TensorFlow-Object-Detection/blob/master/cv_test.py 38 | -------------------------------------------------------------------------------- /安装指引-Guide of install: -------------------------------------------------------------------------------- 1 | 感谢 @ggguoxxxiang 他为这个API做了一个详细的中文教程 2 | https://www.bilibili.com/video/av21539370 3 | windows安装:https://blog.csdn.net/dy_guox/article/details/79081499 4 | 5 | 6 | 7 | # 我只负责安装 8 | 9 | # 视频经过大量裁剪,真实安装时间大约在2-3个小时左右(树莓派3B) 10 | 11 | # 树莓派上不推荐训练模型,他是来实现功能的 12 | 13 | # 树莓派一定要散热片,最好有风扇 14 | 15 | 16 | 17 | 视频地址: 18 | 19 | P1: https://www.bilibili.com/video/av31697636 # 系统安装 20 | 21 | P2: https://www.bilibili.com/video/av31706598 # TensorFlow 安装 22 | 23 | P3: https://www.bilibili.com/video/av31721104 # Object Detection API 安装 24 | 25 | 26 | -------------------------------------------------------------------------------------------- 27 | 28 | Part 1: 树莓派系统安装 29 | 30 | -------------------------------------------------------------------------------------------- 31 | 32 | 1、格式化 33 | 34 | --SDFormatter 35 | 36 | 37 | 38 | 2、写镜像 39 | 40 | --Disk Imager 41 | 42 | 43 | 44 | 3、在boot下添加文件名为SSH的文件(无后缀) 45 | 46 | 47 | 48 | 4、网线+电源(至少5V 1A) 49 | 50 | # 官方建议2A,但1A也可以,若使用1A,请在后面的编译环节使用单线程 51 | 52 | 53 | 54 | 5、Putty连接 55 | 56 | 57 | 58 | 6、改源(apt-get) 59 | 60 | sudo sed -i 's#://raspbian.raspberrypi.org#s://mirrors.ustc.edu.cn/raspbian#g' /etc/apt/sources.list 61 | 62 | sudo sed -i 's#://archive.raspberrypi.org/debian#s://mirrors.ustc.edu.cn/archive.raspberrypi.org#g' /etc/apt/sources.list.d/raspi.list 63 | 64 | sudo apt-get update 65 | 66 | 67 | 68 | 7、装vnc server与设置tightvncpasswd(密码) 69 | 70 | sudo apt-get install tightvncserver 71 | 72 | tightvncpasswd(设置密码) 73 | 74 | tightvncserver(启动) 75 | 76 | 77 | 78 | 8、在电脑上连VNC 79 | 80 | 81 | 82 | 9、连接到无线网络(可选) 83 | 84 | 85 | -------------------------------------------------------------------------------------------- 86 | 87 | Part2: TensorFlow安装 88 | 89 | -------------------------------------------------------------------------------------------- 90 | 91 | 10、换默认python版本(改py3) 92 | 93 | sudo rm -rf /usr/bin/python # 请仔细检查输入是否准确 94 | 95 | sudo ln -s /usr/bin/python3.5 /usr/bin/python #依据版本的不同可能py3的版本也不同,可以用 "ls /usr/bin/python*" 查看 96 | 97 | 98 | 99 | 11、安装TensorFlow 100 | 101 | sudo pip3 install tensorflow 102 | 103 | 104 | 105 | 12、安装atlas 106 | 107 | sudo apt-get install libatlas-base-dev 108 | 109 | 110 | 111 | 13、安装pillow 112 | 113 | sudo pip3 install pillow 114 | 115 | 116 | 117 | 14、安装matplotlib 118 | 119 | sudo pip3 install matplotlib 120 | 121 | 122 | 123 | tip:现在先别装Opencv 124 | 125 | 126 | 127 | 15、测试TensorFlow 128 | 129 | 130 | -------------------------------------------------------------------------------------------- 131 | 132 | Part3: Object Detection API 安装 133 | 134 | -------------------------------------------------------------------------------------------- 135 | 136 | 16、去github下载protobuf 137 | 138 | https://github.com/protocolbuffers/protobuf/releases 139 | 140 | 选择 protobuf-all-3.6.1.tar.gz #可能与这里不同,但必须是-all-,一般是第一个 141 | 142 | 143 | 144 | 17、安装 145 | 146 | cd home/pi/Downloads 147 | 148 | tar xvfz protobuf-all-3.6.1.tar.gz #可能不同 149 | 150 | cd protobuf-3.6.1 #可能不同 151 | 152 | ./configure # 感谢@时间发货几十块 指出的问题 153 | 154 | sudo make –j 4 155 | 156 | # 有风扇就用4个线程,没有就2-3个; 157 | 158 | # 1A电源的建议使用 “sudo make” ,以免卡死 159 | 160 | sudo make install 161 | 162 | sudo ldconfig # refresh shared library cache. 163 | 164 | 165 | 166 | 18、获得API 167 | 168 | 推荐旧版,新的建议在电脑上改好再复制到树莓派上 169 | 170 | https://github.com/tensorflow/models # 这是最新的获取路径 171 | 172 | https://pan.baidu.com/s/1LxJ9TpGGDyas_sCVanSZgQ # 老版API下载地址 173 | 174 | 175 | 19、生成protoc 176 | 177 | cd /home/pi/tf/models-master/research/ # 可能路径与我不同 178 | 179 | protoc object_detection/protos/*.proto --python_out=. 180 | 181 | 182 | 183 | 20、添加环境变量 184 | 185 | sudo nano /etc/profile 186 | 187 | export PYTHONPATH=/home/pi/tf/models-master/research/:/home/pi/tf/models-master/research/slim/ 188 | 189 | # 在最后一行添加,请使用方向键移动光标 190 | 191 | 192 | 193 | 21、重启 194 | 195 | sudo reboot # 重启!!请保证已保存重要信息 196 | 197 | 198 | 199 | 22、查看环境变量 200 | 201 | env 202 | 203 | # 确认PYTHONPATH内信息无误 204 | 205 | 206 | 207 | 23、测试 208 | 209 | cd /home/pi/tf/models-master/research/ # 可能路径与我不同 210 | 211 | python object_detection/builders/model_builder_test.py 212 | 213 | 214 | 215 | Ok--测试成功 216 | --------------------------------------------------------------------------------