• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 import AVFoundation
10 import ImageClassification
11 import SwiftUI
12 
13 struct TopBar: View {
14   let title: String
15 
16   var body: some View {
17     Text(title)
18       .font(.title)
19       .foregroundColor(.white)
20       .frame(maxWidth: .infinity)
21       .background(Color.black.opacity(0.5))
22   }
23 }
24 
25 struct ClassificationLabelView: View {
26   @ObservedObject var controller: ClassificationController
27 
28   var body: some View {
29     VStack(alignment: .leading) {
30       ForEach(controller.classifications.prefix(3), id: \.label) { classification in
31         Text("\(classification.label) \(Int(classification.confidence * 100))%")
32           .font(.footnote)
33           .foregroundColor(.white)
34       }
35     }
36     .padding()
37     .frame(maxWidth: .infinity)
38     .background(Color.black.opacity(0.5))
39   }
40 }
41 
42 struct ClassificationTimeView: View {
43   @ObservedObject var controller: ClassificationController
44 
45   var body: some View {
46     VStack {
47       if controller.isRunning {
48         ProgressView()
49           .progressViewStyle(CircularProgressViewStyle(tint: .white))
50           .frame(width: nil, height: 34, alignment: .center)
51       } else {
52         Text("\n\(controller.elapsedTime, specifier: "%.2f") ms")
53           .font(.footnote)
54           .foregroundColor(.white)
55       }
56     }
57     .frame(maxWidth: .infinity)
58     .background(Color.black.opacity(0.5))
59   }
60 }
61 
62 struct ModeSelector: View {
63   @ObservedObject var controller: ClassificationController
64 
65   var body: some View {
66     HStack {
67       ForEach(Mode.allCases, id: \.self) { mode in
68         ModeButton(mode: mode, controller: controller)
69       }
70     }
71     .padding()
72     .frame(maxWidth: .infinity)
73     .background(Color.black.opacity(0.5))
74   }
75 }
76 
77 struct ModeButton: View {
78   var mode: Mode
79   @ObservedObject var controller: ClassificationController
80 
81   var body: some View {
82     Button(action: { controller.mode = mode }) {
83       Text(mode.rawValue)
84         .fontWeight(.semibold)
85         .foregroundColor(.white)
86         .padding(.horizontal, 10)
87         .padding(.vertical, 5)
88         .background(controller.mode == mode ? Color.red : Color.clear)
89         .cornerRadius(15)
90     }
91   }
92 }
93