Video related work in opencv
How to play a video?
Input
import numpy as np
import cv2
video_path=r"E:\Notes Programmingg\CSS\css(transaction)\my_video.mp4"
cap=cv2.VideoCapture(video_path)
while cap.isOpened():
ret,frame=cap.read()
if ret:
vid_image=cv2.resize(frame,(600,400))
cv2.imshow("mid_vid",vid_image)
if cv2.waitKey(25) & 0xff==ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Output
Input
import numpy as np
import cv2
video_path=r"E:\Notes Programming\CSS\css(transaction)\my_video.mp4"
cap=cv2.VideoCapture(video_path)
while cap.isOpened():
ret,frame=cap.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
if ret:
vid_image=cv2.resize(gray,(600,400))
cv2.imshow("mid_vid",vid_image)
if cv2.waitKey(25) & 0xff==ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Output
How to open system camera?
To access the system camera we have to pass 0 in VideoCapture function. If there is any secondary camera then
you have to write 1, -1 or name of the camera.
Input
import numpy as np
import cv2
cap=cv2.VideoCapture(0)
while cap.isOpened():
ret,frame=cap.read()
if ret:
vid_image=cv2.resize(frame,(600,400))
cv2.imshow("mid_vid",vid_image)
if cv2.waitKey(25) & 0xff==ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
Output
How to capture image from system camera?
Input
import matplotlib.pyplot as plt
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
if cap.isOpened():
ret,frame=cap.read()
print(ret)
print(frame)
else:
ret = False
img1=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
plt.imshow(img1)
plt.title("capture image")
plt.xticks([])
plt.yticks([])
plt.show()
cap.release()
Output
How to capture and save video from system camera?
To save the video, the videoWriter() function is used.
Input
import matplotlib.pyplot as plt
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
fourcc=cv2.VideoWriter_fourcc(*"XVID")
output= cv2.VideoWriter("E:\\output.avi",fourcc,20.0,(640,480))
while cap.isOpened():
ret,frame=cap.read()
if ret==True:
cv2.imshow("frame",frame)
output.write(frame)
if cv2.waitKey(25) & 0xff==ord("q"):
break
else:
break
cap.release()
output.release()
cv2.destroyAllWindows()
Output
How to capture and save video from system camera in gray scale?
Input
import matplotlib.pyplot as plt
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
fourcc=cv2.VideoWriter_fourcc(*"XVID")
output= cv2.VideoWriter("E:\\output.avi",fourcc,20.0,(640,480),0)
while cap.isOpened():
ret,frame=cap.read()
if ret==True:
gary=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow("frame",gary)
output.write(frame)
if cv2.waitKey(25) & 0xff==ord("q"):
break
else:
break
cap.release()
output.release()
cv2.destroyAllWindows()
Output
How to do screen record?
At first we have to install pyautogui library.
Command to install:
!pip install pyautogui
Input
import cv2
import pyautogui as p
import numpy as np
rs=p.size()
fn=input("Please enter the path and file name ")
fps=60.0
fourcc=cv2.VideoWriter_fourcc(*"XVID")
output=cv2.VideoWriter(fn,fourcc,fps,rs)
cv2.namedWindow("recording window",cv2.WINDOW_NORMAL)
while True:
img=p.screenshot()
f=np.array(img)
f=cv2.cvtColor(f,cv2.COLOR_BGR2RGB)
output.write(f)
cv2.imshow("recording window",f)
if cv2.waitKey(25) & 0xff==ord("q"):
output.release()
cv2.destroyAllWindows()
Output
How to save image from video?
Input
import cv2
import os
import numpy as np
video_path=r"E:\Notes Programming\CSS\css(transaction)\my_video.mp4"
os.makedirs("E:\sub my files")
cap=cv2.VideoCapture(video_path)
img_count=1
while cap.isOpened():
ret,frame=cap.read()
if not ret:
print("unable to read frame")
break
is_img_write=cv2.imwrite(f"E:\sub my
files\image{img_count}.jpeg",frame)
if is_img_write:
print(f"E:\sub my files\image{img_count}.jpeg")
cv2.waitKey(25) & 0xff == ord("q")
img_count+=1
cap.release()
cv2.destroyAllWindows()
Output
How to remove background from camera or video?
Input
import cv2
video_path=r"D:\vv.mp4"
cap=cv2.VideoCapture(video_path)
b_r_v=cv2.createBackgroundSubtractorKNN(detectShadows=True)
while cap.isOpened():
ret,frame=cap.read()
if ret:
vid_image=cv2.resize(frame,(600,400))
cv2.imshow("mid_vid",vid_image)
res=b_r_v.apply(frame)
cv2.imshow("background removed",res)
if cv2.waitKey(25) & 0xff==ord("q"):
break else:
break
cap.release()
cv2.destroyAllWindows()
Output