Post

Replies

Boosts

Views

Activity

Issues while using struct as a binding within a view
I am currently trying to refactor some of my variables that I am using within a View into a struct for clarity. However, when I try to do this, I get a bunch of errors such as Value of type 'Binding<DrawableStaff>' has no dynamic member 'drawings' using key path from root type 'DrawableStaff'. How do I fix these issues? I have looked into the documentation, and I have looked around online, and I haven't found a solution to this issue. Here is what the refactored struct looks like struct DrawableStaff{ &#9;&#9;var drawing: Drawing = Drawing() &#9;&#9;var drawingList: [Drawing] = [Drawing]() &#9;&#9;var paths: [Path] = [Path]() &#9;&#9;var PathHolder = Path() &#9;&#9;var color = Color.primary &#9;&#9;var lineWidth: CGFloat = 3.0 } Here is where DrawableStaff appears in my main ContentView struct ContentView: View { &#9;&#9;@State private var staff1DrawableStaff = DrawableStaff()&#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;VStack {&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;let staff1 = ZStack{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;DrawingPad(drawableStaff: $staff1DrawableStaff) &#9;&#9;&#9;&#9;} &#9;&#9;} } Here is where DrawingPad is defined: struct DrawingPad: View { &#9;&#9; &#9;&#9;@Binding var drawableStaff: DrawableStaff &#9;&#9; &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;GeometryReader { geometry in &#9;&#9;&#9;&#9;&#9;&#9;Path { path in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;for drawing in self.drawableStaff.drawings { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.add(drawing: drawableStaff.drawing, toPath: &path) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.add(drawing: self.drawableStaff.currentDrawing, toPath: &path) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.drawableStaff.pathHolder = path &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;.stroke(self.drawableStaff.color, lineWidth: self.drawableStaff.lineWidth) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.background(Color(UIColor.systemBackground)) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.gesture( &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;DragGesture(minimumDistance: 0.1) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onChanged({ (value) in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let currentPoint = value.location &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;if currentPoint.y >= 0 &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&& currentPoint.y < geometry.size.height { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.drawableStaff.currentDrawing.points.append(currentPoint) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.onEnded({ (value) in &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.drawableStaff.drawings.append(self.currentDrawing) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.drawableStaff.currentDrawing = Drawing() &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;}) &#9;&#9;&#9;&#9;&#9;&#9;) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.frame(maxHeight: .infinity) &#9;&#9;}     private func add(drawing: Drawing, toPath path: inout Path) {         let points = drawing.points         if points.count > 1 {             for i in 0..<points.count-1 {                 let current = points[i]                 let next = points[i+1]                 path.move(to: current)                 path.addLine(to: next)             }         }     } } Here is my Drawing definition: struct Drawing {     var points: [CGPoint] = [CGPoint]() }
10
0
1.9k
Oct ’20
Classifying SwiftUI Paths using Core ML
I am working on an app that classifies drawings made by user. Currently, the user made drawings are represented as a set of SwiftUI Paths, and I have a trained Core ML model that takes images and outputs class names. I have written some code that is able to take in images in the form of UIImages and feed it into my classifier, but I am unsure of how I should adapt my code to take Paths. Here is my current code: import UIKit import CoreML import Vision import ImageIO import SwiftUI struct ImageClassifier{ &#9;&#9;var classifier = SymbolClassifier() &#9;&#9;func classify(image: CGImage) -> String?{ &#9;&#9;&#9;&#9;let pixelBuffer = image.pixelBuffer(width: 300, height: 300, orientation: CGImagePropertyOrientation.up)! &#9;&#9;&#9;&#9;let output = try? self.classifier.prediction(image: pixelBuffer) &#9;&#9;&#9;&#9;return output?.classLabel &#9;&#9;} &#9;&#9;func classifyUIImage(image: UIImage)-> String?{ &#9;&#9;&#9;&#9;guard let imageAsUIImage:CGImage = convertUIImageToCGImage(image: image) else{ &#9;&#9;&#9;&#9;&#9;&#9;return nil &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return classify(image: imageAsUIImage) &#9;&#9;} &#9;&#9;func classifyPath(path:Path) -> String?{ &#9;&#9;&#9;&#9;//??? &#9;&#9;&#9;&#9;return nil &#9;&#9;} &#9;&#9;func convertUIImageToCGImage(image: UIImage) -> CGImage? { &#9;&#9;&#9;&#9;let inputImage = CIImage(image: image)! &#9;&#9;&#9;&#9;let context = CIContext(options: nil) &#9;&#9;&#9;&#9;return context.createCGImage(inputImage, from: inputImage.extent) &#9;&#9;} } Here - https://github.com/ivyjsgit/Manuscript-iOS/blob/main/Manuscript/Libraries/CGImage%2BCVPixelBuffer.swift is the image.pixelBuffer library and here - https://github.com/ivyjsgit/Manuscript-iOS/blob/main/Manuscript/Manuscript/SymbolClassifier.mlmodel is the model
1
0
545
Oct ’20
"No training data found. 0 invalid files found." when training a set of images into Core ML
I am attempting to load a set of images into Create ML so that I can train an image classifier model. However, when I select the folder that my training data is in, I get the error "No training data found. 0 invalid files found." My data is laid out as such. Parent folder -Training data (Selected in Create ML) -Individuals for each class -Image files -Test data -Individuals for each class -Image files My dataset can be viewed here - https://github.com/ivyjsgit/HOMUS-Bitmap/tree/master/Splitted
2
2
1.2k
Nov ’20