When I am uploading a picture to check a picture according to tensorflow h5
model, I am loading the image using load_model
of tensorflow.keras.models
but it is not accepting. For JPG, it is showing TypeError: expected str, bytes or os.PathLike object, not JpegImageFile
and for PNG, it is showing as TypeError: expected str, bytes or os.PathLike object, not PngImageFile
. What to do now?
I tried the code with raw python but it worked nicely.
Code:
JavaScript
x
34
34
1
#views.py
2
3
import numpy as np
4
from tensorflow.keras.models import load_model
5
from tensorflow.keras.preprocessing import image
6
7
pic = request.FILES['image']
8
img = Image.open(pic)
9
detection=load_model(os.path.join(settings.BASE_DIR,'static/auto_chloro_model.h5'))
10
test_img=image.load_img(img,target_size=(48,48))
11
test_img=image.img_to_array(test_img)
12
test_img=np.expand_dims(test_img,axis=0)
13
result=detection.predict(test_img)
14
a=result.argmax()
15
print(a)
16
17
#models.py
18
19
class images(models.Model):
20
img_main = models.ImageField(upload_to="images_api", default="")
21
22
def __str__(self):
23
return self.product_name
24
25
#forms.py
26
27
class imageForm(forms.ModelForm):
28
image = forms.ImageField()
29
30
class Meta:
31
model = images
32
fields = ['image']
33
34
Traceback:
JavaScript
1
11
11
1
Traceback (most recent call last):
2
File "C:Usersjoyan.condaenvstensorflow-djangolibsite-packagesdjangocorehandlersexception.py", line 47, in inner
3
response = get_response(request)
4
File "C:Usersjoyan.condaenvstensorflow-djangolibsite-packagesdjangocorehandlersbase.py", line 181, in _get_response
5
response = wrapped_callback(request, *callback_args, **callback_kwargs)
6
File "H:Projects + Programming ProjectsAuto Chloroplantdetectionviews.py", line 50, in uploadImage
7
test_img=image.load_img(img,target_size=(48,48))
8
File "C:Usersjoyan.condaenvstensorflow-djangolibsite-packageskeras_preprocessingimageutils.py", line 113, in load_img
9
with open(path, 'rb') as f:
10
TypeError: expected str, bytes or os.PathLike object, not JpegImageFile```
11
Advertisement
Answer
This solved my problem.
JavaScript
1
10
10
1
pic = request.FILES['image']
2
img_new = images(img_main= pic)
3
img_new.save()
4
detection=load_model(os.path.join(settings.BASE_DIR,'staticfiles/auto_chloro_model.h5'))
5
test_img=image.load_img(img_new.img_main.path,target_size=(48,48))
6
test_img=image.img_to_array(test_img)
7
test_img=np.expand_dims(test_img,axis=0)
8
result=detection.predict(test_img)
9
a=result.argmax()
10