def test_video_embedding(): """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash""" v = display.Video("http://ignored") assert not v.embed html = v._repr_html_() nt.assert_not_in('src="data:', html) nt.assert_in('src="http://ignored"', html) with nt.assert_raises(ValueError): v = display.Video(b'abc') with NamedFileInTemporaryDirectory('test.mp4') as f: f.write(b'abc') f.close() v = display.Video(f.name) assert not v.embed html = v._repr_html_() nt.assert_not_in('src="data:', html) v = display.Video(f.name, embed=True) html = v._repr_html_() nt.assert_in('src="data:video/mp4;base64,YWJj"', html) v = display.Video(f.name, embed=True, mimetype='video/other') html = v._repr_html_() nt.assert_in('src="data:video/other;base64,YWJj"', html) v = display.Video(b'abc', embed=True, mimetype='video/mp4') html = v._repr_html_() nt.assert_in('src="data:video/mp4;base64,YWJj"', html) v = display.Video(u'YWJj', embed=True, mimetype='video/xyz') html = v._repr_html_() nt.assert_in('src="data:video/xyz;base64,YWJj"', html)
import numpy as np import mmcv, cv2 from PIL import Image, ImageDraw from IPython import display device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') print('Running on device: {}'.format(device)) mtcnn = MTCNN(keep_all=True, device=device) video = mmcv.VideoReader('video.mp4') frames = [ Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in video ] display.Video('video.mp4', width=640) 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
cap.release() out.release() cv2.destroyAllWindows() # define MTCNN module mtcnn = MTCNN(keep_all=True, device=device) mtcnn.to(device) if __name__ == '__main__': saveVideo() video = mmcv.VideoReader(FILE_OUT) frames = [Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) for frame in video] display.Video(FILE_OUT, width=640) 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) frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR)) print('\nDone')