정의:
0. 얼굴 인식후 478개의 특징점 표현하기
1. 캠의 고정영역에 스파이더맨 마스크 씌우기
import cv2
# 한번만 실향하면 되는 코드
spider = cv2.imread('./data/spiderman.jpg')
spider = cv2.resize(spider,(250,250))
mask2gray = cv2.cvtColor(spider, cv2.COLOR_RGB2GRAY)
_, mask_b = cv2.threshold(mask2gray, 200, 255, cv2.THRESH_BINARY)
mask_b_inv = cv2.bitwise_not(mask_b)
img_fg = cv2.bitwise_and(spider, spider, mask = mask_b_inv)
# 마스크이미지에서 사용할 영역의 값만 출출
video = cv2.VideoCapture(0)
while video.isOpened():
ret, img = video.read()
if not ret:
break
k = cv2.waitKey(30)
if k==49:
break
# 마스크 띄울 중앙영역(320,240)
# 640,480 사용할 이미지, 원의 위치, 윈의 크기, 원의 색, 내부를 채울기 걸정
# 매 프레임마다 실행해야하는 코드
# 좌상단(195,115)
# 우하단(445,365)
roi = img[115:365,195:445]
img_bg = cv2.bitwise_and(roi,roi, mask = mask_b)
bg_fg = cv2.add(img_bg, img_fg)
img[115:365,195:445] = bg_fg
cv2.imshow('video',img)
video.release()
cv2.destroyAllWindows()
import cv2
import mediapipe as mp
# 얼굴에서 특징점 찾기 관련 기능
mp_face = mp.solutions.face_mesh
# 특징점 찾기 세부 기능
face = mp_face.FaceMesh(
min_detection_confidence = 0.5, # 얼굴 표현할 최소 정확도
min_tracking_confidence = 0.5 # 특징점 표현할 최서 정확도
)
# 특징점 표현 기능
mp_drawing = mp.solutions.drawing_utils
video = cv2.VideoCapture(0)
while video.isOpened():
ret, img = video.read()
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = cv2.flip(img,1)
# 얼굴에서 특징점 검출하기
face_result = face.process(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if not ret:
break
if face_result.multi_face_landmarks is not None:
for res in face_result.multi_face_landmarks:
nose = face_result.multi_face_landmarks[0].landmark[4]
x_nose = int(nose.x * img.shape[1])
y_nose = int(nose.y * img.shape[0])
cv2.circle(img,(x_nose, y_nose),20,(0,0,255), cv2.FILLED)
k = cv2.waitKey(30)
if k == 49:
break
cv2.imshow('face', img)
video.release()
cv2.destroyAllWindows()
import cv2
import mediapipe as mp
# 얼굴에서 특징점 찾기 관련 기능
mp_face = mp.solutions.face_mesh
spider = cv2.resize(spider,(250,250))
# 특징점 찾기 세부 기능
face = mp_face.FaceMesh(
min_detection_confidence = 0.5, # 얼굴 표현할 최소 정확도
min_tracking_confidence = 0.5 # 특징점 표현할 최서 정확도
)
# 특징점 표현 기능
mp_drawing = mp.solutions.drawing_utils
spider = cv2.imread('./data/spiderman.jpg')
spider = cv2.resize(spider,(250,250))
mask2gray = cv2.cvtColor(spider, cv2.COLOR_RGB2GRAY)
_, mask_b = cv2.threshold(mask2gray, 250, 250, cv2.THRESH_BINARY)
mask_b_inv = cv2.bitwise_not(mask_b)
img_fg = cv2.bitwise_and(spider, spider, mask = mask_b_inv)
video = cv2.VideoCapture(0)
while video.isOpened():
ret, img = video.read()
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = cv2.flip(img,1)
# 얼굴에서 특징점 검출하기
face_result = face.process(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if not ret:
break
if face_result.multi_face_landmarks is not None:
for res in face_result.multi_face_landmarks:
nose = face_result.multi_face_landmarks[0].landmark[4]
x_nose = int(nose.x * img.shape[1])
y_nose = int(nose.y * img.shape[0])
roi = img[y_nose -75 : y_nose +75, x_nose -75 : x_nose + 75] # 어디 위치에 표현할건지
img_bg = cv2.bitwise_and(roi , roi , mask = mask_b)
bg_fg = cv2.add(img_bg, img_fg)
img[y_nose -75 : y_nose +75, x_nose -75 : x_nose + 75] = bg_fg
k = cv2.waitKey(30)
if k == 49:
break
cv2.imshow('face', img)
video.release()
cv2.destroyAllWindows()
import cv2
import mediapipe as mp
# 얼굴에서 특징점 찾기 관련 기능
mp_face = mp.solutions.face_mesh
# 특징점 찾기 세부 기능
face = mp_face.FaceMesh(
min_detection_confidence = 0.5, # 얼굴 표현할 최소 정확도
min_tracking_confidence = 0.5 # 특징점 표현할 최서 정확도
)
# 특징점 표현 기능
mp_drawing = mp.solutions.drawing_utils
spider = cv2.imread('./data/sunglass.jpg') # 225,150
mask2gray = cv2.cvtColor(spider, cv2.COLOR_RGB2GRAY)
_, mask_b = cv2.threshold(mask2gray, 200, 255, cv2.THRESH_BINARY)
mask_b_inv = cv2.bitwise_not(mask_b)
img_fg = cv2.bitwise_and(spider, spider, mask = mask_b_inv)
video = cv2.VideoCapture(0)
while video.isOpened():
ret, img = video.read()
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = cv2.flip(img,1)
# 얼굴에서 특징점 검출하기
face_result = face.process(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if not ret:
break
if face_result.multi_face_landmarks is not None:
for res in face_result.multi_face_landmarks:
#0 번이 입의점
eye = face_result.multi_face_landmarks[0].landmark[6]
x_eye = int(eye.x * img.shape[1])
y_eye = int(eye.y * img.shape[0])
try:
roi = img[y_eye -75 : y_eye +75, x_eye -112 : x_eye + 113] # 어디 위치에 표현할건지
img_bg = cv2.bitwise_and(roi , roi , mask = mask_b)
bg_fg = cv2.add(img_bg, img_fg)
img[y_eye -75 : y_eye +75, x_eye -112 : x_eye + 113] = bg_fg
except:
pass
k = cv2.waitKey(30)
if k == 49:
break
cv2.imshow('face', img)
video.release()
cv2.destroyAllWindows()
import cv2
import mediapipe as mp
# 얼굴에서 특징점 찾기 관련 기능
mp_face = mp.solutions.face_mesh
# 특징점 찾기 세부 기능
face = mp_face.FaceMesh(
min_detection_confidence = 0.5, # 얼굴 표현할 최소 정확도
min_tracking_confidence = 0.5 # 특징점 표현할 최서 정확도
)
# 특징점 표현 기능
mp_drawing = mp.solutions.drawing_utils
spider = cv2.imread('./data/sunglass.jpg') # 225,150
mask2gray = cv2.cvtColor(spider, cv2.COLOR_RGB2GRAY)
_, mask_b = cv2.threshold(mask2gray, 200, 255, cv2.THRESH_BINARY)
mask_b_inv = cv2.bitwise_not(mask_b)
img_fg = cv2.bitwise_and(spider, spider, mask = mask_b_inv)
video = cv2.VideoCapture(0)
cnt = 0
record = False
fps = 10
fcc = cv2.VideoWriter_fourcc(*'DIVX')
width = int(video.get(3))
height = int(video.get(4))
while video.isOpened():
ret, img = video.read()
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = cv2.flip(img,1)
# 얼굴에서 특징점 검출하기
face_result = face.process(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if not ret:
break
if face_result.multi_face_landmarks is not None:
for res in face_result.multi_face_landmarks:
#0 번이 입의점
eye = face_result.multi_face_landmarks[0].landmark[6]
x_eye = int(eye.x * img.shape[1])
y_eye = int(eye.y * img.shape[0])
try:
roi = img[y_eye -75 : y_eye +75, x_eye -112 : x_eye + 113] # 어디 위치에 표현할건지
img_bg = cv2.bitwise_and(roi , roi , mask = mask_b)
bg_fg = cv2.add(img_bg, img_fg)
img[y_eye -75 : y_eye +75, x_eye -112 : x_eye + 113] = bg_fg
except:
pass
k = cv2.waitKey(30)
if k == 49:
break
elif k ==50:
cv2.imwrite(f'./data/cap_sun{cnt}.png', img, params=[cv2.IMWRITE_PNG_COMPRESSION, 0])
cnt+=1
elif k == 51:
out = cv2.VideoWriter('./data/sample_sunglass.mp4',fcc, fps, (width, height))
record = True
elif k == 52:
record = False
out.release()
if record:
out.write(img)
cv2.imshow('face', img)
video.release()
cv2.destroyAllWindows()