I'm trying to make an screen recording program with interface in it, using opencv and tkinter.
I'm intending to make it both presentation and cam recorded. so I made a window which displays my cam, inside the screnn recording frame.
I think I'm almost there, but my webcam displays nothing. recording works fine, though.
Can I get some help? I've searched for this, but I don't see a solution for me.
thank you :)
I think this part of the code makes problem :
resolution = (1920,1080)
codec = cv2.VideoWriter_fourcc(*'XVID')
filename = '{}.avi'.format(file_name.get())
location = save_dest.get()
fps = 60.0
out = cv2.VideoWriter(location+'/'+filename, codec, fps, resolution)
cv2.namedWindow('Cam', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Cam', 480, 270)
cam = cv2.VideoCapture(0)
while record:
res, frame_cam = cam.read()
if res:
cv2.imshow('Cam', frame_cam)
img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
out.write(frame)
if cv2.waitKey(1) == ord('q'):
break
out.release()
cv2.destroyAllWindows()
Related
I'm a python beginner so sorry in advance for the mistakes.
I'm trying to rotate a video 180 degrees and then work with that video that I should have created.
At the moment I'm doing this:
import cv2
import numpy as np
#that's my original video - the one that I want to rotate 180 degrees
cap = cv2.VideoCapture('/Users/name/Desktop/VIDEO 12.05/PIC_P01.mp4')
frame_number = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Original Frames
frames = []
for i in range(frame_number):
ret, frame = cap.read()
frames.append(frame)
#here's where I try to rotate the video
new = cv2.rotate(frame, cv2.ROTATE_180)
cv2.imshow('output', new)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#here I'm trying to write the new rotated video
newvideoR = cv2.VideoWriter('PIC_P01R.mp4',0x7634706d, 50, (360, 640))
for jj in range(len(new)):
newvideoR.write(new[jj])
newvideoR.release()
cap.release()
In the end, however, when I go to read the file I find only an empty file that I cannot open.
From this, I deduce that there is an error in the steps that I have made.
Would anyone have any advice on how am I suppose to do this?
** I've also tried to change the parameter on cv2.Videowriter (for example fps, size) but is still not working
You don't need to accumulate the frames, and write them in another loop.
You can read a frame, rotate it, and write it in the same loop.
Get width and height:
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
Open the output video file before the loop:
newvideoR = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*"mp4v"), 50, (frame_width, frame_height))
Read the frame, rotate it and write it in a loop:
for i in range(frame_number):
ret, frame = cap.read()
new = cv2.rotate(frame, cv2.ROTATE_180)
cv2.imshow('output', new)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
newvideoR.write(new)
Release video reader and writer:
newvideoR.release()
cap.release()
Complete code sample (I named the files input.mp4 and output.mp4):
import cv2
#that's my original video - the one that I want to rotate 180 degrees
cap = cv2.VideoCapture('input.mp4')
frame_number = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Get width and height
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# here I'm trying to write the new rotated video
# Open the output video file before the loop, cv2.VideoWriter_fourcc(*"mp4v") = 0x7634706d
newvideoR = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*"mp4v"), 50, (frame_width, frame_height))
# Original Frames
#frames = []
for i in range(frame_number):
ret, frame = cap.read()
#frames.append(frame) # No need to append the original frames
#here's where I try to rotate the video
new = cv2.rotate(frame, cv2.ROTATE_180)
cv2.imshow('output', new)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
newvideoR.write(new)
newvideoR.release()
cap.release()
There is a video, that is being processed. The process can be seen in console as frame processing 1/1000, 2/1000 etc. The output video has done ok, but if i want to see the results during the run, there is a grey screen - not responding (screenshot of program running).
Code, where movi is loaded:
input_movie = cv2.VideoCapture(r"test.mp4")
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_movie = cv2.VideoWriter('myoutput_01.avi', fourcc, 29.97, (480, 360))
Code to show video duriing the run:
cv2.imshow('Video', frame)
How to see the process?
UPDATE
I used the while cycle, i just didn't want to include much code.
But here it is:
while True:
ret, frame = input_movie.read()
frame_number += 1
if not ret:
break
cv2.imshow('Video', frame)
rgb_frame = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
Looking at what you have here; I'm not sure you understood how to deal with videos in opencv-python.
input_movie = cv2.VideoCapture(r"test.mp4")
here will open the test.mp4 video (as maybe you understood).
But now, you'll need to tell to opencv to read each frame of this video using a while function and read input_movie
In general, this is how we do:
input_movie = cv2.VideoCapture(r"test.mp4")
while (input_movie.isOpened()):
ret, frame = input_movie.read() # here we extract the frame
cv2.imshow('Video',frame) # here we display it
if cv2.waitKey(1) & 0xFF == ord('q'): # by press 'q' you quit the process
break
input_movie.release() # remove input_movie from memory
cv2.destroyAllWindows() # destroy all opencv windows
more informations here https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html
I have taken the following code from this website.
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'avc1')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if ret:
out.write(frame)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
The problem I am facing is that the video is being stored, but I am not able to open it. The video size is about 6KB, but the duration is 0 seconds. How can I fix this?
I did check the other questions similar to this, but none of them solve the issue I am facing.
I had problem with opening file if I saved frames with wrong size.
If camera gives frame with size ie. (800, 600) then you have to write with the same size (800, 600) or you have to use CV to resize frame to (640, 480) before you save it.
frame = cv2.resize(frame, (640, 480))
Full code
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'avc1') #(*'MP42')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if ret:
frame = cv2.resize(frame, (640, 480))
out.write(frame)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Example on GitHub: furas/python-examples/cv2/record-file
One thing that I found out after much googling was that the VideoWriter fails silently.
In my case, I didn't have a VideoCapture object, but a list of frames. I followed the guide similar to what you have done, but the problem was that I was passing in the shapes of the array according to what img.shape[:2] was giving me. IIRC, OpenCV has a different ordering of the widths and heights than numpy arrays do which was the source of my problem. See below for the comment from here
As have been stated by #pstch, when creating VideoWriter in Python one should pass frame dimensions in form cv.VideoWriter(filename, fourcc, fps, (w, h), ...). And when creating frame itself - in reverse order: frame = np.zeros((h, w), ...)
I am trying to save a video file with opencv3 in python. The video that I want to save comes from another video file which I modify for tracking purposes. What I get is an empty .avi file, and I don't understand why.
If it helps, I'm on OSX.
Thanks for the help.
Here is the relevant part of the code:
import cv2
import numpy as np
cap = cv2.VideoCapture('Vid.avi')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (width-110, heigh-210))
while (cap.isOpened()):
success, frame = cap.read()
if success:
hsv_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_frame,lower,upper) #lower and upper are defined in another part of the code
hsv_res = cv2.bitwise_and(hsv_frame,hsv_frame, mask= mask)
out.write(hsv_res)
cv2.imshow('Video', hsv_res)
if cv2.waitKey(50) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
I try to change video resolution (with mp4!) (to 800x600) in this way : but its doesn't work, when I use cap.get(3) and (4), its return every time defualt 1280x720!
import cv2
cap = cv2.VideoCapture('file')
while(cap.isOpened()):
cv2.waitKey(10)
ret, frame = cap.read()
cap.set(3, 800)
cap.set(4, 600)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
print cap.get(3) # return default 1280
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
What I'm doing wrong?
I tried -
cv2.resizeWindow("ssss", 300, 300),
and
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
no effect !
import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
cv2.waitKey(10)
ret, frame = cap.read()
cap.set(3, 800)
cap.set(4, 600)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
print cap.get(3) # return default 1280
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This your code works with webcame, not with file
for a video file, you can resize the window
cv2.resizeWindow(winname, width, height)
for that first define window with name and resize it
example
cv2.namedWindow("frame", 0);
cv2.resizeWindow("frame", 800,600);
for Detail resize window
I think there are a few things in your code that might need attention.
As described in the OpenCV documentation for VideoCapture, if you want to access your default WebCam, you'd need to initialise the class as follows:
cap = cv2.VideoCapture('file')
If you are trying to then change the resolution of the camera, I'd suggest to move the two set lines right below the initialisation of cap and only perform it once - not each time you read in the frame. You can also use constants to access the right attributes:
cap = cv2.VideoCapture('file')
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Your while loop and the rest of the code...
If you are trying to read the frame from a file and want to change it's resolution, you'd probably want to use the resize method as described here. This would need to be done inside the loop, right after you read in the frame. It could be something like:
resize(ret, ret, Size(800, 600), 0, 0, INTER_CUBIC);
I hope this helps.
cap.set() has no effect below resolutions of 640,480
(at least for my macbook pro)
You can increase the resolution, but for example setting it to 300,300 has no effect.
As for my experience you ned to call resize() on the frame after you read() it.