I have a webcam and I have installed the camera drivers and it is using the vendor's drivers (not UVC). I am trying to capture an image and it is resulting in an array of zeros (frame). I have used another software on windows and it is able to get the video without issues. Any ideas on how to fix this issue?
import cv2
cam = cv2.VideoCapture(1)
f1 = cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
f2 = cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
f2 = cam.set(cv2.CAP_PROP_FPS, 1)
print(cam.get(3),cam.get(4))
if cam.isOpened():
_,frame = cam.read()
cv2.imshow('',frame)
Edit: Please check the comments. I tried a few suggestions and they didn't work. I changed the camera, but with the new camera, I get noise.
Related
Edit: The problem was fixed after using VLC media player. The standard windows media player could not read the videos.
I am attempting to take a video, run it through an object detector, add the bounding boxes, and output the video with the bounding boxes. However, I'm having a strange problem where the video I output cannot be viewed. I get a "Server execution failed" error when attempting to play the video on my local windows 10 machine. I am developing through SSH on ubuntu 20.04 with the vs code remote development extension.
Here is some OpenCV code that does not work with my setup. The video is written to the disk, and OpenCV is able to read frames from output5.avi, however the output5.avi file cannot be opened as I described.
import cv2
cap = cv2.VideoCapture("video.mov")
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output5.avi', fourcc, 30, (width, height), isColor=True)
while cap.isOpened():
# get validity boolean and current frame
ret, frame = cap.read()
# if valid tag is false, loop back to start
if not ret:
break
else:
frame = cv2.resize(frame, (width, height))
out.write(frame)
cap.release()
out.release()
I have also attempted to save the video through torchvision.io.write_video, but the exact same problem occurs. Tensorboard similarly doesn't work.
It must be something wrong with how the remote machine is set up, but I have no idea what could be wrong.
I'm trying to read images from an IDS GV-5240CP camera plugged to my laptop via ethernet using Python and OpenCV.
This is what I am supposed to get :
A 1280x1024 image (here resized for upload)
But using this code:
import cv2
cap = cv2.VideoCapture(1)
_, frame = cap.read()
print(frame.shape)
cv2.imshow("Out", frame)
cv2.waitKey(2000)
cap.release()
cv2.destroyAllWindows()
cv2.imwrite('Test2.png', frame)
I get:
A 640x480 cropped image
How can I set my video capture to the native resolution?
VideoCapture uses 640x480 by default.
If you want a different resolution, specify it in the constructor or use the set() method with CAP_PROP_FRAME_WIDTH and so on. Details in the docs.
cap = cv2.VideoCapture(1, apiPreference=cv2.CAP_ANY, params=[
cv2.CAP_PROP_FRAME_WIDTH, 1280,
cv2.CAP_PROP_FRAME_HEIGHT, 1024])
out = cv2.VideoWriter(("E:", 'output.avi'), fourcc, 20.0, (640, 480))
try using this and change the resolution according to your need,
tho it will download or use
cv2.resize(frame,(w,h),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
since you don't want to do this then use set() module in OpenCV to set specific resolution. docs.opencv.org/4.x/d8/dfe/… this link for the full info about set()
I am trying to stream and save video from my webcam with the Python script shown below but, for some reason, the 'myvideo.mp4' file has a very small size and cannot be opened with QuickTime (or other players) - it seems to be empty. However, the video stream works perfectly.
As suggested in other topics, I have tried different file formats and codecs and I pass exact fps, width and height that my webcam returns. Perhaps anyone knows what can be the issue here? Thanks in advance!
import cv2
cap = cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = cap.get(cv2.CAP_PROP_FPS)
writer = cv2.VideoWriter('myvideo.mp4',cv2.VideoWriter_fourcc(*'mp4v'),fps,(width,height))
while True:
ret,frame = cap.read()
# OPERATIONS (DRAWING)
writer.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
writer.release()
cv2.destroyAllWindows()
I have also tried running the script as superuser but it did not help. I am using Mac.
QuickTime error:
The document “myvideo.mp4” could not be opened.
The file isn’t compatible with QuickTime Player.
Try to change fourcc(4-character code of codec used to compress the frames.)
writer = cv2.VideoWriter('myvideo.mp4',cv2.VideoWriter_fourcc(*'mp4v'),fps,(width,height))
replace it with
writer = cv2.VideoWriter('myvideo.mp4',cv2.VideoWriter_fourcc(*'XVID'),fps,(width,height))
Simply fix typo
Change
height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
to
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
You were taking CAP_PROP_FRAME_WIDTH twice.
If the problem persists...
If this still does not help, try to swap them. It seems stupid, but helped for me. I guess that the get method somehow takes orientation into account, but then reading a frame neglects the orientation of the video (or opposite, does not matter, they are just inconsistent). I had exactly the same problem, and swapped width with height and it solved it.
Extra
A bit old list of codecs tested on mac.
I'm working on project were I have a Raspberry Pi (Running Raspbian Jessie) with a camera (PiCam or a USB webcam) and I want to stream the images from the cam to Youtube and edit the images in realtime using OpenCV (add text and a logo) but I just can't figure it out. The fact that I'm a novice in Unix and Python doesn't help either.
I am able to edit each frame of the video stream using the following python code
import cv2, sys
import time
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
while True:
rc, img = capture.read()
if not rc:
continue
#make a copy of the image to add the semi-transparant bar
overlay = img.copy()
cv2.rectangle(overlay, (0,700), (1280, 720), (1,1,1),-1)
cv2.addWeighted(overlay, 0.75, img, 0.25, 0, img)
#add the text
imageText = "[{}]".format(time.strftime("%d %b %Y %H:%M:%S", time.localtime()))
cv2.putText(img, imageText, (10, 713), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(255,255,255))
But now I have to add these frames to a output stream and send it to the rtmp stream of Youtube in the right format. I've been searching the internet and documentation all day but I can't seem to find a answer that does the job. I really can't see the wood for the trees...
Has anybody got a solution or can point me in the right direction.
I am using opencv version 3.1.0 with zbar (latest version as of this post) and PIL (latest version as of this post)
import zbar
import Image
import cv2
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)
while(True):
ret, cv = cap.read()
cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB)
pil = Image.fromarray(cv)
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
print "/n ...Done"
I don't understand why this is not working it is supposed to constantly check for qrcodes in the current frame of the Video Stream and if it sees one it decodes it and prints what it says inside I hold up printed out qrcodes in front of my webcam and it does not work it shows that my camera is on and that there is a video stream occurring so somewhere in the while loop something is going wrong
I tried it before with qr codes on my computer not printed out and it worked fine
I also tried having it show me the current frame with cv2.imshow("out",cv) but when I did the program showed just a big grey square where it should show the video stream and then it froze so I had to kill Netbeans.
zbar works with grayscale images. Change cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB) to cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY).
I'm guessing you're using this example code to base your program off of. They do the color to grayscale conversion with convert('L') on line 15.