Brak opisu

loadPlantVillage.ts 766B

12345678910111213141516171819202122232425262728
  1. import * as ort from "onnxruntime-react-native";
  2. import * as FileSystem from "expo-file-system/legacy";
  3. import { Asset } from "expo-asset";
  4. let cachedSession: ort.InferenceSession | null = null;
  5. export async function loadPlantVillageSession() {
  6. if (cachedSession) return cachedSession;
  7. const asset = Asset.fromModule(
  8. require("../../assets/plantvillage_mnv3s_224.onnx")
  9. );
  10. await asset.downloadAsync();
  11. const modelPath = FileSystem.documentDirectory + "plantvillage_mnv3s_224.onnx";
  12. const info = await FileSystem.getInfoAsync(modelPath);
  13. if (!info.exists) {
  14. await FileSystem.copyAsync({
  15. from: asset.localUri!,
  16. to: modelPath,
  17. });
  18. }
  19. cachedSession = await ort.InferenceSession.create(modelPath);
  20. return cachedSession;
  21. }