将图像叠加到网络摄像头中

时间:2019-02-17 09:19:40

标签: python opencv

我正在创建一个程序,将图像放置在网络摄像头源上。但发生了错误,例如“ image1_alpha = image1 [:,:,3] / 255.0 TypeError:字符串索引必须是整数,而不是元组”

我已经尝试将“:”更改为1或0,但是它表示操作数不受支持。

我跟随here

这是我的代码

import numpy as np
import cv2

from utils import CFEVideoConf, image_resize

cap = cv2.VideoCapture(0)

save_path = 'saved-media/watermark.mp4'
frames_per_seconds = 24
config = CFEVideoConf(cap, filepath=save_path, res='720p')
out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)

img_path = 'College Girl 3.png'
logo = cv2.imread(img_path, -1)
watermark = image_resize(logo, height=275)
watermark = cv2.cvtColor(watermark, cv2.COLOR_BGR2BGRA)

img_counter = 0


while(True):
   # Capture frame-by-frame
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
    frame_h, frame_w, frame_c = frame.shape

    def better_overlay(image1, image2, x, y):
        image1_alpha = image1[:, :, 3] / 255.0
        height, width = image1.shape[0], image1.shape[1]
        for c in range(3):
            image2[y:y+height, x:x+width, c] = image1_alpha * image1[:, :, c] + (1.0 - image1_alpha)* image2[y:y+height, x:x+width, c]
        return image2

    if __name__ == '__main__':
        overlayed = better_overlay(img_path, frame, 300, 300)


    if not ret:
        break
    k = cv2.waitKey(1)

    if k%256 == 27:
        # ESC pressed
        print("Escape hit, closing...")
        break
    elif k%256 == 32:
        # SPACE pressed
        img_name = "College_Female_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1

    frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
    out.write(frame)
    # Display the resulting frame
    cv2.imshow('Photobooth',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
out.release()
cv2.destroyAllWindows()

0 个答案:

没有答案