当前位置:网站首页>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 .
边栏推荐
猜你喜欢
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
数字电路基础(五)算术运算电路
Keil5 MDK's formatting code tool and adding shortcuts
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
Login the system in the background, connect the database with JDBC, and do small case exercises
Software testing interview summary - common interview questions
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
Summary of thread implementation
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
1.支付系统
随机推荐
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
数字电路基础(一)数制与码制
Functions: Finding Roots of equations
指针:最大值、最小值和平均值
flask实现强制登陆
【指针】数组逆序重新存放后并输出
“Hello IC World”
Pointer -- output all characters in the string in reverse order
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
New version of postman flows [introductory teaching chapter 01 send request]
【指针】使用插入排序法将n个数从小到大进行排列
c语言学习总结(上)(更新中)
About the garbled code problem of superstar script
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Login the system in the background, connect the database with JDBC, and do small case exercises
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
Based on authorized access, cross host, and permission allocation under sqlserver