当前位置:网站首页>Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
2022-07-06 14:53:00 【gmHappy】
Based on the previous article, based on Google open source TensorFlow Object Detection API Video object recognition system builds its own application ( One ), Realize video object recognition
Based on the first part , newly build VideoTest.py, And put a video file into object_detection Under the table of contents
The main steps are as follows :
1. Use VideoFileClip Function to grab an image from a video .
2. use fl_image Function to replace the original image with the modified image , Each captured image used to transmit object recognition .
3. All the modified clip images are combined into a new video .
I don't say much nonsense , Go straight to the code :
# coding: utf-8
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")
from object_detection.utils import ops as utils_ops
if tf.__version__ < '1.4.0':
raise ImportError('Please upgrade your tensorflow installation to v1.4.* or later!')
# This is needed to display the images.
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
##Model preparation##
# What model to download.
MODEL_NAME = 'ssd_mobilenet_v2_coco_2018_03_29'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
NUM_CLASSES = 90
## Download Model##
#opener = urllib.request.URLopener()
#opener.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())
## Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
## Loading label map
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
import imageio
imageio.plugins.ffmpeg.download()
from moviepy.editor import VideoFileClip
from IPython.display import HTML
def detect_objects(image_np, sess, detection_graph):
# Expand dimensions , Expected for model : [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents an object detected
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represents the reliability of the detected object .
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')
# Perform detection task .
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of test results
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 processing video 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'
clip1 = VideoFileClip("video1.mp4").subclip(10,20)
white_clip = clip1.fl_image(process_image) #NOTE: this function expects color images!!s
white_clip.write_videofile(white_output, audio=False)
from moviepy.editor import *
clip1 = VideoFileClip("video1_out.mp4")
clip1.write_gif("final.gif")
- 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.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
Code interpretation :
A necessary program for editing will be downloaded here ffmpeg.win32.exe, It's easy to disconnect during intranet downloading , You can use the download tool to download it and put it in the following path :
C:\Users\ user name \AppData\Local\imageio\ffmpeg\ffmpeg.win32.exe
First , Use VideoFileClip Function to extract images from video , extract 10 to 20S Image
And then use fl_image Function modifies the image of the clip by replacing the frame , And by calling process_image Apply object recognition on it API.fl_image It's a very useful function , You can extract an image and replace it with a modified image . Through this function, you can extract images from each video and apply object recognition ;
Last , Combine all the processed image clips into a new video .
So far, the object recognition in the video has been completed .
边栏推荐
- Four methods of exchanging the values of a and B
- Interview Essentials: what is the mysterious framework asking?
- Summary of thread implementation
- Markdown font color editing teaching
- 《统计学》第八版贾俊平第四章总结及课后习题答案
- 函数:字符串反序存放
- Constants, variables, and operators of SystemVerilog usage
- {1,2,3,2,5} duplicate checking problem
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- Based on authorized access, cross host, and permission allocation under sqlserver
猜你喜欢
《统计学》第八版贾俊平第二章课后习题及答案总结
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Intranet information collection of Intranet penetration (3)
How to transform functional testing into automated testing?
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
Es full text index
Cadence physical library lef file syntax learning [continuous update]
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
随机推荐
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
Why can swing implement a form program by inheriting the JFrame class?
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
四元数---基本概念(转载)
Proceedingjoinpoint API use
DVWA exercise 05 file upload file upload
王爽汇编语言详细学习笔记二:寄存器
Fundamentals of digital circuit (V) arithmetic operation circuit
函数:求方程的根
【指针】求二维数组中最大元素的值
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
How to use Moment. JS to check whether the current time is between 2 times
SystemVerilog discusses loop loop structure and built-in loop variable I
Binary search tree concept
How to learn automated testing in 2022? This article tells you
c语言学习总结(上)(更新中)
[oiclass] maximum formula
5分钟掌握机器学习鸢尾花逻辑回归分类
数字电路基础(一)数制与码制
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?