I am trying to save a video using open CV, the idea is to alternate the video frames from colored to gray scale for few seconds. When I do the saving, the video saves the colored frames only.
import cv2
def saving(cap):
width=cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height=cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps= cap.get(cv2.CAP_PROP_FPS)
fourcc=cv2.VideoWriter_fourcc(*'XVID')
out=cv2.VideoWriter('Project11.mp4',fourcc,fps,(int(width),int(height)))
return (out)
def first4seconds(video):
c=saving(video)
while(video.isOpened()):
print(video.get(cv2.CAP_PROP_POS_MSEC))
ret, frame = video.read()
if ret==True:
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
if(500<=int(video.get(cv2.CAP_PROP_POS_MSEC))<1000 or 2000<=int(video.get(cv2.CAP_PROP_POS_MSEC))<3000):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
c.write(gray)
cv2.imshow('frame',gray)
else:
c.write(frame)
cv2.imshow('frame',frame)
video.release()
cv2.destroyAllWindows()
cap = cv2.VideoCapture('test.mp4')
first4seconds(cap)
if(500<=int(video.get(cv2.CAP_PROP_POS_MSEC))<1000 or 2000<=int(video.get(cv2.CAP_PROP_POS_MSEC))<3000):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
**gray_2=cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)**
**cv2.imshow('frame',gray_2)**
**c.write(gray_2)**
else:
c.write(frame)
cv2.imshow('frame',frame)
Related
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()
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()
When I run my python opencv code, the web camera does not read a video feed. There are no any errors, but the black color output with wifi sign and loading sign i there. How to fix that and read the video feed. Here is my code and the output.
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))
print(cap.isOpened())
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out.write(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
If you are only using one camera you can try the following:
cap = cv2.VideoCapture(-1)
This will pick up the first webcam the system can find.
How are you overlaying the wifi/loading signs - I assume they are not part of the webcam feed?
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'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()