cxFreeze issues with OpenCV - video doesn't play - python

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.

Related

Facecam Video does not open with cv2.VideoCapture() and cannot be read

I'm new to OpenCV and just started learning
I am using macOS, python version 3.8.1
My code:
import cv2
# takes facecam video
cap = cv2.VideoCapture(-1)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if cap.isOpened() == True:
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
cap.open(-1)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
This works for me
import cv2
cap = cv2.VideoCapture(0)
while True:
ne, frame = cap.read()
cv2.imshow("Webcam Capture", frame)
if cv2.waitKey(1) & 0xFF == ord("A"):
break
cap.release()
cv2.destroyAllWindows()

Does anyone know how to code for getting a frame using your webcam?

I'm trying to get a frame from my webcam but I don't know where the frame is located.
this is my code:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
if frame is None:
break
cv2.imshow('app', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
In the code that you posted, the frame is the image that is captured from the webcam at that point in time.
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
if frame is None:
break
cv2.imshow('app', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
In your program indentation problem in if condition
If you press "q" then the frame is captured, and the frame stored in "frame" variable

Cannot Close Video Window in OpenCV

I would like to play a video in openCV using python and close that window at any time, but it is not working.
import numpy as np
import cv2
fileName='test.mp4' # change the file name if needed
cap = cv2.VideoCapture(fileName) # load the video
while(cap.isOpened()):
# play the video by reading frame by frame
ret, frame = cap.read()
if ret==True:
# optional: do some image processing here
cv2.imshow('frame',frame) # show the video
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)
The window opens and starts playing the video, but I cannot close the window.
You can use cv2.getWindowProperty('window-name', index) to detect if the window is closed. I'm not totally sure about the index but this worked for me:
import cv2
filename = 'test.mp4'
cam = cv2.VideoCapture(filename)
while True:
ret, frame = cam.read()
if not ret:
break
cv2.imshow('asd', frame)
cv2.waitKey(1)
if cv2.getWindowProperty('asd', 4) < 1:
break

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

Stream video from drone Parrot 2.0. Python + cv2

I am trying to have access to the stream of my drone's camera.
Here my code:
import cv2
import numpy
import libardrone
drone = libardrone.ARDrone()
cap = drone.image
while(True):
cap = drone.image
if not cap:
continue
ret, frame = convert
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It does not work. It does not open any frame where I can see the stream video of my drone's camera.
What's wrong? Do you have any suggestions?
Thank you!
import cv2
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
running = True
while running:
# get current frame of video
running, frame = cam.read()
if running:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 27:
# escape key pressed
running = False
else:
# error reading frame
print 'error reading video feed'
cam.release()
cv2.destroyAllWindows()
Try this code...This works for me.

Categories