使用华为云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检测人脸

结果有点怪怪的,川普的照片没有被检测出人脸。

华为云ModelArts OpenCV检测人脸

 

相关文章:

  • 2021-04-22
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
  • 2021-04-21
  • 2021-11-03
  • 2021-11-30
猜你喜欢
  • 2021-09-20
  • 2021-11-09
  • 2021-09-10
  • 2022-12-23
  • 2021-11-01
相关资源
相似解决方案