Real-time screen capture and feature detection using OpenCV - python

I successfully processed a video and had the algorithm detect faces, but I am attempting to detect faces in real-time, capturing images from the screen (such as when I'm playing games, etc.) This is the bit of the code I used to process a captured video:
capture = cv2.VideoCapture('source_video.avi')
How can I change this to capture images from the screen in real-time? Please give me some code examples if possible.

Don't use openCV for this. Better use
from PIL import ImageGrab
ImageGrab.grab().save("screen_capture.jpg", "JPEG")

Related

OpenCV & MoviePy - Analyzing video frames

I've been using OpenCV and MoviePy to get images out of a video (1 image per second) and once extracted, I analyze the image with pytesseract. The part where the script extract images takes quite a bit of time. Is it possible or is there a function that I've overlooked in MoviePy or OpenCV that allows video frames to be analyzed without having to create images first? This could tremendously speed up the process.
Current steps:
Scan and extract 1fps with a specific video as argument
From each of those images, perform analysis on a specific area
Desired:
Perform analysis on a specific area of the video itself at 1 fps.
If this function exists, please inform me. Otherwise, would there be a workaround for this? Suggestions?
Thanks!!

How do I input OBS virtual cam in my python code using opencv?

I am trying to write a code for detecting the color green from a live video. I want to make a detector so that whenever the color green pops up in the screen, a counter starts counting how many times the color appears.
So for the video source, I am using the OBS Virtual Camera. But I have no idea how to input it as the source. I have seen codes inputting web cams as the source as shown below:
import numpy as np
import cv2
# Capturing video through webcam
webcam = cv2.VideoCapture(0)
Anyone have any idea how I can input the OBS virtual cam? Or does anyone know any alternative like switching to another language to do said task?
Windows will treat OBS Virtual Camera as a regular camera. The argument for cv2.VideoCapture is camera number. So up that number by 1 over and over again until the program uses the OBS Virtual Camera. And there you go.
Keep in mind that there is a bug currently reported that opencv is not parsing the stream from OBS virtual cam and just showing a black background.
https://github.com/obsproject/obs-studio/issues/3635

How to take still images in python? (Other than opencv CaptureVideo)

I would like to take pictures using the USB webcam. When I use the VideoCapture method of OpenCV, it actually gives frames from the video. In most cases, still images cover more area than the video. Therefore, I am looking for a way to take pictures using the webcam which cover more of the available camera FOV.

Take an input stream from the desktop in OpenCV

I am using Python 3.5.1 and OpenCV 3.0.0.
I am working on a python program that can play games, so it needs to 'see' what is going on, on the screen. How can this be achieved?
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
#work with frames here
Is there a int 'a' such that cv2.VideoCapture(a) will take the desktop screen as video input? I tried making it and I followed a rather clumsy approach, I captured screen repeatedly using:
import os
os.system('screencapture test.jpg')
Then opening test.jpg using cv2.imread. This was a very slow approach, on searching online I found this question Screen Capture with OpenCV and Python-2.7 which does the same thing, but more efficiently. But the fact still remains that it is capturing individual screenshots and processing them one by one and not a true video stream. I also found this How to capture the desktop in OpenCV (ie. turn a bitmap into a Mat)? which I think is close to what I am trying but is in C++, if someone can help me convert this to Python, I will highly appreciate it.
The main thing is that the program will be doing something like MarI/O, so speed is a concern, any help is appreciated, go easy on me, I am (relatively) new to OpenCV.
Thanks.
Just an update on this question in case anyone wants a solution.
Taking screenshot can be achieved by using module pyautogui
import pyautogui
import matplotlib.pyplot as plt
image = pyautogui.screenshot()
plt.imshow(image)
If you want to read it as a stream,
while(True):
image = pyautogui.screenshot()
#further processing
if finished:
break
According to the documentation,
On a 1920 x 1080 screen, the screenshot() function takes roughly 100 milliseconds
So this solution can be used if your application does not demand high fps rate.
Taking screenshots in separate thread sounds good solution.
Also you can use virtual webcam, but it is a heavy solution.
Or you would capture desktop directly by using ffmpeg. https://trac.ffmpeg.org/wiki/Capture/Desktop

noise in webcam frames Python + Opencv

I'm using Opencv 2.4.5 with python 2.7 to track people in video surveillance. At the beginning I used .avi and .mpeg videos to test my code, now I want to use a hcv-m100c camera. I am using a simple difference between frames (an initial frame compared with each frame) to identify the objects in movement, It works very well with the .avi and .mpeg videos I have, but when I use the camera the results are so bad because a lot of noise and stains appear in my video. I thought that the problem was my camera, but I made an .avi video with the same camera and I tested that video with my code and it works fine.
Now, I'm using the cv2.BackgroundSubtractorMOG but the problem is still there.
So, I think I need to do a pre-processing when I use the camera
Just for completeness:
Solution concept:
Possibly you could stream the video camera with something like ffmpeg which can transcode as well and then use OpenCV to read the network stream. It might be easier to use VLC to stream instead.
Solution detail:
VLC Stream code (Shell):
vlc "http://192.168.180.60:82/videostream.cgi?user=admin&pwd=" --sout "#transcode{vcodec=mp2v,vb=800,scale=1,acodec=mpga,ab=128,channels=2,samplerate=??44100}:duplicate{dst=rtp{sdp=rtsp://:8554/output.mpeg},dst=display}" --sout-keep
OpenCV Code (Python):
cap=cv2.VideoCapture("rtsp://:8554/output.mpeg")

Categories