首页 > 其他分享 >实验18-使用TensorFlow完成视频物体检测

实验18-使用TensorFlow完成视频物体检测

时间:2024-06-05 21:33:00浏览次数:13  
标签:视频 tensor 18 image label detection graph np TensorFlow

image_object_detection.py

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from PIL import Image

import label_map_util
import visualization_utils as vis_util

PATH_TO_CKPT = 'ssd_mobilenet_v1_coco_2018_01_28/frozen_inference_graph.pb'
PATH_TO_LABELS = 'data/mscoco_label_map.pbtxt'
NUM_CLASSES = 90

detection_graph = tf.Graph()

with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        od_graph_def.ParseFromString(fid.read())
        tf.import_graph_def(od_graph_def, name='')

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)

def load_image_into_numpy_array(image):
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)

TEST_IMAGE_PATHS = ['test_data/image1.jpg']

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        for image_path in TEST_IMAGE_PATHS:
            image = Image.open(image_path)
            image_np = load_image_into_numpy_array(image)
            image_np_expanded = np.expand_dims(image_np, axis=0)
            (boxes, scores, classes, num) = sess.run(
                [detection_boxes, detection_scores, detection_classes, num_detections],
                feed_dict={image_tensor: image_np_expanded})
            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)
            plt.figure(figsize=[12, 8])
            plt.imshow(image_np)
            plt.show()

 

import numpy as np
import tensorflow as tf
import cv2

import label_map_util
import visualization_utils as vis_util

cap = cv2.VideoCapture('test_data/test_video.mp4')
ret, image_np = cap.read()
out = cv2.VideoWriter('output_video.mp4', -1, cap.get(cv2.CAP_PROP_FPS), (image_np.shape[1], image_np.shape[0]))

PATH_TO_CKPT = 'ssd_mobilenet_v1_coco_2018_01_28/frozen_inference_graph.pb'
PATH_TO_LABELS = 'data/mscoco_label_map.pbtxt'
NUM_CLASSES = 90

detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        od_graph_def.ParseFromString(fid.read())
        tf.import_graph_def(od_graph_def, name='')

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)

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')
        while cap.isOpened():
            ret, image_np = cap.read()
            if len((np.array(image_np)).shape) == 0:
                break

            image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
            image_np_expanded = np.expand_dims(image_np, axis=0)

            (boxes, scores, classes, num) = sess.run(
                [detection_boxes, detection_scores, detection_classes, num_detections],
                feed_dict={image_tensor: image_np_expanded})

            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)
            out.write(cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR))

cap.release()
out.release()
cv2.destroyAllWindows()

 

标签:视频,tensor,18,image,label,detection,graph,np,TensorFlow
From: https://www.cnblogs.com/liucaizhi/p/18233868

相关文章

  • 利用短视频平台,轻松引流获客:自动私信评论策略全解析
    在数字化时代,短视频已成为互联网流量的新蓝海,其独特的视觉吸引力和高度的用户粘性为各行各业提供了前所未有的营销机遇。无论是初创企业还是成熟品牌,都能通过短视频平台有效触达目标客户,实现高效引流与获客。本文将深入探讨如何利用短视频平台,结合精准推广策略,轻松吸引潜在客户......
  • Qt6 播放音视频
    一、概述QT6相较于Qt5引入了许多新特性和改进,包括对音视频开发的增强支持。QT6中的音视频支持QT6提供了一套完整的音视频处理功能,这些功能被整合在QtAV项目中。QtAV是一个基于Qt的音视频处理框架,用于处理音视频播放、录制、编解码、处理等任务。QT6对QtAV进行了进一步的集成......
  • 无缝融合:使用 Python 和 PyFFmpeg 合并视频的完整指南
    前言在当今数字化时代,视频内容无处不在。从社交媒体到在线教育,视频已经成为我们生活中不可或缺的一部分。但是,有时候我们可能需要将多个视频片段合并成一个,创造出更丰富、更有吸引力的内容。而今天,我们将向您展示如何使用Python和PyFFmpeg工具实现这一目标。准备工作:安装P......
  • iOS 18 Beta版将于下周发布:预计将带来这25项新功能
    iOS18即将发布,苹果将在6月10日(北京时间6月11日凌晨1点)的WWDC主题演讲中发布软件更新,预计将推出许多新功能和变化。iOS18的第一个测试版应该会在WWDC主题演讲后立即向Apple开发者计划的成员提供,公开测试版可能会在7月发布。该更新应该会在9月向所有用户广泛发布,据传它将与iOS1......
  • 自媒体--视频技巧
    多个图片+文字做视频          ......
  • 视频文件批量更换名称的方法有哪些?分享4个高效的方法
    我们有一大批视频文件需要重新命名时,手动一个一个地操作显然非常低效。为了解决这个问题,我们可以借助一些工具来实现批量重命名操作。下面分享4个高效的方法,帮助你快速完成大量视频文件的重命名方法一:使用【汇帮批量重命名】以下是具体的操作步骤:1:下载并安装“汇帮批量重命......
  • 如何将onnx稳定的转换为tensorflow, 甚至转换为tflite(float32/int8)
    做模型部署边缘设备的时候,我们经常会遇到特定格式的要求。但常见的onnx2tf很多时候都不能满足我们的要求。因此,记录一下我的操作过程。1.环境:(linux18.04)#NameVersionBuildChannel_libgcc_mutex0.1......
  • 操作简单中医电子处方中药划价系统软件视频教程,佳易王诊所电子处方管理系统软件
    操作简单中医电子处方中药划价系统软件视频教程,佳易王诊所电子处方管理系统软件一、前言以下软件操作教程以,佳易王中西医诊所电子处方软件为例说明软件文件下载可以点击最下方官网卡片——软件下载——试用版软件下载 1、软件支持中医和西医处方开单2、系统自动计算费用......
  • 今天我们来聊一聊视频号小店的优点和缺点~
    大家好,我是喷火龙。话不多说,直接进入正题。先来讲讲视频号小店平台发展问题与项目优势:1,关于视频号:视频号本质是微信体系里面的短视频平台,依托着微信庞大的流量和用户所展开的短视频和直播内容的对外输出。在2023微信公开课PRO上,视频号团队介绍,现阶段用户使用时长已经超过了......
  • 即时通讯技术文集(第39期):推送技术合集(Part1) [共18篇]
    为了更好地分类阅读52im.net总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第 39 期。[- 1 -] iOS的推送服务APNs详解:设计思路、技术原理及缺陷等[链接] http://www.52im.net/thread-345-1-1.html[摘要] 本文重点介绍APNs的设计思路、技术原理以及各......