计算机视觉人像抠图 SemanticSegmentation 2022-09-28 Code PV: 人像动态抠图 实现视频背景替换 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import cv2import cvzonefrom cvzone.SelfiSegmentationModule import SelfiSegmentationimport oscap = cv2.VideoCapture(0)cap.set(3, 1280)cap.set(4, 720)cap.set(cv2.CAP_PROP_FPS, 30)segmentor = SelfiSegmentation()fpsReader = cvzone.FPS()saveVideoPath = 'output.avi'fourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWriter(saveVideoPath, fourcc, 30.0, (2560, 720))ListImg = os.listdir("Images")imgList = []for imgPath in ListImg: img = cv2.imread(f'Images/{imgPath}') imgList.append(img)indexImg = 0while True: success, img = cap.read() if success == True: imgOut = segmentor.removeBG(img, imgList[indexImg], threshold=0.80) imgStacked = cvzone.stackImages([img, imgOut], 2, 1) _, imgStacked = fpsReader.update(imgStacked, color=(0, 255, 0)) out.write(imgStacked) cv2.imshow("Image", imgStacked) key = cv2.waitKey(1) if key == ord('a'): if indexImg > 0: indexImg -= 1 else: indexImg = len(imgList)-1 elif key == ord('d'): if indexImg < len(imgList)-1: indexImg += 1 else: indexImg = 0 elif key == ord('q'): break# Release everything if job is finishedcap.release()out.release()cv2.destroyAllWindows()