计算机视觉人像抠图 SemanticSegmentation

人像动态抠图 实现视频背景替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import cv2
import cvzone
from cvzone.SelfiSegmentationModule import SelfiSegmentation
import os


cap = 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 = 0


while 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 finished
cap.release()
out.release()
cv2.destroyAllWindows()

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2024 HELLO WORLD All Rights Reserved.

UV : | PV :