I am trying to write a program for image display and face capture. I am somewhat new to python and OpenCV.
Please find the details below:
I am running Python 2.7.5 on win32 on windows XP
Open cv2 version is 3.0.0
For the program,
Images from a predefined folder needs to be displayed for a fixed time of 500 millisecond in random sequence.
The gap between the images should be covered through a black screen, which should come for any random time gap interval between 1000-1500 millisecond.
Face capture of the viewer needs to be done via webcam once image showed, in between the image show, i.e. at the point of 250 millisecond. The output of the face should be stored in another newly created folder each time the program is run.
I have written the code below, but not getting the sequence right with a synchronised face capture with Haarcascade integration(perhaps required).
I also read somewhere that 'camera index' could be involved in this with possibly the value zero assigned to it. What exactly could be its role?
Please assist in this. Thanks in advance.
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Sourceimagepath.jpg', 1)
cv2.startWindowThread()
cv2.namedWindow("Demo")
cv2.imshow("Demo", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Related
Hi whenever I use the cv2.imshow function all I get is a black rectangle like this:
My code is here, I am using python:
from cv2 import cv2
image = cv2.imread("scren.png")
cv2.imshow("image2", image)
I have tried using different file types as well as restarting my computer. Does anyone know how to fix this? Thanks.
I'll put the answer from #fmw42 here as it solved for me too and I was looking for it for some hours
Add cv2.waitKey(0) after cv2.imshow(...).
I don't know why it solves, but solved for me.
According to the documentation the imshow function requires a subsequent command to waitKey or else the image will not show. I believe this functionality has to do with how highgui works. So to display an image in Python using opencv you will always have to do something like this:
import cv2
image = cv2.imread(r"path\to\image")
cv2.imshow("image", image)
cv2.waitKey(0)
Note about imshow
This function should be followed by a call to cv::waitKey or cv::pollKey to perform GUI housekeeping tasks that are necessary to actually show the given image and make the window respond to mouse and keyboard events. Otherwise, it won't display the image and the window might lock up. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame and wait approximately 25 ms for a key press (suitable for displaying a video frame-by-frame). To remove the window, use cv::destroyWindow.
See also.
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
I take part in a project in which we are making a sudoku solver. I want to print the image of the solved sudoku grid on the screen while our drawing table is drawing the solution on the paper grid.
But I can't find a way to display an image and my code keeps running.
I have looked into - I think - all of the opencv and matplotlib.pyplot functions to display images but every time the code stops when the image is displayed and continues once the image is closed (plt.show() or using cv2.waitKey()).
So if anyone has an idea of a way to display an image while the python code keeps running, I'd be glad to hear it.
Thanks
The PIL/Pillow Image.show() method will leave your image showing on the screen and your code will continue to run.
If you have a black and white image in a Numpy/OpenCV array, you can make it into a PIL Image and display it like this:
from PIL import Image
Image.fromarray(NumpyImg).show()
If your image is colour, you'll need to go from BGR to RGB either using cv2.cvtColor(...BGR2RGB..) or by reversing your 3rd channel something like (untested):
Image.fromarray(NumpyImg[:,:,::-1]).show()
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
I am trying to build an app that does 2 things.
Get live feed from webcam and display it using OpenCV. (tried IP Camera but gave up, its still not working)
Plot a chart on the basis of the video input.
The webcam feed is working, I am able to display it using imshow() and namedWindow()
.
The chart I have made using Tkinter.
The two outputs above, I want to add them in a single frame. Is it possible to do so?
And what do I use to embed them in a single window?
Please note I am using Python and developing on Windows.
You can combine two or more output windows into a single output window using numpy stack concept.
Referene Link:-
http://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html
http://docs.scipy.org/doc/numpy/reference/generated/numpy.vstack.html#numpy.vstack
Sample Code:-
import cv2
import numpy as np
img1 = cv2.imread('Bird1.jpg')
img2 = cv2.imread('Bird2.jpg')
img_stack = np.hstack((img1,img2))
cv2.imshow('Image Stack',img_stack)
cv2.waitKey(0)
cv2.destroyAllWindows()
Note:-
You can combine any number of output windows into single one. To do this,
the Input Images Height, Width and Channel must be same.
Channel means, If images are in RGB Mode means all Images should be in RGB Mode.
You cannot combine, one RGB Mode Image and one Grayscale Mode Image into a single window.
Like Images, you may also stack videos.