Depth in Depth #iOSDC
by
shu223
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
Depth in Depth Shuichi Tsutsumi @shu223 iOSDC 2018
Slide 2
Slide 2 text
ࣗݾհ • Fyusion Inc.ࣾһ@αϯϑϥϯγείʢH-1BϏβʣ • Senior Principal Software Engineer • ࠓ͔ΒύʔτλΠϜܖʹ → ॊೈʹಇ͖ͭͭϏβҡ ࣋ • ϑϦʔϥϯε@౦ژ • ݱࡏΞΫςΟϒʹ͓ख͍͍ͯ͠Δͷ8ࣾ΄ͲʢIoTܥɺ MetalɺARKitɺNFCɺυϩʔϯɺٕज़ސɺւ֎ɺetc...ʣ
Slide 3
Slide 3 text
• GitHub: shu223 • ϒϩά: ͯͳɺQiitaɺNote • ॻ੶:
Slide 4
Slide 4 text
Depth in Depth σϓεʢਂʣৄղ
Slide 5
Slide 5 text
ਂͱʁ
Slide 6
Slide 6 text
Ԟߦ͖Λࣔ͢ใ 1νϟϯωϧͷը૾σʔλʢਂϚοϓͱݺΕΔʣͰද͞ΕΔ ʢࠨɿRGBը૾ɹʗɹӈɿਂϚοϓʣ
Slide 7
Slide 7 text
ʮ͋ʔ͍͍ɻࣸਅΛϘέͤ͞ΔͭͰ͠ΐʯ
Slide 8
Slide 8 text
✋
Slide 9
Slide 9 text
ਂͷ༻్: എܠΛඈ͢ • ྫɿϑϦϚܥΞϓϦͰग़࣌ʹഎܠΛҰൃͰඈͤΔػೳ
Slide 10
Slide 10 text
ʢσϞʣ
Slide 11
Slide 11 text
ਂͷ༻్: 3DεΩϟϯ • 3DϞσϧΛ࡞ • ෦ͷݟऔΓਤΛ࡞
Slide 12
Slide 12 text
ਂͷ༻్: ͦͷଞ ԕִࡱӨ࣌ͷ ԻʹΑΔΠϯετϥΫγϣϯ ʮ30ηϯνޙΖʹԼ͕͍ͬͯͩ͘͞ʯ ※࣮ࡍʹਂͬͯͳ͍͔͠Ε·ͤΜ͕ɺਂͷ༻్ͷՄೳੑ ͱͯ͠ྫ͍ࣔͯ͠·͢
Slide 13
Slide 13 text
ਂൃ࣍ୈͰ ৭ʑͳ͍ํ͕Ͱ͖Δʂ
Slide 14
Slide 14 text
Ͳͷ͋ͨΓΛʮৄղʯ͢Δͷ͔ʁ • ʮiOSͷAPIͷ͍ํʯ࠷খݶʹ • εϥΠυʹࡌ͓͖ͤͯ·͢ • ຊ࣭తɾ൚༻తͳʮਂσʔλͷऔΓѻ͍ํ๏ʯΛཧղ͢Δ
Slide 15
Slide 15 text
ΞδΣϯμ • ਂͷछྨͱηϯαͷ͘͠Έ • ਂσʔλΛऔಘ͢Δ • ਂσʔλΛͬͯΈΔ • Ͱ͖Δ͜ͱɾͰ͖ͳ͍͜ͱࣄྫू
Slide 16
Slide 16 text
ΞδΣϯμ • ਂͷछྨͱηϯαͷ͘͠Έ • ਂσʔλΛऔಘ͢Δ • ਂσʔλΛͬͯΈΔ • Ͱ͖Δ͜ͱɾͰ͖ͳ͍͜ͱࣄྫू
Slide 17
Slide 17 text
Disparityʢࢹࠩʣ iPhone 7/8 Plus, Xͷഎ໘Χϝϥ
Slide 18
Slide 18 text
ϐϯϗʔϧΧϝϥ
Slide 19
Slide 19 text
̎ͷಉ͡Focal LengthΛ࣋ͭΧϝϥ
Slide 20
Slide 20 text
Disparityʢࢹࠩʣ
Slide 21
Slide 21 text
DepthͱDisparityͷؔ
Slide 22
Slide 22 text
DepthDisparity͔ΒܭࢉͰ͖Δ • Depth(z) ͷٯ = ਖ਼نԽ͞Εͨ Disparity(d)
Slide 23
Slide 23 text
Time of Flight (ToF) • DepthΛଌΔํࣜ • ରʹরࣹͨ͠ޫ͕ࣹͯͬͯ͘͠Δ·Ͱͷԟ෮ͷ࣌ؒ ͔ΒڑΛܭଌ
Slide 24
Slide 24 text
TrueDepth iPhone Xͷલ໘Χϝϥ
Slide 25
Slide 25 text
• Proximity sensorʢۙηϯαʣɾɾɾToFํࣜͰإ·Ͱͷڑ Λܭଌ
Slide 26
Slide 26 text
• Dot projector (υοτϓϩδΣΫλ)ɾɾɾ3ສҎ্ͷݟ͑ͳ͍ υοτΛإͷ্ʹরࣹ
Slide 27
Slide 27 text
• Infrated camera (֎ઢΧϝϥ)ɾɾɾυοτͷύλʔϯΛಡ ΈऔΔ → إͷϚοϓը૾
Slide 28
Slide 28 text
ΞδΣϯμ • ਂͷछྨͱηϯαͷ͘͠Έ • ਂσʔλΛऔಘ͢Δ • ਂσʔλΛͬͯΈΔ • Ͱ͖Δ͜ͱɾͰ͖ͳ͍͜ͱࣄྫू
Slide 29
Slide 29 text
AVDepthData • ਂΛද͢Ϋϥε • Ͳͷํ๏Ͱऔಘͯ͠࠷ऴతʹ͜Ε͕ಘΒΕΔ • @available(iOS 11.0, *)
Slide 30
Slide 30 text
AVDepthData var depthDataMap: CVPixelBuffer { get } • ਂϚοϓͷϐΫηϧσʔλ • ैདྷ͔Β͋ΔCVPixelBufferͳͷͰɺCore ImageͰ MetalͰɺͦͷޙͷॲཧํ๏ࣗࡏ
Slide 31
Slide 31 text
iOSͰਂσʔλΛऔಘ͢Δํ๏ ༻్ผʹ͚Δͱ̏छྨ • ํ๏1: ࡱӨࡁΈࣸਅ͔Βऔಘ • ํ๏2: Χϝϥ͔ΒϦΞϧλΠϜʹऔಘ • ํ๏3: ARKit͔Βऔಘ
Slide 32
Slide 32 text
ਂऔಘํ๏1: ࡱӨࡁΈࣸਅ͔Βऔಘ • ImageIOΛར༻ • ʢΧϝϥϩʔϧ͔ΒಡΈࠐΜͰΞηοτͷURLΛಛఆ͢Δͱ ͜Ζ·ͰPhotosʣ
Slide 33
Slide 33 text
ΞηοτͷURL͔ΒCGImageSourceΛ࡞ let source = CGImageSourceCreateWithURL(url as CFURL, nil)
Slide 34
Slide 34 text
CGImageSource͔ΒਂσʔλͷDictionaryΛऔಘ let info = CGImageSourceCopyAuxiliaryDataInfoAtIndex( source, 0, kCGImageAuxiliaryDataTypeDisparity // ଞͷλΠϓ͋Γ ) as? [String : AnyObject] // infoʹਂϚοϓͷ CFDataRef // ϝλσʔλͷCGImageMetadataRef͕ೖ͍ͬͯΔ
Slide 35
Slide 35 text
औಘͨ͠Dictionary͔ΒAVDepthDataΛॳظԽ let depthData = try! AVDepthData(fromDictionaryRepresentation: info)
Slide 36
Slide 36 text
ਂऔಘํ๏2: Χϝϥ͔ΒϦΞϧλΠϜʹऔಘ • AVFoundationΛར༻ • جຊతʹैདྷͷΧϝϥࡱӨͷ࡞๏௨Γ
Slide 37
Slide 37 text
ਂ͕औΕΔσόΠελΠϓͷ AVCaptureDevice Λ͏ .builtInDualCamera, .builtInTrueDepthCamera let device = AVCaptureDevice.DiscoverySession( deviceTypes: [.builtInDualCamera], mediaType: .video, position: .back).devices.first
Slide 38
Slide 38 text
ਂ͕औΕΔΩϟϓνϟʔϑΥʔϚοτΛࢦఆ͢Δ var supportedDepthDataFormats: [AVCaptureDevice.Format] • AVCaptureDevice.Format ͷϓϩύςΟ • ॴͷͷΛબͼɺAVCaptureDevice ͷ activeDepthDataFormat ʹηοτ͢Δ
Slide 39
Slide 39 text
ηογϣϯͷग़ྗʹ AVCaptureDepthDataOutput ΛՃ let depthOutput = AVCaptureDepthDataOutput() captureSession.addOutput(depthOutput) depthOutput.setDelegate( self, callbackQueue: dataOutputQueue) • AVCaptureVideoDataOutputʢϏσΦग़ྗʣ AVCaptureAudioʙʢΦʔσΟΦग़ྗʣΛՃ͢Δͷͱಉ
Slide 40
Slide 40 text
AVCaptureDepthDataOutputDelegate ͷ didOutput ϝιου ͔Β AVDepthData ͕ಘΒΕΔ func depthDataOutput( _ output: AVCaptureDepthDataOutput, didOutput depthData: AVDepthData, timestamp: CMTime, connection: AVCaptureConnection)
Slide 41
Slide 41 text
ਂऔಘํ๏3: ARKit͔Βऔಘ ARFrameʹcapturedDepthDataͱ͍͏ϓϩύςΟ͕͋Δ var capturedDepthData: AVDepthData? { get }
Slide 42
Slide 42 text
⚠ ํ๏ʹ༏ྼͳ͍ɻ༻్ʹΑͬͯબͿɻ • ํ๏1: ࡱӨࡁΈࣸਅ͔Βऔಘ • ํ๏2: Χϝϥ͔ΒϦΞϧλΠϜʹऔಘ • ํ๏3: ARKit͔Βऔಘ
Slide 43
Slide 43 text
ΞδΣϯμ • ਂͷछྨͱηϯαͷ͘͠Έ • ਂσʔλΛऔಘ͢Δ • ਂσʔλΛͬͯΈΔ • Ͱ͖Δ͜ͱɾͰ͖ͳ͍͜ͱࣄྫू
Slide 44
Slide 44 text
ਂσʔλΛͬͯΈΔ ਂσʔλͰഎܠ߹
Slide 45
Slide 45 text
No content
Slide 46
Slide 46 text
എܠ߹ͷख๏ ϚεΫը૾Λج४ʹɺΦϦδφϧը૾ͱഎܠը૾ΛϒϨϯυ͢ Δ • ΫϩϚΩʔ߹ɺϚεΫͷΘΓʹchromaʢ࠼ʣΛར ༻
Slide 47
Slide 47 text
CIBlendWithMask Parameters inputImage ೖྗը૾ inputBackgroundImage എܠը૾ inputMaskImage ϚεΫը૾ • ೖྗը૾͕ἧ͍ͬͯ͑͞Ε͜ΕΛ͏͚ͩʂ • CIFilterMetalύΠϓϥΠϯͰॲཧͰ͖Δ
Slide 48
Slide 48 text
ϚεΫͱͯ͠ਂΛ༻͍ΕOK!?
Slide 49
Slide 49 text
࣮ • ϑϩϯτΧϝϥ͔ΒϦΞϧλΠϜʹਂΛऔಘ • CIFilterͷCIBlendWithMaskͰϒϨϯυ • MetalͰϨϯμϦϯάɺMTKViewʹඳը
Slide 50
Slide 50 text
݁ՌʢσϞʣ
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 text
Կ͕ى͖͍ͯΔͷ͔ʁ
Slide 53
Slide 53 text
ਂΛՄࢹԽͯ͠ΈΔ
Slide 54
Slide 54 text
ͦͷ̍ɿ നͱࠇ͕ର • ϚεΫɺΦϦδφϧ෦͕നͰ͋ Δ͖
Slide 55
Slide 55 text
ըૉΛసʢന˱ࠇʣͤ͞ΕΑ͍ʁ
Slide 56
Slide 56 text
ͦͷ̎ɿ إ͕άϨʔͷ·· • શʹനͰͳ͍ͱɺഎܠ͕ϒϨϯυ ͞Εͯ͠·͏
Slide 57
Slide 57 text
ରࡦɿ إͷਂΛ͖͍͠ʹɺਂ ϚοϓΛ2Խ͢Δ • AVCaptureMetadataOutputͰإΛೝ ࣝ • إͷத৺ͷਂ+offsetΛ͖͍͠ ʢcutOffʣͱ͢Δ • ਂϚοϓʹ͓͍ͯɺ • ਂ͕cutOffҎ্Ͱ͋Εࠇ • ਂ͕cutOffҎԼͰ͋Εന
Slide 58
Slide 58 text
݁ՌʢσϞʣ
Slide 59
Slide 59 text
No content
Slide 60
Slide 60 text
ͦͷଞͷ • ڥք͕ΪβΪβ • ϝΨω͕എܠʹɾɾɾ"Holes"
Slide 61
Slide 61 text
ରࡦɿ • ΪβΪβɾɾɾεϜʔδϯά͢Δ • HolesɾɾɾRGBΛར༻ͯ͠ຒΊΔ
Slide 62
Slide 62 text
isFilteringEnabled • AVCaptureDepthDataOutputͷϓϩύςΟ • ۭؒํ͚ͩͰͳ͘ɺ࣌ܥྻํʹεϜʔδϯά • "Holes"ຒΊͯ͘ΕΔʢRGBΛར༻ʣ
Slide 63
Slide 63 text
No content
Slide 64
Slide 64 text
݁ՌʢσϞʣ
Slide 65
Slide 65 text
ʂ
Slide 66
Slide 66 text
ඍົʁ • ͷໟͷ·ΘΓ • إͷਂΛج४ʹ͖͍͠Λઃఆ͠ ͍ͯΔɾɾɾ൚༻ੑʹ͚ܽΔ
Slide 67
Slide 67 text
Portrait Matte
Slide 68
Slide 68 text
Portrait Matteͱʁ • എܠͱલܠͷʢηάϝϯςʔγϣϯʣʹಛԽͨ͠ϑΥʔ Ϛοτ • iOS 12͔ΒऔಘՄೳʹͳͬͨ
Slide 69
Slide 69 text
ैདྷͷਂϚοϓʢࠨʣͱPortrait Matteʢӈʣ
Slide 70
Slide 70 text
ͷໟ͖Ε͍ʹ
Slide 71
Slide 71 text
औಘํ๏ • લड़ͷCGImageSourceΛར༻͢Δํ๏ͱ΄΅ಉ͡ • λΠϓͱͯ͠ ʙPortraitEffectsMatte Λࢦఆ͢Δ • AVDapthData ͷΘΓʹ AVPortraitEffectsMatte
Slide 72
Slide 72 text
guard let info = CGImageSourceCopyAuxiliaryDataInfoAtIndex( self, 0, kCGImageAuxiliaryDataTypePortraitEffectsMatte ) as? [String : AnyObject] else { return } let matte = AVPortraitEffectsMatte(fromDictionaryRepresentation: info)
Slide 73
Slide 73 text
No content
Slide 74
Slide 74 text
Portrait Matteͷ੍ • ੩ࢭըͷΈ • ਓ͕ө͍ͬͯΔ߹ͷΈ • iOS 12Ҏ্
Slide 75
Slide 75 text
ΞδΣϯμ • ਂͷछྨͱηϯαͷ͘͠Έ • ਂσʔλΛऔಘ͢Δ • ਂσʔλΛͬͯΈΔ • Ͱ͖Δ͜ͱɾͰ͖ͳ͍͜ͱࣄྫू
Slide 76
Slide 76 text
Ͳ͏͍͏έʔεͰɺԿ͕Ͱ͖ͯԿ͕Ͱ͖ͳ͍ͷ͔ Ѳͮ͠Β͍ • ࡱӨࡁΈࣸਅ͔Β / ϦΞϧλΠϜ / ARKit • Dual / TrueDepth / ͦΕҎ֎ • ϑϩϯτ / ϦΞ • iOS 12 / 11 / ͦΕҎԼ • ର͕إ / શ / ମ
Slide 77
Slide 77 text
۩ମతͳࣄྫͰઆ໌ͯ͠Έ·͢
Slide 78
Slide 78 text
ʮ͜ΕͰ͏Γൈ͖࡞ۀPhotoshop͍Βͣ ͩʂʯ • ରը૾ʹਂσʔλ͕ೖ͍ͬͯΔඞཁ͕͋Δ • Portrait Matteͷ࣭Λظ͢ΔͳΒɺਓ͕ؒࣸͬͯͳ͍ͱ͍ ͚ͳ͍
Slide 79
Slide 79 text
ʮ෦͕ࢄΒ͔ͬͯͯϏσΦձٞͰ͖Δʂʯ • Portrait MatteϦΞϧλΠϜͰར༻Ͱ͖ͳ͍ • ͰϑϩϯτΧϝϥͰਓؒͷإΛத৺ʹࡱӨ͢ΔͷͰɺී௨ ͷਂϕʔεͰͦΕͳΓʹ͍͚ͦ͏
Slide 80
Slide 80 text
ʮϑϦϚΞϓϦͰग़࣌ʹࣗಈͰഎܠΛඈͤΔ ػೳΛೖΕΑ͏ʯ • ਓҎ֎ͷʮϞϊʯPortrait MatteऔΕͳ͍ • DisprityϕʔεͷਂͱΕΔ • ͦͷʮϞϊʯͷਂͷ͖͍͠Ͳ͏ܾͬͯΊΔʁ • ϑΝογϣϯʹݶఆ͢Εྑͦ͞͏
Slide 81
Slide 81 text
ʮARKitͷΦΫϧʔδϣϯ͕͜ΕͰͰ ͖Δʂʯ • ϑϩϯτɾɾɾOK • ϦΞɾɾɾNG • ARFaceTrackingConfiguration༻࣌ͷΈ༗ޮ • എ໘ΧϝϥΛ͏ίϯϑΟΪϡϨʔγϣϯʢྫɿ ARWorldTrackingʙʣͰ capturedDepth ͕ nil
Slide 82
Slide 82 text
·ͱΊ • ਂʢࣸਅͷϘέҎ֎ʹʣൃ࣍ୈͰ͍Ζ͍Ζͱ͑Δ • ਂͲ͏ͬͯܭଌ͢Δͷ͔ʁ • ਂσʔλɺͲ͏ͬͯ͏ͷ͔ʁ • Կ͕Ͱ͖ͯɺԿ͕Ͱ͖ͳ͍ͷ͔ʁ
Slide 83
Slide 83 text
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ • Twitter: https://twitter.com/shu223 • GitHub: https://github.com/shu223 • ϒϩά: http://shu223.hatenablog.com/
Slide 84
Slide 84 text
ิ༻ࢿྉ
Slide 85
Slide 85 text
AVCaptureDataOutputSynchronizer
Slide 86
Slide 86 text
// ϏσΦग़ྗͱਂग़ྗΛಉظ synchronizer = AVCaptureDataOutputSynchronizer( dataOutputs: [videoOutput, depthOutput])
Slide 87
Slide 87 text
ಉظͨ͠ग़ྗΛड͚औΔ AVCaptureDataOutputSynchronizerDelegate didOutputσϦήʔτϝιου͔Β AVCaptureSynchronizedDataCollectionΦϒδΣΫτ͕ಘΒΕ Δ • → AVCaptureSynchronizedDepthData • → AVDepthData
Slide 88
Slide 88 text
// ਂσʔλ͕AVCaptureSynchronizedDepthDataܕͰऔΓग़ͤΔ if let syncedDepthData = synchronizedDataCollection.synchronizedData(for: depthOutput) as? AVCaptureSynchronizedDepthData, !syncedDepthData.depthDataWasDropped { // AVDepthDataΦϒδΣΫτΛऔಘ let depthData = syncedDepthData.depthData ... }
Slide 89
Slide 89 text
ਂσʔλΛͬͯΈΔ MetalͰࣸਅΛ3DϚοϐ ϯά
Slide 90
Slide 90 text
Processed by Focos
Slide 91
Slide 91 text
• γΣʔμ • ਂσʔλΛz࠲ඪʹ • ϑϥάϝϯτγΣʔμ • RGBΧϥʔΛద༻