日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PaddleHub创意项目-制作证件照(抠图换底美颜)

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PaddleHub创意项目-制作证件照(抠图换底美颜) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PaddleHub創意項目-制作證件照(摳圖換底美顏)

經過三個星期的百度架構師手把手帶你零基礎實踐深度學習,對paddle有了一定了解,現在利用PaddleHub實踐一個對圖像進行摳圖換底加美顏的小項目。

1、安裝python 3.6+;
2、安裝jupyter notebook;
3、安裝paddle 1.8.0;
前三步不清楚的可以百度一下!

4、啟動jupyter notebook,添加如下代碼并執行;

#下載摳圖模型 !hub install deeplabv3p_xception65_humanseg==1.1.1 #調用一些相關的包 import paddlehub as hub import matplotlib.pyplot as plt import matplotlib.image as mpimg import cv2 from PIL import Image import numpy as np import math # 展示處理前的圖片 img = mpimg.imread("data/obm.jpg") #修改為自己要處理的圖片名 plt.figure(figsize=(8,8)) plt.imshow(img) plt.axis('off') plt.show() # 開始摳圖 # 指定模型名稱、待預測的圖片路徑、輸出結果的路徑,執行并輸出預測結果 module = hub.Module(name="deeplabv3p_xception65_humanseg") res = module.segmentation(paths = ["data/obm.jpg"], visualization=True, output_dir='data/') # 展示摳圖后的圖片 img = mpimg.imread("data/obm.png") plt.figure(figsize=(8,8)) plt.imshow(img) plt.axis('off') plt.show()

使用圖像加權合成的方法進行圖像換底 ,參照https://aistudio.baidu.com/aistudio/projectdetail/509443

#圖像背景和前景融合------換藍底 fore_image = Image.open(f'data/obm.png') #按照前景尺寸來改變背景的尺寸 base_image = Image.open(f'data/base/blue.jpg').convert('RGB').resize(fore_image.size) # 圖片加權合成 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(f'data/obm_blue.jpg') plt.figure(figsize=(8,8)) plt.imshow(res_image) plt.axis('off') plt.show() #圖像背景和前景融合------換紅底 fore_image = Image.open(f'data/obm.png') base_image = Image.open(f'data//base/red.jpg').convert('RGB').resize(fore_image.size)#按照前景尺寸來改變背景的尺寸 # 圖片加權合成 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(f'data/obm_red.jpg') plt.figure(figsize=(8,8)) plt.imshow(res_image) plt.axis('off') plt.show() #下載人臉關鍵點檢測模型 !hub install face_landmark_localization==1.0.2 #人臉關鍵點檢測 face_landmark = hub.Module(name="face_landmark_localization") result_point = face_landmark.keypoint_detection(images=[cv2.imread('data/obm_blue.jpg')])src_img = cv2.imread('data/obm_blue.jpg') tmp_img = src_img.copy() for index, point in enumerate(result_point[0]['data'][0]):# cv2.putText(img, str(index), (int(point[0]), int(point[1])), cv2.FONT_HERSHEY_COMPLEX, 3, (0,0,255), -1)cv2.circle(tmp_img, (int(point[0]), int(point[1])), 2, (0, 0, 255), -1)res_img_path = 'data/obm_point.jpg' cv2.imwrite(res_img_path, tmp_img) #關鍵點檢測圖像 img_point = mpimg.imread(f'data/obm_point.jpg') plt.figure(figsize=(10,10)) plt.imshow(img_point) plt.axis('off') plt.show()

實現美顏方法 瘦臉 首先介紹如何利用識別到的68個關鍵點完成瘦臉功能。 利用其中3號點到5號點距離作為瘦左臉距離,13號點到15號點距離作為瘦右臉距離(有點問題)。 同時利用局部平移算法完成瘦臉,參考:https://aistudio.baidu.com/aistudio/projectdetail/705875

def thin_face(image, face_landmark):"""實現自動人像瘦臉image: 人像圖片face_landmark: 人臉關鍵點"""end_point = face_landmark[30]# 瘦左臉,3號點到5號點的距離作為瘦臉距離dist_left = np.linalg.norm(face_landmark[3] - face_landmark[5])image = local_traslation_warp(image, face_landmark[3], end_point, dist_left)# 瘦右臉,7號點到9號點的距離作為瘦臉距離(搞不懂)dist_right = np.linalg.norm(face_landmark[7] - face_landmark[9])image = local_traslation_warp(image, face_landmark[7], end_point, dist_right)#dist_right = np.linalg.norm(face_landmark[13] - face_landmark[15])#image = local_traslation_warp(image, face_landmark[13], end_point, dist_right)return image def local_traslation_warp(image, start_point, end_point, radius):"""局部平移算法"""radius_square = math.pow(radius, 2)image_cp = image.copy()dist_se = math.pow(np.linalg.norm(end_point - start_point), 2)height, width, channel = image.shapefor i in range(width):for j in range(height):# 計算該點是否在形變圓的范圍之內# 優化,第一步,直接判斷是會在(start_point[0], start_point[1])的矩陣框中if math.fabs(i - start_point[0]) > radius and math.fabs(j - start_point[1]) > radius:continuedistance = (i - start_point[0]) * (i - start_point[0]) + (j - start_point[1]) * (j - start_point[1])if (distance < radius_square):# 計算出(i,j)坐標的原坐標# 計算公式中右邊平方號里的部分ratio = (radius_square - distance) / (radius_square - distance + dist_se)ratio = ratio * ratio# 映射原位置new_x = i - ratio * (end_point[0] - start_point[0])new_y = j - ratio * (end_point[1] - start_point[1])new_x = new_x if new_x >=0 else 0new_x = new_x if new_x < height-1 else height-2new_y = new_y if new_y >= 0 else 0new_y = new_y if new_y < width-1 else width-2# 根據雙線性插值法得到new_x, new_y的值image_cp[j, i] = bilinear_insert(image, new_x, new_y)return image_cpdef bilinear_insert(image, new_x, new_y):"""雙線性插值法"""w, h, c = image.shapeif c == 3:x1 = int(new_x)x2 = x1 + 1y1 = int(new_y)y2 = y1 + 1part1 = image[y1, x1].astype(np.float) * (float(x2) - new_x) * (float(y2) - new_y)part2 = image[y1, x2].astype(np.float) * (new_x - float(x1)) * (float(y2) - new_y)part3 = image[y2, x1].astype(np.float) * (float(x2) - new_x) * (new_y - float(y1))part4 = image[y2, x2].astype(np.float) * (new_x - float(x1)) * (new_y - float(y1))insertValue = part1 + part2 + part3 + part4return insertValue.astype(np.int8) #美顏瘦臉 face_landmark = np.array(result_point[0]['data'][0], dtype='int') src_img = thin_face(src_img, face_landmark)res_img_path = 'data/obm_res.jpg' cv2.imwrite(res_img_path, src_img)img = mpimg.imread(res_img_path) # 展示瘦臉圖片 plt.figure(figsize=(10,10)) plt.imshow(img) plt.axis('off') plt.show()

瘦臉效果不明顯,技術有待提升,新手,勿怪!
總之經過三周的學習,感覺還是收獲滿滿。
具體可見:https://aistudio.baidu.com/aistudio/projectdetail/780408

總結

以上是生活随笔為你收集整理的PaddleHub创意项目-制作证件照(抠图换底美颜)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。