簡體   English   中英

在OpenCV - python中檢測到人臉時如何畫一個圓

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

所以我寫了這段代碼,它可以使用網絡攝像頭檢測我的臉,並在臉周圍畫一個矩形。 但是我怎樣才能在臉上畫一個圓圈呢?

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)

使用坐標找到面的中心,然后從該點繪制一個半徑為 w/2 或 h/2 的矩形。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM