BabitMF / bmf

Cross-platform, customizable multimedia/video processing framework. With strong GPU acceleration, heterogeneous design, multi-language support, easy to use, multi-framework compatible and high performance, the framework is ideal for transcoding, AI inference, algorithm integration, live video streaming, and more.
https://babitmf.github.io/
Apache License 2.0
733 stars 60 forks source link

core dumped in frame extract #61

Open RoyaltyLJW opened 9 months ago

RoyaltyLJW commented 9 months ago

Hi, when I run the code below in a 4 CPU machine,Aborted (core dumped) happen. Error rate is 6/10. (Run 10 times and error occur 6 times). But in a 16 CPU machine, it doesn't happen. I observed that when executing on the 4cpu machine, the cpu usage is almost 100%. Maybe it is the reason. Apart from adding more CPU, is there any way to avoid this problem?

import bmf
import time
from multiprocessing.pool import ThreadPool
import glob
import numpy as np

def generator_mode(input_list):
    input_path,threads = input_list
    start = time.time()
    graph = bmf.graph()
    video =  graph.decode({
                    'input_path': input_path,
                    "log_level":"quiet",
                    "dec_params": {"threads": threads},
                })['video'].start() # this will return a packet generator
    for pkt in video:
        # convert frame to a nd array
        if pkt.is_(bmf.VideoFrame):
            vf = pkt.get(bmf.VideoFrame)
            v_frame = vf.frame().plane(2).numpy()
        else:
            break
    use = time.time() - start
    return use

if __name__ == '__main__':
    #串行
    # print(time.time())
    test_threads = [0,2,4,6,8]
    video_paths = glob.glob("/root/ori/*.mp4")

    for threads in test_threads:
        for infilename in video_paths:
            extract_u_frame_time = []
            run_path = []
            for i in range(20):
                run_path.append([infilename, str(threads)])
            with ThreadPool(2) as p:
                extract_u_frame_time.extend(p.map(generator_mode, run_path))

the environment version is below:

python=3.7.12
ffmpeg version 4.1.11-0+deb10u1
numpy==1.21.6
BabitMF==0.0.8

stdout of error:

terminate called without an active exception
Aborted (core dumped) happen

Running command

nohup python3 generator_mode.py 
RoyaltyLJW commented 9 months ago

What's more, memory leak happen in the code below due to opencv operation


def generator_mode(input_list):
    input_path,threads = input_list
    start = time.time()
    graph = bmf.graph()
    video =  graph.decode({
                    'input_path': input_path,
                    "log_level":"quiet",
                    "dec_params": {"threads": threads},
                })['video'].start() # this will return a packet generator
    for pkt in video:
        # convert frame to a nd array
        if pkt.is_(bmf.VideoFrame):
            vf = pkt.get(bmf.VideoFrame)
            v_frame = vf.frame().plane(2).numpy()
            a = cv2.blur(v_frame, (7, 7) ,borderType=cv2.BORDER_REFLECT_101).astype(np.float32)
        else:
            break
    use = time.time() - start
    return use
HuHeng commented 9 months ago

The memory leak is likely caused by uncontrolled output speed in asynchronous decoding, resulting in memory growth. You may consider using bmf.bmf_sync.process as an alternative or switch to the graph mode to avoid this issue.

Thank you very much for the feedback. We will look into those issues in the coming days.