Hi guys, i want to make a program in which i will upload an image and then i will extract all the data from it.
But i’m stuck on the basics…i’m new on anvil and i don’t know how to play with the media objects.
I want after someone upload an image and click on a button to take that image and extract the data.
But i made it easy and put it in data tables as media objects. But in server module when i call the cv2.imread(table_row) it gives me an error
Client
def button_4_click(self, **event_args):
table_row = app_tables.data.search()[0]
anvil.server.call('main_function', table_row)
Error
TypeError: Can't convert object to 'str' for 'filename'
at ServerModule1, line 24
called from Form1, line 27
This is the app. I bought online courses from udemy on opencv and i want to use it with anvil and i need your help guys, thanks!
https://anvil.works/build#clone:MVMLQWYIAOS2WO5O=I6EXBJOKXUF5DQ6P4XSBXUDZ
You haven’t shown the code where the error is occurring, so it’s a little hard to comment on what’s happening there. However, you can see the code, and there are some powerful clues right in front of you. How are you interpreting the error message? What does it mean to you?
… and by the way, welcome to the Forum!
This is server module.
![image](https://anvil.works/forum/uploads/default/original/2X/8/84ff950cb4f2e331c8e5c7ac1c4d27c59199dded.png)
and i get None when i print it…and i need to get byte string or something…sorry but i’m new to anvil and python honestly.
If you can help me! Thanks !
When main_function
is called, you are supplying table_row
, which is, in fact, a variable referring to a database table row. So that’s what main_function
will receive: a (copy of) that table row object.
See Storing Data in Data Tables for how that works, and how to get specific column values out of the object. (Hint: Such an object behaves (mostly) like a Python dict
.)
Once you’ve extracted the Media Object from the row, see Files, Media and Binary Data for how to work with that object. (Hint: look for the get_bytes
function.)
I’m not familiar with OpenCV. But search this forum for topics where it is discussed.
1 Like
I try and i try and i can’t get the value of that media…
table_row = app_tables.data.get(media="invoice.jpeg")
print(table_row)
and i get this error
anvil.tables.TableError: Column 'media' can only be searched with a media (not a string) in table 'data'
How to get this done?! Thanks mate…
Your data
table has only one column called media
of type media.
You cannot search a media column with a string.
You could add a second column called name
and use that for your searches:
row = app_tables.data.get(name="invoice.jpeg")
media = row['media']
Or, if you plan to have only one row, you don’t need any filter, simply:
row = app_tables.data.get()
media = row['media']
3 Likes
This is the client now.
row = app_tables.data.get()
media = row['media']
self.image_2.source = media
anvil.server.call('main_function', media)
And this is the server
@anvil.server.callable
def main_function(media_objectx):
img = cv2.imread(f"{media_objectx.get_bytes()}", cv2.IMREAD_COLOR)
print(img)
i tried with get_bytes and without it. I still get None…just…why? How to workaround this? I just want to do a simple opencv task.
And from that print i get “None”. Just that. In the client i get my image to show it…but for opencv i can’t.
Sorry guys for my incompetence… but i have tried all. I bought the personal plan just for this…and i don’t want to do it in flask…
A quick scan of the opencv docs suggests that imread takes a file name as its first argument. You are passing a media object or bytes, so that’s not going to work.
Anvil’s docs have a section on precisely this situation at the bottom of Anvil Docs | Files, Media and Binary Data
1 Like
Thank you!! It worked! Thanks a lot!! God bless you. I’m so happy. Thanks guys for everything i tried all the solutions you gave me and the “convert the TempFile to opencv” from stefano.menci worked. Thanks all of you for the help! I can go on with my project! Have a nice day!
1 Like