简体   繁体   English

在OpenCV - python中检测到人脸时如何画一个圆

[英]How to draw a circle when the face is detected in OpenCV - python

So I wrote this code that can detect my face using the webcam and make a rectangle around the face.所以我写了这段代码,它可以使用网络摄像头检测我的脸,并在脸周围画一个矩形。 But how can I draw a circle around the face instead?但是我怎样才能在脸上画一个圆圈呢?

import cv2


face_cascade = cv2.CascadeClassifier("face.xml")


cap = cv2.VideoCapture(0)

while True:
    _, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 100), 3)

    cv2.imshow('img', img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break


cap.release()
for (x, y, w, h) in faces:
    center_coordinates = x + w // 2, y + h // 2
    radius = w // 2 # or can be h / 2 or can be anything based on your requirements
    cv2.circle(img, center_coordinates, radius, (0, 0, 100), 3)

Use the coordinates to find the center of the face and from that point draw a rectangle of radius w/2 or h/2.使用坐标找到面的中心,然后从该点绘制一个半径为 w/2 或 h/2 的矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM