Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Computer Terminating OpenCv Image Grabber Program

I Have Created a Program in Python in which it uses opencv to grab some images and save it.I have runned this program on my mac and it works fine does what i expect but on my computer the webcam feed shows and the first image when i click space works but when i click for the second time to grab images it doesnt works.The following is the error message that it shows

[ WARN:0] global C:UsersappveyorAppDataLocalTemp1pip-req-build-hfoi92lmopencvmodulesvideoiosrccap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Below is my code

import cv2
import subprocess
from sys import exit
import os

# Code!


import time

cam = cv2.VideoCapture(0)

cv2.namedWindow("test")
cv2.startWindowThread()
img_counter = 0

while True:
    ret, frame = cam.read()
    if not ret:
        print("failed to grab frame")
        break
    cv2.imshow("test", frame)

    k = cv2.waitKey(1)
    if k%256 == 27:
        # ESC pressed
        print("Escape hit, closing...")

        break

    elif k%256 == 32:
        # SPACE pressed
        img_name = f'\Users\Anush\PycharmProjects\PelletInspection\Sample_Images\Sample_Image_{img_counter}.jpg'
        cv2.imwrite(img_name, frame)
        print("{} written!".format(img_name))
        img_counter += 1
        

cam.release()
cv2.destroyAllWindows()
question from:https://stackoverflow.com/questions/65887311/computer-terminating-opencv-image-grabber-program

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

1 Reply

0 votes
by (71.8m points)

try to comment #cv2.startWindowThread() try editing cv2.waitKey(33) instead of cv2.waitKey(1)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...