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()
Related
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()
.
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()
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()
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.
i've been trying to display the camera feed from my laptops web cam in grayscale and i've done it using the following code:
import cv2
import numpy as np
clicked = False
def onMouse(event, x, y, flags, param):
global clicked
if event == cv2.cv.CV_EVENT_LBUTTONUP:
clicked = True
cv2.namedWindow('image capture', cv2.WINDOW_NORMAL)
cv2.setMouseCallback('image capture', onMouse)
#initialize the camera object with VideoCapture
camera = cv2.VideoCapture(0)
sucess, frame = camera.read()
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
while sucess and cv2.waitKey(1) == -1 and not clicked:
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
cv2.imshow('image capture', gray)
sucess, frame = camera.read()
cv2.imwrite('snapshot.png', frame)
print 'photo taken press any key to exit'
cv2.waitKey()
cv2.destroyAllWindows()
Here what i've done is saved the frame in 'snapshot.png' and again reloaded it in grayscale and display that grayscale image. Is there any method to directly read the camera frame in grayscale rather than going through all this mess. Thanks in advance.
wow, what a mess ;)
you simply want:
gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )
In the latest version of opencv, the cvtColor expects it's scr to be not None and therefore gives 215-assertion error.
This is basically like a scenario where you have to use a catch block and try to handle exceptions.
Code to overcome this problem:
while True:
ret, frame = cap.read()
if frame.any():
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)