您的位置:首页 > 移动开发 > Objective-C

TF ObjectDetectionAPI的安装、验证测试(3)

2017-11-12 20:01 1191 查看
3. 视频识别物体。

        
一般Anaconda3安装时不会安装opencv_python版的。要自己动手安装。步骤如下。
      
(安装opencv_python时也试过condainstall
-c https://conda.binstar.org/menpoopencv3
这个方式,但提示找不到opencv3,故保险起见用如下方式)
        
1.下载opencv包,
                  
在Python官网即可下载opencv相关库,点击https://pypi.python.org/pypi/opencv-python直接进入。
        
本人选用opencv_python-3.3.0.10-cp36-cp36m-win_amd64.whl
        
下载完成后,放到某个目录下,然后在cmd中进入此目录,之后执行安装命令:
pip install opencv_python-3.3.0.10-cp36-cp36m-win_amd64.whl
安装完成即可。(可以点击Anacondaprompt进入cmd,使用conda
list就能查看到opencv_python)
        

        
2.如果没有安装过moviepy,则用如下方式来安装。(过程遇到了问题,参见“moviepy安装过程中的问题”博文)
        
从moviepy官网下载源码包https://pypi.python.org/pypi/moviepy。即moviepy-0.2.3.2.tar。
        
解压到任何目录下然后以管理员身份在进入cmd下,敲入python
setup.py install 命令。
        
即可安装成功。
        

        
3.相应的代码参见如下。
        
(参考了http://blog.csdn.net/xiaoxiao123jun/article/details/76605928)
 
#importlib for edit/save/watch vedio clip 
import imageio
imageio.plugins.ffmpeg.download()
 
# Import everything needed toedit/save/watch video clips
from moviepy.editor import VideoFileClip
from IPython.display import HTML
 
#定义一个检测对象的函数
参数-图像
会话

def detect_objects(image_np, sess,detection_graph):
   ##Expand dimensions since the model expects images to have shape: [1,None, None, 3]
   ##扩展尺度直到模型期望图像该有的形状[1,None,None,3]
   image_np_expanded = np.expand_dims(image_np, axis=0)
   

   ##为detection_graph图定义输入输出的张量tensor
   image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
 
   ##Each box represents a part of the image where a particular object wasdetected.
   ##每个矩形框代表图像的一部分就是被检测的特定对象
   boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
 
   ##Each score represent how level of confidence for each of the objects.
   ##Score is shown on the result image, together with the class label.
   ##每个数值代表每个对象的置信度到了什么层次
   ##数值显示在结果图像上与类别标签一起
   scores = detection_graph.get_tensor_by_name('detection_scores:0')
   classes = detection_graph.get_tensor_by_name('detection_classes:0')
   num_detections = detection_graph.get_tensor_by_name('num_detections:0')
 
   ##Actual detection.
   ##实际检测
   (boxes, scores, classes, num_detections) = sess.run(
       [boxes, scores, classes, num_detections],
       feed_dict={image_tensor: image_np_expanded})
 
   ##Visualization of the results of a detection.
   ##检测结果显示出来
   vis_util.visualize_boxes_and_labels_on_image_array(
       image_np,
       np.squeeze(boxes),
       np.squeeze(classes).astype(np.int32),
       np.squeeze(scores),
       category_index,
       use_normalized_coordinates=True,
       line_thickness=8)
   return image_np
 
#定义图像处理流程
def process_image(image):
   
#NOTE: The output you return should be a color image (3 channel) for processingvideo below
   
#you should return the final output (image with lines are drawn on lanes)
   with detection_graph.as_default():
       with tf.Session(graph=detection_graph) as sess:
           image_process = detect_objects(image, sess, detection_graph)
           return image_process
 
##定义输出的视频文件名
white_output = 'video1_out.mp4'
 
##从视频文件中取得25-30秒间的视频帧
clip1 = VideoFileClip("video1.mp4").subclip(1,3)
 
##对获取的视频帧进行图像处理流程
white_clip = clip1.fl_image(process_image)#NOTE: this function expects color images!!s
 
##把处理后的视频帧写入输出视频中
%timewhite_clip.write_videofile(white_output, audio=False)
 
##在网页上显示视频
HTML("""
<video width="960"height="540" controls>
 <source src="{0}">
</video>
""".format(white_output))
 
##把视频转换成gif格式
from moviepy.editor import *
clip1 =VideoFileClip("video1_out.mp4")
clip1.write_gif("video1_final.gif")
        

结果会保存在object_detection文件夹下。
至此完成了使用谷歌ObjectDetecitonAPI进行视频物体识别的练习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息