if get error with open cv install contrib version
pip uninstall opencv-python
then install opencv-contrib-python
pip install opencv-contrib-python
pip install opencv-contrib-python --upgrade
import cv2
# used for accessing url to download files
import urllib.request as urlreq
# used to access local directory
import os
# used to plot our images
import matplotlib.pyplot as plt
# used to change image size
from pylab import rcParams
# fix https://stackoverflow.com/questions/24974827/cascadeclassifier-in-opencv-generates-error
import numpy as np
pics_url = "https://tse2.mm.bing.net/th?id=OIP.vmeZBBDKUlTYmIyQfTYRugHaEK&pid=Api&P=0&w=301&h=170"
# save picture's name as pic
pic = "image.jpg"
# download picture from url and save locally as image.jpg
urlreq.urlretrieve(pics_url, pic)
# read image with openCV
image = cv2.imread(pic)
# plot image with matplotlib package
plt.imshow(image)
<matplotlib.image.AxesImage at 0x7fcdbc811c88>
# convert image to RGB colour
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# plot image with matplotlib package
plt.imshow(image_rgb)
<matplotlib.image.AxesImage at 0x7fcdb8eac400>
def rgb2gray(rgb):
r, g, b = rgb[:,:,0], rgb[:,:,1], rgb[:,:,2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
# set dimension for cropping image
x, y, width, depth = 50, 200, 950, 500
image_cropped = image_rgb[y:(y+depth), x:(x+width)]
# create a copy of the cropped image to be used later
image_template = image_cropped.copy()
# convert image to Grayscale
image_gray = np.array(rgb2gray(image), dtype='uint8')
#image_gray = rgb2gray(image)
plt.axis("off")
plt.imshow(image_gray, cmap = "gray")
<matplotlib.image.AxesImage at 0x7fcdb8b85c50>
# save face detection algorithm's url in haarcascade_url variable
haarcascade_url = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_alt2.xml"
# save face detection algorithm's name as haarcascade
haarcascade = "haarcascade_frontalface_alt2.xml"
# chech if file is in working directory
if (haarcascade in os.listdir(os.curdir)):
print("File exists")
else:
# download file from url and save locally as haarcascade_frontalface_alt2.xml, < 1MB
urlreq.urlretrieve(haarcascade_url, haarcascade)
print("File downloaded")
# create an instance of the Face Detection Cascade Classifier
detector = cv2.CascadeClassifier(haarcascade)
# Detect faces using the haarcascade classifier on the "grayscale image"
faces = detector.detectMultiScale(image_gray)
# Print coordinates of detected faces
print("Faces:\n", faces)
for face in faces:
# save the coordinates in x, y, w, d variables
(x,y,w,d) = face
# Draw a white coloured rectangle around each face using the face's coordinates
# on the "image_template" with the thickness of 2
cv2.rectangle(image_gray,(x,y),(x+w, y+d),(255, 255, 255), 2)
plt.axis("off")
plt.imshow(image_gray)
plt.title('Face Detection')
File exists
Faces:
()
Text(0.5, 1.0, 'Face Detection')
# save facial landmark detection model's url in LBFmodel_url variable
LBFmodel_url = "https://github.com/kurnianggoro/GSOC2017/raw/master/data/lbfmodel.yaml"
# save facial landmark detection model's name as LBFmodel
LBFmodel = "lbfmodel.yaml"
# check if file is in working directory
if (LBFmodel in os.listdir(os.curdir)):
print("File exists")
else:
# download picture from url and save locally as lbfmodel.yaml, < 54MB
urlreq.urlretrieve(LBFmodel_url, LBFmodel)
print("File downloaded")
# create an instance of the Facial landmark Detector with the model
landmark_detector = cv2.face.createFacemarkLBF() # fix with
# https://stackoverflow.com/questions/44633378/attributeerror-module-cv2-cv2-has-no-attribute-createlbphfacerecognizer
landmark_detector.loadModel(LBFmodel)
# Detect landmarks on "image_gray"
_, landmarks = landmark_detector.fit(image_gray, faces)
for landmark in landmarks:
for x,y in landmark[0]:
# display landmarks on "image_cropped"
# with white colour in BGR and thickness 1
cv2.circle(image_gray, (x, y), 1, (255, 255, 255), 1)
plt.axis("off")
plt.imshow(image_gray)
File exists
<matplotlib.image.AxesImage at 0x7fcdb005bf28>