Skip to content

Commit

Permalink
Refactor location argument into object, validate said option
Browse files Browse the repository at this point in the history
  • Loading branch information
jtklein committed Nov 21, 2024
1 parent 68ba2e7 commit 0cf480b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 60 deletions.
14 changes: 7 additions & 7 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ export default function App(): React.JSX.Element {
try {
const timeBefore = new Date().getTime();

const latitude = 37.28889;
const longitude = -121.94415;
const elevation = 15.0;
const locationObject = {
latitude: 37.28889,
longitude: -121.94415,
elevation: 15,
};

const cvResult: InatVision.Result = InatVision.inatVision(frame, {
version: modelVersion,
Expand All @@ -186,11 +188,9 @@ export default function App(): React.JSX.Element {
negativeFilter,
numStoredResults: 4,
cropRatio: 0.9,
latitude,
longitude,
elevation,
useGeoModel: true,
geoModelPath,
useGeoModel,
location: locationObject,
patchedOrientationAndroid: 'portrait',
});
const timeAfter = new Date().getTime();
Expand Down
124 changes: 71 additions & 53 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ function optionsAreValid(options: Options | OptionsForImage): boolean {
options.confidenceThreshold < 0 ||
options.confidenceThreshold > 1
) {
throw new INatVisionError(
'getPredictionsForImage option confidenceThreshold must be a string for a number between 0 and 1.'
// have not used INatVisionError here because I can not test it due to issue #36
throw new Error(
'confidenceThreshold must be a string for a number between 0 and 1.'
);
}
}
Expand All @@ -216,8 +217,23 @@ function optionsAreValid(options: Options | OptionsForImage): boolean {
options.cropRatio < 0 ||
options.cropRatio > 1
) {
throw new INatVisionError(
'option cropRatio must be a number between 0 and 1.'
// have not used INatVisionError here because I can not test it due to issue #36
throw new Error('cropRatio must be a number between 0 and 1.');
}
}
if (options.useGeoModel) {
if (!options.location) {
// have not used INatVisionError here because I can not test it due to issue #36
throw new Error('location must be set when useGeoModel is true.');
}
if (
!options.location.latitude ||
!options.location.longitude ||
!options.location.elevation
) {
// have not used INatVisionError here because I can not test it due to issue #36
throw new Error(
'location must have latitude, longitude, and elevation set when useGeoModel is true.'
);
}
}
Expand All @@ -233,9 +249,7 @@ function optionsAreValidForFrame(options: Options): boolean {
options.taxonomyRollupCutoff > 1
) {
// have not used INatVisionError here because I can not test it due to issue #36
throw new Error(
'option taxonomyRollupCutoff must be a number between 0 and 1.'
);
throw new Error('taxonomyRollupCutoff must be a number between 0 and 1.');
}
}
return optionsAreValid(options);
Expand Down Expand Up @@ -320,10 +334,7 @@ function handleResult(result: any, options: Options): Result {
return handledResult;
}

/**
* Represents the options for a call to use the plugin to predict on a frame.
*/
interface Options {
interface BaseOptions {
// Required
/**
* The version of the model to use.
Expand All @@ -342,33 +353,6 @@ interface Options {
* The confidence threshold for the predictions.
*/
confidenceThreshold?: number;
/**
* A taxonomy rollup cutoff threshold.
* As a fraction of 1. After computer vision predictions are returned, this value filters out all nodes with
* a lower score for the caluclation of the best branch or top predictions.
* Defaults to 0.0.
*/
taxonomyRollupCutoff?: number;
/**
* *Android only.*
*
* The iconic taxon id to filter by.
*/
filterByTaxonId?: null | string;
/**
* *Android only.*
*
* Wether to exclude the taxon set by filterByTaxonId or to only include it (and exclude all other).
*/
negativeFilter?: null | boolean;
/**
* The number of results to keep stored internally.
*
* Specifies the integer number of results to store internally that the plugin serves the best out of.
* E.g. if the plugin is called with this number set to 5, the plugin will serve the best result out of the 5 stored previous results.
* Setting this number to 0 or 1 will always return the current result (i.e. none or only one frame result will be stored at a time).
*/
numStoredResults?: number;
/**
* *Android only.*
*
Expand All @@ -384,24 +368,64 @@ interface Options {
useGeoModel?: boolean;
/**
*
* The latitude of the location.
* The location object used for geomodel prediction.
*/
location?: {
/**
*
* The latitude of the location.
*/
latitude?: number;
/**
*
* The longitude of the location.
*/
longitude?: number;
/**
*
* The elevation of the location.
*/
elevation?: number;
};
/**
*
* The path to the geo model file.
*/
latitude?: number;
geoModelPath?: string;
}

/**
* Represents the options for a call to use the plugin to predict on a frame.
*/
interface Options extends BaseOptions {
// Optional
/**
* The number of results to keep stored internally.
*
* The longitude of the location.
* Specifies the integer number of results to store internally that the plugin serves the best out of.
* E.g. if the plugin is called with this number set to 5, the plugin will serve the best result out of the 5 stored previous results.
* Setting this number to 0 or 1 will always return the current result (i.e. none or only one frame result will be stored at a time).
*/
numStoredResults?: number;
/**
* A taxonomy rollup cutoff threshold.
* As a fraction of 1. After computer vision predictions are returned, this value filters out all nodes with
* a lower score for the caluclation of the best branch or top predictions.
* Defaults to 0.0.
*/
longitude?: number;
taxonomyRollupCutoff?: number;
/**
* *Android only.*
*
* The elevation of the location.
* The iconic taxon id to filter by.
*/
elevation?: number;
filterByTaxonId?: null | string;
/**
* *Android only.*
*
* The path to the geo model file.
* Wether to exclude the taxon set by filterByTaxonId or to only include it (and exclude all other).
*/
geoModelPath?: string;
negativeFilter?: null | boolean;
// Patches
/**
* Currently, using react-native-vision-camera v3.9.1, Android does not support orientation changes.
Expand All @@ -428,15 +452,9 @@ export function inatVision(frame: Frame, options: Options): Result {
return handledResult;
}

interface OptionsForImage {
interface OptionsForImage extends BaseOptions {
// Required
uri: string;
version: string;
modelPath: string;
taxonomyPath: string;
// Optional
confidenceThreshold?: number;
cropRatio?: number;
}

/**
Expand Down

0 comments on commit 0cf480b

Please sign in to comment.