Python調(diào)用百度AI實現(xiàn)人像分割詳解
更新時間:2021年12月21日 08:54:47 作者:學術(shù)菜鳥小晨
本文主要介紹了如何通過Python調(diào)用百度AI從而實現(xiàn)人像的分割與合成,文中的示例代碼對我們的工作或?qū)W習有一定的幫助,需要的朋友可以參考一下
一、原始視頻截圖
import cv2
cap=cv2.VideoCapture(r"【小仙若】shake it !冬日也要活力滿滿! (P1. shake it).mp4")
ret,frame=cap.read()
i =0
timeF=3
j=0
num=0
while 1:
i=i+1
if (i%timeF==0):
j=j+1
cv2.imwrite("./pictures/"+str(num)+".jpg",frame)
num+=1
print("save image:",i)
ret,frame=cap.read()


二、提取人像
# -*- coding:utf-8 -*-
import cv2
import base64
import numpy as np
import os
from aip import AipBodyAnalysis
import time
import random
APP_ID = '25365416'
API_KEY = 'pS5cVzzw2iBfLY6MKRhUE4cw'
SECRET_KEY = '×××××××××××××××××××××××××'
client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
# 保存圖像分割后的路徑
path = './mask_img/'
# os.listdir 列出保存到圖片名稱
pics = os.listdir('./pictures')
print(pics)
for im in pics:
# 按順序構(gòu)造出圖片路徑
img = os.path.join("./pictures",im)
img1 = cv2.imread(img)
height, width, _ = img1.shape
# print(height, width)
# 二進制方式讀取圖片
with open(img, 'rb') as fp:
img_info = fp.read()
# 設(shè)置只返回前景 也就是分割出來的人像
res = client.bodySeg(img_info)
seg_res = client.bodySeg(img_info)
labelmap = base64.b64decode(seg_res['foreground'])
file = open('./he/'+im.split(".")[0]+'.png','wb')
file.write(labelmap)
file.close()
print('======== 圖像分割完成 ========')


三、和背景圖合并
import cv2
import os
from PIL import Image
import numpy as np
background='1.jpg'
def blend_images(fore_image, base_image):
"""
將摳出的人物圖像換背景
fore_image: 前景圖片,摳出的人物圖片
base_image: 背景圖片
"""
# 讀入圖片
base_image = Image.open(base_image).convert('RGB')
fore_image = Image.open(fore_image).resize(base_image.size)
# 圖片加權(quán)合成
scope_map = np.array(fore_image)[:,:,-1] / 255
scope_map = scope_map[:,:,np.newaxis]
scope_map = np.repeat(scope_map, repeats=3, axis=2)
res_image = np.multiply(scope_map, np.array(fore_image)[:,:,:3]) + np.multiply((1-scope_map), np.array(base_image))
#保存圖片
res_image = Image.fromarray(np.uint8(res_image))
res_image.save(os.path.join('./he/',im))
#cv2.imwrite(os.path.join(path1,im), result)
# os.listdir 列出保存到圖片名稱
pics = os.listdir('./he1/')
print(pics)
for im in pics:
img='./he1/'+im
blend_images(img, background)


四、合成視頻
我的背景圖尺寸是3840×2160
# 圖片合成視頻
import cv2
import os
pics = os.listdir('./he/')
fourcc = cv2.VideoWriter_fourcc('X','V','I','D')
# 保存格式,參數(shù)分別為filename,編碼器,幀率,尺寸
out=cv2.VideoWriter("2.avi",fourcc,10,(3840,2160))
print(pics)
for im in pics:
# 按順序構(gòu)造出圖片路徑
img = os.path.join("./he/",im)
img1 = cv2.imread(img)
# 指定編碼器
print(img1)
# 寫入視頻
out.write(img1)
cv2.imshow("detections", img1)
# 注意:尺寸一定要和圖像保持一致,否則看不了視頻
# 如果想改變保存視頻尺寸,應該先把讀入的圖像的尺寸改變
out.release()
cv2.destoryAllWindows()
到此這篇關(guān)于Python調(diào)用百度AI實現(xiàn)人像分割詳解的文章就介紹到這了,更多相關(guān)Python人像分割內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Tensorflow2.4從頭訓練Word?Embedding實現(xiàn)文本分類
這篇文章主要為大家介紹了Tensorflow2.4從頭訓練Word?Embedding實現(xiàn)文本分類,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
python?reshape和transpose的區(qū)別小結(jié)
reshape()?和?transpose()?是用于改變數(shù)組或張量形狀的兩種不同方法,本文主要介紹了python?reshape和transpose的區(qū)別小結(jié),具有一定參考價值,感興趣的可以了解一下2024-02-02
selenium執(zhí)行js并繞過webdriver監(jiān)測常見方法
這篇文章主要為大家介紹了selenium執(zhí)行js并繞過webdriver監(jiān)測常見方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04

