Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] ios Image segmenter sample #292

Merged
merged 16 commits into from
Mar 26, 2024

Conversation

st-tuanmai
Copy link
Contributor

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context.

Fixes # (issue)

Checklist

Please ensure the following items are complete before submitting a pull request:

  • My code follows the code style of the project.
  • I have updated the documentation (if applicable).
  • I have added tests to cover my changes.

Type of Change

Please check the relevant option below:

  • Bug fix (non-breaking change which fixes an issue)
  • Documentation update (non-breaking change which updates documentation)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Screenshots

If applicable, please add screenshots to help explain your changes.

Additional Notes

Add any additional information or context about the pull request here.

Copy link
Contributor

@priankakariat priankakariat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@st-tuanmai Can you implement the landscape orientations ?

Copy link
Contributor

@priankakariat priankakariat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@st-tuanmai The image selected from the gallery is not displayed on the screen. The screen remains blank

@st-tuanmai
Copy link
Contributor Author

st-tuanmai commented Jan 10, 2024

@st-tuanmai The image selected from the gallery is not displayed on the screen. The screen remains blank

Can you help me check the result of the image segmented api when segmenting UIImage when the orientation is left or right, the result returned the wrong width, height, and orientation is reverse (up -> down).
Thank you.

@priankakariat
Copy link
Contributor

@st-tuanmai The image selected from the gallery is not displayed on the screen. The screen remains blank

Can you help me check the result of the image segmented api when segmenting UIImage when the orientation is left or right, the result returned the wrong width, height, and orientation is reverse (up -> down). Thank you.

I can help you. But could you check why an image is not displayed on screen when it is selected ?

@st-tuanmai
Copy link
Contributor Author

st-tuanmai commented Jan 16, 2024

I can help you. But could you check why an image is not displayed on screen when it is selected ?

@priankakariatyml I will check again, it works on my device. could you use the latest code? i think some codes is missing.
thanks you!

@st-tuanmai
Copy link
Contributor Author

st-tuanmai commented Jan 16, 2024

I can help you. But could you check why an image is not displayed on screen when it is selected ?

@priankakariatyml
I have reproduced the issues when segmenting the image many times, probably related to freeing memory when rendering the image, I will try to fix it.

@st-tuanmai
Copy link
Contributor Author

@priankakariatyml Please check the latest code, I fixed the image orientation and memory issues.
Next, I will fix the code conventions and change UI before sending you the final version.
Thanks you!

@priankakariat
Copy link
Contributor

priankakariat commented Jan 18, 2024

@priankakariatyml Please check the latest code, I fixed the image orientation and memory issues.
Next, I will fix the code conventions and change UI before sending you the final version.
Thanks you!

Hi I can see the results of segmentation of gallery images now. But the UI seems to be out of place. I can’t see the bottom bar button. My device is iPhone 11 Pro Max. There is only a white band at the bottom.
IMG_5185
Also after performing inference on images from gallery the resulting image seems to be a little stretched. Can you check if the uiimageview is using the correct content mode.

Also I don’t think the orientation fix has been pushed. The app is still restricted to portrait I think.

@st-tuanmai
Copy link
Contributor Author

st-tuanmai commented Jan 19, 2024

Hi I can see the results of segmentation of gallery images now. But the UI seems to be out of place. I can’t see the bottom bar button. My device is iPhone 11 Pro Max. There is only a white band at the bottom. Also after performing inference on images from gallery the resulting image seems to be a little stretched. Can you check if the uiimageview is using the correct content mode.

Thanks @priankakariatyml
I will check the UI.

Also I don’t think the orientation fix has been pushed. The app is still restricted to portrait I think.

I have fixed the image to portrait and passed in the fixed orientation as .up.

Copy link
Contributor

@priankakariat priankakariat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@st-tuanmai Can you replicate the bottom bar exactly like android. Right now the iOS UI only shows inference time. Android app has controls to switch between the model and change the delegate. These bottom bar is collapsible like in the image classification sample. Can you refer to the Android app and implement these changes ?

@priankakariat
Copy link
Contributor

Hi I can see the results of segmentation of gallery images now. But the UI seems to be out of place. I can’t see the bottom bar button. My device is iPhone 11 Pro Max. There is only a white band at the bottom. Also after performing inference on images from gallery the resulting image seems to be a little stretched. Can you check if the uiimageview is using the correct content mode.

Thanks @priankakariatyml I will check the UI.

Also I don’t think the orientation fix has been pushed. The app is still restricted to portrait I think.

I have fixed the image to portrait and passed in the fixed orientation as .up.

Is there a reason why other orientations are not supported ? Like the previous samples

@st-tuanmai
Copy link
Contributor Author

Is there a reason why other orientations are not supported ? Like the previous samples

I'm having issues with the live camera when rendering in landscape orientation. I will change to support it soon.

