AdSense

Thursday, May 21, 2020

Traditional Face Detection With Python

Traditional Face Detection With Python



0_MacOS_Python_setup.txt
# Install on Terminal of MacOS

# 1. scikit-image (skimage)
#pip3 install -U scikit-image

# 2. scikit-learn (sklearn)
#pip3 install -U scikit-learn

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


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

python3 fd01.py img.jpg haarcascade_frontalface_alt.xml




0. Data


img.jpg



haarcascade_frontalface_alt.xml
#Download this file from
#https://github.com/opencv/opencv/tree/master/data/haarcascades



Python files

fd01.py
########## Face DETECTION (not Recognition) with Python ##########

#Run this py code on Python as follows:
#
#fd01.py (first argument: your image file for face detection) (cascades xml file)
#
#For instance, it goes like this.
#fd01.py img.jpg haarcascade_frontalface_alt.xml

# Download following files from
# https://realpython.com/traditional-face-detection-python/
# https://github.com/opencv/opencv/tree/master/data/haarcascades
#
# and save it to your working directory.
#
# img.jpg
# haarcascade_frontalface_alt.xml
#
#
# Reference
#
# Traditional Face Detection With Python
# by Kristijan Ivancic
# https://realpython.com/traditional-face-detection-python/


##### import
import skimage
import sklearn
import cv2 as cv
import sys
import os


##### Get user supplied values
#
imageFile = sys.argv[1]
#imageFile = "img.jpg"

cascFile = sys.argv[2]
#cascFile = "haarcascade_frontalface_alt.xml"

#Get working directory
workPath = os. getcwd()
#print(workPath)

imagePath = workPath + "/" + imageFile
#print(imagePath)

cascPath = workPath + "/" + cascFile
print(cascPath)


# Read image from your local file system
#original_image = cv.imread('img.jpg')
original_image = cv.imread(imagePath)

# Convert color image to grayscale for Viola-Jones
grayscale_image = cv.cvtColor(original_image, cv.COLOR_BGR2GRAY)


# Load the classifier and create a cascade object for face detection
#face_cascade = cv.CascadeClassifier('path/to/haarcascade_frontalface_alt.xml')
face_cascade = cv.CascadeClassifier(cascPath)


detected_faces = face_cascade.detectMultiScale(grayscale_image)


for (column, row, width, height) in detected_faces:
    cv.rectangle(
        original_image,
        (column, row),
        (column + width, row + height),
        (0, 255, 0),
        2
    )


# save a face-detected image file
imageFile2 = "Face_Detected_" + imageFile
#cv2.imwrite(imageFile2,original_image)
cv.imwrite(imageFile2,original_image)


cv.imshow('Image', original_image)
cv.waitKey(0)
cv.destroyAllWindows()




Face_Detected_img.jpg




Reference

Traditional Face Detection With Python
by Kristijan Ivancic
https://realpython.com/traditional-face-detection-python/

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 ...