I’m trying to display an image that has a bounding box over face. The box is calculated on an uplinked server.
When I display it using image_1.source = img, it errors with “TypeError: The ‘source’ property can only be set to a Media object or a URL”.
Code Sample:
Here’s code from my Form
def file_loader_1_change(self, file, **event_args):
result = anvil.server.call("identify_celeb", file)
img = anvil.server.call("bound_box", file)
self.image_1.source = img # ←- Error occurs here
self.result_lbl.text = "Our model thinks it's " + result
Here’s the code from the uplinked server:
@anvil.server.callable
def bound_box (file):
with anvil.media.TempFile(file) as filename:
img_bgr = cv2.imread(filename)
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(img_gray, 1.5, 5)
class_names=list(classes.keys()) #List of the class names
label = class_names[np.argmax(score)][5:]
height, width, _ = img_rgb.shape
left = int(width/17)
down = int(height/11)
fontsize = 0.00211*width + 0.01
for (x,y,w,h) in face:
img = cv2.rectangle(img_rgb,(x, y),(x + w, y + h),(255,255,0),2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img_rgb, label, (x,y-15), font, fontsize, (200,255,155),2)
roi_gray = img_gray[y: y + h, x: x + w]
roi_color = img_rgb[y: y + h, x: x + w]
return img
Clearly img
is an image file, as I can display it in the Jupyter Notebook with plt.show(img)
. Is there a special function to use for this? I am using import anvil.media
.