保存tensorflow物体检测增强图像
创始人
2024-11-22 19:01:42
0次

要保存 TensorFlow 物体检测增强图像,可以使用以下代码示例:

import tensorflow as tf
import numpy as np
import cv2

# 加载模型和标签
model = tf.saved_model.load('path_to_saved_model')
labels = ['label1', 'label2', ...]  # 替换为实际的标签列表

# 读取和预处理图像
image = cv2.imread('input_image.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = tf.image.resize(image, (300, 300))
image = tf.expand_dims(image, axis=0)
image = tf.cast(image, tf.float32) / 255.0

# 推断和后处理
output = model(image)
detections = output['detection_boxes'][0].numpy()
scores = output['detection_scores'][0].numpy()
classes = output['detection_classes'][0].numpy().astype(np.int32)
num_detections = int(output['num_detections'][0])

# 绘制边界框和标签
for i in range(num_detections):
    if scores[i] >= 0.5:  # 设置阈值来过滤低置信度的检测结果
        ymin, xmin, ymax, xmax = detections[i]
        xmin = int(xmin * image.shape[2])
        xmax = int(xmax * image.shape[2])
        ymin = int(ymin * image.shape[1])
        ymax = int(ymax * image.shape[1])

        cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (255, 0, 0), 2)
        cv2.putText(image, labels[classes[i]], (xmin, ymin-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)

# 保存增强后的图像
output_image = cv2.cvtColor(image.numpy()[0], cv2.COLOR_RGB2BGR)
cv2.imwrite('output_image.jpg', output_image)

需要注意的是,上述代码示例假设你已经训练好了一个物体检测模型,并将其保存为 TensorFlow SavedModel 的格式。你需要将 path_to_saved_model 替换为你实际保存模型的路径。另外,如果你的模型有自定义标签,需要将 labels 列表替换为你实际的标签列表。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...