顔を認識したらメールを読み上げる
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import fitz from playsound import playsound import subprocess import pygame import time # テキストから音声を生成して再生する関数 def generate_and_play_audio_from_text(text): command_json = [ "curl" , "-s" , "-X" , "POST" , "192.168.1.69:50021/audio_query?speaker=1" , "--get" , "--data-urlencode" , f "text={text}" ] command_audio = [ "curl" , "-s" , "-H" , "Content-Type: application/json" , "-X" , "POST" , "-d" , "@query.json" , "192.168.1.69:50021/synthesis?speaker=1" ] with open ( 'query.json' , 'w' ) as file : subprocess.run(command_json, stdout= file ) with open ( 'audio_output.wav' , 'wb' ) as file : subprocess.run(command_audio, stdout= file ) pygame.init() pygame.mixer.init() sound = pygame.mixer.Sound( "audio_output.wav" ) sound.play() while pygame.mixer.get_busy(): time . sleep (0.1) # PDFファイルから文字数をカウントする関数 def count_pdf_characters(file_path): doc = fitz. open (file_path) text = "" for page in doc: text += page.get_text() return len(text) # メールの処理を行う関数 def process_email(service, label_id): from gmail_utils import gmail_get_latest_unread_message_body from pdf_downloader import find_preview_link, download_pdf body, urls = gmail_get_latest_unread_message_body(service, label_id) if body: playsound( 'notice.wav' ) generate_and_play_audio_from_text(body) if urls: for url in urls: preview_url = find_preview_link(url) if preview_url: download_pdf(preview_url, "downloaded_file.pdf" ) char_count = count_pdf_characters( "downloaded_file.pdf" ) if char_count >= 100: playsound( 'notice_pdf.wav' ) else : print( "プレビューリンクが見つかりませんでした。" ) else : print( "メールにURLが見つかりませんでした。" ) else : print( "未読メールはありません。" ) |
というようにモジュールにして保存
ファイル名は
1 | email_processor.py |
とした
次に
Kao,pyで顔認識したらメールを読み上げるようにする
1 | git clone https: //github .com /Snowpooll/face_weather .git |
で以前作成した顔認識したら天気を知らせるの中にあるものを
1 | cp ../.. /face_weather/haarcascade_ * . |
でコピーして使う
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import cv2 import time from email_processor import process_email from gmail_utils import gmail_init # Haar Cascade分類器の読み込み face_cascade = cv2.CascadeClassifier( 'haarcascade_frontalface_default.xml' ) # Webカメラの設定 cap = cv2.VideoCapture(0) # 0番目のカメラを使用する場合 # Gmailサービスの初期化 service = gmail_init() label_id = "ラベルID" # 例として特定のラベルIDを設定 # 最後の顔検出時刻 lastTime = None # メインループ while True: # カメラからのフレームの取得 ret, frame = cap. read () # フレームのグレースケール化 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 顔の検出 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # 検出された顔に対する処理 for (x, y, w, h) in faces: # 検出自の処理(検出から1分たったら再度イベント動かす) if lastTime is None or time .perf_counter() - lastTime > 60: # 検出時刻更新 lastTime = time .perf_counter() # メール処理関数を呼び出し process_email(service, label_id) # 後処理 cap.release() cv2.destroyAllWindows() |
で実行したら成功したが
間隔を一分にしたため
メールばかり読み上げになる
なので感覚を20分ぐらいに変更する
1 2 | # 検出自の処理(検出から20分たったら再度イベント動かす) if lastTime is None or time .perf_counter() - lastTime > 1200: |
へ変更した
とりあえず動作確認は取れたので
次に設定ファイルを作成し
コードのメンテをしやすくする
とりあえず
docker サーバーIP
gmailのラベル
次の検出までの待ち時間
は設定ファイルにまとめておき
これを編集するだけでできるようにする