h5 换脸 php,【部分原创】python实现视频内的face swap(换脸)
1.準備工作,按博主的環境為準
Python 3.5
Opencv 3
Tensorflow 1.3.1
Keras 2
cudnn和CUDA,如果你的GPU足夠厲害并且支持的話,可以選擇安裝
那就先安裝起來,有興趣的朋友給我個暗示,好讓我有動力寫下去,想實現整套的功能還是有點復雜的
第一部分,數據采集,及視頻內人物臉
import cv2
save_path = 'your save path'
cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml path')
cap = cv2.VideoCapture('your video path')
i = 0
while True:
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
rect = cascade.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=9,minSize=(50,50),flags = cv2.CASCADE_SCALE_IMAGE)
print ("rect",rect)
if not rect is ():
for x,y,z,w in rect:
roiImg = frame[y:y+w,x:x+z]
cv2.imwrite(save_path+str(i)+'.jpg',roiImg)
cv2.rectangle(frame,(x,y),(x+z,y+w),(0,0,255),2)
i +=1
cv2.imshow('frame',frame)
if cv2.waitKey(1) &0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
第二部分,國外大神開源代碼,用于模型訓練
import cv2
import numpy
from utils import get_image_paths, load_images, stack_images
from training_data import get_training_data
from model import autoencoder_A
from model import autoencoder_B
from model import encoder, decoder_A, decoder_B
try:
encoder .load_weights( "models/encoder.h5" )
decoder_A.load_weights( "models/decoder_A.h5" )
decoder_B.load_weights( "models/decoder_B.h5" )
except:
pass
def save_model_weights():
encoder .save_weights( "models/encoder.h5" )
decoder_A.save_weights( "models/decoder_A.h5" )
decoder_B.save_weights( "models/decoder_B.h5" )
print( "save model weights" )
images_A = get_image_paths( "data/trump" )
images_B = get_image_paths( "data/cage" )
images_A = load_images( images_A ) / 255.0
images_B = load_images( images_B ) / 255.0
images_A += images_B.mean( axis=(0,1,2) ) - images_A.mean( axis=(0,1,2) )
print( "press 'q' to stop training and save model" )
for epoch in range(1000000):
batch_size = 64
warped_A, target_A = get_training_data( images_A, batch_size )
warped_B, target_B = get_training_data( images_B, batch_size )
loss_A = autoencoder_A.train_on_batch( warped_A, target_A )
loss_B = autoencoder_B.train_on_batch( warped_B, target_B )
print( loss_A, loss_B )
if epoch % 100 == 0:
save_model_weights()
test_A = target_A[0:14]
test_B = target_B[0:14]
figure_A = numpy.stack([
test_A,
autoencoder_A.predict( test_A ),
autoencoder_B.predict( test_A ),
], axis=1 )
figure_B = numpy.stack([
test_B,
autoencoder_B.predict( test_B ),
autoencoder_A.predict( test_B ),
], axis=1 )
figure = numpy.concatenate( [ figure_A, figure_B ], axis=0 )
figure = figure.reshape( (4,7) + figure.shape[1:] )
figure = stack_images( figure )
figure = numpy.clip( figure * 255, 0, 255 ).astype('uint8')
cv2.imshow( "", figure )
key = cv2.waitKey(1)
if key == ord('q'):
save_model_weights()
exit()
第三部分,國外大神開源代碼,人臉輸出
import cv2
import numpy
from pathlib import Path
from utils import get_image_paths
from model import autoencoder_A
from model import autoencoder_B
from model import encoder, decoder_A, decoder_B
encoder .load_weights( "models/encoder.h5" )
decoder_A.load_weights( "models/decoder_A.h5" )
decoder_B.load_weights( "models/decoder_B.h5" )
images_A = get_image_paths( "data/trump" )
images_B = get_image_paths( "data/cage" )
def convert_one_image( autoencoder, image ):
assert image.shape == (256,256,3)
crop = slice(48,208)
face = image[crop,crop]
face = cv2.resize( face, (64,64) )
face = numpy.expand_dims( face, 0 )
new_face = autoencoder.predict( face / 255.0 )[0]
new_face = numpy.clip( new_face * 255, 0, 255 ).astype( image.dtype )
new_face = cv2.resize( new_face, (160,160) )
new_image = image.copy()
new_image[crop,crop] = new_face
return new_image
output_dir = Path( 'output' )
output_dir.mkdir( parents=True, exist_ok=True )
for fn in images_A:
image = cv2.imread(fn)
new_image = convert_one_image( autoencoder_B, image )
output_file = output_dir / Path(fn).name
cv2.imwrite( str(output_file), new_image )
第四部分,人臉替換
#import necessary libraries
import cv2
import glob as gb
# import numpy
#capture video from the webcam
cap = cv2.VideoCapture('your video path')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('your output video path', fourcc, 20.0, (1920, 1080))
#load the face finder
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml path')
#load the face that will be swapped in
img_path = gb.glob("your image path")
#start loop
for path in img_path:
face_img = cv2.imread(path)
while True:
ret, img = cap.read() # read image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 3) # find faces
# for all the faces found in the frame
for (x, y, w, h) in faces:
# resize and blend the face to be swapped in
face = cv2.resize(face_img, (h, w), interpolation=cv2.INTER_CUBIC)
face = cv2.addWeighted(img[y:y + h, x:x + w], .5, face, .5, 1)
# swap faces
img[y:y + h, x:x + w] = face
out.write(img)
# show the image
cv2.imshow('img', img)
key = cv2.waitKey(1)
if key == ord('q'):
exit()
cap.release()
cv2.destroyAllWindows()
最后放一張訓練一小時后的視頻截圖,用的是尼古拉斯凱奇的臉
【原創】python實現視頻內的face swap(換臉)
1.準備工作,按博主的環境為準 Python 3.5 Opencv 3 Tensorflow 1.3.1 Keras 2 cudnn和CUDA,如果你的GPU足夠厲害并且支持的話,可以選擇安裝 那就先 ...
[原創]使用python對視頻/音頻文件進行詳細信息采集,并進行去重操作
[原創]使用python對視頻/音頻文件進行詳細信息采集,并進行去重操作 轉載請注明出處 一.關于為什么用pymediainfo以及pymediainfo的安裝 使用python對視頻/音頻文件進行詳 ...
Python的常用內置函數介紹
Python的常用內置函數介紹 作者:尹正杰 版權聲明:原創作品,謝絕轉載!否則將追究法律責任. 一.取絕對值(abs) #!/usr/bin/env python #_*_coding:utf-8_ ...
零基礎快速掌握Python系統管理視頻課程【獵豹網校】
點擊了解更多Python課程>>> 零基礎快速掌握Python系統管理視頻課程[獵豹網校] 課程目錄 01.第01章 Python簡介.mp4 02.第02章 IPython基礎.m ...
Python學習教程(Python學習視頻_Python學些路線):Day06 函數和模塊的使用
Python學習教程(Python學習視頻_Python學些路線):函數和模塊的使用 在講解本章節的內容之前,我們先來研究一道數學題,請說出下面的方程有多少組正整數解. $$x_1 + x_2 + x ...
[Python] 將視頻轉成ASCII符號形式、生成GIF圖片
一.簡要說明 簡述:本文主要展示將視頻轉成ASCII符號形式展示出來,帶音頻. 運行環境:Win10/Python3.5. 主要模塊: PIL.numpy.shutil. [PIL]: 圖像處理 [n ...
python常用數據類型內置方法介紹
熟練掌握python常用數據類型內置方法是每個初學者必須具備的內功. 下面介紹了python常用的集中數據類型及其方法,點開源代碼,其中對主要方法都進行了中文注釋. 一.整型 a = 100 a.xx ...
python計算非內置數據類型占用內存
getsizeof的局限 python非內置數據類型的對象無法用sys.getsizeof()獲得真實的大小,例: import networkx as nx import sys G = nx.Gr ...
Python遠程視頻監控
Python遠程視頻監控程序 ? 老板由于事務繁忙無法經常親臨教研室,于是讓我搞個監控系統,讓他在辦公室就能看到教研室來了多少人.o(>﹏
隨機推薦
mysql關聯表的復制
1. 復制被參照的表: CREATE TABLE clone_product_1 LIKE product_1; INSERT INTO clone_product_1 SELECT * FROM p ...
Web API數據傳輸加密
http://www.cnblogs.com/wuhuacong/p/4620300.html Web API應用架構設計分析(2) 在上篇隨筆, ...
類型轉換輔助工具類TypeCaseHelper
package org.sakaiproject.util; import java.math.BigDecimal; import java.sql.Date; import java.sql.Ti ...
仿souhu頁面設計
仿搜狐頁面設計 Html頁面設計代碼: ...
【ASP.NET+MVC4+Web+編程】讀書筆記
模型:數據和業務邏輯 視圖:展示 控制器:接收視圖輸入數據,通過模型層業務邏輯處理后 返回給視圖 分離關注點(模型 視圖 控制器).慣例優先原則 browser-->routing-->c ...
CentOS IP DNS設置
1.CentOS 修改DNS 修改對應網卡的DNS的配置文件 # vi /etc/resolv.conf 修改以下內容 nameserver 8.8.8.8 #google域名服務器 nameserv ...
centos 下安裝 Jre 及 selenium
下載軟件包 下載鏈接: jre-7u55-linux-i586.tar.gz : http://pan.baidu.com/s/14cjds selenium-server-standalone-2. ...
前端框架Bootstrap - 快速搭建網站
Bootstrap簡介 ? ? ? ? Bootstrap是Twitter推出的一個開源的用于前端開發的工具包.是一個CSS/HTML/JavaScript框架.Bootstrap是基于HTML5和C ...
Windows Server 2016-清理殘留域控信息
本章緊接上文,當生產環境中域控出現問題無法修復以后,一方面我們需要考慮搶奪FSMO角色,另一方面我們需要考慮的問題是清理當前域控的殘留信息,以防止殘留數據信息導致用戶驗證或者解析異常等問題.本章講到如 ...
總結
以上是生活随笔為你收集整理的h5 换脸 php,【部分原创】python实现视频内的face swap(换脸)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu mysql5.7配置_ub
- 下一篇: 动态规划算法php,php算法学习之动态