Last active
April 28, 2023 08:53
-
-
Save heestand-xyz/6ba9a77867dfde4c95b19d3affa140c2 to your computer and use it in GitHub Desktop.
Video Stabilization in Swift with AsyncGraphics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by Anton Heestand with the help of GPT-4 | |
// AsyncGraphics: https://github.com/heestand-xyz/AsyncGraphics | |
import SwiftUI | |
import AsyncGraphics | |
import Vision | |
struct ContentView: View { | |
@State var graphic: Graphic? | |
var body: some View { | |
ZStack { | |
if let graphic { | |
GraphicView(graphic: graphic) | |
} | |
} | |
.task { | |
do { | |
let url = Bundle.main.url(forResource: "video", withExtension: "mp4")! | |
var lastGraphic: Graphic? | |
for try await graphic in try Graphic.processVideo(url: url) { | |
defer { lastGraphic = graphic } | |
guard let lastGraphic else { continue } | |
let request = try VNTranslationalImageRegistrationRequest(targetedCIImage: graphic.ciImage) | |
try VNSequenceRequestHandler().perform([request], on: lastGraphic.ciImage) | |
guard let result = request.results?.first else { continue } | |
let transform = result.alignmentTransform | |
let translation = CGPoint(x: transform.tx, y: transform.ty) | |
let rotation: Angle = .radians(atan2(transform.c, transform.a)) | |
self.graphic = try await graphic | |
.transformed(translation: translation, rotation: rotation) | |
} | |
} catch { | |
print(error) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment