Skip to content

Instantly share code, notes, and snippets.

@bookjan
Last active January 18, 2022 08:02
Show Gist options
  • Save bookjan/7360dae561cfde5998b88fb5e4d6fee1 to your computer and use it in GitHub Desktop.
Save bookjan/7360dae561cfde5998b88fb5e4d6fee1 to your computer and use it in GitHub Desktop.
Using python opencv to detect face and send the frames to FFmpeg to create HLS(HTTP Live Streaming)
import numpy as np
import cv2
import sys
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('<PATH_TO_CASCADES_FOLDER>/haarcascade_frontalface_default.xml')
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
# Print frame height width channels
# height, width, channels = frame.shape
# print height, width, channels
framestring = frame.tostring()
sys.stdout.write(framestring)
# Display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
#!/bin/bash
# Create folder from args
mkdir -p $1
# Get into the folder
cd $1
# Start running FFmpeg HLS Live streaming
python face-detection.py | ffmpeg \
-f rawvideo \
-pixel_format bgr24 \
-framerate 10 \
-video_size 1280x720 \
-i - foo.mp4 \
-vcodec libx264 \
-acodec copy \
-pix_fmt yuv420p \
-color_range 2 \
-hls_time 1 \
-hls_list_size 5 \
-hls_flags delete_segments \
-use_localtime 1 \
-hls_segment_filename '%Y%m%d-%s.ts' \
./playlist.m3u8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment