Skip to content

Instantly share code, notes, and snippets.

@mortenboldt
Created December 19, 2018 16:53
Show Gist options
  • Select an option

  • Save mortenboldt/2a0fde0cd2abed168518e9c82e84069f to your computer and use it in GitHub Desktop.

Select an option

Save mortenboldt/2a0fde0cd2abed168518e9c82e84069f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import numpy as np
import cv2
import subprocess as sp
from mtcnn.mtcnn import MTCNN
detector = MTCNN()
VIDEO_URL = "/home/morten/kode/videofaces/testvideo.mp4"
VIDEO_URL = "http://www.streambox.fr/playlists/test_001/stream.m3u8"
VIDEO_URL = "https://content.jwplatform.com/manifests/yp34SRmf.m3u8"
FFMPEG_BIN = "/home/morten/miniconda2/envs/videofaces/bin/ffmpeg"
cv2.namedWindow("hat")
# pipe = sp.Popen([FFMPEG_BIN, "-i", VIDEO_URL,
# "-loglevel", "quiet", # no text output
# "-an", # disable audio
# "-f", "image2pipe",
# "-pix_fmt", "bgr24",
# "-vcodec", "rawvideo", "-"],
# stdin = sp.PIPE, stdout = sp.PIPE)
pipe = sp.Popen([FFMPEG_BIN, "-i", VIDEO_URL,
"-loglevel", "quiet", # no text output
"-an", # disable audio
"-f", "image2pipe",
"-pix_fmt", "bgr24",
# "-vf", "select=not(mod(n\,10))",
# "-vsync", "vfr",
# "-q:v 1", "",
# "-flags", "+bitexact",
"-vcodec", "rawvideo", "-"],
stdin=sp.PIPE, stdout=sp.PIPE)
frame_num = 0
while True:
raw_image = pipe.stdout.read(1280 * 720 * 3) # read 1280*720*3 bytes (= 1 frame)
image = np.fromstring(raw_image, dtype='uint8').reshape((720, 1280, 3))
result = detector.detect_faces(image)
if result:
bounding_box = result[0]['box']
keypoints = result[0]['keypoints']
cv2.rectangle(image,
(bounding_box[0], bounding_box[1]),
(bounding_box[0] + bounding_box[2], bounding_box[1] + bounding_box[3]),
(0, 155, 255),
2)
cv2.circle(image, (keypoints['left_eye']), 2, (0, 155, 255), 2)
cv2.circle(image, (keypoints['right_eye']), 2, (0, 155, 255), 2)
cv2.circle(image, (keypoints['nose']), 2, (0, 155, 255), 2)
cv2.circle(image, (keypoints['mouth_left']), 2, (0, 155, 255), 2)
cv2.circle(image, (keypoints['mouth_right']), 2, (0, 155, 255), 2)
cv2.imshow("hat", image)
frame_num += 1
if cv2.waitKey(5) == 27:
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment