人脸检测(mtcnn自带人脸检测)

安装:conda会自动帮你判断环境冲突的

# conda install -c conda-forge mtcnn
# conda install -c conda-forge tensorflow
import cv2


def cv_show(neme, img):
    cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
    cv2.imshow(neme, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


# 读取照片
img = cv2.imread('./images/faces2.jpg')
# MTCNN需要RGB通道顺序
img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# 导入MTCNN
from mtcnn.mtcnn import MTCNN

# 加载模型
face_detetor = MTCNN()
# 检测人脸
detections = face_detetor.detect_faces(img_cvt)
for face in detections:
    (x, y, w, h) = face['box']
    cv2.rectangle(img_cvt, (x, y), (x + w, y + h), (0, 255, 0), 5)

cv_show('neme', cv2.cvtColor(img_cvt, cv2.COLOR_RGB2BGR))

你可能感兴趣的:(计算机视觉项目,python,opencv,开发语言)