简体   繁体   中英

get the upper center of bounding box opencv

Code:

# OpenCV Python program to detect cars in video frame
# import libraries of python OpenCV
import cv2
import face_recognition

# capture frames from a video
cap = cv2.VideoCapture(0)

# Initialize variables
face_locations = []

while True:
    # Grab a single frame of video
    ret, frame = cap.read()

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_frame = frame[:, :, ::-1]

    # Find all the faces in the current frame of video
    face_locations = face_recognition.face_locations(rgb_frame)

    # Display the results
    for top, right, bottom, left in face_locations:
        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)


        cv2.circle(frame,(left, top),5,(0, 255, 0), 1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Wait for Enter key to stop
    if cv2.waitKey(25) == 13:
        break

# Release everything if job is finished
cap.release()

This gives me, 结果截图

I need to mark the yellow circle in center of the upper line of the bounding box. I have managed to draw the small circle in top left corner but have no idea how to make them appear on the center of the upper bounding box.

If I understand correctly I think you could do it by changing this line

cv2.circle(frame,(left, top),5,(0, 255, 0), 1)

For this line:

cv2.circle(frame,(int((left + right) / 2), top),5,(0, 255, 0), 1)

Edited to fix the float error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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