{"id":21352,"url":"https://patchwork.libcamera.org/api/patches/21352/?format=json","web_url":"https://patchwork.libcamera.org/patch/21352/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20240925081551.3265942-4-chenghaoyang@google.com>","date":"2024-09-25T08:12:26","name":"[v8,3/3] libcamera: android: Add face detection control support","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"60ee843782cfd03e0a73c8c800808699beccfd45","submitter":{"id":117,"url":"https://patchwork.libcamera.org/api/people/117/?format=json","name":"Cheng-Hao Yang","email":"chenghaoyang@chromium.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21352/mbox/","series":[{"id":4622,"url":"https://patchwork.libcamera.org/api/series/4622/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4622","date":"2024-09-25T08:12:23","name":"Add Face Detection Controls","version":8,"mbox":"https://patchwork.libcamera.org/series/4622/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21352/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21352/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 D320EC0F1B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 25 Sep 2024 08:16:12 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 78FD863513;\n\tWed, 25 Sep 2024 10:16:12 +0200 (CEST)","from mail-pl1-x635.google.com (mail-pl1-x635.google.com\n\t[IPv6:2607:f8b0:4864:20::635])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8AEA363513\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 25 Sep 2024 10:16:07 +0200 (CEST)","by mail-pl1-x635.google.com with SMTP id\n\td9443c01a7336-206bd1c6ccdso61060965ad.3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 25 Sep 2024 01:16:07 -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-20b002aaa8fsm6336465ad.240.2024.09.25.01.16.03\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tWed, 25 Sep 2024 01:16:05 -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=\"akeOn2os\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=chromium.org; s=google; t=1727252165; x=1727856965;\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=wbC26ApX1s3jY2T8aakSTnoyjjRaO6JZdJgUgnelnAc=;\n\tb=akeOn2osiyvxQbFK98SndtCnRZQSkRTcwzRylm2T1IwKALAZWN5LTnvD2FhvWqxRPF\n\tsUlcnuecY0JgtGCo7gX1JWqgwrieED76D+Ib3cvMfDxU0uhY/QWxm9cTh+di0GSO8QMw\n\t3InJGKXCcD83cALJs4x86H+X2xE2mX09ySkRY=","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1727252165; x=1727856965;\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=wbC26ApX1s3jY2T8aakSTnoyjjRaO6JZdJgUgnelnAc=;\n\tb=Ncv60Q5OmtgXd9+CeANH79Pg1zZgyAvdx11bsSnnrQsnLpqIIsyDOA46oghcPz5ySY\n\tcoywRHOXJgwDNTcaJeWzrcaHKMz2b4ziG4GK+dJbt3EYQ6sQW7NIKXpi7S3xm2y7OG4x\n\tzd3wUPdcpGfUit4hURb/l9LuKH0H8mUlFWGqAVyuOBs/CT6zPFy03FwyYf67XkMrPusI\n\tEuwO94qhLVH66Sz5CC7q7AOayh8uXyVK9SzIw1iESvReMrFKb1u2kN/s6DUfdeH1TC/E\n\tNLDSdAaeBUhduOznz+qxAnlmGr0eAZEAHcRuf7TSN9WWLwekfoN6t84ge7Z8mq2FEnET\n\tegzw==","X-Gm-Message-State":"AOJu0YwVzc+UKntBGBxVeOL7taHouwhwFm3xlf8U3t+wbQVszWg2OoVI\n\tw8BwLR93MvQlI3sJyq2l+EbZ+L4G5Eu65VTLLiZ+u8mX1fJDuu/knJfqkpP0h0dnUbMehjmkIS4\n\t=","X-Google-Smtp-Source":"AGHT+IEWC5hzX5lmMv3nRzFu5mEiiHItJ+kI728bQJX+yVdV3kGw0UWZa1cLUgWsZmIjZKxV+EXC8A==","X-Received":"by 2002:a17:902:e54c:b0:1fb:90e1:c8c5 with SMTP id\n\td9443c01a7336-20afc48670bmr28580705ad.33.1727252165506; \n\tWed, 25 Sep 2024 01:16:05 -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>,\n\tJacopo Mondi <jacopo.mondi@ideasonboard.com>","Subject":"[PATCH v8 3/3] libcamera: android: Add face detection control\n\tsupport","Date":"Wed, 25 Sep 2024 08:12:26 +0000","Message-ID":"<20240925081551.3265942-4-chenghaoyang@google.com>","X-Mailer":"git-send-email 2.46.0.792.g87dc391469-goog","In-Reply-To":"<20240925081551.3265942-1-chenghaoyang@google.com>","References":"<20240925081551.3265942-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>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n---\n src/android/camera_capabilities.cpp | 45 +++++++++++++++++--\n src/android/camera_device.cpp       | 70 ++++++++++++++++++++++++++++-\n 2 files changed, 109 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..e80b9e17 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,64 @@ 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\tstd::vector<int32_t> flatRectangles;\n+\t\tfor (const Rectangle &rect : *faceDetectRectangles) {\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\tif (faceDetectFaceScores->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: \" << faceDetectFaceScores->size();\n+\t\t}\n+\t\tresultMetadata->addEntry(ANDROID_STATISTICS_FACE_SCORES,\n+\t\t\t\t\t *faceDetectRectangles);\n+\t}\n+\n+\tconst auto &faceDetectFaceLandmarks =\n+\t\tmetadata.get(controls::draft::FaceDetectFaceLandmarks);\n+\tif (faceDetectRectangles && faceDetectFaceLandmarks) {\n+\t\tsize_t expectedLandmarks = faceDetectRectangles->size() * 3;\n+\t\tif (faceDetectFaceLandmarks->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: \" << faceDetectFaceLandmarks->size();\n+\t\t}\n+\n+\t\tstd::vector<int32_t> androidLandmarks;\n+\t\tfor (const auto &landmark : *faceDetectFaceLandmarks) {\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 && faceDetectFaceIds) {\n+\t\tif (faceDetectFaceIds->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: \" << faceDetectFaceIds->size();\n+\t\t}\n+\t\tresultMetadata->addEntry(ANDROID_STATISTICS_FACE_IDS, *faceDetectFaceIds);\n+\t}\n+\n \tconst auto &scalerCrop = metadata.get(controls::ScalerCrop);\n \tif (scalerCrop) {\n \t\tconst Rectangle &crop = *scalerCrop;\n","prefixes":["v8","3/3"]}