python - 谁能帮我用 python 读取不断更新的视频文件?

标签 python opencv video video-streaming video-processing

我有一个小型 ImageAI 程序,它通过我的网络摄像头和/或 IP 网络摄像头检测一些对象, 最后一部分看起来像这样:

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)

这会创建一个 avi 文件,用于录制视频并检测对象。 虽然我可以通过打开此文件来查看进度,但我必须关闭它并再次打开才能查看正在进行的更新进度。 有没有办法用 %matplotlib inline 等来显示这个视频?

enter image description here

最佳答案

我没用过,但是如果你look at the documentation它有一个可选参数来返回检测到的帧:

– parameter return_detected_frame (optional) : This parameter allows you to return the detected frame as a Numpy array at every frame, second and minute of the video detected. The returned Numpy array will be parsed into the respective per_frame_function, per_second_function and per_minute_function (See details below)

然后您还需要向此参数传递一个函数:

—parameter per_frame_function (optional ) : This parameter allows you to parse in the name of a function you define. Then, for every frame of the video that is detected, the function will be parsed into the parameter will be executed and and analytical data of the video will be parsed into the function. The data returned can be visualized or saved in a NoSQL database for future processing and visualization.

新函数应该与文档中的函数类似:

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)

记下最后两个参数。

希望对你有帮助

关于python - 谁能帮我用 python 读取不断更新的视频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58323337/

相关文章:

python - 如何检查这是否是 Django 中的索引页面?

python - AES 加密 Golang 和 Python

python - Qt 和 opencv 应用程序无法在虚拟环境中运行

c++ - Opencv - Haar 级联 - 面部跟踪非常慢

安卓 MediaCodec 解码器 : Slow Down Video Playback

python - 绘制每列随时间的观察结果(数据可用性)

python - 如何在django中存储复杂的csv数据?

python - 使用两个网络摄像头进行 3D 对象跟踪

jquery - Vimeo/Youtube 播放 Jquery .click() 函数...我什至不知道如何表达这个问题

c - Gstreamer 将decodebin2链接到autovideosink