-
Notifications
You must be signed in to change notification settings - Fork 488
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
[WIP] ios Image segmenter sample #292
Conversation
There was a problem hiding this 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 ?
There was a problem hiding this 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
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). |
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. |
@priankakariatyml |
@priankakariatyml Please check the latest code, I fixed the image orientation and memory issues. |
Thanks @priankakariatyml
I have fixed the image to portrait and passed in the fixed orientation as .up. |
There was a problem hiding this 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 ?
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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
examples/image_segmentation/ios/ImageSegmenter/ViewContoller/MediaLibraryViewController.swift
Outdated
Show resolved
Hide resolved
@st-tuanmai Could you resolve the issue with the bottom bar and adding the capability to choose the model? |
We can support it after the app is made feature complete |
@@ -0,0 +1,578 @@ | |||
|
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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? { |
There was a problem hiding this comment.
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? { |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert the nested if
s in this block to guard let
statements to improve readability
} | ||
|
||
private func playVideoAnDetect(asset: AVAsset) { | ||
let videoDescription = getVideoFormatDescription(from: asset) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my comment here https://github.com/googlesamples/mediapipe/pull/292/files#r1481541212
@priankakariatyml Hey Prianka, any updates here? |
|
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
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:
Type of Change
Please check the relevant option below:
Screenshots
If applicable, please add screenshots to help explain your changes.
Additional Notes
Add any additional information or context about the pull request here.