내가 좋아하는 침착맨님 , 윈도우 배경으로 만드는 것은 전게시물에 있다 그 결과물을 바탕으로 얼굴을 인식하여 모자이크를 해주는 프로그램이다.
import dlib
import cv2
import numpy as np
from imutils import face_utils,resize
# cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture('remove.avi')
if not cap.isOpened():
exit()
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
width = round(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# 저장 파일명, 코덱, FPS, 크기 (width, height)
out = cv2.VideoWriter('mosaic_chim.avi', fourcc, fps, (width, height))
while True:
ret, frame = cap.read()
if not ret:
break
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(img_gray)
for face in faces:
landmarks = predictor(img_gray, face)
points = face_utils.shape_to_np(landmarks)
rect = cv2.boundingRect(points)
x,y,w,h = rect
# cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 1)
mosaic = frame[y:y+h, x:x+w].copy()
mosaic = cv2.resize(mosaic, dsize=None, fx=0.1, fy=0.1, interpolation=cv2.INTER_NEAREST)
mosaic = cv2.resize(mosaic, dsize=(w,h), interpolation=cv2.INTER_NEAREST)
frame[y:y+h, x:x+w] = mosaic
out.write(frame)
cv2.imshow('camera', frame)
if cv2.waitKey(1) == ord('q'): # 사용자가 q 를 입력하면
break
out.release()
cap.release()
cv2.destroyAllWindows()

dlib은 얼굴과 라이브러리이다. python에서는 pip로 쉽게 설치할 수 있다.
dlib로 face detection, face landmark detection 등을 수행가능하다.
shape_predictor_68_face_landmarks.data로 사람얼굴의 68가지 랜드마크를 인식하고
외접하는 사각형을 만들어 그부분의 해상도를 극히 낮추어 씌운 방식이다.
'CV > open cv' 카테고리의 다른 글
[cv] python 픽셀아트 만들기 (0) | 2022.04.14 |
---|