wear용 코데 이미지의 얼굴 숨김 작업을 자동화해 보았다
WEAR는 패션 코디네이터 사이트로 당사 서비스의 하나입니다.
요컨대 각자의 패션 코디네이터를 공유하는 SNS입니다.
그런 WEAR 중에서 이유는 잘 모르겠지만 얼굴을 아이콘 등으로 숨기고 투고하는 것이 유행하고있는 것 같습니다.
얼굴이 없는 쪽이 코디네이트를 객관적으로 볼 수 있으니까요?
다만, 얼굴 숨기는 작업은 의외로 귀찮게, 이것 자동화할 수 있으면 좋겠다...라고 생각해, 아이콘 자동 배치의 프로그램을 구현해 보았습니다.
소스 코드
import os.path
import datetime
import cv2
import time
# 定数
# dataフォルダ
DATA_PATH = "data"
# resultフォルダ
RESULT_PATH = "result"
# カスケードパス
CASCADE_PATH = "haarcascade_frontalface_default.xml"
def main():
    # カレントディレクトリを取得
    current_path = os.getcwd()
    icon_image_path = os.path.join(current_path, "icon.png")
    icon_image = cv2.imread(cv2.samples.findFile(icon_image_path))
    # dataディレクトリを取得
    data_path = os.path.join(current_path, DATA_PATH)
    # dataディレクトリを取得
    result_path = os.path.join(current_path, RESULT_PATH)
    # ディレクトリ直下のファイル一覧を取得
    data_list = os.listdir(data_path)
    for file in data_list:
        # 処理時間計測タイマースタート
        start = time.time()
        # ファイルの拡張子を取得
        file_name, ext = os.path.splitext(file)
        # 拡張子がpng、jpegの場合
        if ext == u'.png' or ext == u'.jpg' or ext == u'.jpeg':
            # 画像を読み込む
            input_path = os.path.join(data_path, file)
            # 入力画像格納
            image = cv2.imread(cv2.samples.findFile(input_path))
            # グレースケール変換
            image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            # カスケード分類器の特徴量を取得する
            cascade = cv2.CascadeClassifier(CASCADE_PATH)
            face_rect = cascade.detectMultiScale(image_gray, 1.1, 3, 5)
            if 0 < len(face_rect):
                print("Face discovery")
                for x, y, w, h in face_rect:
                    # 顔を隠す
                    image = put_icon(image, (x, y, x + w, y + h), icon_image)
            else:
                print("Face not found")
            output_path = os.path.join(result_path, create_time_path(file_name, ".png"))
            # 画像を保存
            cv2.imwrite(output_path, image)
            # 処理時間計測タイマーストップ
            t = time.time() - start
            print(output_path, ":", t)
# 時刻込みのユニークのファイルパスを出力する
def create_time_path(file_name, ext):
    # 現在時刻を取得
    time = datetime.datetime.now()
    # パスを作成
    path = file_name + time.strftime("%Y%m%d%H%M%S") + ext
    return path
def put_icon(img, rect, icon_image):
    # アイコンを被せる領域を取得
    x1, y1, x2, y2 = rect
    w = x2 - x1
    h = y2 - y1
    # アイコンを画像に重ねる
    img2 = img.copy()
    icon_image = cv2.resize(icon_image, (w, h), cv2.INTER_AREA)
    img2[y1:y2, x1:x2] = icon_image
    return img2
if __name__ == '__main__':
    main()
 실행 결과
 
원화상의 코데는 이런 느낌.
(활성화 된 사진 사용 ...)
 
실행 후 멋지게 아이콘이 맞습니다! ! !
번거로웠던 작업이 순식간에! !
아이콘 이미지가 적당하다는 것을 걱정하지 마십시오 ...
 마지막으로
얼굴 인증의 정밀도가 그렇게 좋은 것은 아니기 때문에, 사진에 따라서는 몇개나 아이콘이 세트 되어 버리므로, 가볍게 사용해 보는 정도가 좋을지도 모릅니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(wear용 코데 이미지의 얼굴 숨김 작업을 자동화해 보았다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/ikeponsu/items/dab3b4a0ce47190c2659
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
import os.path
import datetime
import cv2
import time
# 定数
# dataフォルダ
DATA_PATH = "data"
# resultフォルダ
RESULT_PATH = "result"
# カスケードパス
CASCADE_PATH = "haarcascade_frontalface_default.xml"
def main():
    # カレントディレクトリを取得
    current_path = os.getcwd()
    icon_image_path = os.path.join(current_path, "icon.png")
    icon_image = cv2.imread(cv2.samples.findFile(icon_image_path))
    # dataディレクトリを取得
    data_path = os.path.join(current_path, DATA_PATH)
    # dataディレクトリを取得
    result_path = os.path.join(current_path, RESULT_PATH)
    # ディレクトリ直下のファイル一覧を取得
    data_list = os.listdir(data_path)
    for file in data_list:
        # 処理時間計測タイマースタート
        start = time.time()
        # ファイルの拡張子を取得
        file_name, ext = os.path.splitext(file)
        # 拡張子がpng、jpegの場合
        if ext == u'.png' or ext == u'.jpg' or ext == u'.jpeg':
            # 画像を読み込む
            input_path = os.path.join(data_path, file)
            # 入力画像格納
            image = cv2.imread(cv2.samples.findFile(input_path))
            # グレースケール変換
            image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            # カスケード分類器の特徴量を取得する
            cascade = cv2.CascadeClassifier(CASCADE_PATH)
            face_rect = cascade.detectMultiScale(image_gray, 1.1, 3, 5)
            if 0 < len(face_rect):
                print("Face discovery")
                for x, y, w, h in face_rect:
                    # 顔を隠す
                    image = put_icon(image, (x, y, x + w, y + h), icon_image)
            else:
                print("Face not found")
            output_path = os.path.join(result_path, create_time_path(file_name, ".png"))
            # 画像を保存
            cv2.imwrite(output_path, image)
            # 処理時間計測タイマーストップ
            t = time.time() - start
            print(output_path, ":", t)
# 時刻込みのユニークのファイルパスを出力する
def create_time_path(file_name, ext):
    # 現在時刻を取得
    time = datetime.datetime.now()
    # パスを作成
    path = file_name + time.strftime("%Y%m%d%H%M%S") + ext
    return path
def put_icon(img, rect, icon_image):
    # アイコンを被せる領域を取得
    x1, y1, x2, y2 = rect
    w = x2 - x1
    h = y2 - y1
    # アイコンを画像に重ねる
    img2 = img.copy()
    icon_image = cv2.resize(icon_image, (w, h), cv2.INTER_AREA)
    img2[y1:y2, x1:x2] = icon_image
    return img2
if __name__ == '__main__':
    main()

원화상의 코데는 이런 느낌.
(활성화 된 사진 사용 ...)

실행 후 멋지게 아이콘이 맞습니다! ! !
번거로웠던 작업이 순식간에! !
아이콘 이미지가 적당하다는 것을 걱정하지 마십시오 ...
마지막으로
얼굴 인증의 정밀도가 그렇게 좋은 것은 아니기 때문에, 사진에 따라서는 몇개나 아이콘이 세트 되어 버리므로, 가볍게 사용해 보는 정도가 좋을지도 모릅니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(wear용 코데 이미지의 얼굴 숨김 작업을 자동화해 보았다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/ikeponsu/items/dab3b4a0ce47190c2659
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Reference
이 문제에 관하여(wear용 코데 이미지의 얼굴 숨김 작업을 자동화해 보았다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ikeponsu/items/dab3b4a0ce47190c2659텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)