OpenCV python process dead - python

.
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.

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()

Related

Show another image while showing one image in loop in opencv python

I am trying to show an image, named "Result", And if a person clicks on the image then, it should show another image, named "Result image", but after clicking, the image Result which has to be live input from webcam freezes. Can anyone help me with this ?
Here is my code :
import cv2
cap = cv2.VideoCapture(0)
def showImage(event,x,y,flags,param):
if event == 1:
cv2.imshow('Result Image', img)
cv2.namedWindow('Result')
cv2.setMouseCallback('Result', showImage)
while True:
_, img = cap.read()
cv2.imshow('Result', img)
cv2.waitKey(1)
I have tried to set the waitKey(1) if the image is clicked in the if event == 1 statement
This code worked for me:
import cv2
def showImage(event,x,y,flags,param):
if event == 1:
cv2.imshow('Result Image', img)
cv2.namedWindow('Result')
cv2.setMouseCallback('Result', showImage)
cv2.namedWindow('Result Image')
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
cv2.imshow('Result', img)
if cv2.waitKey(1) != -1: break
cv2.destroyAllWindows()

failing to play the whole video using cv2

i am trying to play a video using cv2 but it's only showing one frame and the video disappears
import cv2
img_file = 'car image.jpg'
video = cv2.VideoCapture('Tesla Dashcam AccidentTrim.mp4')
while True:
(read_successful, frame) = video.read()
if read_successful:
grayscaled_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
break
classifier_file = 'car_detector.xml'
#Display the image with the faces spotted
cv2.imshow('Newton Caffrey Face Detector', grayscaled_frame)
#Don't Autoclose here (wait here in the code and listen for a key press)
cv2.waitKey(1)
I used the following code for displaying the video using cv2. It will keep displaying frames untill video ends. hope this will work for you, peace!
import cv2
cap = cv2.VideoCapture("Video.mp4")
width = 400
height = 300
num = 0
while True:
ret, frame = cap.read()
if ret:
frame = cv2.resize (frame, (width, height))
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cv2.destroyAllWindows()

how to save an image once certain condition accures in cam or video in opencv

hello i want to save an image if it meets certain condition in video capture
for example save an image if the person do the v sign for example how can i do that?
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
tf, img = cap.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#cv2.imwrite('v_sign.png',img)
cv2.imshow('img', img)
if img == cv2.imread('v_sign.png'):
cv2.imwrite('captured frame.png',img)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()

cxFreeze issues with OpenCV - video doesn't play

When I use cx_Frezze to create an exe for the below OpenCV code it works as expected:
import cv2
import numpy as np
# mouse callback function
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDBLCLK:
cv2.circle(img,(x,y),100,(255,50,70),-1)
# Create a black image, a window and bind the function to window
img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)
while(1):
cv2.imshow('image',img)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.destroyAllWindows()
But, when I try to do the same thing with the simple code below, the video is not displayed. Basically, nothing happens. No error message, no crash... nothing. But, no video.
import numpy as np
import cv2
try:
cap = cv2.VideoCapture('some_video.wmv')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
except:
pass
The setup.py code is the same for both, except for the file name.
Add print statements to your code for debugging:
import numpy as np
import cv2
try:
cap = cv2.VideoCapture('some_video.wmv')
if not cap.isOpened():
print('VideoCapture not opened')
exit(1)
while True:
ret, frame = cap.read()
if not ret:
print('frame is empty')
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
except:
pass
If VideoCapture is not opened or frame is empty then check this answer.

Issue at opening a video with VideoCapture

I'm unable to open some avi files with Videocapture from opencv. In fact, I just have one video which works, but none of my other videos will open.
The code I use:
import numpy as np
import cv2
cap = cv2.VideoCapture('test.avi')
while True:
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()

Categories