Skip to content

Commit 2c7740e

Browse files
authored
Update CTfLiteClass.cpp (#3397)
1 parent 3ee32d8 commit 2c7740e

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

code/components/jomjol_tfliteclass/CTfLiteClass.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,18 @@ void CTfLiteClass::GetInputTensorSize()
246246

247247
long CTfLiteClass::GetFileSize(std::string filename)
248248
{
249-
struct stat stat_buf;
250-
long rc = stat(filename.c_str(), &stat_buf);
251-
return rc == 0 ? stat_buf.st_size : -1;
249+
struct stat stat_buf;
250+
long rc = -1;
251+
252+
FILE *pFile = fopen(filename.c_str(), "rb"); // previously only "rb
253+
254+
if (pFile != NULL)
255+
{
256+
rc = stat(filename.c_str(), &stat_buf);
257+
fclose(pFile);
258+
}
259+
260+
return rc == 0 ? stat_buf.st_size : -1;
252261
}
253262

254263

@@ -270,25 +279,33 @@ bool CTfLiteClass::ReadFileToModel(std::string _fn)
270279

271280
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Loading Model " + _fn + " /size: " + std::to_string(size) + " bytes...");
272281

273-
274282
#ifdef DEBUG_DETAIL_ON
275283
LogFile.WriteHeapInfo("CTLiteClass::Alloc modelfile start");
276284
#endif
277285

278286
modelfile = (unsigned char*)psram_get_shared_model_memory();
279287

280-
if(modelfile != NULL)
288+
if (modelfile != NULL)
281289
{
282-
FILE* f = fopen(_fn.c_str(), "rb"); // previously only "r
283-
fread(modelfile, 1, size, f);
284-
fclose(f);
290+
FILE *pFile = fopen(_fn.c_str(), "rb"); // previously only "rb
291+
292+
if (pFile != NULL)
293+
{
294+
fread(modelfile, 1, size, pFile);
295+
fclose(pFile);
285296

286-
#ifdef DEBUG_DETAIL_ON
287-
LogFile.WriteHeapInfo("CTLiteClass::Alloc modelfile successful");
288-
#endif
297+
#ifdef DEBUG_DETAIL_ON
298+
LogFile.WriteHeapInfo("CTLiteClass::Alloc modelfile successful");
299+
#endif
289300

290-
return true;
291-
}
301+
return true;
302+
}
303+
else
304+
{
305+
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CTfLiteClass::ReadFileToModel: Model does not exist");
306+
return false;
307+
}
308+
}
292309
else
293310
{
294311
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CTfLiteClass::ReadFileToModel: Can't allocate enough memory: " + std::to_string(size));

0 commit comments

Comments
 (0)