有人能帮我用python阅读不断更新的视频文件吗?

有人能帮我用python阅读不断更新的视频文件吗?,python,opencv,video,video-streaming,video-processing,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 loa

我有一个小型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)
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)
这将导致创建一个avi文件,用于记录视频和检测对象。 虽然我可以通过打开此文件来查看进度,但我必须先关闭该文件,然后再次打开以查看正在进行的更新进度。 是否有一种方法可以使用%matplotlib inline等内容显示此视频


我没有使用它,但是如果您使用它,它有一个可选参数来返回检测到的帧:

–参数返回检测到的帧(可选):此参数允许 您需要在每一帧以Numpy数组的形式返回检测到的帧, 检测到视频的秒和分钟数。返回的Numpy数组将 被解析为相应的每帧函数、每秒钟函数 和每分钟功能(详见下文)

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

-每帧参数功能(可选):此参数允许 以您定义的函数的名称进行解析。然后,对于每一帧 对于检测到的视频,函数将被解析为 参数将被执行,视频的分析数据将被删除 被解析为函数。返回的数据可以可视化或 保存在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)
这也将打印其他分析数据,但您可以只打印框架

您的代码必须更改为如下所示:

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)
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)
注意最后两个论点


我希望这对你有帮助

嗯,我不敢相信答案就在我眼前。这对我有用。谢谢!!我认为作为一个初学者,我真的应该开始仔细阅读长长的代码行。再次感谢。真的很感激。@RiteshKankonkar没问题,解决这类问题的最佳方法是查看文档,如果已经有了一个预期的方法:)如果这对您有帮助,请接受答案,这样其他人会更快地找到答案。