Last active
January 9, 2023 07:56
-
-
Save gaoyifan/a6d53f820240ee5054af6c0cea8835c4 to your computer and use it in GitHub Desktop.
frame rate testing with opencv
This file contains hidden or 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
import cv2 | |
import pyqrcode | |
import numpy as np | |
import io | |
# Set the width, height and frame rate of the video | |
width, height, fps = 512, 512, 10 | |
# Create a video writer object, using FFmpeg | |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
video_writer = cv2.VideoWriter('qrcode.mp4', fourcc, fps, (width, height)) | |
# Loop 10*FPS times, namely 10 seconds, generating a different QR code each time | |
for i in range(10*fps): | |
# Generate a QR code | |
qr = pyqrcode.create(str(i)) | |
# Create a writable stream in memory | |
stream = io.BytesIO() | |
# Write the QR code to the writable stream | |
qr.png(file=stream, scale=20) | |
# Convert the image in the writable stream to a bytes array | |
png_bytes = stream.getvalue() | |
# Convert the PNG image to a format supported by OpenCV | |
image = np.frombuffer(png_bytes, dtype='uint8') | |
image = cv2.imdecode(image, cv2.IMREAD_COLOR) | |
# Resize the image to the size of the video | |
image = cv2.resize(image, (width, height)) | |
# Add the frame to the video | |
video_writer.write(image) | |
# Release the video writer | |
video_writer.release() |
This file contains hidden or 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
import cv2 | |
import pyzbar.pyzbar as pyzbar | |
# Open the video file | |
video = cv2.VideoCapture('IMG_2717.MOV') | |
# Set up the output file | |
output_file = open('output.txt', 'w') | |
while True: | |
# Read the next frame from the video | |
success, frame = video.read() | |
# If we reached the end of the video, break out of the loop | |
if not success: | |
break | |
# Decode the QR codes in the frame | |
decoded_objects = pyzbar.decode(frame) | |
# Iterate over the decoded objects | |
for obj in decoded_objects: | |
# Extract the QR code data as a string | |
qr_data = obj.data.decode('utf-8') | |
# Write the QR code data to the output file | |
output_file.write(qr_data + '\n') | |
# Close the video file and the output file | |
video.release() | |
output_file.close() |
This file contains hidden or 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
import cv2 | |
# Read in the video file | |
video = cv2.VideoCapture("input.mp4") | |
# Get the width and height of the video | |
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
# Create two video writers for the output videos | |
fourcc = cv2.VideoWriter_fourcc(*"MP4V") | |
video_1 = cv2.VideoWriter("output_1.mp4", fourcc, 30.0, (width, height // 2)) | |
video_2 = cv2.VideoWriter("output_2.mp4", fourcc, 30.0, (width, height // 2)) | |
# Read each frame of the video | |
while True: | |
ret, frame = video.read() | |
if not ret: | |
break | |
# Split the frame into top and bottom halves | |
top_frame = frame[:height // 2] | |
bottom_frame = frame[height // 2:] | |
# Write the top and bottom halves of the frame to the output videos | |
video_1.write(top_frame) | |
video_2.write(bottom_frame) | |
# Release all resources | |
video.release() | |
video_1.release() | |
video_2.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment