Skip to content

Commit

Permalink
feat: allow the default camera resolution to be set (#347)
Browse files Browse the repository at this point in the history
* Fix IOS Bundle ID issue for Xcode Archive/App uploads

* -Fix flipped front camera on mobile
-Allow default resolution of camera to be set to 1280x720 for devices that have high resolution cameras (prevents crash behaviour on mobile)

* Revert "Fix IOS Bundle ID issue for Xcode Archive/App uploads"

This reverts commit a13a722.

* Revert "-Fix flipped front camera on mobile"

This reverts commit fea330c.

* Use a manually set default resolution within 300px to choose the nearest default resolution on mobile
-Prevents huge performance issues on phones with high resolution cameras when using the sample app

* Apply suggestions from code review

Co-authored-by: Junrou Nishida <[email protected]>

* Refactor to improve the default width functionality - tested

* Check if default resolution matches first, if not fall back to use a resolution within 300px, else return the first resolution found

* Update Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs

Co-authored-by: Junrou Nishida <[email protected]>

* Move resolution switching from ImageSource to WebCamSource exclusively

* Revert ImageConfig changes to allow default active resolution to populate resolution dropdown

* Implemented resolution comparer

* Ensure lf file ending again

* Remove redundant boolean

* Update tooltip text - Update Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs

Co-authored-by: Junrou Nishida <[email protected]>

* Make _preferableDefaultWidth private - Update Assets/Mediapipe/Samples/Common/Scripts/ImageSource/WebCamSource.cs

Co-authored-by: Junrou Nishida <[email protected]>

* Remove unnecessary UnityEngine import

* Move ResolutionStructComparer to the end of WebCamSource.cs

* Ensure all lf maintained

Co-authored-by: RobFarthing_EPM <[email protected]>
Co-authored-by: Junrou Nishida <[email protected]>
  • Loading branch information
3 people committed Nov 10, 2021
1 parent 2994e44 commit 4826ee2
Showing 1 changed file with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

Expand All @@ -17,6 +18,9 @@ namespace Mediapipe.Unity
{
public class WebCamSource : ImageSource
{
[Tooltip("For the default resolution, the one whose width is closest to this value will be chosen")]
[SerializeField] private int _preferableDefaultWidth = 1280;

private const string _TAG = nameof(WebCamSource);

[SerializeField] private ResolutionStruct[] _defaultAvailableResolutions;
Expand Down Expand Up @@ -227,8 +231,7 @@ public override Texture GetCurrentTexture()
private ResolutionStruct GetDefaultResolution()
{
var resolutions = availableResolutions;

return (resolutions == null || resolutions.Length == 0) ? new ResolutionStruct() : resolutions[0];
return resolutions == null || resolutions.Length == 0 ? new ResolutionStruct() : resolutions.OrderBy(resolution => resolution, new ResolutionStructComparer(_preferableDefaultWidth)).First();
}

private void InitializeWebCamTexture()
Expand All @@ -254,5 +257,32 @@ private IEnumerator WaitForWebCamTexture()
throw new TimeoutException("Failed to start WebCam");
}
}

private class ResolutionStructComparer : IComparer<ResolutionStruct>
{
private readonly int _preferableDefaultWidth;

public ResolutionStructComparer(int preferableDefaultWidth)
{
_preferableDefaultWidth = preferableDefaultWidth;
}

public int Compare(ResolutionStruct a, ResolutionStruct b)
{
var aDiff = Mathf.Abs(a.width - _preferableDefaultWidth);
var bDiff = Mathf.Abs(b.width - _preferableDefaultWidth);
if (aDiff != bDiff)
{
return aDiff - bDiff;
}
if (a.height != b.height)
{
// prefer smaller height
return a.height - b.height;
}
// prefer smaller frame rate
return (int)(a.frameRate - b.frameRate);
}
}
}
}

0 comments on commit 4826ee2

Please sign in to comment.