OpenCV Alpha层PNG边缘检测

2024-10-03 11:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难得到我的背景和增强目标的numpy矩阵。该项目旨在创建一个带有情感检测的人脸过滤器,该过滤器最终将集成到API中。我收到的问题是,我想把它作为一个尊重透明度的PNG加载,因为使用JPG并剪切掉白色轴并不是剪切它。Plz-hlp。先谢谢你

Traceback (most recent call last):
  File "/Users/dev/Desktop/capstone/bvarface/bvarface/main.py", line 67, in <module>
    main()
  File "/Users/dev/Desktop/capstone/bvarface/bvarface/main.py", line 53, in main
    window[height[0]: height[1], width[0]: width[1]] = augment_face(window[height[0]: height[1], width[0]: width[1]], augment)
  File "/Users/dev/Desktop/capstone/bvarface/bvarface/main.py", line 22, in augment_face
    augment_target[offset[1]: offset[1] + shape[1], offset[0]: offset[0] + shape[0]] = scaled_target
ValueError: could not broadcast input array from shape (316,204,4) into shape (316,204,3)

从我所能收集到的资料来看,这个形状有点不对劲,但我尝试了很多不同的东西,这个尺寸是我能得到的最接近的

import cv2
import numpy

def augment_face(target: numpy.array, augment: numpy.array) -> numpy.array:
    target_height, target_width, _ = target.shape
    augment_height, augment_width, _ = augment.shape
    augment_copy = augment

    scalar = min(target_width / augment_width,
                target_height / augment_height)

    shape = (int(augment_width * scalar), int(augment_height * scalar))
    offset = (int((target_width - shape[0]) * 0.5), int((target_height - shape[1]) * 0.5))
    scaled_target = cv2.resize(augment, shape)


    augment_target = target.copy()
    #white_filter = (scaled_target < 250).all(axis=2)

    augment_target[offset[1]: offset[1] + shape[1], offset[0]: offset[0] + shape[0]] = scaled_target
    return augment_target



def main():
    augment = cv2.imread("bvarface/static/neutral.png", cv2.IMREAD_UNCHANGED)
    image = augment[:,:,0:3]
    augment_image = augment[:,:,3] / 255.0
    augment_border = 1.0 - augment_image

    cap = cv2.VideoCapture(0)

    cascade = cv2.CascadeClassifier(
        cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

    while True:
        _, window = cap.read()
        bw = cv2.equalizeHist(cv2.cvtColor(window, cv2.COLOR_BGR2GRAY))

        faces = cascade.detectMultiScale(
            bw, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
            flags=cv2.CASCADE_SCALE_IMAGE)

        for x0, y0, x1, y1 in faces:
            width = (x0, x0 + x1)
            height = (int(y0 - (y1 * 0.25)), int(y0 + (y1 * 0.75)))

            if width[0] < 0 or width[1] > window.shape[1] or height[0] < 0 or height[1] > window.shape[0]:
                continue

            window[height[0]: height[1], width[0]: width[1]] = augment_face(window[height[0]: height[1], width[0]: width[1]], augment)
            for c in range(0, 3):
                window[height[0]: height[1], width[0]: width[1], c] = \
                    (image[:, :, c] * augment_image + window[height[0]: height[1], width[0]: width[1], c] * augment_border)

        cv2.imshow("BVAR Face", window)
        if cv2.waitKey(1) == ord("q"):
            break

    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    main()



Tags: inimagenumpytargetmainwindowwidthcv2
1条回答
网友
1楼 · 发布于 2024-10-03 11:19:47
def augment_face(target: numpy.array, augment: numpy.array) -> numpy.array:
    target_height, target_width, _ = target.shape
    augment_height, augment_width, _ = augment.shape
    augment_copy = augment

    scalar = min(target_width / augment_width,
                target_height / augment_height)

    shape = (int(augment_width * scalar), int(augment_height * scalar))
    offset = (int((target_width - shape[0]) * 0.5), int((target_height - shape[1]) * 0.5))
    scaled_target = cv2.resize(augment, shape)
    image = scaled_target[:,:,0:3]
    mask = scaled_target[:,:,3] / 255.0
    border = 1.0 - mask

    augment_target = target.copy()
    for c in range(0, 3):
        augment_target[offset[1]: offset[1] + shape[1], offset[0]: offset[0] + shape[0], c] = \
            image[:, :, c] * mask + augment_target[offset[1]: offset[1] + shape[1], offset[0]: offset[0] + shape[0], c] * border
    return augment_target

通过将alpha直接放在增强函数中找到了答案

相关问题 更多 >