如何将一个ndarray添加到另一个ndarray

2024-04-27 03:35:43 发布

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

我想在另一个数组中添加一个数组。我尝试过不同的技术,比如追加、扩展,甚至vstack,但我不断地出错

在我的项目中,我正在使用Facenet创建从网络摄像头检测到的人脸的嵌入。嵌入是128维的。此外,我创建了一个包含26个面的数据集,所有面都作为相同维度的嵌入加载

首先,将数据集嵌入加载到名为Data_1的变量中。稍后,这将用于创建t-SNE图以可视化数据。绘图效果很好,但我希望能够比较我的网络摄像头中的人脸与数据集的点的比较

代码如下:

def detembed():
    cam = cv2.VideoCapture(0)
    _,frame = cam.read()
    info = fd.detect_faces(frame)
    if info != []:
        for i in info:
            x,y,w,h = i['box']
            x,y = abs(x), abs(y)
            w,h = abs(w), abs(h)
            xx, yy = x+w, y+h
            #cv2.rectangle(frame, (x,y), (xx,yy), (0,0,255),2)
            face = frame[y:yy, x:xx]
            image = Image.fromarray(face)
            image = image.resize(size)
            arr = asarray(image)

            arr = arr.astype('float32')
            mean, std = arr.mean(), arr.std()
            arr = (arr - mean) / std
            samples = expand_dims(arr, axis=0)
            faces.append(samples)
        #cv2.imshow('Camera Feed', frame)

while True:
    detembed()
    embeddings = Data_1
    if not faces:
        continue
    else:
        for face in faces:
            embeds = facenet.predict(face)
            print(embeds.shape, embeddings.shape)
            embeds = expand_dims(embeds, axis=0)
            embeddings = np.concatenate((embeddings,embeds), axis = 0 )

    points = TSNE(random_state=seed).fit_transform(embeddings)

我不想用代码填充页面,但请记住,我尝试了各种方法(包括连接、附加、扩展和插入),但都没有用我的大多数错误发生在某些维数较高的数组中

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 3 dimension

我希望有人能帮助我