Search code examples
pythonwebcamopencv

Python cv2, putting text on webcam capture


I am a Python beginner. I managed to make a webcam capture program but I wanted to put some text on this window. I found the .putText() method. However I can't get it to work. So I know I shouldn't do it but I have no other options... Here is the full code:

import cv2

video = cv2.VideoCapture(0)
a = 0
img_counter = 0
videoNpArr = cv2.imread("test-image.jpg")

while True:

    a = a + 1
    check, frame = video.read()

    print(check)
    print(frame)

    cv2.putText(videoNpArr, "Hello World!!!", (200, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255))

    cv2.imshow("Main frame thing", frame)

    # cv2.waitKey(0)
    key = cv2.waitKey(1)

    if key == ord('q'):
        break
    elif key == ord('i'):
        # SPACE pressed
        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1
    print(a)

video.release()
cv2.destroyAllWindows()

I will be thankfull for any help. <3


Solution

  • Change

    cv2.putText(videoNpArr, "Hello World!!!", (200, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255))

    to

    cv2.putText(frame, "Hello World!!!", (200, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255))