react-native-executorch
Version:
An easy way to run AI models in react native with ExecuTorch
40 lines (39 loc) • 1.22 kB
JavaScript
;
import { useEffect, useMemo, useState } from 'react';
import { OCRController } from '../../controllers/OCRController';
export const useOCR = ({
detectorSource,
recognizerSources,
language = 'en',
preventLoad = false
}) => {
const [error, setError] = useState(null);
const [isReady, setIsReady] = useState(false);
const [isGenerating, setIsGenerating] = useState(false);
const [downloadProgress, setDownloadProgress] = useState(0);
const model = useMemo(() => new OCRController({
modelDownloadProgressCallback: setDownloadProgress,
isReadyCallback: setIsReady,
isGeneratingCallback: setIsGenerating,
errorCallback: setError
}), []);
useEffect(() => {
const loadModel = async () => {
await model.loadModel(detectorSource, recognizerSources, language);
};
if (!preventLoad) {
loadModel();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [model, detectorSource, language,
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(recognizerSources), preventLoad]);
return {
error,
isReady,
isGenerating,
forward: model.forward,
downloadProgress
};
};
//# sourceMappingURL=useOCR.js.map