3703浏览
查看: 3703|回复: 5

【行空板】实时背景替换

[复制链接]
本帖最后由 云天 于 2022-5-11 09:26 编辑

【行空板】实时背景替换图1

由于许多原因,视频的背景需要修改,如背景中有很多其他中断或背景颜色不适合该人。因此,我们使用实时背景替换技术来替换背景并添加替换为所需内容。
流行的背景去除技术
图像剪切路径 - 如果图像的主题具有锐利的边缘,则使用此技术。所有落在路径之外的元素都将被消除。

图像剪切 – 在这里,我们剪切帧中所需的区域或主题并删除背景。

图像遮罩 – 如果图像有褶边或细边缘,我们可以使用图像遮罩技术。

擦除背景 – 使用任何不同的工具擦除图像的背景

许多著名的应用程序使用背景去除技术并用自定义技术替换它。在这里,我们将实现类似的东西,使用 OpenCV 和 MediaPipe 。


MediaPipe 库可以让我们轻松运行像人脸检测、手部跟踪、姿势估计等,以及图像处理和其他 AI 功能。
【安装MediaPipe 库】
安装Mediapipe库与OpenCV库,参考AI人工智能应用:https://wiki.unihiker.com/ai_project
【行空板】实时背景替换图4

【单背景测试】
  1. import cv2
  2. import mediapipe as mp
  3. import numpy as np
  4. mp_drawing = mp.solutions.drawing_utils
  5. mp_selfie_segmentation = mp.solutions.selfie_segmentation
  6. # For webcam input:
  7. BG_COLOR = (192, 192, 192) # gray
  8. cap = cv2.VideoCapture(0)
  9. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
  10. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
  11. cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
  12. cv2.namedWindow('MediaPipe Selfie Segmentation',cv2.WND_PROP_FULLSCREEN)    #Set the windows to be full screen.
  13. cv2.setWindowProperty('MediaPipe Selfie Segmentation', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)    #Set the windows to be full screen.
  14. with mp_selfie_segmentation.SelfieSegmentation(
  15.     model_selection=1) as selfie_segmentation:
  16.   bg_image = cv2.imread("back.png")
  17.   while cap.isOpened():
  18.     success, image = cap.read()
  19.     if not success:
  20.       print("Ignoring empty camera frame.")
  21.       # If loading a video, use 'break' instead of 'continue'.
  22.       continue
  23.     # Flip the image horizontally for a later selfie-view display, and convert
  24.     # the BGR image to RGB.
  25.     image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
  26.     # To improve performance, optionally mark the image as not writeable to
  27.     # pass by reference.
  28.     image.flags.writeable = False
  29.     results = selfie_segmentation.process(image)
  30.     image.flags.writeable = True
  31.     image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  32.     # Draw selfie segmentation on the background image.
  33.     # To improve segmentation around boundaries, consider applying a joint
  34.     # bilateral filter to "results.segmentation_mask" with "image".
  35.     condition = np.stack(
  36.       (results.segmentation_mask,) * 3, axis=-1) > 0.1
  37.     # The background can be customized.
  38.     #   a) Load an image (with the same width and height of the input image) to
  39.     #      be the background, e.g., bg_image = cv2.imread('/path/to/image/file')
  40.     #   b) Blur the input image by applying image filtering, e.g.,
  41.     #      bg_image = cv2.GaussianBlur(image,(55,55),0)
  42.     if bg_image is None:
  43.       bg_image = np.zeros(image.shape, dtype=np.uint8)
  44.       bg_image[:] = BG_COLOR
  45.    
  46.     output_image = np.where(condition, image, bg_image)
  47.     output_image = cv2.rotate(output_image, cv2.ROTATE_90_COUNTERCLOCKWISE)
  48.     cv2.imshow('MediaPipe Selfie Segmentation', output_image)
  49.     if cv2.waitKey(5) & 0xFF == 27:
  50.       break
  51. cap.release()
复制代码
【行空板】实时背景替换图2

【按键A、B测试】

  1. from pinpong.extension.unihiker import *
  2. from pinpong.board import Board,Pin
  3. Board().begin()
  4. while True:
  5.   if (button_a.is_pressed()==True):
  6.     buzzer.play(buzzer.DADADADUM,buzzer.Once)
  7.   if (button_b.is_pressed()==True):
  8.     buzzer.play(buzzer.ENTERTAINER,buzzer.Once)
