TsTeaTime
TsTeaTime

Reputation: 921

Draw Line on Webcam Stream -Python

I am attempting to draw a line to the webcam output. However, I am having difficulty with the following code and specifically with "img" portion of the draw line function. I have seen numerous examples of adding an image to another image, so please don't refer me to those examples. This is specifically a question of a line or square on the output of the webcam output.

cv2.line(img= vc, pt1= 10, pt2= 50, color =black,thickness = 1, lineType = 8, shift = 0)

Below is the full code:

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break
    else:
        cv2.line(img= vc, pt1= 10, pt2= 50, color =black,thickness = 1, lineType = 8, shift = 0)
vc.release()
cv2.destroyWindow("preview")

Upvotes: 4

Views: 9838

Answers (1)

Martin Evans
Martin Evans

Reputation: 46759

You need to draw the line on the frame you get. Try the following:

import cv2

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break
    else:
        cv2.line(img=frame, pt1=(10, 10), pt2=(100, 10), color=(255, 0, 0), thickness=5, lineType=8, shift=0)

vc.release()
cv2.destroyWindow("preview")   

Upvotes: 7

Related Questions