Convert ndarray to numpy (perform tracking on screenshot) [closed]

asked 2019-05-23 07:58:12 -0600

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

berak gravatar image

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.

edit retag flag offensive reopen merge delete

Closed for the following reason question is off-topic or not relevant by berak
close date 2019-05-23 09:01:42.503743

Comments

sorry, but we cannot help with your 3rdparty screengrabbing library.

berak gravatar imageberak ( 2019-05-23 09:02:29 -0600 )edit

opencv can perform operations on the screenshot. So it fully relates to opencv. The screenshot is a numpy array.

Anomalix gravatar imageAnomalix ( 2019-05-23 13:21:14 -0600 )edit

again, we cannot help with your screenshot aqcuisition.

but btw, you have to enable a flag in the videowriter to write grayscale frames, like now it will just reject them.

berak gravatar imageberak ( 2019-05-23 13:30:18 -0600 )edit

I don't have a problem with the screenshot acquisition, I have a problem with the tracker.update(screen) part, since opencv returns 'False' for the 'ok' variable each time. Trying to convert that into a 'frame' (basically a frame from a webcam video) with screen.read() results in the above error.

Anomalix gravatar imageAnomalix ( 2019-05-23 13:46:42 -0600 )edit