{"id":21333,"url":"https://patchwork.libcamera.org/api/1.1/patches/21333/?format=json","web_url":"https://patchwork.libcamera.org/patch/21333/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20240923093317.3500444-4-chenghaoyang@google.com>","date":"2024-09-23T09:30:46","name":"[v5,3/3] libcamera: android: Add face detection control support","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"ea49367b89b27e29d75fee07b4751cd213b77abb","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/1.1/people/117/?format=json","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21333/mbox/","series":[{"id":4614,"url":"https://patchwork.libcamera.org/api/1.1/series/4614/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4614","date":"2024-09-23T09:30:43","name":"Add Face Detection Controls","version":5,"mbox":"https://patchwork.libcamera.org/series/4614/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21333/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21333/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4E145C0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 23 Sep 2024 09:33:36 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id F037663511;\n\tMon, 23 Sep 2024 11:33:35 +0200 (CEST)","from mail-pl1-x629.google.com (mail-pl1-x629.google.com\n\t[IPv6:2607:f8b0:4864:20::629])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 7C67C63512\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Sep 2024 11:33:29 +0200 (CEST)","by mail-pl1-x629.google.com with SMTP id\n\td9443c01a7336-20543fdb7acso32164775ad.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Sep 2024 02:33:29 -0700 (PDT)","from chenghaoyang-low.c.googlers.com.com\n\t(0.223.81.34.bc.googleusercontent.com. [34.81.223.0])\n\tby smtp.gmail.com with ESMTPSA id\n\td9443c01a7336-207946d6673sm129864995ad.158.2024.09.23.02.33.25\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 23 Sep 2024 02:33:26 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=chromium.org header.i=@chromium.org\n\theader.b=\"lfINlWdX\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1727084007; x=1727688807;\n\tdarn=lists.libcamera.org; \n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=E/ouNh/kwG+GNdwoclRM6KB8psYsFquTmInQtIIqDoo=;\n\tb=lfINlWdX0uepLGFx2Rm0v+xBB7sADGcc1W93gpdKcv2vC6xaFDQSJXxrRvBTaeaqze\n\toviDYh2zeCNpCD1VET3IdMJ3fLGkOvSYjmr9JbR3YR1qMH9HVF6+8sQ70rjJAi3CDxRn\n\t7nP6vsfT+hWYdD/nYzxL+fHeov3Dxi99KW9GU=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1727084007; x=1727688807;\n\th=content-transfer-encoding:mime-version:references:in-reply-to\n\t:message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc\n\t:subject:date:message-id:reply-to;\n\tbh=E/ouNh/kwG+GNdwoclRM6KB8psYsFquTmInQtIIqDoo=;\n\tb=gg0Z8gQIf/NQOxS+/fBu8pu6nNupxIfbRF00L+UvWeP+Yz9dGZIdlZUjbGhaonfDKq\n\tNv3gAkpInuczOeP46kmAraRuFL83fAa9H142ivecW6MU2oAYiYlwA/CDhJRGpXX31FTE\n\t1jhTgH+fFGXI5rLWjq6+M1NtLWYSfIBKkedvsuINYCyGEANq2WdWSw2FKqlZ0H6p0i23\n\tPJN/FQDDb+D0Id4XUfjtwI62M92rmQ+m6SLavId2Zzpkd8kDJzqPUdre0YSfZl6iuyz2\n\ttG7E6VpHa1Ok2nGJFF++NGmiJ1mHKza7vhMO4iM9kzVQk3+muSfuSjdnnryCenVJt6xZ\n\tsQYA==","X-Gm-Message-State":"AOJu0YxXBzql14ST7zJ3R5UJfvAOP+xyWaSiN2xOHzQpNV+740hGiwpP\n\tFpFE0kKnEK/vI0MPBgy0kfCRlRLOjEac3mogxOuYPk4JxsKqWbJ3yiB6DRWaIDWh8U1biqiiQ4o\n\t=","X-Google-Smtp-Source":"AGHT+IEuwNS3OjMSCvrsgnygm4MnF/XedDDmd38zkxL/UQ4xfdkldOECJwo6L+sBzhv/w9l3kQE5sA==","X-Received":"by 2002:a17:902:cf4b:b0:200:869c:95e3 with SMTP id\n\td9443c01a7336-208d8370e93mr191608335ad.4.1727084007163; \n\tMon, 23 Sep 2024 02:33:27 -0700 (PDT)","From":"Harvey Yang <chenghaoyang@chromium.org>","X-Google-Original-From":"Harvey Yang <chenghaoyang@google.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Harvey Yang <chenghaoyang@chromium.org>,\n\tYudhistira Erlandinata <yerlandinata@chromium.org>","Subject":"[PATCH v5 3/3] libcamera: android: Add face detection control\n\tsupport","Date":"Mon, 23 Sep 2024 09:30:46 +0000","Message-ID":"<20240923093317.3500444-4-chenghaoyang@google.com>","X-Mailer":"git-send-email 2.46.0.792.g87dc391469-goog","In-Reply-To":"<20240923093317.3500444-1-chenghaoyang@google.com>","References":"<20240923093317.3500444-1-chenghaoyang@google.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"From: Harvey Yang <chenghaoyang@chromium.org>\n\nAllow Android HAL adapter to pass the face detection metadata control to\nthe pipeline and also send face detection metadata to the camera client\nif the pipeline generates it.\n\nSigned-off-by: Yudhistira Erlandinata <yerlandinata@chromium.org>\nCo-developed-by: Harvey Yang <chenghaoyang@chromium.org>\n---\n src/android/camera_capabilities.cpp | 45 +++++++++++++++--\n src/android/camera_device.cpp       | 75 ++++++++++++++++++++++++++++-\n 2 files changed, 114 insertions(+), 6 deletions(-)","diff":"diff --git a/src/android/camera_capabilities.cpp b/src/android/camera_capabilities.cpp\nindex 71043e12..a89a3115 100644\n--- a/src/android/camera_capabilities.cpp\n+++ b/src/android/camera_capabilities.cpp\n@@ -7,6 +7,8 @@\n \n #include \"camera_capabilities.h\"\n \n+#include <stdint.h>\n+\n #include <algorithm>\n #include <array>\n #include <cmath>\n@@ -1176,11 +1178,46 @@ int CameraCapabilities::initializeStaticMetadata()\n \t\t\t\t  maxFrameDuration_);\n \n \t/* Statistics static metadata. */\n-\tuint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;\n-\tstaticMetadata_->addEntry(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,\n-\t\t\t\t  faceDetectMode);\n-\n \tint32_t maxFaceCount = 0;\n+\tauto iter = camera_->controls().find(controls::draft::FaceDetectMode.id());\n+\tif (iter != camera_->controls().end()) {\n+\t\tconst ControlInfo &faceDetectCtrlInfo = iter->second;\n+\t\tstd::vector<uint8_t> faceDetectModes;\n+\t\tbool hasFaceDetection = false;\n+\n+\t\tfor (const auto &value : faceDetectCtrlInfo.values()) {\n+\t\t\tuint8_t mode = value.get<uint8_t>();\n+\t\t\tuint8_t androidMode = 0;\n+\n+\t\t\tswitch (mode) {\n+\t\t\tcase controls::draft::FaceDetectModeOff:\n+\t\t\t\tandroidMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;\n+\t\t\t\tbreak;\n+\t\t\tcase controls::draft::FaceDetectModeSimple:\n+\t\t\t\tandroidMode = ANDROID_STATISTICS_FACE_DETECT_MODE_SIMPLE;\n+\t\t\t\thasFaceDetection = true;\n+\t\t\t\tbreak;\n+\t\t\tdefault:\n+\t\t\t\tLOG(HAL, Fatal) << \"Received invalid face detect mode: \" << mode;\n+\t\t\t}\n+\t\t\tfaceDetectModes.push_back(androidMode);\n+\t\t}\n+\t\tif (hasFaceDetection) {\n+\t\t\t/*\n+\t\t\t * \\todo Create new libcamera controls to query max\n+\t\t\t * possible faces detected.\n+\t\t\t */\n+\t\t\tmaxFaceCount = 10;\n+\t\t\tstaticMetadata_->addEntry(\n+\t\t\t\tANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,\n+\t\t\t\tfaceDetectModes.data(), faceDetectModes.size());\n+\t\t}\n+\t} else {\n+\t\tuint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;\n+\t\tstaticMetadata_->addEntry(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,\n+\t\t\t\t\t  faceDetectMode);\n+\t}\n+\n \tstaticMetadata_->addEntry(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,\n \t\t\t\t  maxFaceCount);\n \ndiff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp\nindex 493f66e7..50caa14a 100644\n--- a/src/android/camera_device.cpp\n+++ b/src/android/camera_device.cpp\n@@ -15,6 +15,7 @@\n #include <vector>\n \n #include <libcamera/base/log.h>\n+#include <libcamera/base/span.h>\n #include <libcamera/base/unique_fd.h>\n #include <libcamera/base/utils.h>\n \n@@ -22,6 +23,7 @@\n #include <libcamera/controls.h>\n #include <libcamera/fence.h>\n #include <libcamera/formats.h>\n+#include <libcamera/geometry.h>\n #include <libcamera/property_ids.h>\n \n #include \"system/graphics.h\"\n@@ -813,6 +815,11 @@ int CameraDevice::processControls(Camera3RequestDescriptor *descriptor)\n \t\tcontrols.set(controls::ScalerCrop, cropRegion);\n \t}\n \n+\tif (settings.getEntry(ANDROID_STATISTICS_FACE_DETECT_MODE, &entry)) {\n+\t\tconst uint8_t *data = entry.data.u8;\n+\t\tcontrols.set(controls::draft::FaceDetectMode, data[0]);\n+\t}\n+\n \tif (settings.getEntry(ANDROID_SENSOR_TEST_PATTERN_MODE, &entry)) {\n \t\tconst int32_t data = *entry.data.i32;\n \t\tint32_t testPatternMode = controls::draft::TestPatternModeOff;\n@@ -1540,8 +1547,9 @@ CameraDevice::getResultMetadata(const Camera3RequestDescriptor &descriptor) cons\n \tvalue32 = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;\n \tresultMetadata->addEntry(ANDROID_SENSOR_TEST_PATTERN_MODE, value32);\n \n-\tvalue = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;\n-\tresultMetadata->addEntry(ANDROID_STATISTICS_FACE_DETECT_MODE, value);\n+\tsettings.getEntry(ANDROID_STATISTICS_FACE_DETECT_MODE, &entry);\n+\tresultMetadata->addEntry(ANDROID_STATISTICS_FACE_DETECT_MODE,\n+\t\t\t\t entry.data.u8, 1);\n \n \tvalue = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;\n \tresultMetadata->addEntry(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,\n@@ -1580,6 +1588,69 @@ CameraDevice::getResultMetadata(const Camera3RequestDescriptor &descriptor) cons\n \t\tresultMetadata->addEntry(ANDROID_SENSOR_FRAME_DURATION,\n \t\t\t\t\t *frameDuration * 1000);\n \n+\tconst auto &faceDetectRectangles =\n+\t\tmetadata.get(controls::draft::FaceDetectFaceRectangles);\n+\tif (faceDetectRectangles) {\n+\t\tconst Span<const Rectangle> rectangles = *faceDetectRectangles;\n+\t\tstd::vector<int32_t> flatRectangles;\n+\t\tfor (const Rectangle &rect : rectangles) {\n+\t\t\tflatRectangles.push_back(rect.x);\n+\t\t\tflatRectangles.push_back(rect.y);\n+\t\t\tflatRectangles.push_back(rect.x + rect.width);\n+\t\t\tflatRectangles.push_back(rect.y + rect.height);\n+\t\t}\n+\t\tresultMetadata->addEntry(\n+\t\t\tANDROID_STATISTICS_FACE_RECTANGLES, flatRectangles);\n+\t}\n+\n+\tconst auto &faceDetectFaceScores =\n+\t\tmetadata.get(controls::draft::FaceDetectFaceScores);\n+\tif (faceDetectRectangles && faceDetectFaceScores) {\n+\t\tconst Span<const uint8_t> &scores = *faceDetectFaceScores;\n+\t\tif (scores.size() != faceDetectRectangles->size()) {\n+\t\t\tLOG(HAL, Error) << \"Pipeline returned wrong number of face scores; \"\n+\t\t\t\t\t<< \"Expected: \"\n+\t\t\t\t\t<< faceDetectRectangles->size()\n+\t\t\t\t\t<< \", got:\" << scores.size();\n+\t\t}\n+\t\tresultMetadata->addEntry(ANDROID_STATISTICS_FACE_SCORES, scores);\n+\t}\n+\n+\tconst auto &faceDetectFaceLandmarks =\n+\t\tmetadata.get(controls::draft::FaceDetectFaceLandmarks);\n+\tif (faceDetectRectangles && faceDetectFaceScores &&\n+\t    faceDetectFaceLandmarks) {\n+\t\tconst auto &landmarks = *faceDetectFaceLandmarks;\n+\t\tsize_t expectedLandmarks = faceDetectRectangles->size() * 3;\n+\t\tif (landmarks.size() != expectedLandmarks) {\n+\t\t\tLOG(HAL, Error) << \"Pipeline returned wrong number of face landmarks; \"\n+\t\t\t\t\t<< \"Expected: \" << expectedLandmarks\n+\t\t\t\t\t<< \", got: \" << landmarks.size();\n+\t\t}\n+\n+\t\tstd::vector<int32_t> androidLandmarks;\n+\t\tfor (const auto &landmark : landmarks) {\n+\t\t\tandroidLandmarks.push_back(landmark.x);\n+\t\t\tandroidLandmarks.push_back(landmark.y);\n+\t\t}\n+\t\tresultMetadata->addEntry(\n+\t\t\tANDROID_STATISTICS_FACE_LANDMARKS, androidLandmarks);\n+\t}\n+\n+\tconst auto &faceDetectFaceIds =\n+\t\tmetadata.get(controls::draft::FaceDetectFaceIds);\n+\tif (faceDetectRectangles && faceDetectFaceScores &&\n+\t    faceDetectFaceLandmarks && faceDetectFaceIds) {\n+\t\tconst Span<const int32_t> &ids = *faceDetectFaceIds;\n+\t\tif (ids.size() != faceDetectRectangles->size()) {\n+\t\t\tLOG(HAL, Error) << \"Pipeline returned wrong number of face ids; \"\n+\t\t\t\t\t<< \"Expected: \"\n+\t\t\t\t\t<< faceDetectRectangles->size()\n+\t\t\t\t\t<< \", got:\" << ids.size();\n+\t\t}\n+\t\tresultMetadata->addEntry(ANDROID_STATISTICS_FACE_IDS, ids);\n+\t}\n+\n \tconst auto &scalerCrop = metadata.get(controls::ScalerCrop);\n \tif (scalerCrop) {\n \t\tconst Rectangle &crop = *scalerCrop;\n","prefixes":["v5","3/3"]}