Capture video data from screen in Python - python

Is there a way with Python (maybe with OpenCV or PIL) to continuously grab frames of all or a portion of the screen, at least at 15 fps or more? I've seen it done in other languages, so in theory it should be possible.
I do not need to save the image data to a file. I actually just want it to output an array containing the raw RGB data (like in a numpy array or something) since I'm going to just take it and send it to a large LED display (probably after re-sizing it).

With all of the above solutions, I was unable to get a usable frame rate until I modified my code in the following way:
import numpy as np
import cv2
from mss import mss
from PIL import Image
bounding_box = {'top': 100, 'left': 0, 'width': 400, 'height': 300}
sct = mss()
while True:
sct_img = sct.grab(bounding_box)
cv2.imshow('screen', np.array(sct_img))
if (cv2.waitKey(1) & 0xFF) == ord('q'):
cv2.destroyAllWindows()
break
With this solution, I easily get 20+ frames/second.
For reference, check this link: OpenCV/Numpy example with mss

There is an other solution with mss which provide much better frame rate. (Tested on a Macbook Pro with MacOS Sierra)
import numpy as np
import cv2
from mss import mss
from PIL import Image
mon = {'left': 160, 'top': 160, 'width': 200, 'height': 200}
with mss() as sct:
while True:
screenShot = sct.grab(mon)
img = Image.frombytes(
'RGB',
(screenShot.width, screenShot.height),
screenShot.rgb,
)
cv2.imshow('test', np.array(img))
if cv2.waitKey(33) & 0xFF in (
ord('q'),
27,
):
break

