Ask Your Question

Revision history [back]

Convert ndarray to numpy (perform tracking on screenshot)

I want to be able to take an opencv screenshot image, and apply image tracking to it. But I get this error:

AttributeError: 'numpy.ndarray' object has no attribute 'read'

I use win32api to take a screenshot, then convert it to a numpy array with opencv.

The tutorials I've looked at involved using a webcam, and apparently the frame that you would read from it using ___.read() is not the same as the opencv screenshot image.

I was thinking of using OBS or another streaming service to send a video to python, and then everything should work, but I've found nothing on that.

I've tried converting the screenshot to a video, then doing the read() function, but nothing works.

Main python file (in a while loop)

if __name__ == '__main__':
    screen = g.grab_screen(region = (0, 0, 640, 480))

    scr = screen.copy()

    screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    vid = cv2.VideoWriter('output.avi', fourcc, 6, (640,480))

    vid.write(screen)
    frame = screen.read()

    # Update tracker
    ok, bbox = tracker.update(frame)

grab_screen() comes from custom python file which uses win32api to take a screenshot, then return an image which you can then perform cv2.cvtcolour() on.

click to hide/show revision 2
retagged

updated 2019-05-23 09:02:49 -0600

berak gravatar image

Convert ndarray to numpy (perform tracking on screenshot)

I want to be able to take an opencv screenshot image, and apply image tracking to it. But I get this error:

AttributeError: 'numpy.ndarray' object has no attribute 'read'

I use win32api to take a screenshot, then convert it to a numpy array with opencv.

The tutorials I've looked at involved using a webcam, and apparently the frame that you would read from it using ___.read() is not the same as the opencv screenshot image.

I was thinking of using OBS or another streaming service to send a video to python, and then everything should work, but I've found nothing on that.

I've tried converting the screenshot to a video, then doing the read() function, but nothing works.

Main python file (in a while loop)

if __name__ == '__main__':
    screen = g.grab_screen(region = (0, 0, 640, 480))

    scr = screen.copy()

    screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)

    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    vid = cv2.VideoWriter('output.avi', fourcc, 6, (640,480))

    vid.write(screen)
    frame = screen.read()

    # Update tracker
    ok, bbox = tracker.update(frame)

grab_screen() comes from custom python file which uses win32api to take a screenshot, then return an image which you can then perform cv2.cvtcolour() on.