| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/shapedetection/FaceDetector.h" | 5 #include "modules/shapedetection/FaceDetector.h" |
| 6 | 6 |
| 7 #include "core/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/DOMRect.h" |
| 8 #include "core/frame/LocalDOMWindow.h" | 9 #include "core/frame/LocalDOMWindow.h" |
| 9 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| 10 #include "core/html/canvas/CanvasImageSource.h" | 11 #include "core/html/canvas/CanvasImageSource.h" |
| 12 #include "public/platform/InterfaceProvider.h" |
| 11 | 13 |
| 12 namespace blink { | 14 namespace blink { |
| 13 | 15 |
| 14 FaceDetector* FaceDetector::create(Document& document, | 16 FaceDetector* FaceDetector::create(Document& document, |
| 15 const FaceDetectorOptions& options) { | 17 const FaceDetectorOptions& options) { |
| 16 return new FaceDetector(*document.frame(), options); | 18 return new FaceDetector(*document.frame(), options); |
| 17 } | 19 } |
| 18 | 20 |
| 19 FaceDetector::FaceDetector(LocalFrame& frame, | 21 FaceDetector::FaceDetector(LocalFrame& frame, |
| 20 const FaceDetectorOptions& options) | 22 const FaceDetectorOptions& options) |
| 21 : ShapeDetector(frame, options) {} | 23 : ShapeDetector(frame), |
| 24 m_faceDetectorOptions(mojom::blink::FaceDetectorOptions::New()) { |
| 25 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_faceService)); |
| 26 m_faceService.set_connection_error_handler(convertToBaseCallback(WTF::bind( |
| 27 &FaceDetector::onFaceServiceConnectionError, wrapWeakPersistent(this)))); |
| 28 m_faceDetectorOptions->max_detected_faces = options.maxDetectedFaces(); |
| 29 m_faceDetectorOptions->fast_mode = options.fastMode(); |
| 30 } |
| 22 | 31 |
| 23 ScriptPromise FaceDetector::detect(ScriptState* scriptState, | 32 ScriptPromise FaceDetector::doDetect( |
| 24 const CanvasImageSourceUnion& imageSource) { | 33 ScriptPromiseResolver* resolver, |
| 25 return detectShapes(scriptState, ShapeDetector::DetectorType::Face, | 34 mojo::ScopedSharedBufferHandle sharedBufferHandle, |
| 26 imageSource); | 35 int imageWidth, |
| 36 int imageHeight) { |
| 37 ScriptPromise promise = resolver->promise(); |
| 38 if (!m_faceService) { |
| 39 resolver->reject(DOMException::create( |
| 40 NotSupportedError, "Face detection service unavailable.")); |
| 41 return promise; |
| 42 } |
| 43 m_faceServiceRequests.add(resolver); |
| 44 m_faceService->Detect(std::move(sharedBufferHandle), imageWidth, imageHeight, |
| 45 m_faceDetectorOptions.Clone(), |
| 46 convertToBaseCallback(WTF::bind( |
| 47 &FaceDetector::onDetectFaces, wrapPersistent(this), |
| 48 wrapPersistent(resolver)))); |
| 49 return promise; |
| 50 } |
| 51 |
| 52 void FaceDetector::onDetectFaces( |
| 53 ScriptPromiseResolver* resolver, |
| 54 mojom::blink::FaceDetectionResultPtr faceDetectionResult) { |
| 55 DCHECK(m_faceServiceRequests.contains(resolver)); |
| 56 m_faceServiceRequests.remove(resolver); |
| 57 |
| 58 HeapVector<Member<DOMRect>> detectedFaces; |
| 59 for (const auto& boundingBox : faceDetectionResult->bounding_boxes) { |
| 60 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, |
| 61 boundingBox->width, |
| 62 boundingBox->height)); |
| 63 } |
| 64 |
| 65 resolver->resolve(detectedFaces); |
| 66 } |
| 67 |
| 68 void FaceDetector::onFaceServiceConnectionError() { |
| 69 for (const auto& request : m_faceServiceRequests) { |
| 70 request->reject(DOMException::create(NotSupportedError, |
| 71 "Face Detection not implemented.")); |
| 72 } |
| 73 m_faceServiceRequests.clear(); |
| 74 m_faceService.reset(); |
| 27 } | 75 } |
| 28 | 76 |
| 29 DEFINE_TRACE(FaceDetector) { | 77 DEFINE_TRACE(FaceDetector) { |
| 30 ShapeDetector::trace(visitor); | 78 ShapeDetector::trace(visitor); |
| 79 visitor->trace(m_faceServiceRequests); |
| 31 } | 80 } |
| 32 | 81 |
| 33 } // namespace blink | 82 } // namespace blink |
| OLD | NEW |