Drawing fancy rectangle around face

前端 未结 4 833
情深已故
情深已故 2021-02-14 23:40

I am using the following code to detect face and draw rectangle on top of the face.

while True:
    # get video frame
    ret, img = cap.read()

    input_img =          


        
4条回答
  •  渐次进展
    2021-02-14 23:53

    Instead of looking for a function/lib that lets you make fancy rectangles, the following tactic might be easier:

    Step 1 - Download an image of the rectangle that you want, such that it should only contain the 4 strokes at the corner, and the rest of the background should be black.

    Step 2 - In your code, use imread to save this image as a Mat object:

    border = cv2.imread('your_img.jpg')

    Step 3 - Modify your for loop to superimpose the border Mat on the detected rectangle, as shown below:

    for i, d in enumerate(detected):
        x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
    
        #cv2.rectangle won't be needed anymore
        #cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
    
        roi=img[y1+h/2-100:y1+h/2+100,x1+w/2-100:x1+w/2+100]
        #this points to a section in original image
        cv2.addWeighted(roi,1,border,1,0,roi)
    

    Ensure that the size of roi and border is the same, or your code will crash.

    This will superimpose the corner strokes on your input frame, and neglect the black background.

提交回复
热议问题