What I’m trying to do:
Send an opencv image from python 3.10 server to webapp.
What I’ve tried and what’s not working:
I have 2 identical webapps, one running the full python 3 environment and one with a custom python 3.10 environment.
On the client side the user uploads an image using the file uploader component and upon a change event I call a function on the server, sending the image media file to the server, doing some image analysis and manipulations in opencv and sending the resulting image back to be displayed on the client.
Code run on client side at change event from file uploader component:
def file_loader_1_change(self, file, **event_args):
self.image_1.source = anvil.server.call('runInference', file)
Importing and exporting image media file to and from server:
@anvil.server.callable
def runInference(file):
with anvil.media.TempFile(file) as filename:
img = cv2.imread(filename)
# Inference
boxes, segments, _ = model(img, conf_threshold=0.5, iou_threshold=0.45)
# Draw bboxes and polygons
if len(boxes) > 0:
res = model.draw_and_visualize(img, boxes, segments)
cv2.imwrite("res.jpeg", res)
return anvil.media.from_file("res.jpeg", 'image/jpeg')
I don’t know if saving the image with imwrite is the ideal solution, but I was not able to use the from_file function with the “res” variable as it needs a “filename”.
Anyway, on the full python 3 environment, this mostly works, the resulting image is sent back and displayed in the webbrowser correctly, however, one of the image operations I need to do is to find aruco markers in the image, the aruco module is not present in the pre-installed version of opencv in the full environment, so I made a custom python 3.10 environment, using the “Machine learning” base environtment, with these only 2 additional packages installed:
- onnxruntime (version 1.16.3)
- opencv-python-headless (version 4.8.1.78)
On this new environment I no longer get errors about the aruco module not being present in opencv, however, I get the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'res.jpeg'
at /downlink-sources/downlink-2023-11-22-11-25-32/anvil/media.py:36
Does anybody know if I am missing some file/directory operations package or if there is a better way of converting a numpy/opencv image to a anvil.media?
thanks.
All server imports on both environments:
import anvil.files
from anvil.files import data_files
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables
import anvil.server
import anvil.media
import cv2
import numpy as np
import onnxruntime as ort
P.S.: Even though I am currently only importing numpy, from the base environment, some optional extra operations I would like to do in the future uses scipy, matplotlib and sklearn, hence why i went with the “machine learning” environment.