- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 SSD tflite 检测模型,正在台式计算机上使用 Python 运行。就目前而言,我的下面的脚本将单个图像作为推理的输入,并且运行良好:
# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()
img_resized = Image.open(file_name)
input_data = np.expand_dims(img_resized, axis=0)
input_data = (np.float32(input_data) - input_mean) / input_std
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
如何对 .mp4 视频作为输入运行推理?
是否也可以从该视频上检测到的对象绘制边界框?
最佳答案
回答您在视频上运行推理的第一个问题。这是您可以使用的代码。我为分类模型的推理编写了这段代码,因此在您的情况下,output_data变量的输出将采用边界框的形式,您必须使用OpenCV将它们映射到框架上,这也回答了您的第二个问题(绘制边界视频上的方框)。
import cv2
from PIL import Image
import numpy as np
import tensorflow as tf
def read_tensor_from_readed_frame(frame, input_height=224, input_width=224,
input_mean=0, input_std=255):
output_name = "normalized"
float_caster = tf.cast(frame, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, input_width])
normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)
return result
def load_labels(label_file):
label = []
proto_as_ascii_lines = tf.gfile.GFile(label_file).readlines()
for l in proto_as_ascii_lines:
label.append(l.rstrip())
return label
def VideoSrcInit(paath):
cap = cv2.VideoCapture(paath)
flag, image = cap.read()
if flag:
print("Valid Video Path. Lets move to detection!")
else:
raise ValueError("Video Initialization Failed. Please make sure video path is valid.")
return cap
def main():
Labels_Path = "labels.txt"
Model_Path = "model.tflite"
input_path = "video.mp4"
##Loading labels
labels = load_labels(Labels_Path)
##Load tflite model and allocate tensors
interpreter = tf.lite.Interpreter(model_path=Model_Path)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
##Read video
cap = VideoSrcInit(input_path)
while True:
ok, cv_image = cap.read()
if not ok:
break
##Converting the readed frame to RGB as opencv reads frame in BGR
image = Image.fromarray(cv_image).convert('RGB')
##Converting image into tensor
image_tensor = read_tensor_from_readed_frame(image ,224, 224)
##Test model
interpreter.set_tensor(input_details[0]['index'], image_tensor)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
## You need to check the output of the output_data variable and
## map it on the frame in order to draw the bounding boxes.
cv2.namedWindow("cv_image", cv2.WINDOW_NORMAL)
cv2.imshow("cv_image",cv_image)
##Use p to pause the video and use q to termiate the program
key = cv2.waitKey(10) & 0xFF
if key == ord("q"):
break
elif key == ord("p"):
cv2.waitKey(0)
continue
cap.release()
if __name__ == '__main__':
main()
关于python - 视频输入上的 TFLite 推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58786001/
现在,我正在使用 MALLET 包中的 LDA 主题建模工具对我的文档进行一些主题检测。最初一切都很好,我从中得到了 20 个主题。但是,当我尝试使用该模型推断新文档时,结果有点莫名其妙。 例如,我故
我正在使用 Jersey 在 Scala 中开发 REST web 服务JAX-RS 引用实现,我收到一个奇怪的错误。 我正在尝试创建一个 ContentDisposition对象使用 Content
以下两个用于计算斐波那契数列第 n 项的 Haskell 程序具有截然不同的性能特征: fib1 n = case n of 0 -> 1 1 -> 1 x -> (fib
所以在来自 another question 的评论中,我刚刚看到了这个计算字符串中 L 数量的例子: "hello".count('l'==) 而且够疯狂……它有效。 从完全扩展的版本开始,我们有:
我在 android 上运行训练有素的 yolov2 网络时遇到问题。我正在使用这个项目进行测试 https://github.com/szaza/android-yolo-v2 . 提供的网络工作正
我目前在我的 iOS 应用程序中使用 Tensorflow 的 Swift 版本。我的模型工作正常,但我无法将数据复制到第一个张量中,因此我可以使用神经网络来检测东西。 我咨询了the testsui
我有一个 SSD tflite 检测模型,正在台式计算机上使用 Python 运行。就目前而言,我的下面的脚本将单个图像作为推理的输入,并且运行良好: # Load TFLite model
我所拥有的:在 Tensorflow 中经过训练的递归神经网络。 我想要的:一个可以尽可能快地运行这个网络的移动应用程序(只有推理模式,没有训练)。 我相信有多种方法可以实现我的目标,但我希望您能提供
**我得到了一些让我的函数成为纯通用函数的建议,这可行,但我更愿意将函数限制为仅接受 Base 及其子项。 在创建可以接受可变模板类基类型参数的函数时遇到问题,而该函数实际上将使用从 Base 派生的
我想使用 TF 2.0 在我的 GPU 集群上运行分布式预测。我使用 MirroredStrategy 训练了一个用 Keras 制作的 CNN 并保存了它。我可以加载模型并在其上使用 .predic
实现一个 C++ 代码来加载一个已经训练好的模型然后获取它而不是使用 Python 真的值得吗? 我想知道这一点,因为据我所知,用于 python 的 Tensorflow 是幕后的 C++(对于 n
我将在网站上提供 pytorch 模型(resnet18)。 然而,在 cpu(amd3600) 中进行推理需要 70% 的 cpu 资源。 我不认为服务器(heroku)可以处理这个计算。 有什么方
为了充分利用 CPU/GPU,我运行了多个对不同数据集进行 DNN 推理(前馈)的进程。由于进程在前馈期间分配了 CUDA 内存,因此我收到了 CUDA 内存不足错误。为了缓解这种情况,我添加了 to
你知道用 1 个 GPU tensorflow 对 2 个 python 进程进行推理的优雅方法吗? 假设我有 2 个进程,第一个是分类猫/狗,第二个是分类鸟/飞机,每个进程运行不同的 tensorf
我是 Scala 的初学者,不明白这里发生了什么: 给定: val reverse:Option[MyObject] = ... 并且myObject.isNaire返回 bool 值。 如果我这样做
我正在尝试通过用我常用的语言 Clojure 实现算法 W 来自学 Hindley-Milner 类型推理。我遇到了 let 推理的问题,我不确定我是否做错了什么,或者我期望的结果是否需要算法之外的东
我正在尝试通过用我常用的语言 Clojure 实现算法 W 来自学 Hindley-Milner 类型推理。我遇到了 let 推理的问题,我不确定我是否做错了什么,或者我期望的结果是否需要算法之外的东
我做了一个项目,基本上使用带有 tensorflow 的 googles object detection api。 我所做的只是使用预训练模型进行推理:这意味着实时对象检测,其中输入是网络摄像头的视
我有一台带有多个 GPU 的服务器,我想在 Java 应用程序内的模型推理期间充分利用它们。默认情况下,tensorflow 占用所有可用的 GPU,但仅使用第一个。 我可以想到三个选项来解决这个问题
这个预测时间190ms,应该是cpu版本 昨天修改了个OpenCV DNN支持部署YOLOv5,6.1版本的Python代码,今天重新转换为C 代码了!貌似帧率比之前涨了点!说明C的确是比Python
我是一名优秀的程序员,十分优秀!