华为云ModelArts OpenCV检测人脸

使用华为云ModelArts 调用OpenCV实现检测人脸,

先下载haarcascade_frontalface_alt.xml,这是预先训练好的模型,上传到notebook环境中,

之后,创建tensorflow的notebook文件,输入代码

path = "/home/ma-user/anaconda3/envs/TensorFlow-1.13.1/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_alt.xml"

import cv2 as cv

face_cascade = cv.CascadeClassifier(path)


image_path = "./1.jpeg"        #注释,自己选择一张图吧
image_path = "./Donald Trump.jpeg"       #注释,自己选择一张图吧

img = cv.imread(image_path)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for(x, y, w, h) in faces:
    cv.rectangle(img, (x, y),(x+w, y+h), (0,255, 0), 2)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB) 
from PIL import Image 
Image.fromarray(img)

 

只要图中出现了人脸,就会用绿色方框标记

华为云ModelArts OpenCV检测人脸_第1张图片

 

 

你可能感兴趣的:(OpenCV,人工智能,华为云)