foreground extraction in opencv using python - python

I am trying to do foreground extraction(Grab cut algorithm) in open CV using python. I am using the code from the link . The code runs but everytime I am moving the mouse it is drawing a rectangle.
Problem: I cannot draw the touching strokes even after pressing the 0,1,2,3 keys as mentioned. Can someone please suggest how to come out of the rectangle mode and fix the rectangle after i release the mouse button.
I am new to open CV,any help would be appreciated

The code is structured very clearly. You might start by adding print statements in the onmouse method, in each of the
if event == ...
blocks, to figure out which events are correctly registered and which don't come through.

Related

How do I create something as shown below (volume change indicator) in linux?

I would like to show an image with transparent background to indicate something when a key combination is pressed.
Let's say I pressed ctrl+f3, I trigger a python script. Is there anyway I can make that happen?
What python library can I use to show an image without window border and background?
I have figured out how to trigger the file on key press. How to I deal with the (imshow) thing?
Thank you.
show an image without window border and background
This sound like task for some GUI library. There are many available but you would need test them in order to find which one can do it. First feature is generally known as frameless or borderless window. tkinter which ships with python has ability to work this way, see for example tutorialspoint.com tutorial, though I do not know how it will work with alpha channel of your image.

I am trying to move my camera in minecraft with python

I have successfully found out how to move, drag, and click in Minecraft with python code. The only thing is that I have gotten the cursor to work in the inventory/crafting menu. One line I used to go up was
pyautogui.moveRel(0, -33, duration=0.1)
For some reason, this does not work outside the crafting menu. I am not able to move the camera in the open-world part of Minecraft. I had a problem with the keyboard as well, it would type the keys I would want it to in a text box but not in the real game. I fixed this by using keyboard.press() and keyboard.release() instead of using pyautogui. Pyautogui works for clicking and moving in the crafting menu but not in the real game.
Any thoughts on how to move the camera in-game?
Pyautogui does not work on video games on Windows or any other common OS. I suggest trying https://pypi.org/project/mouse/, as the keyboard function worked for other people.
Pyhon DirectInput Mouse Relative Moving act not as expected
Looking at this, I found the answer. I was trying to move the mouse where the game has a specific directinput I needed to use. I just called MouseMoveTo() with the desired numbers.
For some reason it only works like this MouseMoveTo(0,100,) with the added comma at the end
I got it to work by turning off Raw Input in the Mouse Settings. Which are in Options > Controls > Mouse Settings > Raw Input

How to get tootip(infotip) text in python? (a popup text box appearing when a mouse cursor is hovered on)

I am currently working for an automatic screen capture program in Python3. While developing it, I ran into a couple of issues on Windows tooltip(infotip) boxes.
What I need to do is this:
1. First I need to find out whether there is any tooltip(infotip - a small text box appearing when a mouse cursor is hovered on an icon or a button) at a specified mouse coordinates. (e.g.) When a mouse cursor is moved to (300, 300) in the main monitor and hovered for a while, does a tooltip box appear? I hope I can get the result with a boolean variable if possible.
2. (main problem) If there is a tooltip, how can I get the content(text) in it?
I have tried solving the problems using pywinauto module, but as there is not much documentation about it so I am completely stucked... Can anyone help me with this? Using pywinauto module is not necessary if there is an easy way!!
Thanks in advance!!

Using PIL (Python Image Library) to detect image on screen

I am trying to understand how I can use PIL in Python 2.7 to search the whole screen for a certain image and click on it. I've been searching around and haven't been able to find a solution. I want to create a small GUI with one button in the middle of it that when clicked will search the entire screen for a predefined image. Once the image is found the program will then click in the centre of it and end. In short the program will detect if an image is present on the users screen and click it.
I did find an interesting bit on Sikuli, but that doesn't help me because it's unable to export to an .exe.
The image that the program will look for will most likely be in the same place each time it searches, but I didn't want to hard-code the location as it has the potential to move and I don't want that being an issue later on.
What I need is the code method I would use to search for the image on screen and send back the cords to a variable.
Image explanation/example:
Reference image of rifle:
PIL is the wrong tool for this job. Instead you should look into openCV (open source computer vision), which has fantastic python bindings. Here is a link to an example (in C but should be easy to redo with the python bindings) that does what you are looking for, but even allows the image to be rotated, scaled, etc.
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
http://docs.opencv.org/doc/tutorials/features2d/detection_of_planar_objects/detection_of_planar_objects.html
Edit:
I assume you are using windows, as your example image looks like window. In this case you can use:
from PIL import ImageGrab
pil_img = ImageGrab.grab()
opencv_img = numpy.array(pil_img)
then use opencv to process the image to find sub image you are looking for.
If you want to do this cross platform, then you will need to use wxWidgets to do the screengrab: https://stackoverflow.com/a/10089645/455532
Even I wanted to do the same but using different module - pyautogui. I finally found the solution for my problem and I am sure this solution will also help you.
You have to just go to this webpage and read the locate function topic completely
and you'll be able to solve your problem.
I recommend you give a look on PyAutoGUI, a well documented library to control mouse and keyboard, also can locate imagens on screen, find the position, move the mouse to any location and clicks on location, also can simulate drag and drop, type on input fields, give double clicks and much more.

Undo steps in Pygame

I was wondering how I would undo drawing steps in Python/Pygame.
Basically I have made the mouse draw lines, and when I click my undo Rect, I want the screen to revert back to its original state (before mouse was pressed).
Here is my code but it does not work well.
copy=screen.copy()
if undoRect.collidepoint(mx,my) and mb[0]==1:
screen.blit(copy,(0,0))
if mb[0]==1 and omb[0]==1:
draw.line(screen,color,(omx,omy),(mx,my),5)
can someone who knows what I mean please give me ideas on how to make an undo last drawing button?
Thank you.
Ps. I am fairly new to pygame/python so please go a little slowly.
You just need to save the frames when the user interacts with the application.
For example in a demo paint application I wrote with opengl I used a list with 20 elements max and I was updating it when the user started a new action.
If user clicks to the screen, save the current frame to the list. If user stop clicking save the new frame. Then, when you need to go back you just have to take the last element of your list and draw it to the screen.

Categories