谁能帮我用 python 阅读一个不断更新的视频文件?

Can anyone help me with reading a continuously updating video file with python?

我有一个小的 ImageAI 程序,它通过我的网络摄像头检测一些物体 and/or 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 等来显示这个视频?

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

– 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)

注意最后两个参数。

希望对你有帮助