I am trying to capture a video and convert into frames using python and openCV. I followed the steps that need to done to import openCV2 libraries for python in windows 8 which is as follows:
Download Python, Numpy, OpenCV from their official sites.
Extract OpenCV (will be extracted to a folder opencv)
Copy ..\opencv\build\python\x86\2.7\cv2.pyd
Paste it in C:\Python27\Lib\site-packages
Open Python IDLE or terminal, and type
I have done the above steps to import opencv libraries into python.
But when I import CV2 libraries and try to capture a video, I was unable to generate frames using the function cv.CaptureFromFile() and accessing the frames using cv.QueryFrame() functions. I was unable to capture the frames. Please find the code below.
import sys
import os
import numpy as np
import PIL
from matplotlib import pyplot as plt
import cv2.cv as cv
winname = "myWindow"
win = cv.NamedWindow(winname, cv.CV_WINDOW_AUTOSIZE)
invideo = cv.CaptureFromFile("chayya1.avi")
totalNumberOfFrames = int(cv.GetCaptureProperty(invideo, cv.CV_CAP_PROP_FRAME_COUNT))
framesprocessing = totalNumberOfFrames
print(totalNumberOfFrames)
while (True):
im = cv.QueryFrame(invideo)
cv.ShowImage(winname, im)
if cv.WaitKey() == 27: # ASCII 27 is the ESC key
break
del invideo
cv.DestroyWindow(winname)
The output is getting processed without any errors but the frames are not generating and even the print statement for frame count is also zero.
I just want to what is procedure for installing OpenCV for python in windows and accessing the frames of a video using the obove mentioned functions.
Thanks in advance
Using opencv to read video files
As mentioned in the tutorial here, the recommended approach now is to use cv2 (not cv as you did) to read video files using, e.g.
import numpy as np
import cv2
cap = cv2.VideoCapture("chayya1.avi")
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Python & OpenCV installation on windows
For a simple solution to installing OpenCV's python bindings on windows you might want to try the Python(xy) distribution which bundles a bunch of python modules including opencv.
Python's opencv bindings are included with opencv, so if you didn't get any import errors then your opencv setup is likely to be correct.
Alternative bindings
If you don't like the native bindings, you could try using pip from the command line to install 3rd party bindings, though as berak pointed out this is not generally recommended;
pip install pyopencv
Pip itself can be installed as outlined in this answer
Related
I'm trying to insert an image into a Python program, but I just can't figure out how to link the image to code so I can view it once the code is run. I am new to python, I have version 3.9.7 on a Mac computer.
Thanks in advance to anyone who can give me a hand in solving the problem.
Greetings, Gaia.
You can use OpenCV for this
https://learnopencv.com/read-display-and-write-an-image-using-opencv/
You need to install the library first, running the following command on your terminal pip install opencv-python
# import the cv2 library
import cv2
# The function cv2.imread() is used to read an image.
img_grayscale = cv2.imread('test.jpg',0)
# The function cv2.imshow() is used to display an image in a window.
cv2.imshow('graycsale image',img_grayscale)
# waitKey() waits for a key press to close the window and 0 specifies indefinite loop
cv2.waitKey(0)
# cv2.destroyAllWindows() simply destroys all the windows we created.
cv2.destroyAllWindows()
# The function cv2.imwrite() is used to write an image.
cv2.imwrite('grayscale.jpg',img_grayscale)
You can use theses modules:
opencv-python:
import cv2
Pillow:
from PIL import Image
scikit-image
import skimage
I'm trying to use code from the official openCV tutorial for showing video from webcam using cv2.imshow() in Ubuntu/Python 3.6:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
And I get the following error for cv2.imshow():
The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage
When searching for the error I stumbled upon this post as an alternate answer to similar questions:
If you installed OpenCV using the opencv-python pip package at any point in time, be aware of the following note, taken from https://pypi.python.org/pypi/opencv-python
IMPORTANT NOTE MacOS and Linux wheels have currently some limitations:
video related functionality is not supported (not compiled with FFmpeg)
for example cv2.imshow() will not work (not compiled with GTK+ 2.x or Carbon support)
Also note that to install from another source, first you must remove the opencv-python package
OpenCV error: the function is not implemented
OpenCV not working properly with python on Linux with anaconda. Getting error that cv2.imshow() is not implemented
Most other openCV functions work properly.
Is there an alternative to cv2.imshow() that uses standard anaconda libraries so I don't have to recompile openCV or use Python 2.7?
I hacked together a quick and dirty snippet that uses matplotlib.animation and resembles what cv2.imshow() is expected to do with webcam video:
import cv2
import matplotlib.pyplot as plt
import matplotlib.animation as animation
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
# The following is the replacement for cv2.imshow():
fig = plt.figure()
ax = fig.add_subplot(111)
im = ax.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), animated=True)
def updatefig(*args):
ret, frame = cap.read()
im.set_array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
return im
ani = animation.FuncAnimation(fig, updatefig, interval=10)
plt.show()
I found this very useful, as I was able to output many overlapping plots on top of the webcam video thanks to the fact that matplotlib.animation.FuncAnimation can animate as many plots as there are attached to the ax object as long as they are updated in the updatefig() function above.
Edit: As I was working in a Jupyter notebook, I added the following in order to see the video in a new window:
import matplotlib
matplotlib.use('qt5agg')
I'm trying to write an app using python and PIL that involves processing frames from my macbook camera. Is there an easy way to capture frames from the iSight for use with PIL without installing too many libraries?
I've seen a few questions on SO about doing this with OpenCV, which I don't want to do. Can I do this without using OpenCV as I've had trouble installing that?
Maybe you can try using the SimpleCV library. After installing it, the following code should work:
from SimpleCV import Camera
from SimpleCV import Image
webcam_camera = Camera()
webcam_image = webcam_camera.getImage()
webcam_image.save("frame.jpg")
Although OpenCV would be a much better option. If you follow the instructions on their website, it wouldn't be too hard to get it up and running.
I am trying to open a Windows Media Video file on a macintosh using OpenCV. To view this video in MacOS I had to install a player called Flip4Mac. I am assuming that this came with the codecs for decoding WMV. Is there something I can now do to get OpenCV to open the videos using the codec?
In python/opencv2 opening a video should be super easy:
cap = cv2.VideoCapture('0009.wmv')
But I get this:
WARNING: Couldn't read movie file 0009.wmv
OpenCV video on OS X is very buggy in my experience. However try installing ffmpeg using Homebrew (I also recommend installing OpenCV using homebrew if you haven't done that already):
$ brew install ffmpeg
On Mavericks I've had all kinds of problems getting OpenCV and ffmpeg work together, so you might want to try installing ffmpeg and opencv in different orders, if this does not work. But for me now the following code works, provided that there is a file named test.wmv in the directory:
import cv2
vid = cv2.VideoCapture("test.wmv")
while True:
vid.grab()
retval, image = vid.retrieve()
if not retval:
break
cv2.imshow("Test", image)
cv2.waitKey(1)
I have had the problem before and it was simply that VideoCapture needed the full path to the file and not a relative path.
hope this helps.
I'm trying to write some programs using opencv and python. I installed opencv and the python libraries from the repositories. I'm using ubuntu 12.04. The folder /var/lib/python-support/python2.7 contains just 5 files :
cv2.so
cv.py
cv.pyc
simplegeneric-0.7.egg-info
simplegeneric.py
simplegeneric.pyc
From what reading I have done, I think there's supposed to be an opencv folder around here. I'm able to import cv library using
import cv
but
from opencv import cv
And I cannot load the highgui module. Any way to work around this? I would really really like to do something in opencv
You must have installed OpenCV >= 2.3.1. In OpenCV 2.3.1 and higher, Python bindings do not have highgui.
import cv2
import cv2.cv as cv
and you're good to go.
import cv2
img = cv2.imread("image name")
cv2.imshow("window name", img)
cv2.waitKey(0)
You can find more help on OpenCV docs and you could also look at some of the opwncv stuff that I have been doing here.
I hope this helps.
You will get the highgui file in OpenCV folder(installed folder) in include/opencv/highgui.h.