Unable to read video file using cv2.VideoCapture() function in OpenCv - python

import numpy as np
import cv2
cap = cv2.VideoCapture("1.mp4")
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Its throwing error
libv4l2: error getting capabilities: Inappropriate ioctl for device
VIDEOIO ERROR: V4L: device 1.mp4: Unable to query number of channels
after compiling.

Related

Error in opening video using openCv (python)?

i'm trying to read videos with OpenCv python, but for some videos i can't do so, and i don't now why, or how to fix it (knowing that i can see the video on my pc )
here's the code i'm using :
# importing libraries
import cv2
video_path = 'C:/Videos/abc.avi'
cap = cv2.VideoCapture(video_path)
if (cap.isOpened() == False):
print("Error opening video")
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Frame', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
How can i fix this, and make it work ?
i'm working on :
python version : 3.10.0 and openCv version : 4.7.0.68

How to use hardware decoder for decode RTSP stream in python? (NVidia JetSon Nano)

I have NVIDIA Jetson Nano and FullHD Ip camera.
Camera streams RTSP/h264.
I want to decode frames in python script from this camera for analizies.
So, i tried use something like that:
# import the necessary packages
from imutils.video import VideoStream
import imutils
import time
import cv2
# grab a reference to the webcam
print("[INFO] starting video stream...")
#vs = VideoStream(src=0).start()
vs = VideoStream(src="rtsp://login:password#192.168.1.180").start()
time.sleep(2.0)
# loop over frames
while True:
# grab the next frame
frame = vs.read()
# resize the frame to have a maximum width of 500 pixels
frame = imutils.resize(frame, width=500)
# show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# release the video stream and close open windows
vs.stop()
cv2.destroyAllWindows()
That's works, but in that way frame decoded on CPU. How to use GPU decoder?
The solution:
Use cv2.VideoCapture with GStreamer backend:
import cv2
pipeline = "rtspsrc location=\"rtsp://login:password#host:port/\" ! rtph264depay ! h264parse ! omxh264dec ! nvvidconv ! video/x-raw, format=(string)BGRx! videoconvert ! appsink"
capture = cv2.VideoCaputure(pipeline, cv2.CAP_GSTREAMER)
while capture.isOpened():
res, frame = capture.read()
cv2.imshow("Video", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
capture.release()
cv2.destroyAllWindows()

How to extract text from webcam image

I am trying to extract text from the image taken by webcam. My code is below :
from PIL import Image
from pytesseract import image_to_string
import cv2
import time
cap = cv2.VideoCapture(0)
time.sleep(3)
ret, frame = cap.read()
if ret != True:
raise ValueError("Can't read frame")
cv2.imwrite('img2.png', frame)
cv2.imshow("img1", frame)
cv2.waitKey()
cv2.destroyAllWindows()
img = Image.open('img2.png')
text = image_to_string(img, lang='eng')
print(text)
I could extract text from screen shot image and now trying same thing in terms of webcam.
I just attempted to take photo with webcam and the download image taken by webcam and used image_to_string() to extract text from that image.
But it does not return any text even though the image is correctly taken.
How should I edit this ?
Otherwise, if you have any other good idea, I would like to know.

How to create executable application to using opencv?

I am greener in Python .I have design a small application for my personal project . But the problem is that when i use it on python then it's ok after create a executable(exe) application by pyinstaller then it's show "Fatal error" and "cam returned -1" my python project name is cam.py.
enter image description here
I don't know what's the problem of this application .
and my code is below:
import cv2
import numpy
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
while True :
ret,frame = cam.read()
gray = cv2.cvtColor (frame, cv2.COLOR_BGR2BGRA)
faces = face_cascade.detectMultiScale(gray, 1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow("LubnaX(NSL2)",frame)
k = cv2.waitKey(10)
if k==27:
break
cam.release()
cv2.destroyAllWindows()

OpenCV cv2 not working in Windows 7

I have a Windows 7 SP1 64 Bit machine with open cv2.4.9 installed and python 2.7.6 installed.
I use pre compiled version of opencv
The following code works perfectly for me
import cv2.cv as cv
import time
cv.NamedWindow("camera", 0)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
cv.DestroyAllWindows()
Now when I try to use this code
import cv2
import numpy as np
cam = cv2.VideoCapture(0)
s, img = cam.read()
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
while s:
cv2.imshow( winName,img )
s, img = cam.read()
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
print "Goodbye"
The window is opened , the camera is initialized (as camera lights are on) , but nothing is displayed and the window closes and the program exits.
WHERE am I going wrong??
QUESTION 2
Can any one also suggest me how to capture live video stream from my Linux machine 192.168.1.3 . The stream is being generated by ffmpeg.
The video stream can be opened in web browser. But I want to capture it with opencv and python.

Categories