-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New camera quality test using brisque algorithm included in "camera-cert-automated"
- Loading branch information
1 parent
b8e5c08
commit 8cc0b39
Showing
3 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# This file is part of Checkbox. | ||
# | ||
# Copyright 2008-2023 Canonical Ltd. | ||
# Written by: | ||
# Fernando Bravo <[email protected]> | ||
# | ||
# Checkbox is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3, | ||
# as published by the Free Software Foundation. | ||
# | ||
# Checkbox is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
import argparse | ||
import sys | ||
|
||
from cv2 import VideoCapture, imwrite | ||
from numpy import isnan | ||
|
||
from checkbox_support.vendor.brisque import BRISQUE | ||
from tempfile import NamedTemporaryFile | ||
|
||
THRESHOLD = 60 | ||
|
||
|
||
def brisque(device: str = "video0"): | ||
""" | ||
Captures an image to a file and computes the quality using the | ||
Blinded/Unreferenced Spatial Image Quality Evaluator (BRISQUE). If the | ||
score is below a certain threshold, the test passes. | ||
""" | ||
|
||
brisque = BRISQUE() | ||
|
||
# Set the video device | ||
cam = VideoCapture(f"/dev/{device}") | ||
if not cam.isOpened(): | ||
print("Cannot open the selected device", file=sys.stderr) | ||
return 1 | ||
|
||
# Discard the first frames | ||
for i in range(15): | ||
if not cam.grab(): | ||
print("Cannot read from the selected device", file=sys.stderr) | ||
return 1 | ||
|
||
# Get the image | ||
result, image = cam.read() | ||
if not result: | ||
print("Cannot read from the selected device", file=sys.stderr) | ||
return 1 | ||
|
||
# Save it in a temporary file | ||
f = NamedTemporaryFile(prefix=f'camera_test_brisque_{device}_', | ||
suffix='.jpg', delete=False) | ||
imwrite(f.name, image) | ||
print("Image saved to %s" % f.name) | ||
|
||
# Compute the BRISQUE score | ||
score = brisque.score(f.name) | ||
if isnan(score): | ||
print("Unable to compute BRISQUE score", file=sys.stderr) | ||
return 1 | ||
elif score > THRESHOLD: | ||
print("The BRISQUE score is too high: %s" % score, file=sys.stderr) | ||
return 1 | ||
|
||
print("BRISQUE score: %s" % score) | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Run the image quality test") | ||
parser.add_argument("-d", "--device", default="video0", | ||
help="Device for the webcam to use") | ||
args = parser.parse_args() | ||
|
||
sys.exit(brisque(args.device)) |
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
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