Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

python - OpenCV - Loop an image as Webcam Input

I began using OpenCV(Python) a while ago and everything went great until now. My Webcam broke and now I cant use it to test my code. Is there a way to fake the input with a simple image? It's not perfect but should be fine for simple things until the new webcam arrives.

Let's assume I have something like this:

windowWidth = 640
windowHeight = 840
brightness = 100

cap = cv2.VideoCapture(0)
cap.set(3,windowWidth)
cap.set(4,windowHeight)
cap.set(10,brightness)



while (cap.isOpened()):
    success, img = cap.read()
    if success== True:
        img = cv2.flip(img, 1)
        cv2.imshow("output", img)
        if cv2.waitKey(1)& 0xFF ==ord("q"):
            break
question from:https://stackoverflow.com/questions/65875509/opencv-loop-an-image-as-webcam-input

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

VideoCapture is a class to interface with video streams, and a single image is returned via call to its read() method. When you call cap = cv2.imread(), you are replacing the newly created VideoCapture instance with np.ndarray instance which is returned by cv2.imread. I believe that replacing

success, img = cap.read()

with

img = cv2.imread(testImg.PNG)

would solve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...