1

. I am using anaconda 4.8.3, spyder 4.1.3 and opencv-python 4.2.0.34. When I am trying to read simple image, then the python process is suspend. when I am trying to read the video there is the same problem. After each run of program I must restart the kernel.

my code for image:

import cv2

img = cv2.imread("lena.png")
cv2.imshow("Output",img)
cv2.waitKey(0)

my code for video:

import cv2
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture("test.mp4")
while True:
    success, img = cap.read()
    img = cv2.resize(img, (frameWidth, frameHeight))
    cv2.imshow("Result", img)
    if cv2.waitKey(1) and 0xFF == ord('q'):
         break

The video and image are in the same folder as project. Have you got any idea why this is happening? Thanks for help.

m1kelle
  • 11
  • 2
  • I don't know what do you mean by saying python process is suspend. In your first reading image program, python will open a *Output* window which displays your *lena.png* until any keypress. In your second program, python will open a *Result* windows which will display the video by frame untill you press `q`. https://stackoverflow.com/q/51143458/10315163 and https://stackoverflow.com/q/53357877/10315163 may help you understand it better. – Ynjxsjmh Jun 12 '20 at 11:01
  • add ' cv2.destroyAllWindows() ' after waitKey – Hariprasad Jun 12 '20 at 11:05
  • Thank you guys for fast reply, the cv2.destroyAllWindows() work for me. Sorry for such a stupid question but I am a beginner. Have a nice day! – m1kelle Jun 12 '20 at 11:11

1 Answers1

0

Code for Image

import cv2

img = cv2.imread("lena.png")
cv2.imshow("Output",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Code for video

import cv2
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture("test.mp4")
while True:
    success, img = cap.read()
    img = cv2.resize(img, (frameWidth, frameHeight))
    cv2.imshow("Result", img)
    if cv2.waitKey(1) and 0xFF == ord('q'):
         break
cap.release()
cv2.destroyAllWindows()
DholuBholu
  • 180
  • 8