learncodebygaming / opencv_tutorials

Tutorials for learning OpenCV by making a video game bot.
MIT License
523 stars 267 forks source link

getting black screen only #7

Open Yusufathi opened 3 years ago

Yusufathi commented 3 years ago

so i used the code from vid #4 and when i run it with Albion Online Client it shows only a black screen . when i use other screens like paints it works perfectly . any got that issue ?

Abhijith14 commented 3 years ago

Same here. When I try any tab in Google Chrome, it gives black screen.

image

I am working on google-dino automation. But because of this issue, I am not able to get the screen. So I had to change the windowcapture.py a little bit.

image

changing the raise Exception('Window not found: {}'.format(window_name)) to capture the whole Desktop self.hwnd = win32gui.GetDesktopWindow()

This temporarily solves my issue. But It would have been better if I could get the specific window. Any better solutions to my issue ?

isinteger commented 3 years ago

Still no fix?

Abhijith14 commented 3 years ago

No. I am literally checking every possible method to find a solution. If i try to capture the whole window; my fps goes down to 5 - 10.

GarrettMaury7921 commented 2 years ago

Did anyone find a solution? For me, my screen is black but it is capturing the right screen, just not rendering. It works for applications like Task Manager but for the game I want it to capture it goes black.

Shikamaru5 commented 2 years ago

I'm having the same issues with the black screen I think, for awhile it was working fine, and I've tried implementing the code from the other tutorials but the issue has something to do with it copying the screenshots and not getting rid of them. When I don't specify the name of the window it shows me the desktop screen and just spams out screenshots one over the other very quickly. It even still does real time object detection but for some reason I can't find the bug that is making it do this, because as far as I can tell, the windowcapture.py and vision.py are exactly the same as the tutorials. The main.py isn't but the only real changes are the name of the game window, the jpg file, and the way to put rectangles around the specific object. Making it so that I don't use vision makes it speed up just, it doesn't change the outcome. I've also tried what was suggested above, where you get rid of the raise exception for compatiblebitmap but that flat out didn't work, it made the problem worse. If maybe there was a way to delete the previous screenshots so that at all times there was only one I think that might do it but I haven't figured out that code yet. So all in all while I try to figure this out, if anyone has suggestions that would be super wizard, thanks.

Lukkasss commented 2 years ago

I had the same issue and what I could find is that chrome uses hardware acceleration features, when you have it turned on, the API can't access the image generated by your videocard. Try disabiling 'Use hardware acceleration when possible' from the chrome menu: Configuration -> Advanced Settings -> System. It should work.

Shikamaru5 commented 2 years ago

Thank you!! It worked just like said it would, I was surprised how easy of a fix disabling and then relaunching that acceleration in Google Chrome was!

Abhijith14 commented 2 years ago

I had the same issue and what I could find is that chrome uses hardware acceleration features, when you have it turned on, the API can't access the image generated by your videocard. Try disabiling 'Use hardware acceleration when possible' from the chrome menu: Configuration -> Advanced Settings -> System. It should work.

Thanks Alott!!. It worked perfectly.

Dirbeq commented 11 months ago

Check this , worked for me

enonymous1 commented 9 months ago

Was also following the "LearnCodeByGaming" guide. I adapted the windowcapture.py to support the capture of the "black" window. Seems to have something to do with hardware accelerated windows? VIA the previous link... Plop the following code as another function in windowcapture.py and call this function instead of the get_screenshot() function as defined in run().

def capture_win_alt(self):

    windll.user32.SetProcessDPIAware()

    # get the window image data
    wDC = win32gui.GetWindowDC(self.hwnd)
    dcObj = win32ui.CreateDCFromHandle(wDC)
    cDC = dcObj.CreateCompatibleDC()
    dataBitMap = win32ui.CreateBitmap()
    dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
    cDC.SelectObject(dataBitMap)

    # If Special K is running, this number is 3. If not, 1
    result = windll.user32.PrintWindow(self.hwnd, cDC.GetSafeHdc(), 3)

    bmpinfo = dataBitMap.GetInfo()
    bmpstr = dataBitMap.GetBitmapBits(True)

    img = np.frombuffer(bmpstr, dtype=np.uint8).reshape((bmpinfo["bmHeight"], bmpinfo["bmWidth"], 4))
    img = np.ascontiguousarray(img)[..., :-1]  # make image C_CONTIGUOUS and drop alpha channel

    if not result:  # result should be 1
        win32gui.DeleteObject(dataBitMap.GetHandle())
        cDC.DeleteDC()
        dcObj.DeleteDC()
        win32gui.ReleaseDC(self.hwnd, wDC)
        raise RuntimeError(f"Unable to acquire screenshot! Result: {result}")

    return img