Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(615)

Unified Diff: third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp

Issue 2502723002: ShapeDetection: implement barcode detection, blink part (Closed)
Patch Set: haraken@ comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
diff --git a/third_party/WebKit/Source/modules/shapedetection/FaceDetector.cpp b/third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
similarity index 69%
copy from third_party/WebKit/Source/modules/shapedetection/FaceDetector.cpp
copy to third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
index eadf3eb0ae2c8255c96b8c4b276a634b3d7c8022..53d95c579b351bd130f9c5bb63937ec230c6091c 100644
--- a/third_party/WebKit/Source/modules/shapedetection/FaceDetector.cpp
+++ b/third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "modules/shapedetection/FaceDetector.h"
+#include "modules/shapedetection/ShapeDetector.h"
#include "core/dom/DOMException.h"
#include "core/dom/DOMRect.h"
@@ -14,6 +14,7 @@
#include "core/html/HTMLImageElement.h"
#include "core/html/HTMLVideoElement.h"
#include "core/html/canvas/CanvasImageSource.h"
+#include "modules/shapedetection/DetectedBarcode.h"
#include "platform/graphics/Image.h"
#include "public/platform/InterfaceProvider.h"
#include "third_party/skia/include/core/SkImage.h"
@@ -42,18 +43,16 @@ static CanvasImageSource* toImageSourceInternal(
} // anonymous namespace
-FaceDetector* FaceDetector::create(ScriptState* scriptState) {
- return new FaceDetector(*scriptState->domWindow()->frame());
-}
-
-FaceDetector::FaceDetector(LocalFrame& frame) {
+ShapeDetector::ShapeDetector(LocalFrame& frame) {
DCHECK(!m_service.is_bound());
DCHECK(frame.interfaceProvider());
frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_service));
}
-ScriptPromise FaceDetector::detect(ScriptState* scriptState,
- const CanvasImageSourceUnion& imageSource) {
+ScriptPromise ShapeDetector::detectShapes(
+ ScriptState* scriptState,
+ DetectorType detectorType,
+ const CanvasImageSourceUnion& imageSource) {
CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource);
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
@@ -75,23 +74,26 @@ ScriptPromise FaceDetector::detect(ScriptState* scriptState,
}
if (imageSource.isHTMLImageElement()) {
- return detectFacesOnImageElement(
- resolver, static_cast<HTMLImageElement*>(imageSourceInternal));
+ return detectShapesOnImageElement(
+ detectorType, resolver,
+ static_cast<HTMLImageElement*>(imageSourceInternal));
}
if (imageSourceInternal->isImageBitmap()) {
- return detectFacesOnImageBitmap(
- resolver, static_cast<ImageBitmap*>(imageSourceInternal));
+ return detectShapesOnImageBitmap(
+ detectorType, resolver, static_cast<ImageBitmap*>(imageSourceInternal));
}
if (imageSourceInternal->isVideoElement()) {
- return detectFacesOnVideoElement(
- resolver, static_cast<HTMLVideoElement*>(imageSourceInternal));
+ return detectShapesOnVideoElement(
+ detectorType, resolver,
+ static_cast<HTMLVideoElement*>(imageSourceInternal));
}
NOTREACHED();
return promise;
}
-ScriptPromise FaceDetector::detectFacesOnImageElement(
+ScriptPromise ShapeDetector::detectShapesOnImageElement(
+ DetectorType detectorType,
ScriptPromiseResolver* resolver,
const HTMLImageElement* img) {
ScriptPromise promise = resolver->promise();
@@ -155,21 +157,35 @@ ScriptPromise FaceDetector::detectFacesOnImageElement(
if (!m_service) {
resolver->reject(DOMException::create(
- NotSupportedError, "Face detection service unavailable."));
+ NotSupportedError, "Shape detection service unavailable."));
return promise;
}
m_serviceRequests.add(resolver);
DCHECK(m_service.is_bound());
- m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(),
- img->naturalHeight(),
- convertToBaseCallback(WTF::bind(
- &FaceDetector::onDetectFace, wrapPersistent(this),
- wrapPersistent(resolver))));
+ if (detectorType == DetectorType::Face) {
+ m_service->DetectFaces(
+ std::move(sharedBufferHandle), img->naturalWidth(),
+ img->naturalHeight(),
+ convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectFaces,
+ wrapPersistent(this),
+ wrapPersistent(resolver))));
+ } else if (detectorType == DetectorType::Barcode) {
+ m_service->DetectBarcodes(
+ std::move(sharedBufferHandle), img->naturalWidth(),
+ img->naturalHeight(),
+ convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes,
+ wrapPersistent(this),
+ wrapPersistent(resolver))));
+ } else {
+ NOTREACHED() << "Unsupported detector type";
+ }
+
return promise;
}
-ScriptPromise FaceDetector::detectFacesOnImageBitmap(
+ScriptPromise ShapeDetector::detectShapesOnImageBitmap(
+ DetectorType detectorType,
ScriptPromiseResolver* resolver,
ImageBitmap* imageBitmap) {
ScriptPromise promise = resolver->promise();
@@ -203,12 +219,13 @@ ScriptPromise FaceDetector::detectFacesOnImageBitmap(
allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */;
}
- return detectFacesOnData(resolver, pixelDataPtr,
- allocationSize.ValueOrDefault(0),
- imageBitmap->width(), imageBitmap->height());
+ return detectShapesOnData(detectorType, resolver, pixelDataPtr,
+ allocationSize.ValueOrDefault(0),
+ imageBitmap->width(), imageBitmap->height());
}
-ScriptPromise FaceDetector::detectFacesOnVideoElement(
+ScriptPromise ShapeDetector::detectShapesOnVideoElement(
+ DetectorType detectorType,
ScriptPromiseResolver* resolver,
const HTMLVideoElement* video) {
ScriptPromise promise = resolver->promise();
@@ -250,16 +267,17 @@ ScriptPromise FaceDetector::detectFacesOnVideoElement(
return promise;
}
- return detectFacesOnData(resolver, pixelDataPtr,
- allocationSize.ValueOrDefault(0), image->width(),
- image->height());
+ return detectShapesOnData(detectorType, resolver, pixelDataPtr,
+ allocationSize.ValueOrDefault(0), image->width(),
+ image->height());
}
-ScriptPromise FaceDetector::detectFacesOnData(ScriptPromiseResolver* resolver,
- uint8_t* data,
- int size,
- int width,
- int height) {
+ScriptPromise ShapeDetector::detectShapesOnData(DetectorType detectorType,
+ ScriptPromiseResolver* resolver,
+ uint8_t* data,
+ int size,
+ int width,
+ int height) {
DCHECK(data);
DCHECK(size);
ScriptPromise promise = resolver->promise();
@@ -274,7 +292,7 @@ ScriptPromise FaceDetector::detectFacesOnData(ScriptPromiseResolver* resolver,
if (!m_service) {
resolver->reject(DOMException::create(
- NotSupportedError, "Face detection service unavailable."));
+ NotSupportedError, "Shape detection service unavailable."));
return promise;
}
@@ -286,15 +304,26 @@ ScriptPromise FaceDetector::detectFacesOnData(ScriptPromiseResolver* resolver,
m_serviceRequests.add(resolver);
DCHECK(m_service.is_bound());
- m_service->DetectFace(std::move(sharedBufferHandle), width, height,
- convertToBaseCallback(WTF::bind(
- &FaceDetector::onDetectFace, wrapPersistent(this),
- wrapPersistent(resolver))));
+ if (detectorType == DetectorType::Face) {
+ m_service->DetectFaces(
+ std::move(sharedBufferHandle), width, height,
+ convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectFaces,
+ wrapPersistent(this),
+ wrapPersistent(resolver))));
+ } else if (detectorType == DetectorType::Barcode) {
+ m_service->DetectBarcodes(
+ std::move(sharedBufferHandle), width, height,
+ convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes,
+ wrapPersistent(this),
+ wrapPersistent(resolver))));
+ } else {
+ NOTREACHED() << "Unsupported detector type";
+ }
sharedBufferHandle.reset();
return promise;
}
-void FaceDetector::onDetectFace(
+void ShapeDetector::onDetectFaces(
ScriptPromiseResolver* resolver,
mojom::blink::FaceDetectionResultPtr faceDetectionResult) {
if (!m_serviceRequests.contains(resolver))
@@ -311,7 +340,26 @@ void FaceDetector::onDetectFace(
m_serviceRequests.remove(resolver);
}
-DEFINE_TRACE(FaceDetector) {
+void ShapeDetector::onDetectBarcodes(
+ ScriptPromiseResolver* resolver,
+ Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) {
+ if (!m_serviceRequests.contains(resolver))
+ return;
+ m_serviceRequests.remove(resolver);
+
+ HeapVector<Member<DetectedBarcode>> detectedBarcodes;
+ for (const auto& barcode : barcodeDetectionResults) {
+ detectedBarcodes.append(DetectedBarcode::create(
+ barcode->raw_value,
+ DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y,
+ barcode->bounding_box->width,
+ barcode->bounding_box->height)));
+ }
+
+ resolver->resolve(detectedBarcodes);
+}
+
+DEFINE_TRACE(ShapeDetector) {
visitor->trace(m_serviceRequests);
}

Powered by Google App Engine
This is Rietveld 408576698