简体   繁体   English

使用ffmpeg或Python从视频中删除随机背景

[英]Remove random background from video using ffmpeg or Python

I want to remove background from a person's video using ffmpeg or Python . 我想使用ffmpegPython从某人的视频中删除背景。 If I record a video at any place, detect the person in the video and then remove anything except that person. 如果我在任何地方录制视频,请检测视频中的那个人,然后删除那个人之外的任何东西。 Not asking for green or single color background as that can be done through chromakey and I am not looking for that. 不要求使用绿色或单色背景,因为可以通过抠像来完成,而我并不是在寻找它。

I've tried this ( https://tryolabs.com/blog/2018/04/17/announcing-luminoth-0-1/ ) approach but it is giving me output of rectangular box. 我已经尝试过这种方法( https://tryolabs.com/blog/2018/04/17/announcing-luminoth-0-1/ )方法,但是它给了我矩形框的输出。 It is informative enough as area to explore is narrow down enough but still need to remove total background. 由于要探索的区域足够狭窄,因此它很有信息,但仍然需要删除整个背景。 I've also tried grabcut ( https://docs.opencv.org/4.1.0/d8/d83/tutorial_py_grabcut.html ) but that need user interaction otherwise result isn't too good. 我也尝试过grabcuthttps://docs.opencv.org/4.1.0/d8/d83/tutorial_py_grabcut.html ),但是需要用户交互,否则结果不太好。 I've also tried to use ffmpeg and found this example ( http://oioiiooixiii.blogspot.com/2016/09/ffmpeg-extract-foreground-moving.html ) but it needs still image so I tried to take background picture before recording video with a person but there are many things required to take difference between background image and video frame. 我也尝试使用ffmpeg并找到了此示例( http://oioiiooixiii.blogspot.com/2016/09/ffmpeg-extract-foreground-moving.html ),但是它需要静止图像,因此我尝试拍摄背景图片与人一起录制视频,但是要使背景图像和视频帧之间有所区别,还需要做很多事情。

For opencv approach, I've tried this. 对于opencv方法,我已经尝试过了。

img = cv.imread('pic.png')
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)
rect = (39, 355, 1977, 2638)
cv.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0), 0, 1).astype('uint8')
img = img*mask2[:, :, np.newaxis]
plt.imshow(img), plt.colorbar(), plt.show()

But it is removing some of person's part too. 但是它也在去除人的某些部分。 Also tried ffmpeg way but not a good result. 还尝试了ffmpeg方式,但是效果不好。

ffmpeg -report -y -i "img.jpg" -i "vid.mov" -filter_complex "[1:v]format=yuva444p,lut=c3=128[video2withAlpha],[0:v][video2withAlpha]blend=all_mode=difference[out]" -map "[out]" "output.mp4"

All I need is just a person's image/video take under any normal background without user interaction like area selection or any other thing like that. 我只需要一个人在任何正常背景下拍摄的图像/视频,而无需用户交互(例如区域选择)或类似的东西。 Luminoth has trained data but that is giving box of person not exact person so that I can remove. Luminoth已训练了数据,但那是给人的盒子而不是确切的人,以便我删除。 Any help or guidance to remove background will be appreciated. 任何帮助或指导,以消除背景将不胜感激。

You could try a deep learning based approach. 您可以尝试基于深度学习的方法。 Here is an example repo: 这是一个示例存储库:

https://github.com/TianzhongSong/Person-Segmentation-Keras

This will give you a mask for a person on arbitrary background. 这将为您屏蔽任意背景的人。 Just load the images with opencv and put them through the network. 只需使用opencv加载图像并使其通过网络即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM