python - Can anyone help me with reading a continuously updating video file with python?
问题描述
I have a small ImageAI program which is detecting a few objects via my webcam and/or IP webcam, the final part looks something like this:
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
print('Model loaded')
cap = cv2.VideoCapture(0)
video_path = detector.detectObjectsFromVideo(camera_input=cap,
output_file_path=os.path.join(execution_path, "captured")
, frames_per_second=5, log_progress=True, detection_timeout=120)
print(video_path)
This is resulting in creating an avi file that is recording the video and detecting the objects. While I can see the progress by opening this file, I have to close it and open again in order to see the ongoing updated progress. Is there a way to display this video with something like %matplotlib inline, etc?
解决方案
我没有使用它,但是如果您查看文档,它有一个可选参数来返回检测到的帧:
– 参数 return_detected_frame(可选):此参数允许您在检测到的视频的每一帧、秒和分钟将检测到的帧作为 Numpy 数组返回。返回的 Numpy 数组将被解析为各自的 per_frame_function、per_second_function 和 per_minute_function(详见下文)
然后你还需要将一个函数传递给这个参数:
--parameter per_frame_function(可选):此参数允许您解析您定义的函数的名称。然后,对于检测到的每一帧视频,该函数将被解析为将执行的参数,并将视频的分析数据解析为该函数。返回的数据可以可视化或保存在 NoSQL 数据库中,以供将来处理和可视化。
新功能应类似于文档中的功能:
def forFrame(frame_number, output_array, output_count, returned_frame):
plt.clf()
this_colors = []
labels = []
sizes = []
counter = 0
for eachItem in output_count:
counter += 1
labels.append(eachItem + " = " + str(output_count[eachItem]))
sizes.append(output_count[eachItem])
this_colors.append(color_index[eachItem])
global resized
if (resized == False):
manager = plt.get_current_fig_manager()
manager.resize(width=1000, height=500)
resized = True
plt.subplot(1, 2, 1)
plt.title("Frame : " + str(frame_number))
plt.axis("off")
plt.imshow(returned_frame, interpolation="none")
plt.subplot(1, 2, 2)
plt.title("Analysis: " + str(frame_number))
plt.pie(sizes, labels=labels, colors=this_colors, shadow=True, startangle=140, autopct="%1.1f%%")
plt.pause(0.01)
这也将绘制其他分析数据,但您可以只绘制框架。
您的代码必须更改为如下所示:
video_path = detector.detectObjectsFromVideo(camera_input=cap,
output_file_path=os.path.join(execution_path, "captured")
, frames_per_second=5, log_progress=True, detection_timeout=120,
return_detected_frame=True, per_frame_function=forFrame)
注意最后两个论点。
我希望这可以帮助你
推荐阅读
- javascript - 数组解构内的解构
- database - Laravel 自定义数据库数据表
- android - Firebase 存储规则 - 授予的访问权限与规则不匹配
- vba - 复制不包括空白单元格的范围 - 粘贴到另一个工作表
- angular - 使用反应形式将 mat-table [dataSource] 中的两个元素(列,例如数量 * unitPrice = enterGrossAmount)相乘
- python - 如何删除一个列表中的元素,因为它们已从另一个列表中删除?
- java - 如何从底部导航栏中隐藏项目
- python-3.x - 为什么 if (integer) 被评估为 True?
- asp.net-core - “正常”环境变量是否比 IIS 环境变量更安全?
- python - Python:使用 sh 加载 venv 的烧瓶