以下のコードで顔検出をし、結果を辞書形式にしたいのですが、
python
1import cv2 2import json 3 4face_cascade_path = './haarcascades/haarcascade_frontalface_default.xml' 5 6face_cascade = cv2.CascadeClassifier(face_cascade_path) 7 8cap = cv2.VideoCapture(0) 9 10while True: 11 file = open('sample2.json','w') 12 key = ["x","y","w","h"] 13 ret, img = cap.read() 14 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 15 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) 16 for x, y, w, h in faces: 17 cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) 18 face = img[y: y + h, x: x + w] 19 face_gray = gray[y: y + h, x: x + w] 20 cv2.imshow('video image', img) 21 target = list(faces) 22 dic = dict(zip(key,target)) 23 print(faces) 24 key = cv2.waitKey(10) 25 if key == 27: # ESCキーで終了 26 break 27 28cap.release() 29cv2.destroyAllWindows()
tuple型とlist型で変わってしまいます。
() () () [[978 441 176 176]] () () () [[672 287 430 430]] () () [[638 280 430 430]] [[643 280 430 430]] () [[528 259 52 52]]
どうにも、空の時はtuple型らしいです。
tuple型だと辞書にできないそうです
何卒、ご教授のほどよろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/04 08:03