基于python的视频或者图片更换背景
2021/7/15 1:04:57
本文主要是介绍基于python的视频或者图片更换背景,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在windows 10 下使用cmd执行以下命令:
pip install opencv-python
pip install cvzone
pip install mediapipe
安装以上python库后,可以使用以下的例程:
import cv2 import cvzone from cvzone.SelfiSegmentationModule import SelfiSegmentation import os cap = cv2.VideoCapture(0) cap.set(3,640) cap.set(4,480) segmentor = SelfiSegmentation() # imgBG = cv2.imread("images/1.jpg") #for Single Image #for multiple img listImg = os.listdir("images") print(listImg) imgList = [] for imgpath in listImg: img = cv2.imread(f'images/{imgpath}') imgList.append(img) indexImg = 0 while True: success,img = cap.read() # imgOut = segmentor.removeBG(img, imgBG, threshold=0.8) #for single img imgOut = segmentor.removeBG(img,imgList[indexImg],threshold=0.8) #for Multi Image imgstack = cvzone.stackImages([img,imgOut],2,1) cv2.imshow("image",imgstack) key = cv2.waitKey(1) if key == ord('a'): if indexImg > 0: indexImg -= 1 elif key == ord('d'): if indexImg < len(imgList)-1: indexImg += 1 elif key == ord('q'): break
这篇关于基于python的视频或者图片更换背景的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-27使用python 将ETH账户的资产打散
- 2024-09-26Python编程基础
- 2024-09-2610 种方法写出更好的 Python 代码
- 2024-09-25Python编程基础详解
- 2024-09-25Python编程入门教程
- 2024-09-25从零开始使用Python构建LLaMA 3
- 2024-09-23Python中理解和使用树形结构的简单教程
- 2024-09-23Python 编程基础入门
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战