You will need to use ImageGrab from Pillow (PIL) Library and convert the capture to numpy array. When you have the array you can do what you please with it using opencv. I converted capture to gray color and used imshow() as a demonstration.
Here is a quick code to get you started:
from PIL import ImageGrab
import numpy as np
import cv2
img = ImageGrab.grab(bbox=(100,10,400,780)) #bbox specifies specific region (bbox= x,y,width,height *starts top-left)
img_np = np.array(img) #this is the array obtained from conversion
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
cv2.imshow("test", frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
you can plug an array there with the frequency you please to keep capturing frames. After that you just decode the frames. don't forget to add before the loop:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vid = cv2.VideoWriter('output.avi', fourcc, 6, (640,480))
and inside the loop you can add:
vid.write(frame) #the edited frame or the original img_np as you please
UPDATE
the end result look something like this (If you want to achieve a stream of frames that is. Storing as video just a demonstration of using opencv on the screen captured):
from PIL import ImageGrab
import numpy as np
import cv2
while(True):
img = ImageGrab.grab(bbox=(100,10,400,780)) #bbox specifies specific region (bbox= x,y,width,height)
img_np = np.array(img)
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
cv2.imshow("test", frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hope that helps

based on this post and others posts, i made something like this .
Its taking a screenshot and writing into a video file without saving the img.
import cv2
import numpy as np
import os
import pyautogui
output = "video.avi"
img = pyautogui.screenshot()
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
#get info from img
height, width, channels = img.shape
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output, fourcc, 20.0, (width, height))
while(True):
try:
img = pyautogui.screenshot()
image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
out.write(image)
StopIteration(0.5)
except KeyboardInterrupt:
break
out.release()
cv2.destroyAllWindows()

You can try this code as it is working for me. I've tested it on Linux
import numpy as np
import cv2
from mss import mss
from PIL import Image
sct = mss()
while 1:
w, h = 800, 640
monitor = {'top': 0, 'left': 0, 'width': w, 'height': h}
img = Image.frombytes('RGB', (w,h), sct.grab(monitor).rgb)
cv2.imshow('test', cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
Make sure that the following packages are installed:
Pillow, opencv-python, numpy, mss

You can try this=>
import mss
import numpy
with mss.mss() as sct:
monitor = {'top': 40, 'left': 0, 'width': 800, 'height': 640}
img = numpy.array(sct.grab(monitor))
print(img)

I tried all of the above but it did not give me the real-time screen update.
You can try this. This code is tested and worked successfully and also give you a good fps output. You can also judge this by each loop time it's needed.
import numpy as np
import cv2
from PIL import ImageGrab as ig
import time
last_time = time.time()
while(True):
screen = ig.grab(bbox=(50,50,800,640))
print('Loop took {} seconds',format(time.time()-last_time))
cv2.imshow("test", np.array(screen))
last_time = time.time()
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break

If anyone looking for a much easier and fastest way to grab screen as frame in python, then try ScreenGear API from my high-performance video-processing vidgear library in just few lines of python code on any machine (Tested on all platforms, including Windows 10, MacOS Serra, Linux Mint) and enjoy threaded screen-casting:
Note: It also supports multiple backends and screens out-of-the box.
# import required libraries
from vidgear.gears import ScreenGear
import cv2
# define dimensions of screen w.r.t to given monitor to be captured
options = {'top': 40, 'left': 0, 'width': 100, 'height': 100}
# open video stream with defined parameters
stream = ScreenGear(logging=True, **options).start()
# loop over
while True:
# read frames from stream
frame = stream.read()
# check for frame if Nonetype
if frame is None:
break
# {do something with the frame here}
# Show output window
cv2.imshow("Output Frame", frame)
# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# close output window
cv2.destroyAllWindows()
# safely close video stream
stream.stop()
VidGear library Docs: https://abhitronix.github.io/vidgear
ScreenGear API: https://abhitronix.github.io/vidgear/latest/gears/screengear/overview/
More examples: https://abhitronix.github.io/vidgear/latest/gears/screengear/usage/

I've tried ImageGrab from PIL and it gave me 20fps which is ok but using win32 libraries gave me +40fps which is amazing!
I used this code by Frannecklp but it didn't work just fine so I needed to modify it:
-Firstly pip install pywin32 in case using the libraries
-import the libraries like this instead:
import cv2
import numpy as np
from win32 import win32gui
from pythonwin import win32ui
from win32.lib import win32con
from win32 import win32api
for geting a simple image screen do:
from grab_screen import grab_screen
import cv2
img = grab_screen()
cv2.imshow('frame',img)
and for getting frames:
while(True):
#frame = grab_screen((0,0,100,100))
frame = grab_screen()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q') or x>150:
break

This task is very simple with opencv, we are just capturing screenshots in loop, and converting it into frames. I created timer for screenrecording, in start you have to enter how many seconds you want to record:) Here is the code.
import cv2
import numpy as np
import pyautogui
from win32api import GetSystemMetrics
import time
#Take resolution from system automatically
w = GetSystemMetrics(0)
h = GetSystemMetrics(1)
SCREEN_SIZE = (w,h)
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("recording.mp4", fourcc, 20.0, (SCREEN_SIZE))
tim = time.time()
tp = int(input('How many times you want to record screen?->(Define value in Seconds): '))
tp = tp+tp
f = tim+tp
while True:
img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
out.write(frame)
tu = time.time()
if tu>f:
break
cv2.destroyAllWindows()
out.release()
So that's how you can use time in screen recording, you don't need to use imshow() because it shows infinitely our screen recording on-screen so output video looks weird.

Related

Capturing a specific webpage or url (python)

Im new in python and opencv and i'm making a project that captures my whole window , eventually i cope up an idea that i want to capture a specific webpage not my whole window . It is possible to Capture a webpage/url that's run on my computer? (for example i want to capture google url or Stack OverFlow )
(The Code is more like Screen Recorder)
Heres my Code :
import numpy as np
import cv2 as cv
import webbrowser
import pyautogui
from PIL import ImageGrab
from time import time
loop_time = time()
while True :
screenshot = pyautogui.screenshot()
#slicing the frame to make readable in opencv
screenshot = np.array(screenshot)
# Our operations on the frame come here
screenshot = cv.cvtColor(screenshot, cv.COLOR_BGRA2BGR)
#Displaying Text on the screen
# text = "Screen Shot"
# coordinates = (100,100)
# font = cv.FONT_HERSHEY_SIMPLEX
# fontScale = 1
# color = (255,0,255)
# thickness = 2
# screenshot = cv.putText(screenshot,text, coordinates, font, fontScale, color, thickness, cv2.LINE_AA)
cv.imshow('Capture Screen', screenshot)
# debuging fps
print(f'FPS { 1/(time()-loop_time):.0f}')
loop_time = time()
# wait time
keyCode = cv.waitKey(1)
#use esc or q key to exit window
if keyCode == 27 or keyCode == ord('q'):
print('Window is Closed')
cv.destroyAllWindows()
break
You could use PyGetWindow to select a specific window/app and get a screenshot of it like so:
import pyautogui
import pygetwindow as gw
gw.getWindowsWithTitle('Firefox')
active_window.activate()
pyautogui.screenshot().save('test.png')
Note: if you're working on a mac then the above will probably not work though.

Problem with this error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' OpenCV

I got stuck with this error after running resize function line:
import cv2
import numpy as np
import matplotlib.pyplot as plt
net = cv2.dnn.readNetFromDarknet('yolov3_custom.cfg', 'yolov3_custom_last.weights')
cap = cv2.VideoCapture(0)
while 1:
_, img = cap.read()
img = cv2.resize(img,(1280,720))
hight,width,_ = img.shape
blob = cv2.dnn.blobFromImage(img, 1/255,(416,416), (0,0,0), swapRB = True,crop= False)
This error is because of no frames is getting captured.
Please try
cap = cv2.VideoCapture(1)
Problem is that there is no frame captured as such, there is no image available to be resized. In order to be sure that your webcam is actually capturing, try this:
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
if not ret:
print("no frame captured")
exit()
img = cv2.resize(img,(1280,720))
#continue processing
Then
Test to be sure that your webcam is working properly OR
if you have multiple webcam, try using another index to init the "VideoCapture"
cap = VideoCapture(1)

how to hide the title bar or window in python

i want to hide my window or tittle bar of my window. as i want to show the output of the opencv frameless.
tell me how can i do that with the help of tkinter or pyqt5.
and also i want the size of the output according to my specation.
and can i resize and change the place of the output like drag and drop anywhere on desktop while the code is running.
import cv2
import os
import numpy as np
import datetime
date=datetime.datetime.now()
framerate=24
screen_size=(640,480) #std res 640,480
filename='E:/project/videos/cam_%s%s%s%s%s%s.avi' %(date.year,date.month,date.day,date.hour,date.minute,date.second)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
cap=cv2.VideoCapture(0)
out= cv2.VideoWriter(filename,fourcc,framerate,screen_size)
cv2.namedWindow('frame',cv2.WINDOW_NORMAL)
cv2.resizeWindow('frame',280,190)
cv2.moveWindow('frame',5,495)
#cv2.namedWindow('frame',cv2.WND_PROP_FULLSCREEN) #use for full screen
#cv2.setWindowProperty('frame', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame=cv2.flip(frame,1)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
screen.mainloop()
Below is an example using tkinter to show the capture images from webcam:
import cv2
import datetime
from tkinter import *
from PIL import Image, ImageTk
screen = Tk()
screen.overrideredirect(1) # remove window decorations
screen.geometry('280x200+5+520') # move the window
framerate = 24
screen_size = (640, 480) #std res 640,480
date = datetime.datetime.now()
filename = 'E:/project/videos/cam_%s%s%s%s%s%s.avi' % (date.year, date.month, date.day,
date.hour, date.minute, date.second)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(filename, fourcc, framerate, screen_size)
cap = cv2.VideoCapture(0)
imgbox = Label(screen)
imgbox.pack(fill=BOTH, expand=1)
def quit(event):
cap.release()
out.release()
screen.destroy()
def read_frame():
ret, frame = cap.read()
if ret:
frame = cv2.flip(frame, 1)
out.write(frame)
# since openCV capture image is in BGR color sequence
# so need to convert it to RGB color sequence
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# convert the openCV image to PIL (Pillow) format
img = Image.fromarray(frame)
#img = img.resize((280, 200)) # not keep aspect ratio
img.thumbnail((280, 200)) # keep aspect ratio
img = ImageTk.PhotoImage(img)
imgbox.config(image=img) # update the image
img.image = img # keep a reference of the image to avoid being garbage collected
screen.after(20, read_frame) # schedule next read after 20 ms
# bind the 'q' key to quit program
screen.bind('q', quit)
read_frame()
screen.mainloop()

Post BG subtraction(using opencv-python) and saving, output video is 2 GB. How to ensure it doesn't explode in size?

# Below piece of code is to read a video file called v1.mp4(size about 14MB), perform background subtraction on it, rotate the frame and save it.
import cv2
import argparse
import datetime
import imutils
import time
import numpy as np
cap = cv2.VideoCapture("C:\\Users\\212458792\\Music\\Downloads\\BasketVideos\\v1.mp4") #Open video file
fgbg = cv2.createBackgroundSubtractorMOG2(detectShadows = True) #Create the background substractor
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# now creating video writer object
bgsubvideo = cv2.VideoWriter('C:\\Users\\212458792\\Music\\Downloads\\BasketVideos\\bgsubvideo.avi', -1, 20.0,size)
#using any other codec like divx or xvid or mpeg for fourcc did create and video #file and write it, but i couldn't play the file. hence using -1
perform background subtraction frame after frame
while True:
ret, frame = cap.read()#read a frame
if ret==True:
fgmask = fgbg.apply(frame) #Use the substractor
cv.Flip(fgmask, flipMode=-1)
bgsubvideo.write(fgmask)
else:
break
cap.release() #release video file
cv2.destroyAllWindows() #close all openCV windows
bgsubvideo.release()

stream from thermal camera (ip camera) on python , connection error

i want to stream from a thermal camera, usually it export its frames as gray scale frames
the thermal camera is an IP camera , i tried different codes and package but with no output.
when i change the code a little bit to view from a USB camera it works normally, so any help please.
this is the code i have tried :
import sys
sys.path.append('C:\Python27\Lib\site-packages')
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
i=0
window = tk.Tk()
window.title('thermal image')
var = tk.IntVar()
width, height = 800, 600
cap = cv2.VideoCapture(0)
cap.open("http://169.254.110.119/")
left_label = tk.Label(window)
left_label.pack(side="left")
right_label = tk.Label(window)
right_label.pack(side="right")
def show_frame():
_, frame = cap.read()
print frame
if frame != None:
frame = cv2.flip(frame, 1)
img = Image.fromarray(frame)
imgtk = ImageTk.PhotoImage(image=img)
left_label.imgtk = imgtk
left_label.configure(image=imgtk)
left_label.after(10, show_frame)
show_frame()
window.mainloop()
I think that the image from the websitre is not being grabbed in the code, what worked for me was
img_requested = requests.get(url)
img_arr = np.array(bytearray(img_requested.content), dtype=np.uint8)
frame = cv2.imdecode(img_arr, -1)
And there you would get the frame (color picures/video).Keep in mind that you need to import requests and numpy as np.
It is important if you are using IP Webcam that you do not forget to write the '/shot.jpg'
at the end of the url, like this: 'http://190.160.0.0:8080/shot.jpg', so that it effectivelly grabs the image.

Categories

Resources