I am working on my capstone project to finish up my CS degree. I am getting stumped on this part…
I am attempting to create a Keras project that allows a user to upload an image of a chest x-ray and classify it. I am getting an “expected str, bytes or os.PathLike object, not ImageFieldFile” error. The error is coming from this line in models.py: img = keras.preprocessing.image.load_img(self.img, target_size=(512, 512)). If I cast self.img to a string with img = keras.preprocessing.image.load_img(str(self.img), target_size=(512, 512)) I get an error of “[Errno 2] No such file or directory: ‘Tuberculosis-24.png’”
# views.py
class RadiographCreateView(CreateView):
model = Radiograph
fields = ['img']
# models.py
class Radiograph(models.Model):
img = models.ImageField(upload_to='images')
result = models.CharField(max_length=200, blank=True)
def get_absolute_url(self):
return reverse('list')
def save(self, *args, **kwargs):
img = keras.preprocessing.image.load_img(self.img, target_size=(512, 512))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = train.model.predict(img_array)
score = tf.nn.softmax(predictions[0])
result = "This image most likely belongs to {} with a {:.2f} percent confidence.".format(train.class_names[np.argmax(score)], 100 * np.max(score))
self.result = result
# self.result = self.read()
super().save(*args, **kwargs)
def __str__(self):
return self.result
Any ideas? Thanks!