AdSense

Wednesday, May 27, 2020

Face Detection in Python Using a Webcam

Face Detection in Python Using a Webcam



0_MacOS_Python_setup.txt
# Install on Terminal of MacOS

# 1. opencv (cv2)
#pip3 install -U opencv-python


1_MacOS_Terminal.txt
########## Run Terminal on MacOS and execute
### TO UPDATE
cd "YOUR_WORKING_DIRECTORY"

python3 fdw01.py haarcascade_frontalface_default.xml

#This script waits for the ‘q’ key to be pressed.




Python files

fdw01.py

########## Face Detection in Python Using a Webcam ##########

# Download the following file(s)
# haarcascade_frontalface_default.xml
#
# from
# https://github.com/shantnu/Webcam-Face-Detect
#
# and save it to your working directory.
#
#
# Run this py code on Python as follows:
# fdw01.py haarcascade_frontalface_default.xml
#
#
# Reference:
# Face Detection in Python Using a Webcam
# by Shantnu Tiwari
# https://realpython.com/face-detection-in-python-using-a-webcam/


#####Pre-requisites
#1. OpenCV installed (see the previous blog post for details)
#2. A working webcam


#####The Code

import cv2
import sys

cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascPath)


video_capture = cv2.VideoCapture(0)
#NOTE: You can also provide a filename here, and Python will read in the video file. However, you need to have ffmpeg installed for that since OpenCV itself cannot decode compressed video. Ffmpeg acts as the front end for OpenCV, and, ideally, it should be compiled directly into OpenCV. This is not easy to do, especially on Windows.


### added to record a video
ret, frame = video_capture.read()
fshape = frame.shape
fheight = fshape[0]
fwidth = fshape[1]
#
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#
video_output = cv2.VideoWriter('video_output.mp4',fourcc, 20.0, (fwidth,fheight))
#video_output.write(frame)
###



while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()
   
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        #flags=cv2.cv.CV_HAAR_SCALE_IMAGE
        #flags=cv2.cv2.CV_HAAR_SCALE_IMAGE
        flags = cv2.cv2.CASCADE_SCALE_IMAGE
    )
   
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
   

    ### added to record a video
    #frame = cv2.flip(frame,0)
    video_output.write(frame)
    ###
   
    # Display the resulting frame
    cv2.imshow('Video', frame)
   
   
    #We wait for the ‘q’ key to be pressed. If it is, we exit the script.
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break



# When everything is done, release the capture
video_capture.release()
video_output.release()    ### added to record a video
cv2.destroyAllWindows()






Figures and other outputs
video_output.mp4 (omitted)




References

Face Detection in Python Using a Webcam
by Shantnu Tiwari
https://realpython.com/face-detection-in-python-using-a-webcam/

haarcascade_frontalface_default.xml


No comments:

Post a Comment

Deep Learning (Regression, Multiple Features/Explanatory Variables, Supervised Learning): Impelementation and Showing Biases and Weights

Deep Learning (Regression, Multiple Features/Explanatory Variables, Supervised Learning): Impelementation and Showing Biases and Weights ...