Skip to content

Commit

Permalink
Remove deprecated model files; Use model on iOS directly from bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
jtklein committed Dec 19, 2024
1 parent 759c8e5 commit 68b56b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
4 changes: 2 additions & 2 deletions utility/dirStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export const dirHome: string = Platform.select( {
} );

export const dirModel: string = Platform.select( {
ios: `${RNFS.DocumentDirectoryPath}/${modelFiles.IOSMODEL}`,
ios: `${RNFS.MainBundlePath}/${modelFiles.IOSMODEL}`,
android: `${RNFS.DocumentDirectoryPath}/${modelFiles.ANDROIDMODEL}`
} );

export const dirTaxonomy: string = Platform.select( {
ios: `${RNFS.DocumentDirectoryPath}/${modelFiles.IOSTAXONOMY}`,
ios: `${RNFS.MainBundlePath}/${modelFiles.IOSTAXONOMY}`,
android: `${RNFS.DocumentDirectoryPath}/${modelFiles.ANDROIDTAXONOMY}`
} );

Expand Down
55 changes: 39 additions & 16 deletions utility/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,12 @@ const addCameraFilesAndroid = () => {
} );
};

const addCameraFilesiOS = () => {
const copyFilesiOS = ( source, destination ) => {
RNFS.copyFile( source, destination ).then( ( result ) => {
console.log( `moved file from ${source} to ${destination}` );
} ).catch( ( error ) => {
console.log( error, `error moving file from ${source} to ${destination}` );
} );
};

const checkForModelFileIOS = () => {
RNFS.readDir( RNFS.MainBundlePath ).then( ( results ) => {
const model = modelFiles.IOSMODEL;
const taxonomy = modelFiles.IOSTAXONOMY;

const hasModel = results.find( ( r ) => r.name === model );

if ( hasModel !== undefined ) {
console.log( "Found model asset with filename", model );
copyFilesiOS( `${RNFS.MainBundlePath}/${model}`, dirModel );
copyFilesiOS( `${RNFS.MainBundlePath}/${taxonomy}`, dirTaxonomy );
} else {
console.log( "No model asset found to copy into document directory." );
Alert.alert(
Expand All @@ -92,12 +79,48 @@ const addCameraFilesiOS = () => {
} );
};

const removeDeprecatedModelFilesIOS = () => {
// On releasing cv model 2.13 (the second one ever), we changed the app to use the model
// from the main bundle directly instead of the document directory. This function removes all
// existing model files from the document directory.
RNFS.readDir( RNFS.DocumentDirectoryPath ).then( ( results ) => {
results.forEach( ( result ) => {
if ( result.name.includes( ".mlmodelc" ) || result.name.includes( "taxonomy" ) ) {
RNFS.unlink( `${RNFS.DocumentDirectoryPath}/${result.name}` ).then( () => {
console.log( "Removed deprecated model file: ", result.name );
} ).catch( ( error ) => {
console.log( error, "error removing deprecated model file" );
} );
}
} );
} );
};

const removeDeprecatedModelFilesAndroid = () => {
RNFS.readDir( RNFS.DocumentDirectoryPath ).then( ( results ) => {
results.forEach( ( result ) => {
if ( result.name === modelFiles.ANDROIDMODEL || result.name === modelFiles.ANDROIDTAXONOMY ) {
console.log( "Not removing model asset with filename", result.name );
return;
}
if ( result.name.includes( ".tflite" ) || result.name.includes( ".csv" ) ) {
RNFS.unlink( `${RNFS.DocumentDirectoryPath}/${result.name}` ).then( () => {
console.log( "Removed deprecated model file: ", result.name );
} ).catch( ( error ) => {
console.log( error, "error removing deprecated model file" );
} );
}
} );
} );
};

const addARCameraFiles = async () => {
// RNFS overwrites whatever files existed before
if ( Platform.OS === "android" ) {
removeDeprecatedModelFilesAndroid();
addCameraFilesAndroid();
} else if ( Platform.OS === "ios" ) {
addCameraFilesiOS();
removeDeprecatedModelFilesIOS();
checkForModelFileIOS();
}
};

Expand Down

0 comments on commit 68b56b3

Please sign in to comment.