How to read video from URL in FileVideoStream in imutils? - python

I have a Python program with a video file reader scenario. To do this, I use the FileVideoStream API from the library imutils.video as follows
from imutils.video import FileVideoStream
import time
import numpy as np
import cv2
vs = FileVideoStream('~/Downloads/capture.webm').start()
time.sleep(1.0)
while True:
if not vs.more():
print("vs", vs.more())
vs.stop()
break
print("vs", vs.more())
frame = vs.read()
The problem is the location "~/Downloads/capture.webm" is not recognized by the FileVideoStream function even the video file is exists in the mentioned directory.
ERROR: OpenCV: Couldn't read video stream from file "~/Downloads/capture.webm"
But when I save the video in the same python project directory and call as "capture.webm" it works!
And also the function doesn't recognize videos from a URL.
How can I solve this?

Completion for ~ inside path ~/Downloads/capture.webm is done by bash/sh/etc.. If you are using python you have to use full path /home/username/Downloads/capture.webm.
You can also use os.environ.get('USER') to get current USER name and insert it inside full path. For example
'/home/%s/Downloads/capture.webm' % os.environ.get('USER')

Related

How am i able to save a picture using OpenCV imwrite to a specific folder

Im back with another probably stupid question!, my new issue is the following:
im trying to save a picture from my webcam into a specific folder using OpenCV imwrite,
its not working obviously here is my code:
import cv2 as cv
cam = cv.VideoCapture(0)
s, img = cam.read()
if s:
path = r"\imgtest\selfietest.jpg"
cv.imwrite(path, img)
Any suggestions for edits or fixes?, i've tried copying the file using shutil and moving it using the same module, i also tried to use the OS module to move it but it threw an "Access Denied" error so i would prefer to not need to grant the application admin rights every time i launched it thanks in advance!!
((ALSO ASSUME I KNOW NOTHING ABOUT PYTHON))
Use a full path, forward slashes, and r'' to pass a raw path. cv2.imwrite(r"D:/....jpg", img)

how to make an xml file open cv readable?

i have an xml file which is haar cascade and supposed to be on server , but I wanna use it in my openCV file code , how should I do that? btw I used <opencv_storage> tag.
You need to give the full paths of the xml. The xml files are located under the
opencv\data\haarcascades
location.
For instance:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('C:\\Users\\opencv\\data\\haarcascades\\haarcascade_frontalface_default.xml')

Opencv VideoCapture always returns false on Heroku

I'm using the following code to open a video stream:
import cv2
video = cv2.VideoCapture()
video.open("some_m3u8_link")
success, image = video.read()
However, even if the code works as intended locally, on Heroku success is always false.
I'm using cedar-14 stack with the following buildpacks:
heroku/python
https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git
(I tried several buildpack options for ffmpeg)
Running ffmpeg --version on heroku instance will return ffmpeg version 4.0-static https://johnvansickle.com/ffmpeg/
Is there any setting/configuration I missed in order to make it work on deployment? Thank you!
Later edit: I tried several links for "some_m3u8_link" including from twitch and other streaming services (including traffic streaming li
An example for reproducing:
python -c "import cv2; video=cv2.VideoCapture(); video.open('https://hddn01.skylinewebcams.com/live.m3u8?a=5tm6kfqrhqbpblan9j5d4bmua4'); success, image = video.read(); print(success)"
Returns True on local machine and False on Heroku.
(the link is taken from here)
You can use pafy module with cv2
-try opencv3 if its not working with cv2
import cv2, pafy
url = "Some url to stream"
video = pafy.new(url)
best = video.getbest(preftype="webm")
video=cv2.VideoCapture(best.url)
pafyPYPI
You can try this:
import cv2
video = cv2.VideoCapture("some_m3u8_link")
success, image = video.read()
Specifying the mode to open it might work.
video.open("some_m3u8_link", "r")
If that doesn't work then specifying the file extension might help.
You might also need to make a variable equal to the function
Ex:
""" replace .mp4 with the applicable file type,
I don't know if you need to specify file mode"""
import cv2
video = cv2.VideoCapture()
video = video.open("some_m3u8_link.mp4")
success, image = video.read()
If this doesn't work then I am out of ideas.

Why geotiff could not be opened by gdal?

I'm newbie in python and in geoprocessing. I'm writing some program to calculate ndwi. To make this, I try to open geotiff dataset with gdal, but dataset can't be opened. I tried to open different tiff files (Landsat8 multiple data, Landsat7 composite, etc), but dataset is always None.
What reason to this could be? Or how can i find it out?
Here's a part of code:
import sys, os, struct
import gdal, gdalconst
from gdalconst import *
import numpy as np
from numpy import *
class GDALCalcNDWI ():
def calcNDWI(self, outFilePath):
gdal.AllRegister()
# this allows GDAL to throw Python Exceptions
gdal.UseExceptions()
filePath = "C:\\Users\\Daria\\Desktop.TIF\\170028-2007-05-21.tif"
# Open
dataset = gdal.Open(filePath, gdal.GA_ReadOnly)
# Check
if dataset is None:
print ("can't open tiff file")
sys.exit(-1)
Thanks
Whenever you have a well-known file reader that is returning None, make sure the path to your file is correct. I doubt you have a directory called Desktop.TIF, I'm assuming you just made a typo in your source code. You probably want C:\\Users\\Dara\\Desktop\\TIF\\170028-2007-05-21.tif as the path (note that Desktop.TIF ==> Desktop\\TIF).
The safest thing to do is right click on the file, go to properties, and copy/paste that path into your python source code.

python reading a video

I am using Python 2.7.11 and OpenCV 2.4.9. I cannot read a video by using cv2.imread() or cv2.VideoCapture().
import cv2
cap = cv2.VideoCapture('cam.avi')
print ("open = ",cap.isOpened())
OR
import cv2
cap = cv2.imread('cam.avi')
print ("open = ",cap.isOpened())
It will return false.
I don't know why. I am sure that the cam.avi is here.
imread() does not support reading from video files directly.
See also the documentation of OpenCV.
If you want to read a video with imread you will first have to convert it to single images, either via a serperate program (ffmpeg comes to mind) or using OpenCV and store the images in memory.
Try providing full path to video, like:
import cv2
cap = cv2.VideoCapture(r'C:\Users\e01069\Downloads\drop.avi')
print ("open = ",cap.isOpened())
If you run following in your same file, you would know that python is looking for your file on some different location.
import os
print os.path.abspath(__file__) #this is your current working directory
Note: .imread wouldn't work this way.

Categories