复制代码
【准备多背景】现在在这里创建项目目录中的文件夹,我正在创建一个名为*'BackgroundImages'*的文件夹。你可以下载任何图像或任意数量的图像并将它们放在此目录中。
【行空板】实时背景替换图3

【完整程序】

这里我们将获得背景替换图像或帧的输出。然后使用一个简单的 if 语句,分配键来更改背景。

例如,如果我们有 5 张背景图像,根据上面的代码,我们可以使用键“A”或键“B”来更改帧的背景。


  1. import cv2
  2. import mediapipe as mp
  3. import numpy as np
  4. import os
  5. from pinpong.extension.unihiker import *
  6. from pinpong.board import Board,Pin
  7. Board().begin()
  8. mp_drawing = mp.solutions.drawing_utils
  9. mp_selfie_segmentation = mp.solutions.selfie_segmentation
  10. # For webcam input:
  11. BG_COLOR = (192, 192, 192) # gray
  12. cap = cv2.VideoCapture(0)
  13. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
  14. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
  15. cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
  16. cv2.namedWindow('MediaPipe Selfie Segmentation',cv2.WND_PROP_FULLSCREEN)    #Set the windows to be full screen.
  17. cv2.setWindowProperty('MediaPipe Selfie Segmentation', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)    #Set the windows to be full screen.
  18. listImg = os.listdir("./BackgroundImages")
  19. imgList = []
  20. for imgPath in listImg:
  21.     img = cv2.imread(f'BackgroundImages/{imgPath}')
  22.     imgList.append(img)
  23. indexImg = 0
  24. with mp_selfie_segmentation.SelfieSegmentation(
  25.     model_selection=1) as selfie_segmentation:
  26.   
  27.   while cap.isOpened():
  28.     success, image = cap.read()
  29.     if not success:
  30.       print("Ignoring empty camera frame.")
  31.       # If loading a video, use 'break' instead of 'continue'.
  32.       continue
  33.     # Flip the image horizontally for a later selfie-view display, and convert
  34.     # the BGR image to RGB.
  35.     image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
  36.     # To improve performance, optionally mark the image as not writeable to
  37.     # pass by reference.
  38.     image.flags.writeable = False
  39.     results = selfie_segmentation.process(image)
  40.     image.flags.writeable = True
  41.     image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  42.     # Draw selfie segmentation on the background image.
  43.     # To improve segmentation around boundaries, consider applying a joint
  44.     # bilateral filter to "results.segmentation_mask" with "image".
  45.     condition = np.stack(
  46.       (results.segmentation_mask,) * 3, axis=-1) > 0.1
  47.     # The background can be customized.
  48.     #   a) Load an image (with the same width and height of the input image) to
  49.     #      be the background, e.g., bg_image = cv2.imread('/path/to/image/file')
  50.     #   b) Blur the input image by applying image filtering, e.g.,
  51.     #      bg_image = cv2.GaussianBlur(image,(55,55),0)
  52.     if (button_a.is_pressed()==True):
  53.       if indexImg>0:
  54.             indexImg -=1
  55.     if (button_b.is_pressed()==True):
  56.       if indexImg<len(imgList)-1:
  57.             indexImg +=1
  58.    
  59.     output_image = np.where(condition, image, imgList[indexImg])
  60.     output_image = cv2.rotate(output_image, cv2.ROTATE_90_COUNTERCLOCKWISE)
  61.     cv2.imshow('MediaPipe Selfie Segmentation', output_image)
  62.     if cv2.waitKey(5) & 0xFF == 27:
  63.       break
  64. cap.release()
复制代码
【行空板】实时背景替换图5


【演示视频】


shzrzxlee  高级技匠

发表于 2022-5-11 14:17:58

mind+1.7.2 RC2.0
回复

使用道具 举报

云天  初级技神
 楼主|

发表于 2022-5-11 20:05:40


行空板专用测试版
回复

使用道具 举报

rzegkly  版主

发表于 2022-5-13 08:04:10

很精彩
回复

使用道具 举报

盐焗海盐  见习技师

发表于 2022-5-21 11:49:28

太实用了!
回复

使用道具 举报

 初级技匠

发表于 2022-5-21 12:52:41

虚拟背景?我喜欢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail