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 60 61
| @logger.catch def save_face(img_file_name, img_file_name_line, img, faces, gray, eye_cascade): """ :param eye_cascade: :param gray: :param img_file_name_line: :param img_file_name: :param img: :param faces: :return: """
target_size = (200, 200)
num_faces = len(faces) faces_image_height = num_faces * target_size[0] faces_image_width = target_size[1]
try: faces_image = np.zeros((faces_image_height, faces_image_width, 3), dtype=np.uint8)
for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_gray) for (ex, ey, ew, eh) in eyes: cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
img[y:y + h, x:x + w] = roi_color
for i, (x, y, w, h) in enumerate(faces): face_resized = cv2.resize(img[y:y + h, x:x + w], target_size)
row = i col = 0
faces_image[row * target_size[0]:(row + 1) * target_size[0], col * target_size[1]:(col + 1) * target_size[1]] = face_resized
cv2.waitKey(0) cv2.destroyAllWindows()
cv2.imwrite(img_file_name, faces_image) cv2.imwrite(img_file_name_line, img) except Exception as e: logger.error(f"unknown error, detail: {e}, name: {img_file_name[-27:]}") return False logger.success(f"save success, name: {img_file_name[-27:]}") return True
|