Skip to content

Instantly share code, notes, and snippets.

@gaoyifan
Last active January 9, 2023 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
video_writer.write(image)
# Release the video writer
video_writer.release()
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()
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