Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow quality_check.py test to use a file for bitrate checks #1068

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions tests/quality_check.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/usr/bin/python3

import io
import os
import time

from picamera2 import Picamera2
import cv2

from picamera2 import MappedArray, Picamera2
from picamera2.encoders import H264Encoder, JpegEncoder, MJPEGEncoder, Quality
from picamera2.outputs import FileOutput

Expand All @@ -10,7 +15,38 @@
# between very low/high is so great that the behaviour should be reliable enough.


# If ASSET_DIR is set, we can inject this file instead of using camera images.
FILENAME = "quality_check.mp4"
FRAMES = []
COUNTER = 0
SIZE = (640, 360)
if os.environ.get('ASSET_DIR') is not None:
filename = os.path.join(os.path.expanduser(os.environ['ASSET_DIR']), FILENAME)
if os.path.isfile(filename):
print("Using file", filename)
cap = cv2.VideoCapture(filename)
while len(FRAMES) < 100:
ret, frame = cap.read()
if not ret:
break
if frame.shape != (SIZE[1], SIZE[0], 3):
frame = cv2.resize(frame, dsize=SIZE)
FRAMES.append(frame)
else:
print("Using camera")


def callback(request):
if FRAMES:
global COUNTER
with MappedArray(request, 'main') as m:
m.array[...] = FRAMES[COUNTER]
COUNTER = (COUNTER + 1) % len(FRAMES)


def do_encode(encoder, quality):
global COUNTER
COUNTER = 0
data = io.BytesIO()
picam2.start_encoder(encoder, output=FileOutput(data), name='main', quality=quality)
time.sleep(time_seconds)
Expand All @@ -19,25 +55,26 @@ def do_encode(encoder, quality):


picam2 = Picamera2()
config = picam2.create_video_configuration({'format': 'RGB888', 'size': (640, 360)}, lores={'size': (640, 360)})
config = picam2.create_video_configuration({'format': 'RGB888', 'size': SIZE}, lores={'size': SIZE})
picam2.configure(config)
picam2.start()
picam2.pre_callback = callback
time_seconds = 5

low_quality = do_encode(MJPEGEncoder(), Quality.VERY_LOW)
high_quality = do_encode(MJPEGEncoder(), Quality.VERY_HIGH)
print("MJPEGEncoder: low quality", low_quality, "high quality", high_quality)
if (low_quality > high_quality):
if (1.5 * low_quality > high_quality):
print("Error: MJPEGEncoder file sizes not as expected")

low_quality = do_encode(H264Encoder(), Quality.VERY_LOW)
high_quality = do_encode(H264Encoder(), Quality.VERY_HIGH)
print("H264Encoder: low quality", low_quality, "high quality", high_quality)
if (low_quality > high_quality):
if (1.5 * low_quality > high_quality):
print("Error: H264Encoder file sizes not as expected")

low_quality = do_encode(JpegEncoder(), Quality.VERY_LOW)
high_quality = do_encode(JpegEncoder(), Quality.VERY_HIGH)
print("JpegEncoder: low quality", low_quality, "high quality", high_quality)
if (low_quality > high_quality):
if (1.5 * low_quality > high_quality):
print("Error: JpegEncoder file sizes not as expected")
Loading