Skip to content

Instantly share code, notes, and snippets.

Last active January 9, 2023 07:56
Show Gist options
  • Save gaoyifan/a6d53f820240ee5054af6c0cea8835c4 to your computer and use it in GitHub Desktop.
Save gaoyifan/a6d53f820240ee5054af6c0cea8835c4 to your computer and use it in GitHub Desktop.
frame rate testing with opencv
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
# Release the video writer
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 =
# If we reached the end of the video, break out of the loop
if not success:
# 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 ='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
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 =
if not ret:
# 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
# Release all resources
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment