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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

人脸图像切割分离工具

發布時間:2024/3/13 pytorch 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 人脸图像切割分离工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在做人臉識別,需要對人臉數據集進行處理,對一張或批量圖像的人臉進行切割分離,并且另保存下來。受網上開源工具的啟發,在借鑒他人的基礎上進行了改進,使得更加方便實用。

以下代碼是改進版,分為兩部分功能:

  • 一張人臉圖片切割并顯示,不保存
  • 一張/批量人臉圖像切割并保存
  • 在這里需要申明一下,尊重原著!(轉發需要標注一下原著信息)

    原創版:

    ? Author: ? ?coneypo
    ? Blog: ? ? ? http://www.cnblogs.com/AdaminXie
    ? GitHub: ? https://github.com/coneypo/Dlib_face_cut

    改進版:

    Improver: ?Cai_90hou

    Blog: ? ? ? ??https://blog.csdn.net/qq_38677310/article/details/84702662

    Github: ? ? ?

    #crop_faces_show.pyimport dlib # 人臉識別的庫dlib import numpy as np # 數據處理的庫numpy import cv2 # 圖像處理的庫OpenCv# Dlib 正向人臉檢測器 detector = dlib.get_frontal_face_detector()# 讀取圖像 path = "faces_for_test/"img = cv2.imread(path+"test_faces_1.jpg")# Dlib 檢測 dets = detector(img, 1)print("檢測到的人臉數 / faces :", len(dets), "\n")# 記錄人臉矩陣大小 height_max = 0 width_sum = 0# 計算要生成的圖像 img_blank 大小 for k, d in enumerate(dets):# 計算矩形大小# (x,y), (寬度width, 高度height)pos_start = tuple([d.left(), d.top()])pos_end = tuple([d.right(), d.bottom()])# 計算矩形框大小height = d.bottom()-d.top()width = d.right()-d.left()# 處理寬度width_sum += width# 處理高度if height > height_max:height_max = heightelse:height_max = height_max# 繪制用來顯示人臉的圖像的大小 print("窗口大小:", '\n', "高度 / height :", height_max, '\n', "寬度 / width : ", width_sum)# 生成用來顯示的圖像 img_blank = np.zeros((height_max, width_sum, 3), np.uint8)# 記錄每次開始寫入人臉像素的寬度位置 blank_start = 0# 將人臉填充到img_blank for k, d in enumerate(dets):height = d.bottom()-d.top()width = d.right()-d.left()# 填充for i in range(height):for j in range(width):img_blank[i][blank_start+j] = img[d.top()+i][d.left()+j]# 調整圖像blank_start += widthcv2.namedWindow("img_faces", 0) cv2.imshow("img_faces", img_blank) cv2.waitKey(0) #crop_faces_save.pyimport dlib # 人臉識別的庫dlib import numpy as np # 數據處理的庫numpy import cv2 # 圖像處理的庫OpenCv import os#切割后的人臉序號,為全局變量 image_num = 0# Dlib 正向人臉檢測器 detector = dlib.get_frontal_face_detector()# 讀取圖像的路徑 path_read = "faces_for_test/" #img = cv2.imread(path_read+"test_faces_3.jpg")# 用來存儲生成的單張人臉的路徑 path_save = "faces_separated/"#將文件夾中待處理的圖片存于列表中 imgs_read = os.listdir(path_read) #源地址文件夾 imgs_write = os.listdir(path_save) #存儲地址文件夾# Delete old images def clear_images():for img in imgs_write:os.remove(path_save + img)print("clean finish", '\n')#處理一張圖片: def cut_one_photo(img):global image_num# Dlib 檢測faces = detector(img, 1)print("人臉數:", len(faces))for k, d in enumerate(faces):# 計算矩形大小# (x,y), (寬度width, 高度height)pos_start = tuple([d.left(), d.top()])pos_end = tuple([d.right(), d.bottom()])# 計算矩形框大小height = d.bottom()-d.top()width = d.right()-d.left()# 根據人臉大小生成空的圖像img_blank = np.zeros((height, width, 3), np.uint8)#復制人臉for i in range(height):for j in range(width):img_blank[i][j] = img[d.top()+i][d.left()+j]# cv2.imshow("face_"+str(k+1), img_blank)# 保存在本地#print("Save to:", path_save+"CWH"+str(image_num+1)+".jpg")print("Save to:", "img_face" + str(image_num + 1) + ".jpg")cv2.imwrite(path_save + "img_face" + str(image_num + 1) + ".jpg", img_blank)image_num += 1#批量處理圖片 def cut_batch_photoes():clear_images() #清除原有圖片for img in imgs_read:try:path = os.path.join(path_read, img)image = cv2.imread(path)cut_one_photo(image)print('\n')except:continueprint('Successful operation!\n')print('total number of faces: ', image_num)def main():# 批量處理圖片并保存cut_batch_photoes()if __name__ == '__main__':main()

    ?

    總結

    以上是生活随笔為你收集整理的人脸图像切割分离工具的全部內容,希望文章能夠幫你解決所遇到的問題。

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