Created
August 31, 2018 08:07
-
-
Save BruceDone/78635b62a550c56a954d2c7c9ac51f32 to your computer and use it in GitHub Desktop.
use opencv 3.4.2 to record video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import time | |
import cv2 | |
import numpy as np | |
import fire | |
RESOLUTION_PARAMS = [ | |
(1920, 1080), | |
(1280, 720) | |
] | |
USING_CAMERA_INDEX = 0 | |
def show(rect_v=1): | |
print("using the res params of {}".format(rect_v)) | |
cap = cv2.VideoCapture(USING_CAMERA_INDEX) | |
rect_params = RESOLUTION_PARAMS[int(rect_v)] | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, rect_params[0]) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, rect_params[1]) | |
# cap.set(cv2.CAP_PROP_FPS, 30) | |
# cap.set(cv2.CAP_PROP_BUFFERSIZE,10) | |
while (True): | |
# Capture frame-by-frame | |
ret, frame = cap.read() | |
# Our operations on the frame come here | |
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
# Display the resulting frame | |
cv2.imshow('frame', frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# When everything done, release the capture | |
cap.release() | |
cv2.destroyAllWindows() | |
def read(filename="web.webm"): | |
cap = cv2.VideoCapture(filename) | |
cap.set(cv2.CAP_PROP_FPS, 20) | |
while (cap.isOpened()): | |
ret, frame = cap.read() | |
if frame: | |
print(frame.shape) | |
time.sleep(0.025) # control the video play speed | |
cv2.imshow('frame', frame) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
cap.release() | |
cv2.destroyAllWindows() | |
def compare(source=None, dest=None): | |
source_imgs = _read_video_file(source) | |
dest_imgs = _read_video_file(dest) | |
print(len(source_imgs)) | |
print(len(dest_imgs)) | |
for a, b in zip(source_imgs, dest_imgs): | |
# frame = np.concatenate((a, b), axis=0) | |
# cv2.imshow("source", a) | |
# cv2.imshow("dest",b) | |
print(_mse(a, b)) | |
# cv2.waitKey() | |
cv2.destroyAllWindows() | |
def _read_video_file(file_name, max_count=100): | |
cap = cv2.VideoCapture(file_name) | |
arr = [] | |
while cap.isOpened(): | |
ret, frame = cap.read() | |
if ret: | |
arr.append(frame) | |
if len(arr) == max_count: | |
break | |
cap.release() | |
return arr | |
def _mse(imageA, imageB): | |
# the 'Mean Squared Error' between the two images is the | |
# sum of the squared difference between the two images; | |
# NOTE: the two images must have the same dimension | |
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2) | |
err /= float(imageA.shape[0] * imageA.shape[1]) | |
# return the MSE, the lower the error, the more "similar" | |
# the two images are | |
return err | |
def screen(rect_v, nums=10): | |
cap = cv2.VideoCapture(0) | |
nums = int(nums) | |
start = 0 | |
rect_params = [int(x) for x in rect_v.split('_')] | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, rect_params[0]) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, rect_params[1]) | |
while cap.isOpened(): | |
time.sleep(1) | |
ret, frame = cap.read() | |
if ret: | |
cv2.imwrite("{}_{}.jpg".format(start, rect_v), frame) | |
start = start + 1 | |
if start >= nums: | |
break | |
cap.release() | |
cv2.destroyAllWindows() | |
print("done with {}".format(rect_v)) | |
def record(minutes=5, fourc="XVID", fps=10): | |
start = int(time.time()) | |
minutes = int(minutes) | |
fps = int(fps) | |
print("now will record {} minutes".format(minutes), "filename is {}".format(start)) | |
cap = cv2.VideoCapture(0) | |
fourcc = cv2.VideoWriter_fourcc(*fourc) | |
rect_params = RESOLUTION_PARAMS[1] | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, rect_params[0]) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, rect_params[1]) | |
cap.set(cv2.CAP_PROP_FPS, fps) | |
# cap.set(cv2.CV_CAP_PROP_BUFFERSIZE, 20) | |
# Define the codec and create VideoWriter objectXVID | |
# fourcc = cv2.VideoWriter_fourcc(*'XVID') | |
out = cv2.VideoWriter('{}_{}.avi'.format(fourc, str(start)), fourcc, fps, rect_params) | |
while (cap.isOpened()): | |
step = int(time.time()) | |
ret, frame = cap.read() | |
if ret == True: | |
# img = cv2.resize(frame,(1920,1080)) | |
# write the flipped frame | |
out.write(frame) | |
cv2.imshow('frame', frame) | |
if (cv2.waitKey(1) & 0xFF == ord('q')) or ((step - start) >= minutes * 60): | |
print("enter to the time used") | |
break | |
else: | |
break | |
# Release everything if job is finished | |
cap.release() | |
out.release() | |
cv2.destroyAllWindows() | |
if __name__ == '__main__': | |
fire.Fire() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment