iPython code在此
print(cv2.__version__)
print(mp.__version__)
#背景圖設定,cap設定
#shape = (720, 1280, 3) # y, x , RGB
# 第一種方法,直接建立圖片
#bg = np.zeros((720, 1280, 3), dtype=np.uint8)
#bg[:, :] = [10,255, 120] #BGR :0,1,2
bg=cv2.imread('hohuan.jpg')
bg=cv2.resize(bg,(1280, 720))
cap = cv2.VideoCapture(0)#('GX017986.MP4')
cap.set(3, 1280)
cap.set(4, 720)
#使用mp套件
mp_draw=mp.solutions.drawing_utils
mp_seg=mp.solutions.selfie_segmentation
#設seg
seg=mp.solutions.selfie_segmentation.SelfieSegmentation(model_selection=1)
#while loop
while cap.isOpened():
success, im = cap.read()
if not success:
break
im=cv2.resize(im, (1280, 720))
im2=cv2.cvtColor(im, cv2.COLOR_BGRA2RGB)
result=seg.process(im2)
#Numpy的tile()函數,就是將原矩陣橫向、縱向地複製
mask=np.tile(result.segmentation_mask[:,:, np.newaxis], (1,1,3))>0.1
grab=np.where(mask, im, bg)
cv2.imshow('grab', grab)
if cv2.waitKey(1) >0:
break # 按鍵停止
seg.close()
cap.release()
cv2.destroyAllWindows()
np.newaxis用法
x = np.array([10, 20, 30], float) x2 = x[:, np.newaxis] print(x2) ---result--- [[ 10.] [ 20.] [ 30.]]
沒有留言:
張貼留言