Python 3 FFMPEG Script to Build & Extract Facial Recognition From RTMP Live Streaming Video in Browser
import syssys.path.append('/home/raiy/.local/lib/python3.7/site-packages') # Path for import following libs import cv2import face_recognitionimport numpy as npfrom datetime import datetimeimport time def extractFaces(media_path, filename, output_path): cap = cv2.VideoCapture(media_path + filename) extracted, face_areas = [], [] process_this_frame = True num_of_captured = 0 while True: try: ret, frame = cap.read() #cv2.imshow(frame) small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) except: break rgb_small_frame = small_frame[:, :, ::-1] if process_this_frame: locations = face_recognition.face_locations(rgb_small_frame, number_of_times_to_upsample=3) # Only works when number_of_times_to_upsample = 3 captured = face_recognition.face_encodings(rgb_small_frame, locations) for i, face in enumerate(captured): top, right, bottom, left = locations[i] area = (right-left)*(bottom-top) matches = face_recognition.compare_faces(extracted, face) if matches and any(matches): face_distances = face_recognition.face_distance(extracted, face) best = np.argmin(face_distances) if matches[best] and area >= face_areas[best]: extracted[best] = face face_areas[best] = area cv2.imwrite(output_path+ filename + '_' + str(best + 1) + '.jpg', frame[top*4:bottom*4, left*4:right*4]) # Save only the face #cv2.imwrite(output_path+ filename + '_' + str(best + 1) + '.jpg', frame) # Save the whole frame else: extracted.append(face) face_areas.append(area) num_of_captured += 1 cv2.imwrite(output_path + filename + '_' + str(num_of_captured) + '.jpg', frame[top*4:bottom*4, left*4:right*4]) # Save only the face #cv2.imwrite(output_path+ filename + '_' + str(num_of_captured) + '.jpg', frame) # Save the whole frame process_this_frame = not process_this_frame return len(extracted) if __name__ == '__main__': media_path, filename, output_path = sys.argv[1:4] #'/var/www/live/', 'android-04-Apr-20-13:52:31.flv', '/home/raiy/Desktop/face_test/results/' with open(output_path + 'extractFaces.log', 'a') as f: f.write(datetime.now().strftime("[%m-%d-%Y, %T] ")) f.write('Start with video = {}\n'.format(media_path+filename)) results = extractFaces(media_path, filename, output_path) f.write(datetime.now().strftime("[%m-%d-%Y, %T] ")) f.write('Finished with {} extracted faces saved in {}\n'.format(results, output_path))
#user www-data; # Default settinguser root; # For exec_push command worker_processes auto;pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf; events {worker_connections 768;# multi_accept on;} http { ### Basic Settings## sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off; # server_names_hash_bucket_size 64;# server_name_in_redirect off; include /etc/nginx/mime.types;default_type application/octet-stream; ### SSL Settings## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLEssl_prefer_server_ciphers on; ### Logging Settings## access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log; ### Gzip Settings## gzip on; # gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ### Virtual Host Configs## include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*; server { # For Django_test listen 5000; server_name 127.0.0.1 charset UTF-8; access_log /var/log/nginx/Django_test_access.log; error_log /var/log/nginx/Django_test_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_read_timeout 2; }} # End for Django_test server { # For playbacklisten 8099;server_name _;#root /var/www/live;location /live {alias /var/www/live;expires max;} } # End for HLS playback } # End for http rtmp { # For RTMP streamingserver {listen 1935; # port to outside#server_name 127.0.0.1chunk_size 4096;#location /live { # To Django server test# include uwsgi_params;# uwsgi_pass 127.0.0.1:8000;# uwsgi_read_timeout 2;#} application live { # Streaming on rtmp://[address_of_site]/live/[filename]live on;record all;record_path /var/www/live; # Save streamed video torecord_suffix -%d-%b-%y-%T.flv; # Save filename as current_time.flvexec_publish /home/raiy/Desktop/face_test/stream_analysis.sh $app $name;} application go {play /var/www/live;} }} #mail {# # See sample authentication script at:# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript## # auth_http localhost/auth.php;# # pop3_capabilities "TOP" "USER";# # imap_capabilities "IMAP4rev1" "UIDPLUS";## server {# listen localhost:110;# protocol pop3;# proxy on;# }## server {# listen localhost:143;# protocol imap;# proxy on;# }#}