前提・実現したいこと
卒業制作でAWSのAPIを叩いて表情を認識するもの作っているのですが、処理が重すぎて
描画がカクカクになってしまいました...
そこでAWSに画像を送信し、帰ってきた情報を書こうする部分(ソースコード #---AWSの処理---)を
別のスレッドで処理させたいと考えているのですが、なかなかうまくいきません。
現状のコードはOpenCVの分類器で顔を判定したらAWSにdetect_faces()で画像を送信、
帰ってきたjsonを加工して、次の処理につなげる...といった流れで書いています。
アドバイスをいただけると嬉しいです...
該当のソースコード
色々いじる前のソースコードを記載しておきます。
python
1import sys 2import cv2 3import numpy as np 4import time 5import pyautogui 6import boto3 7 8import multiprocessing 9 10# VideoCaptureのインスタンスを作成する。 11cap = cv2.VideoCapture(1) 12cap.set(cv2.CAP_PROP_FPS, 30) 13cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('H', '2', '6', '4')) 14 15rekognition = boto3.client('rekognition')#AWSのやつ 16 17happy = 0 18sad = 0 19emotion = 0 20angry = 0 21emotion = 0 22confused = 0 23disgusted = 0 24suprised = 0 25clam = 0 26unknown = 0 27fear = 0 28 29color = (255,255,255) 30 31if cap.isOpened() is False: 32 print("can not open camera") 33 sys.exit() 34 35# 評価器を読み込み 36cascade = cv2.CascadeClassifier('/usr/local/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml') 37 38while True: 39 40 # VideoCaptureから1フレーム読み込む 41 ret, frame ,= cap.read() 42 43 frame = cv2.resize(frame, (int(frame.shape[1]/3), int(frame.shape[0]/3))) 44 frame = cv2.flip(frame, 1) #左右反転 45 46 ret, buf = cv2.imencode('.jpg', frame)#AWS用の画像を用意 47 48 dst = cv2.resize(frame, (int(frame.shape[1]/4), int(frame.shape[0]/4))) 49 gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) 50 51 # 顔検出---------------------------------------------------------------------------------------- 52 facerect = cascade.detectMultiScale( #顔判定 53 gray, 54 scaleFactor=1.11, 55 minNeighbors=3, 56 minSize=(10, 10) 57 ) 58 59 if len(facerect) != 0: 60 pyautogui.keyDown('r') 61 62 #-------------------------------------AWSの処理------------------------------------- 63 64 emoemo = rekognition.detect_faces(Image={'Bytes':buf.tobytes()}, Attributes=['ALL']) 65 66 67 for face in emoemo['FaceDetails']: 68 gender = face['Gender']['Value'] 69 smile = face['Smile']['Value'] 70 emotions = face['Emotions'] 71 72 for emotion in emotions: 73 if emotion['Type'] == 'HAPPY': 74 happy = emotion['Confidence'] 75 elif emotion['Type'] == 'SAD': 76 sad = emotion['Confidence'] 77 elif emotion['Type'] == 'ANGRY': 78 angry = emotion['Confidence'] 79 elif emotion['Type'] == 'CONFUSED': 80 confused = emotion['Confidence'] 81 elif emotion['Type'] == 'DISGUSTED': 82 disgusted = emotion['Confidence'] 83 elif emotion['Type'] == 'SURPRISED': 84 suprised = emotion['Confidence'] 85 elif emotion['Type'] == 'CALM': 86 clam = emotion['Confidence'] 87 elif emotion['Type'] == 'UNKNOWN': 88 unknown = emotion['Confidence'] 89 elif emotion['Type'] == 'FEAR': 90 fear = emotion['Confidence'] 91 92 print(gender) 93 94 if gender == "Female" : 95 pyautogui.keyUp('m') 96 pyautogui.keyDown('f')#女 97 print("Female") 98 elif gender == "Male" : 99 pyautogui.keyUp('f') 100 pyautogui.keyDown('m')#男 101 print("Male") 102 elif len(facerect) == 0 : 103 print("non gender") 104 105 106 if happy >= 80 : 107 color = (255,255,0) 108 pyautogui.keyDown('h') 109 print("HAPPY") 110 111 pyautogui.keyUp('s') 112 elif sad >= 80 : 113 color = (255,255,0) 114 pyautogui.keyDown('s') 115 print("sad") 116 117 pyautogui.keyUp('h') 118 else: 119 pyautogui.keyUp('h') 120 121 #-------------------------------------AWSの処理終わり------------------------------------- 122 123 for x, y, w, h in facerect: 124 125 # 顔検出した部分に枠を描画 126 cv2.rectangle( 127 frame, 128 (x*4, y*4), 129 ((x + w)*4, (y + h)*4), 130 color, 131 thickness=2 132 ) 133 134 frame = cv2.resize(frame, (int(frame.shape[1]*3), int(frame.shape[0]*3))) 135 cv2.imshow('window', frame) 136 137 k = cv2.waitKey(1) 138 if k == 27: 139 break 140 141 elif len(facerect) == 0 : 142 pyautogui.keyUp('r') 143 pyautogui.keyUp('f') 144 pyautogui.keyUp('m') 145 146cap.release() 147cv2.destroyAllWindows() 148ser.close() 149
試したこと
とりあえず処理を関数ごとに分けようとしたのですが、エラーがいっぱい出てしまいお手上げでした...
補足情報(FW/ツールのバージョンなど)
Python 3.6
boto3
openCV
あなたの回答
tips
プレビュー