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() # 設置只返回前景 也就是分割出來的人像 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)文章
Python實現(xiàn)的徑向基(RBF)神經(jīng)網(wǎng)絡示例
這篇文章主要介紹了Python實現(xiàn)的徑向基(RBF)神經(jīng)網(wǎng)絡,結(jié)合完整實例形式分析了Python徑向基(RBF)神經(jīng)網(wǎng)絡定義與實現(xiàn)技巧,需要的朋友可以參考下2018-02-02python中tqdm使用,對于for和while下的兩種不同情況問題
這篇文章主要介紹了python中tqdm使用,對于for和while下的兩種不同情況問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08Python?flask?框架使用flask-login?模塊的詳細過程
Flask-Login?是一個?Flask?模塊,可以為?Flask?應用程序提供用戶登錄功能,這篇文章主要介紹了Python?flask?框架使用?flask-login?模塊,需要的朋友可以參考下2023-01-01Python創(chuàng)建二維數(shù)組與初始化的實踐舉例
二維數(shù)組使用簡便可以有很多簡潔的操作,實現(xiàn)多元的要求,下面這篇文章主要給大家介紹了關(guān)于Python創(chuàng)建二維數(shù)組與初始化的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12