// MARK: Define default constants
struct DefaultConstants {

static let lineWidth: CGFloat = 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except for the model path, remove the other constants since they seem to be unused

let sourceImage = request.sourceImage
let time = Float((request.compositionTime - timeRange.start).seconds)
let cgimage = self.render.getCGImmage(ciImage: sourceImage)
guard let resultBundle = self.imageSegmenterService?.segment(by: cgimage, orientation: .up, timeStamps: Int(time * 1000)) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in the previous samples, please encapsulate all this code in the ImageSegmenterSevice method segment(
videoAsset: AVAsset,
durationInMilliseconds: Double,
inferenceIntervalInMilliseconds: Double) async -> ResultBundle?

@priankakariat
Copy link
Contributor

priankakariat commented Feb 7, 2024

@priankakariatyml Please check the latest code, I fixed the image orientation and memory issues.
Next, I will fix the code conventions and change UI before sending you the final version.
Thanks you!

Hi I can see the results of segmentation of gallery images now. But the UI seems to be out of place. I can’t see the bottom bar button. My device is iPhone 11 Pro Max. There is only a white band at the bottom. IMG_5185 Also after performing inference on images from gallery the resulting image seems to be a little stretched. Can you check if the uiimageview is using the correct content mode.

Also I don’t think the orientation fix has been pushed. The app is still restricted to portrait I think.

@st-tuanmai Could you resolve the issue with the bottom bar and adding the capability to choose the model?

@priankakariat
Copy link
Contributor

Is there a reason why other orientations are not supported ? Like the previous samples

I'm having issues with the live camera when rendering in landscape orientation. I will change to support it soon.

We can support it after the app is made feature complete

@@ -0,0 +1,578 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename file to SegmentedImageRenderer

import MetalPerformanceShaders
import MetalKit

class Render {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Render/SegmentedImageRenderer

#include <metal_stdlib>
using namespace metal;

half4 choseColor(half4 color1, half4 color2, float cf) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/choseColor/ChooseColor

isPrepared = false
}

func render(image: UIImage, segmentDatas: UnsafePointer<Float32>?) -> UIImage? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/segmentDatas/confidenceMasks

isPrepared = false
}

func render(image: UIImage, segmentDatas: UnsafePointer<Float32>?) -> UIImage? {
Copy link
Contributor

@priankakariat priankakariat Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is only the mask at index 0 taken for display ?. This will work for selfie segmentation. But if you use a model like deeplabv3 which will provide segmentation masks for all the categories, this won't work.
The sample needs to support Deeply V3 too. The user should be allowed to switch between deeplabv3 and selfie segmenter model in the bottom bar.
Could you please implement this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do it.

}

func segment(
by videoFrame: CGImage,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove by

let naturalSize = videoTrack.naturalSize
let size = videoTrack.naturalSize.applying(videoTrack.preferredTransform)
let newSize = CGSize(width: abs(size.width), height: abs(size.height))
var needChangeWidthHeight = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this variable and directly return using the condition
return (formatDescription, naturalSize.width == newSize.height)

assetReader.add(trackOutput)

// Start the asset reader
if assetReader.startReading() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert the nested ifs in this block to guard let statements to improve readability

}

private func playVideoAnDetect(asset: AVAsset) {
let videoDescription = getVideoFormatDescription(from: asset)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/playVideoAnDetect/playVideoAndSegment

self.hideProgressView()
self.render.prepare(with: image.size, outputRetainedBufferCountHint: 3)
self.inferenceResultDeliveryDelegate?.didPerformInference(result: resultBundle)
let marks = imageSegmenterResult.confidenceMasks
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/marks/masks

self.render.prepare(with: image.size, outputRetainedBufferCountHint: 3)
self.inferenceResultDeliveryDelegate?.didPerformInference(result: resultBundle)
let marks = imageSegmenterResult.confidenceMasks
let _mark = marks![0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@st-tuanmai
Copy link
Contributor Author

@priankakariatyml
Hi Prianka,
Out of memory error appears when I segment using deeplabv3 model with large image ((3024.0, 4032.0)).
Can you help me check it?
image

@PaulTR
Copy link
Collaborator

PaulTR commented Mar 18, 2024

@priankakariatyml Hey Prianka, any updates here?

@priankakariat
Copy link
Contributor

@priankakariatyml Hey Prianka, any updates here?
I'll review this again. Was pretty busy with the Llm APIs. Haven't come back to this.

@PaulTR
Copy link
Collaborator

PaulTR commented Mar 25, 2024

Sorry, merged in the other PRs that were going in. Can you please merge main back into this PR? Thanks

# Conflicts:
#	examples/object_detection/ios/ObjectDetector/ViewControllers/BottomSheetViewController.swift
@PaulTR PaulTR merged commit 0825e07 into google-ai-edge:main Mar 26, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants