How to assign getOpenFileName to a global variable
-
In frame i have such code
image_import_btn.clicked.connect(partial(display_image, grid_image_detect, image_detect_btn)) #image_detect_btn.clicked.connect(partial(main, image_file, grid_image_detect)) ( wanna use before but image path doesn't have any parameter inside)And such function
def display_image(grid_image_detect, image_detect_btn): global image_file load_image = QLabel() image_file = "" image_file = QFileDialog.getOpenFileName(load_image, 'Open file', 'Images', "Image Files (*.jpg *.gif *.bmp *.png)") loaded_image = QPixmap(image_file[0]) load_image.setPixmap(loaded_image.scaled(800, 500, Qt.AspectRatioMode.KeepAspectRatio)) grid_image_detect.addWidget(load_image, 0, 1) image_detect_btn.clicked.connect(partial(main, image_file)) #That event stores previos images how i can fix it # I open first one it detects when import second it detects first after second. How to clear repeated calls? return grid_image_detectAnd the result of detecting image I wanna show in grid how i can implement it ?
I had an idea to return detected image as a variable from OpenCV on clicked event button but don't know how to pass variables in OpenCV function and return right image variable without repeated calls. -
In frame i have such code
image_import_btn.clicked.connect(partial(display_image, grid_image_detect, image_detect_btn)) #image_detect_btn.clicked.connect(partial(main, image_file, grid_image_detect)) ( wanna use before but image path doesn't have any parameter inside)And such function
def display_image(grid_image_detect, image_detect_btn): global image_file load_image = QLabel() image_file = "" image_file = QFileDialog.getOpenFileName(load_image, 'Open file', 'Images', "Image Files (*.jpg *.gif *.bmp *.png)") loaded_image = QPixmap(image_file[0]) load_image.setPixmap(loaded_image.scaled(800, 500, Qt.AspectRatioMode.KeepAspectRatio)) grid_image_detect.addWidget(load_image, 0, 1) image_detect_btn.clicked.connect(partial(main, image_file)) #That event stores previos images how i can fix it # I open first one it detects when import second it detects first after second. How to clear repeated calls? return grid_image_detectAnd the result of detecting image I wanna show in grid how i can implement it ?
I had an idea to return detected image as a variable from OpenCV on clicked event button but don't know how to pass variables in OpenCV function and return right image variable without repeated calls.@MAX001
I don't know about any OpenCV stuff, but Qt slots cannot return any value (it is simply discarded). You can either do whatever you need called from the slot, or possibly you couldemityour own signal from the slot so that the outside world can in turn place its own slot on that signal and do the work and such a slot. -
@MAX001
I don't know about any OpenCV stuff, but Qt slots cannot return any value (it is simply discarded). You can either do whatever you need called from the slot, or possibly you couldemityour own signal from the slot so that the outside world can in turn place its own slot on that signal and do the work and such a slot.@JonB Is it an idea to work with
QThreadSlotsEmitfunction? And this needs to be added to thedisplay_image()function itself?
I managed to display the image in the grid, but the processing of the recognition algorithm is still repeated.
And I don't really understand whyimage_fileis always empty when I want to call outside of the functiondisplay_image() -
@JonB Is it an idea to work with
QThreadSlotsEmitfunction? And this needs to be added to thedisplay_image()function itself?
I managed to display the image in the grid, but the processing of the recognition algorithm is still repeated.
And I don't really understand whyimage_fileis always empty when I want to call outside of the functiondisplay_image()@MAX001
All I know is don't use threads unless you really need to. They often cause problems. I do not know whether you need to put your Open CV in a thread though.image_fileshould have a value afterdisplay_imagehas been called and thegetOpenFileName()been executed, but not before. Don't forget you need to useglobal image_fileagain elsewhere wherever you want to access it? -
@MAX001
All I know is don't use threads unless you really need to. They often cause problems. I do not know whether you need to put your Open CV in a thread though.image_fileshould have a value afterdisplay_imagehas been called and thegetOpenFileName()been executed, but not before. Don't forget you need to useglobal image_fileagain elsewhere wherever you want to access it?@JonB When I make a
display_imagefunction call, I declare a globalimage_filevariable, aftergetOpenFileName()I return a string with two parameters, I take the first one which is the path and if I callimage_detect_btn.clicked.connect(partial(main, image_file, grid_image_detect))in a function, it works but repeats with the previous images. When I make a call outside the function,IndexError occurs: string index out of range
How it works -
Hi,
It looks like you are over-engineering things a bit.
What is your exact workflow ?
- click button
- select file
- load image
- show image
- process image with OpenCV
- show processed image
?
-
Hi,
It looks like you are over-engineering things a bit.
What is your exact workflow ?
- click button
- select file
- load image
- show image
- process image with OpenCV
- show processed image
?
@SGaist Idea, I have a frame in which on the right side just a picture will open on the left side a picture with recognized objects.
Then I can import another picture, it will open on the right side, then I press the Detect button and the recognized picture will appear on the left side. -
Then you can simply store the image path as class member variable and reuse it in the slot that will do the OpenCV processing.