Python Forum

Full Version: cv2 show image
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to display the images I take with the webcam.
The iteration presents no issue, but right after the loop is finished,
I get this error:

Error:
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback Traceback (most recent call last): File "C:/Users/PycharmProjects/client-server/cv_attempt.py", line 29, in <module> cv2.imshow('image', image) cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
how do I display these images?

Here is my code

import cv2
import time

capture = cv2.VideoCapture(0)
capture.set(3, 640)
capture.set(4, 480)
img_counter = 0
start_time = time.time()

while (img_counter< 5):
    ret, frame = capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    if time.time() - start_time >= 1:  # how often (secs) are pics taken
        filename = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(filename, frame)
        start_time = time.time()
        img_counter += 1
        print("{} written!".format(img_counter))
capture.release()

path=r'C:\\Users\\PycharmProjects\\client-server'
image = cv2.imread(path, 0)

cv2.imshow('image', image)
Hallo Mcgrim,

When you run this code

path=r'C:\\Users\\PycharmProjects\\client-server'
image = cv2.imread(path, 0)
You are not specifying what image it should show, you only gave the path to the folder.
the code lines before line 23 are taking pics with the webcam and storing them in the folder I mention in line 24, so those images should show.
I do not see how. csv2.imshow requires a path to an image, not a folder. Try changing the path to:

path=r'C:\\Users\\PycharmProjects\\client-server\\name_of_the_picture.png'
i think he forgot to add the
+ filename 
which coming from

filename = "opencv_frame_{}.png".format(img_counter)