blob: 8b32057a85e77764d7c48b5f58668e8d222783c4 [file] [log] [blame]
# Copyright 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Verify zoom ratio scales circle sizes correctly."""
import logging
import math
import os.path
from mobly import test_runner
import numpy as np
import cv2
import its_base_test
import camera_properties_utils
import capture_request_utils
import image_processing_utils
import its_session_utils
import opencv_processing_utils
CIRCLE_COLOR = 0 # [0: black, 255: white]
CIRCLE_TOL = 0.05 # contour area vs ideal circle area pi*((w+h)/4)**2
LINE_COLOR = (255, 0, 0) # red
LINE_THICKNESS = 5
MIN_AREA_RATIO = 0.00015 # based on 2000/(4000x3000) pixels
MIN_CIRCLE_PTS = 25
NAME = os.path.splitext(os.path.basename(__file__))[0]
NUM_STEPS = 10
OFFSET_RTOL = 0.15
RADIUS_RTOL = 0.10
ZOOM_MAX_THRESH = 10.0
ZOOM_MIN_THRESH = 2.0
def distance(x, y):
return math.sqrt(x**2 + y**2)
def circle_cropped(circle, size):
"""Determine if a circle is cropped by edge of img.
Args:
circle: list [x, y, radius] of circle
size: tuple (x, y) of size of img
Returns:
Boolean True if selected circle is cropped
"""
cropped = False
circle_x, circle_y = circle[0], circle[1]
circle_r = circle[2]
x_min, x_max = circle_x - circle_r, circle_x + circle_r
y_min, y_max = circle_y - circle_r, circle_y + circle_r
if x_min < 0 or y_min < 0 or x_max > size[0] or y_max > size[1]:
cropped = True
return cropped
def find_center_circle(img, img_name, color, min_area, debug):
"""Find the circle closest to the center of the image.
Finds all contours in the image. Rejects those too small and not enough
points to qualify as a circle. The remaining contours must have center
point of color=color and are sorted based on distance from the center
of the image. The contour closest to the center of the image is returned.
Note: hierarchy is not used as the hierarchy for black circles changes
as the zoom level changes.
Args:
img: numpy img array with pixel values in [0,255].
img_name: str file name for saved image
color: int 0 --> black, 255 --> white
min_area: int minimum area of circles to screen out
debug: bool to save extra data
Returns:
circle: [center_x, center_y, radius]
"""
# gray scale & otsu threshold to binarize the image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, img_bw = cv2.threshold(
np.uint8(gray), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# use OpenCV to find contours (connected components)
_, contours, _ = cv2.findContours(255 - img_bw, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
# check contours and find the best circle candidates
circles = []
img_ctr = [gray.shape[1] // 2, gray.shape[0] // 2]
for contour in contours:
area = cv2.contourArea(contour)
if area > min_area and len(contour) >= MIN_CIRCLE_PTS:
shape = opencv_processing_utils.component_shape(contour)
radius = (shape['width'] + shape['height']) / 4
colour = img_bw[shape['cty']][shape['ctx']]
circlish = round((math.pi * radius**2) / area, 4)
if colour == color and (1 - CIRCLE_TOL <= circlish <= 1 + CIRCLE_TOL):
circles.append([shape['ctx'], shape['cty'], radius, circlish, area])
if debug == 'true':
circles.sort(key=lambda x: abs(x[3] - 1.0)) # sort for best circles
logging.debug('circles [x, y, r, pi*r**2/area, area]: %s', str(circles))
# find circle closest to center
circles.sort(key=lambda x: distance(x[0] - img_ctr[0], x[1] - img_ctr[1]))
circle = circles[0]
# mark image center
size = gray.shape
m_x, m_y = size[1] // 2, size[0] // 2
marker_size = LINE_THICKNESS * 10
cv2.drawMarker(img, (m_x, m_y), LINE_COLOR, markerType=cv2.MARKER_CROSS,
markerSize=marker_size, thickness=LINE_THICKNESS)
# add circle to saved image
center_i = (int(round(circle[0], 0)), int(round(circle[1], 0)))
radius_i = int(round(circle[2], 0))
cv2.circle(img, center_i, radius_i, LINE_COLOR, LINE_THICKNESS)
image_processing_utils.write_image(img / 255.0, img_name)
if not circles:
raise AssertionError('No circle was detected. Please take pictures '
'according to instructions carefully!')
return [circle[0], circle[1], circle[2]]
class ZoomTest(its_base_test.ItsBaseTest):
"""Test the camera zoom behavior.
"""
def test_zoom(self):
z_test_list = []
fls = []
circles = []
with its_session_utils.ItsSession(
device_id=self.dut.serial,
camera_id=self.camera_id,
hidden_physical_id=self.hidden_physical_id) as cam:
props = cam.get_camera_properties()
props = cam.override_with_hidden_physical_camera_props(props)
camera_properties_utils.skip_unless(
camera_properties_utils.zoom_ratio_range(props))
# Load chart for scene
its_session_utils.load_scene(
cam, props, self.scene, self.tablet, self.chart_distance)
z_range = props['android.control.zoomRatioRange']
logging.debug('testing zoomRatioRange: %s', str(z_range))
yuv_size = capture_request_utils.get_largest_yuv_format(props)
size = [yuv_size['width'], yuv_size['height']]
debug = self.debug_mode
z_min, z_max = float(z_range[0]), float(z_range[1])
camera_properties_utils.skip_unless(z_max >= z_min * ZOOM_MIN_THRESH)
z_list = np.arange(z_min, z_max, float(z_max - z_min) / (NUM_STEPS - 1))
z_list = np.append(z_list, z_max)
# do captures over zoom range and find circles with cv2
logging.debug('cv2_version: %s', cv2.__version__)
req = capture_request_utils.auto_capture_request()
for i, z in enumerate(z_list):
logging.debug('zoom ratio: %.2f', z)
req['android.control.zoomRatio'] = z
cap = cam.do_capture(req, cam.CAP_YUV)
img = image_processing_utils.convert_capture_to_rgb_image(
cap, props=props)
img_name = '%s_%s.jpg' % (os.path.join(self.log_path,
NAME), round(z, 2))
image_processing_utils.write_image(img, img_name)
# convert to [0, 255] images with unsigned integer
img *= 255
img = img.astype(np.uint8)
# Find the center circle in img
circle = find_center_circle(
img, img_name, CIRCLE_COLOR,
min_area=MIN_AREA_RATIO * size[0] * size[1] * z * z,
debug=debug)
if circle_cropped(circle, size):
logging.debug('zoom %.2f is too large! Skip further captures', z)
break
circles.append(circle)
z_test_list.append(z)
fls.append(cap['metadata']['android.lens.focalLength'])
# assert some range is tested before circles get too big
zoom_max_thresh = ZOOM_MAX_THRESH
if z_max < ZOOM_MAX_THRESH:
zoom_max_thresh = z_max
if z_test_list[-1] < zoom_max_thresh:
raise AssertionError(f'Max zoom level tested: {z_test_list[-1]}, '
f'THRESH: {zoom_max_thresh}')
# initialize relative size w/ zoom[0] for diff zoom ratio checks
radius_0 = float(circles[0][2])
z_0 = float(z_test_list[0])
for i, z in enumerate(z_test_list):
logging.debug('Zoom: %.2f, fl: %.2f', z, fls[i])
offset_abs = [(circles[i][0] - size[0] // 2),
(circles[i][1] - size[1] // 2)]
logging.debug('Circle r: %.1f, center offset x, y: %d, %d', circles[i][2],
offset_abs[0], offset_abs[1])
z_ratio = z / z_0
# check relative size against zoom[0]
radius_ratio = circles[i][2] / radius_0
logging.debug('r ratio req: %.3f, measured: %.3f', z_ratio, radius_ratio)
if not np.isclose(z_ratio, radius_ratio, rtol=RADIUS_RTOL):
raise AssertionError(f'zoom: {z_ratio:.2f}, radius ratio: '
f'{radius_ratio:.2f}, RTOL: {RADIUS_RTOL}')
# check relative offset against init vals w/ no focal length change
if i == 0 or fls[i - 1] != fls[i]: # set init values
z_init = float(z_test_list[i])
offset_init = [circles[i][0] - size[0]//2, circles[i][1] - size[1]//2]
else: # check
z_ratio = z / z_init
offset_rel = (distance(offset_abs[0], offset_abs[1]) / z_ratio /
distance(offset_init[0], offset_init[1]))
logging.debug('offset_rel: %.3f', offset_rel)
if not np.isclose(offset_rel, 1.0, rtol=OFFSET_RTOL):
raise AssertionError(f'zoom: {z:.2f}, offset(rel): {offset_rel:.4f}, '
f'RTOL: {OFFSET_RTOL}')
if __name__ == '__main__':
test_runner.main()