728x90
pip install facenet_pytorch
pip install mmcv
sample video
from facenet_pytorch import MTCNN
import torch
import numpy as np
import mmcv, cv2
from PIL import Image, ImageDraw
from IPython import display
#Determine if an nvidia GPU is available
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print('Running on device: {}'.format(device))
#Define mtcnn
mtcnn = MTCNN(keep_all=True, device=device)
#Get a sample video
video = mmcv.VideoReader('video.mp4')
frames = [Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in video]
display.Video('video.mp4', width=640)
#Run video through MTCNN
frames_tracked = []
for i, frame in enumerate(frames):
print('\rTracking frame: {}'.format(i + 1), end='')
# Detect faces
boxes, _ = mtcnn.detect(frame)
# Draw faces
frame_draw = frame.copy()
draw = ImageDraw.Draw(frame_draw)
for box in boxes:
draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)
# Add to frame list
frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR))
print('\nDone')
#Display detections
d = display.display(frames_tracked[0], display_id=True)
i = 1
try:
while True:
d.update(frames_tracked[i % len(frames_tracked)])
i += 1
except KeyboardInterrupt:
pass
#Save tracked video
dim = frames_tracked[0].size
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
video_tracked = cv2.VideoWriter('video_tracked.mp4', fourcc, 25.0, dim)
for frame in frames_tracked:
video_tracked.write(cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR))
video_tracked.release()
원문 : https://github.com/timesler/facenet-pytorch/blob/master/examples/face_tracking.ipynb
728x90
'Data Science > Machine Learning' 카테고리의 다른 글
Data Labeling Tools Each Type (0) | 2022.09.07 |
---|---|
[알고리즘] 주성분 분석 (PCA : Principal Component Analysis) (0) | 2022.08.20 |
[스크랩] NVIDIA Research Turns 2D Photos Into 3D Scenes in the Blink of an AI (0) | 2022.03.29 |
[Recommendations]01-추천 시스템이란? (0) | 2022.03.16 |
[스크랩] 상품(콘텐츠) 추천 기능 구현하기: (3) 고객 취향 사로잡기 (0) | 2022.03.16 |
최근댓글