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

Side by Side Diff: third_party/WebKit/Source/modules/shapedetection/FaceDetector.cpp

Issue 2455973007: FaceDetection: add support for <video> input (Closed)
Patch Set: 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 unified diff | Download patch
OLDNEW
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/dom/DOMRect.h"
9 #include "core/dom/Document.h" 9 #include "core/dom/Document.h"
10 #include "core/fetch/ImageResource.h" 10 #include "core/fetch/ImageResource.h"
11 #include "core/frame/ImageBitmap.h" 11 #include "core/frame/ImageBitmap.h"
12 #include "core/frame/LocalDOMWindow.h" 12 #include "core/frame/LocalDOMWindow.h"
13 #include "core/frame/LocalFrame.h" 13 #include "core/frame/LocalFrame.h"
14 #include "core/html/HTMLImageElement.h" 14 #include "core/html/HTMLImageElement.h"
15 #include "core/html/HTMLVideoElement.h"
15 #include "core/html/canvas/CanvasImageSource.h" 16 #include "core/html/canvas/CanvasImageSource.h"
16 #include "platform/graphics/Image.h" 17 #include "platform/graphics/Image.h"
17 #include "public/platform/InterfaceProvider.h" 18 #include "public/platform/InterfaceProvider.h"
18 #include "third_party/skia/include/core/SkImage.h" 19 #include "third_party/skia/include/core/SkImage.h"
19 #include "third_party/skia/include/core/SkImageInfo.h" 20 #include "third_party/skia/include/core/SkImageInfo.h"
20 #include "wtf/CheckedNumeric.h"
21 21
22 namespace blink { 22 namespace blink {
23 23
24 namespace { 24 namespace {
25 25
26 static CanvasImageSource* toImageSourceInternal( 26 static CanvasImageSource* toImageSourceInternal(
27 const CanvasImageSourceUnion& value) { 27 const CanvasImageSourceUnion& value) {
28 if (value.isHTMLImageElement()) 28 if (value.isHTMLImageElement())
29 return value.getAsHTMLImageElement(); 29 return value.getAsHTMLImageElement();
30 30
31 if (value.isImageBitmap()) { 31 if (value.isImageBitmap()) {
32 if (static_cast<ImageBitmap*>(value.getAsImageBitmap())->isNeutered()) 32 if (static_cast<ImageBitmap*>(value.getAsImageBitmap())->isNeutered())
33 return nullptr; 33 return nullptr;
34 return value.getAsImageBitmap(); 34 return value.getAsImageBitmap();
35 } 35 }
36 36
37 if (value.isHTMLVideoElement())
38 return value.getAsHTMLVideoElement();
39
37 return nullptr; 40 return nullptr;
38 } 41 }
39 42
40 } // anonymous namespace 43 } // anonymous namespace
41 44
42 FaceDetector* FaceDetector::create(ScriptState* scriptState) { 45 FaceDetector* FaceDetector::create(ScriptState* scriptState) {
43 return new FaceDetector(*scriptState->domWindow()->frame()); 46 return new FaceDetector(*scriptState->domWindow()->frame());
44 } 47 }
45 48
46 FaceDetector::FaceDetector(LocalFrame& frame) { 49 FaceDetector::FaceDetector(LocalFrame& frame) {
(...skipping 12 matching lines...) Expand all
59 if (!imageSourceInternal) { 62 if (!imageSourceInternal) {
60 // TODO(mcasas): Implement more CanvasImageSources, https://crbug.com/659138 63 // TODO(mcasas): Implement more CanvasImageSources, https://crbug.com/659138
61 NOTIMPLEMENTED() << "Unsupported CanvasImageSource"; 64 NOTIMPLEMENTED() << "Unsupported CanvasImageSource";
62 resolver->reject( 65 resolver->reject(
63 DOMException::create(NotFoundError, "Unsupported source.")); 66 DOMException::create(NotFoundError, "Unsupported source."));
64 return promise; 67 return promise;
65 } 68 }
66 69
67 if (imageSourceInternal->wouldTaintOrigin( 70 if (imageSourceInternal->wouldTaintOrigin(
68 scriptState->getExecutionContext()->getSecurityOrigin())) { 71 scriptState->getExecutionContext()->getSecurityOrigin())) {
69 resolver->reject(DOMException::create(SecurityError, 72 resolver->reject(
70 "Image source would taint origin.")); 73 DOMException::create(SecurityError, "Source would taint origin."));
71 return promise; 74 return promise;
72 } 75 }
73 76
74 if (imageSource.isHTMLImageElement()) { 77 if (imageSource.isHTMLImageElement()) {
75 return detectFacesOnImageElement( 78 return detectFacesOnImageElement(
76 resolver, static_cast<HTMLImageElement*>(imageSourceInternal)); 79 resolver, static_cast<HTMLImageElement*>(imageSourceInternal));
77 } 80 }
78 if (imageSourceInternal->isImageBitmap()) { 81 if (imageSourceInternal->isImageBitmap()) {
79 return detectFacesOnImageBitmap( 82 return detectFacesOnImageBitmap(
80 resolver, static_cast<ImageBitmap*>(imageSourceInternal)); 83 resolver, static_cast<ImageBitmap*>(imageSourceInternal));
81 } 84 }
85 if (imageSourceInternal->isVideoElement()) {
86 return detectFacesOnVideoElement(
87 resolver, static_cast<HTMLVideoElement*>(imageSourceInternal));
88 }
89
82 NOTREACHED(); 90 NOTREACHED();
83 return promise; 91 return promise;
84 } 92 }
85 93
86 ScriptPromise FaceDetector::detectFacesOnImageElement( 94 ScriptPromise FaceDetector::detectFacesOnImageElement(
87 ScriptPromiseResolver* resolver, 95 ScriptPromiseResolver* resolver,
88 const HTMLImageElement* img) { 96 const HTMLImageElement* img) {
89 ScriptPromise promise = resolver->promise(); 97 ScriptPromise promise = resolver->promise();
90 if (img->bitmapSourceSize().isZero()) { 98 if (img->bitmapSourceSize().isZero()) {
91 resolver->resolve(HeapVector<Member<DOMRect>>()); 99 resolver->resolve(HeapVector<Member<DOMRect>>());
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 allocationSize = pixmap.getSafeSize(); 196 allocationSize = pixmap.getSafeSize();
189 } else { 197 } else {
190 pixelData = imageBitmap->copyBitmapData(imageBitmap->isPremultiplied() 198 pixelData = imageBitmap->copyBitmapData(imageBitmap->isPremultiplied()
191 ? PremultiplyAlpha 199 ? PremultiplyAlpha
192 : DontPremultiplyAlpha, 200 : DontPremultiplyAlpha,
193 N32ColorType); 201 N32ColorType);
194 pixelDataPtr = pixelData->data(); 202 pixelDataPtr = pixelData->data();
195 allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */; 203 allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */;
196 } 204 }
197 205
206 return detectFacesOnData(resolver, pixelDataPtr, allocationSize,
207 imageBitmap->width(), imageBitmap->height());
208 }
209
210 ScriptPromise FaceDetector::detectFacesOnVideoElement(
211 ScriptPromiseResolver* resolver,
212 const HTMLVideoElement* video) {
213 ScriptPromise promise = resolver->promise();
214
215 // TODO(mcasas): Check if |video| is actually playing a MediaStream by using
216 // HTMLMediaElement::isMediaStreamURL(video->currentSrc().getString()); if
217 // there is a local WebCam associated, there might be sophisticated ways to
218 // detect faces on it. Until then, treat as a normal <video> element.
219
220 // !hasAvailableVideoFrame() is a bundle of invalid states.
221 if (!video->hasAvailableVideoFrame()) {
222 resolver->reject(DOMException::create(
223 InvalidStateError, "Invalid HTMLVideoElement or state."));
224 return promise;
225 }
226
227 const FloatSize videoSize(video->videoWidth(), video->videoHeight());
228 SourceImageStatus sourceImageStatus = InvalidSourceImageStatus;
229 RefPtr<Image> image =
230 video->getSourceImageForCanvas(&sourceImageStatus, PreferNoAcceleration,
231 SnapshotReasonDrawImage, videoSize);
232
233 DCHECK_EQ(NormalSourceImageStatus, sourceImageStatus);
234
235 SkPixmap pixmap;
236 RefPtr<Uint8Array> pixelData;
237 uint8_t* pixelDataPtr = nullptr;
238 WTF::CheckedNumeric<int> allocationSize = 0;
239 // Use |skImage|'s pixels if it has direct access to them.
240 sk_sp<SkImage> skImage = image->imageForCurrentFrame();
241 if (skImage->peekPixels(&pixmap)) {
242 pixelDataPtr = static_cast<uint8_t*>(pixmap.writable_addr());
243 allocationSize = pixmap.getSafeSize();
244 } else {
245 // TODO(mcasas): retrieve the pixels from elsewhere.
246 NOTREACHED();
247 resolver->reject(DOMException::create(
248 InvalidStateError, "Failed to get pixels for current frame."));
249 return promise;
250 }
251
252 return detectFacesOnData(resolver, pixelDataPtr, allocationSize,
253 image->width(), image->height());
254 }
255
256 ScriptPromise FaceDetector::detectFacesOnData(ScriptPromiseResolver* resolver,
257 uint8_t* data,
258 WTF::CheckedNumeric<int> size,
259 int width,
xianglu 2016/10/29 01:33:39 nit: const
mcasas 2016/10/31 16:36:50 Not for POD types: those are passed by value [1].
260 int height) {
261 DCHECK(data);
262 ScriptPromise promise = resolver->promise();
263
198 mojo::ScopedSharedBufferHandle sharedBufferHandle = 264 mojo::ScopedSharedBufferHandle sharedBufferHandle =
199 mojo::SharedBufferHandle::Create(allocationSize.ValueOrDefault(0)); 265 mojo::SharedBufferHandle::Create(size.ValueOrDefault(0));
200 266 if (!sharedBufferHandle->is_valid()) {
201 if (!pixelDataPtr || !sharedBufferHandle->is_valid()) {
202 resolver->reject( 267 resolver->reject(
203 DOMException::create(InvalidStateError, "Internal allocation error")); 268 DOMException::create(InvalidStateError, "Internal allocation error"));
204 return promise; 269 return promise;
205 } 270 }
206 271
207 if (!m_service) { 272 if (!m_service) {
208 resolver->reject(DOMException::create( 273 resolver->reject(DOMException::create(
209 NotFoundError, "Face detection service unavailable.")); 274 NotSupportedError, "Face detection service unavailable."));
210 return promise; 275 return promise;
211 } 276 }
212 277
213 const mojo::ScopedSharedBufferMapping mappedBuffer = 278 const mojo::ScopedSharedBufferMapping mappedBuffer =
214 sharedBufferHandle->Map(allocationSize.ValueOrDefault(0)); 279 sharedBufferHandle->Map(size.ValueOrDefault(0));
215 DCHECK(mappedBuffer.get()); 280 DCHECK(mappedBuffer.get());
216 281
217 memcpy(mappedBuffer.get(), pixelDataPtr, allocationSize.ValueOrDefault(0)); 282 memcpy(mappedBuffer.get(), data, size.ValueOrDefault(0));
218 283
219 m_serviceRequests.add(resolver); 284 m_serviceRequests.add(resolver);
220 DCHECK(m_service.is_bound()); 285 DCHECK(m_service.is_bound());
221 m_service->DetectFace(std::move(sharedBufferHandle), imageBitmap->width(), 286 m_service->DetectFace(std::move(sharedBufferHandle), width, height,
222 imageBitmap->height(),
223 convertToBaseCallback(WTF::bind( 287 convertToBaseCallback(WTF::bind(
224 &FaceDetector::onDetectFace, wrapPersistent(this), 288 &FaceDetector::onDetectFace, wrapPersistent(this),
225 wrapPersistent(resolver)))); 289 wrapPersistent(resolver))));
226 sharedBufferHandle.reset(); 290 sharedBufferHandle.reset();
227 return promise; 291 return promise;
228 } 292 }
229 293
230 void FaceDetector::onDetectFace( 294 void FaceDetector::onDetectFace(
231 ScriptPromiseResolver* resolver, 295 ScriptPromiseResolver* resolver,
232 mojom::blink::FaceDetectionResultPtr faceDetectionResult) { 296 mojom::blink::FaceDetectionResultPtr faceDetectionResult) {
233 if (!m_serviceRequests.contains(resolver)) 297 if (!m_serviceRequests.contains(resolver))
234 return; 298 return;
235 299
236 HeapVector<Member<DOMRect>> detectedFaces; 300 HeapVector<Member<DOMRect>> detectedFaces;
237 for (const auto& boundingBox : faceDetectionResult->boundingBoxes) { 301 for (const auto& boundingBox : faceDetectionResult->boundingBoxes) {
238 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, 302 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y,
239 boundingBox->width, 303 boundingBox->width,
240 boundingBox->height)); 304 boundingBox->height));
241 } 305 }
242 306
243 resolver->resolve(detectedFaces); 307 resolver->resolve(detectedFaces);
244 m_serviceRequests.remove(resolver); 308 m_serviceRequests.remove(resolver);
245 } 309 }
246 310
247 DEFINE_TRACE(FaceDetector) { 311 DEFINE_TRACE(FaceDetector) {
248 visitor->trace(m_serviceRequests); 312 visitor->trace(m_serviceRequests);
249 } 313 }
250 314
251 } // namespace blink 315 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698