Home IOS Development ios – Is there a solution to optimise the loop within the under swift ui code

ios – Is there a solution to optimise the loop within the under swift ui code

0
ios – Is there a solution to optimise the loop within the under swift ui code

[ad_1]

I’m making an attempt to feed a bunch of photographs in an iphone utilizing swiftui to a tflite mannequin and get outcomes. The form the mannequin accepts is (1, 640, 640, 3). So after a little bit of researching discovered a code that will comparatively map the picture knowledge in to the above form such that it might be fed in to the mannequin. The code I referred to is Carry out Inference on Enter Information – Firebase. I did some minor modifications to the code and was capable of obtain the under code,

func preprocessImage(picture: CGImage) -> Information? {
        guard let context = CGContext(
          knowledge: nil,
          width: picture.width,
          peak: picture.peak,
          bitsPerComponent: 8,
          bytesPerRow: picture.width * 4,
          area: CGColorSpaceCreateDeviceRGB(),
          bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue
        ) else {
            return nil
        }

        context.draw(picture, in: CGRect(x: 0, y: 0, width: picture.width, peak: picture.peak))
        guard let imageData = context.knowledge else { return nil }

        var inputData = Information()
        var rely = 0
        for row in 0..<640 {
            for col in 0..<640 {
                rely+=1
                let offset = 4 * (row * context.width + col)
                // (Ignore offset 0, the unused alpha channel)
                let pink = imageData.load(fromByteOffset: offset + 1, as: UInt8.self)
                let inexperienced = imageData.load(fromByteOffset: offset + 2, as: UInt8.self)
                let blue = imageData.load(fromByteOffset: offset + 3, as: UInt8.self)

                // Normalize channel values to [0.0, 1.0]
                var normalizedRed = Float32(pink) / 255.0
                var normalizedGreen = Float32(inexperienced) / 255.0
                var normalizedBlue = Float32(blue) / 255.0

                // Append normalized values to Information object in RGB order.
                let elementSize = MemoryLayout.dimension(ofValue: normalizedRed)
                var bytes = [UInt8](repeating: 0, rely: elementSize)
                memcpy(&bytes, &normalizedRed, elementSize)
                inputData.append(&bytes, rely: elementSize)
                memcpy(&bytes, &normalizedGreen, elementSize)
                inputData.append(&bytes, rely: elementSize)
                memcpy(&bytes, &normalizedBlue, elementSize)
                inputData.append(&bytes, rely: elementSize)
            }
        }
        print(rely)
        return inputData
    }

However my concern is that the loop runs 409600 occasions for every picture which I might actually love to scale back. Is there any normal method or some other alternate to attain the identical performance however by lowering the loop runs?

Thanks prematurely.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here