cv2.VideoCapture freezes on this specific video - python

For some reason, it hangs on this video:
Here's the code
import cv2
import time
cap = cv2.VideoCapture("http://192.65.213.243/mjpg/video.mjpg")
while(cap.isOpened()):
ret, img = cap.read()
current_time_in_milliseconds = "%.5f" % time.time()
filename="{}.jpg".format(current_time_in_milliseconds)
cv2.imwrite(filename, img)
Any ideas why? Is it something about this video format?
This code works on other mjpg but something about this feed that makes python freeze at cv2.VideoCapture()
I do get this funny error too:
warning: Error opening file
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:792) warning:
http://192.65.213.243/mjpg/video.mjpg
(/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:793)

Related

Can't open file: './config/PixelBasedAdaptiveSegmenter.xml' in write mode

I am testing different background segmentation algorithm from the library pybgs. Unfortunately, I am facing an error that I don't understand.
The code is :
import cv2
import pybgs as bgs
video_path = "video.mp4"
# create VideoCapture object for further video processing
captured_video = cv2.VideoCapture(video_path)
# check video capture status
if not captured_video.isOpened:
print("Unable to open: " + video_path)
exit(0)
background_sub_method = bgs.SuBSENSE()
while True:
# read video frames
ret, frame = captured_video.read()
# check whether the frames have been grabbed
if not ret:
break
# pass the frame to the background subtractor
foreground_mask = background_sub_method.apply(frame)
# obtain the background without foreground mask
img_bg_model = background_sub_method.getBackgroundModel()
cv2.imshow("Initial Frame", frame)
cv2.imshow("FG Mask", foreground_mask)
cv2.imshow("Subtraction Result", img_bg_model)
key = cv2.waitKey(10)
if key == 27:
break
Except that the algorithm don't work properly, I get this error that I don't understand.
[ERROR:0#0.002] global /home/usr/opencv-4.x/modules/core/src/persistence.cpp (505) open Can't open file: './config/SuBSense.xml' in write mode
Failed to open ./config/SuBSense.xml
In my lib pybgs, I have a config folder but there is no SuBSense.xml file.
So I don't know where this error is from, where this SuBSense.xml file is suppose to be.

Visual studio code doesn't import opencv library into python

I was trying to write a python file that opens the camera,only that when I write import cv2 it gives me an error
Try the following:
import cv2
vc = cv2.VideoCapture(0)
while True:
_, frame = vc.read()
cv2.imshow('frame', frame)
vc.release()
cv2.destroyAllWindows()
If this does not work, can you specify your error?

Why is Python Open CV imwrite unable to save a simple image from my webcam?

My goal is to capture some images from my laptop's webcam to use them later. The main problem is that cv2.imwrite doesn't seem to work, the directory is created succesfully but I cannot find a way to save the image. I am currently on Manjaro Linux. I've alredy checked that the frame is not empty and in other Python scripts I've been able to show the image properly, the only problem seems to be when I try to save the image.
Is there any other way to save the image or is something wrong with my code?
I have the following Python code:
import cv2 #opencv
import os
import time
import uuid
IMAGES_PATH = 'Tensorflor/workspace/images/collectedimages'
labels = ['hola', 'gracias', 'si', 'no', 'tequiero']
img_number = 15
for label in labels:
!mkdir {'Tensorflow/workspace/images/collectedimages/'+label}
cap = cv2.VideoCapture(0)
print('Collecting images for {}'.format(label))
time.sleep(5)
for numimg in range(img_number):
ret, frame = cap.read()
img_name = os.path.join(IMAGES_PATH, label, label+'.'+'{}.jpg'.format(str(uuid.uuid1())))
cv2.imwrite(img_name,frame)
cv2.imshow('Frame',frame)
time.sleep(2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()

How to save the capture image file in USB using python opencv function named "imwrite" in Linux

I tried to save capture of webcam image in USB using Python on Linux environment.
"Imwrite" is work in file directory but not work in USB directory.
I tried on 'os' package and path.
Is there other method to doing this?
path='/media/odroid/MYUSB/savefolder/'
capture_img=/demo/capture.jpg
image=cv2.imread(capture_img)
cv2.imwrite(os.path.join(path, resave.jpg),image)
The whole code is running without error, but jpg file is not saved in MYUSB
Perhaps you don't need to use os.join() try this:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
savePath = 'output.jpg' #Replace this with your own path say /media/odroid/MYUSB/savefolder/output.jpg
ret, frame = cap.read()
cv2.imwrite(savePath,frame)
If you want to save the whole video please refer to this answer
Here is the code:
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
savePath = 'output.avi' #Replace this with your own path say /media/odroid/MYUSB/savefolder/output.avi
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter(savePath, fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))))
while True:
newret, newframe = cap.read()
cv2.imshow('orig',newframe)
out.write(newframe)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
ret, frame = cap.read()
cv2.imwrite(savePath,frame)
cap.release()
out.release()
cv2.destroyAllWindows()
Also, your code has some issues, here is a fixed version of it which should work:
path='/media/odroid/MYUSB/savefolder/'
capture_img='/demo/capture.jpg' #it seems path should be demo/capture.jpg
image=cv2.imread(capture_img)
cv2.imwrite(os.path.join(path, 'resave.jpg'),image)

python webcam recorder with opencv

I've write webcam recorder by python and opencv .
now I have problem , I'm using of xvid codec for record video and when run my program on other system which not have xvid codec my program not work .
how i can solve it ? i don't want to install xvid on system
import cv2
import cv2.cv as cv
import numpy
import time
camera = cv2.VideoCapture(0)
time.sleep(5)
i=0
if camera:
try:
video = cv2.VideoWriter()
fourcc=cv.CV_FOURCC(*'XVID')
video.open('video.avi', fourcc, 20.0, (int(camera.get(3)), int(camera.get(4))))
while True and i<10 :
f,img = camera.read()
video.write(img)
i=i+1
except:
pass
video.release()

Categories

Resources