Here is my basic image classifier program in python
enter code here
from skimage import color
from skimage import io
import cv2
import matplotlib.pyplot as plt #to output images
import numpy as np
import tensorflow as tf #main library for AI learning
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.utils import load_img
import os #for directories
import shutil #to copy and paste
def get_prediction(image):
test_img = cv2.imread(image)
img_bcp = test_img.copy()
face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
gray_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)
faces_coordinates = face_classifier.detectMultiScale(gray_img)
for (x, y, w, h) in faces_coordinates:
cv2.rectangle(test_img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cropped_face = img_bcp[y:y+h, x:x+w]
test_img = cv2.cvtColor(cropped_face, cv2.COLOR_BGR2RGB)
imgGray = color.rgb2gray(test_img)
class_dictionary = {0: 'angry', 1: 'fear', 2: 'happy', 3: 'neutral', 4: 'sad', 5: 'surprise'}
imgGray = cv2.resize(cropped_face, (224,224))
imgGray = np.expand_dims(imgGray, axis=0) ## Need 4th dimension
new_model = keras.models.load_model('/content/drive/MyDrive/emotion_detection')
prediction = new_model.predict(imgGray)
lebel = np.argmax(prediction)
return(class_dictionary[lebel])
import anvil.media
@anvil.server.callable
def classify_image(file):
with anvil.media.TempFile(file) as f:
img = load_img(f)
score = get_prediction(img)
return score
Now on the anvil side, There is upload button as well as lebel named results_lbl. Here is the code for it
def file_loader_1_change(self, file, **event_args):
"""This method is called when a new file is loaded into this FileLoader"""
score = anvil.server.call("classify_image",file)
self.results_lbl.text = f"({score:.1f})"
self.image_1.source = file
The error when I upload the image is: TypeError: Can't convert object to 'str' for 'filename' at :14 called from :6enter image description here called from Form1, line 14