I want to increase the resolution of my webcam in OpenCV Python - python

Is there a way to increase the webcam resolution of my webcam in OpenCV Python. The default value is 640x480 but, I want it to be 1365x730. Here's my code:
import cv2
cap = cv2.VideoCapture(0)
while(1):
_, frame = cap.read()
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()

import cv2
cap = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
while(1):
_, frame = cap.read()
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()

Related

How to show 2 usb cameras with openCV and python

I try to show 2 usb_cameras with 2 differents windows at the same time using openCV -python (I want to implement stereo vision and deep estimation), but I only obtain one window at a time. Can you help me please!
import threading
import cv2
def usb_video1():
cap = cv2.VideoCapture(2)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 200)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 150)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('s'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
def usb_video2():
cap = cv2.VideoCapture(4)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 200)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 150)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame_2',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
t01 = threading.Thread(target=usb_video1())
t02 = threading.Thread(target=usb_video2())
t01.start()
t02.start()

Error in OpenCV code: cv2.error: OpenCV(4.5.1)

I am writing a code in python using the OpenCV Python library.
I wanted to access the webcam using the code written, but after executing the code in the terminal, the following error arises again and again.
Code written:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Error:
If this is a camera installed on your computer:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
If this is a camera connected via USB:
import cv2
cap = cv2.VideoCapture(1)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Why cv2.imwrite lags 1 step?

My code:
import cv2
cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while True:
key = cv2.waitKey(0) & 0xFF
ret, frame = cap.read()
cv2.imshow('frame', frame)
if key == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
if key == ord('c'):
cv2.imwrite('capture.jpg', frame)
cap.release()
cv2.destroyAllWindows()
I run this code.
It shows the gray display.
I point the camera at an object and press the 'c' key.
It shows not the object image but the image of what the camera pointed at when I run the code, and saves it.
I point the camera at somewhere else and press 'c' key agein.
It shows the image of the object which it saw at 3. and save it.
The camera lags 1 step. Why?
This could be to do with a lack of cv::waitKey(0) and the window is not getting updated, although this is odd.
Try adding a cv::waitKey command after imshow like this
import cv2
cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while True:
key = cv2.waitKey(0) & 0xFF
ret, frame = cap.read()
cv2.imshow('frame', frame)
cv2.waitKey(0)
if key == ord('q'):
cap.release()
cv2.destroyAllWindows()
break
if key == ord('c'):
cv2.imwrite('capture.jpg', frame)
cap.release()
cv2.destroyAllWindows()
I think it might be this as when you do the imwrite you are effectively breaking out of the while loop (albeit slightly) to do something else with opencv.

can't save video file in opencv

I have videostreams and I'd like to convert them to foreground detected videos on which everything is white that moving and all others are black.
When I run this below script nothing happens, python ide just waits. Should I wait, does the video render or do i make something wrong?
Thanks
import cv2
import numpy
cap = cv2.VideoCapture('2018_02_28_12_07_42.h264')
fgbg = cv2.createBackgroundSubtractorMOG2()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
#while(cap.isOpened()):
while True:
ret, frame = cap.read()
#if ret == True:
fgmask = fgbg.apply(frame)
out.write(frame)
cv2.imshow('original', frame)
cv2.imshow('fg', fgmask)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
out.release()
cv2.destroyWindows()

OpenCV wont' capture from MacBook Pro iSight

Since a couple of days I can't open my iSight camera from inside an opencv application any more. cap = cv2.VideoCapture(0) returns, and cap.isOpened() returns true. However, cap.grab() just returns false. Any ideas?
Example Code:
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
rval = True
while rval:
rval, frame = vc.read()
cv2.imshow("preview", frame)
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
Mac OS 10.8.5
Python 2.7.5 (but also not working from inside a C++ app)
OpenCV 2.4.6.1
This is how I got the camera working for your code (on OSX 10.6):
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
rval, frame = vc.read()
while True:
if frame is not None:
cv2.imshow("preview", frame)
rval, frame = vc.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
I had a segmentation fault after I grab an image. It turned out that I used cv2.destroyAllWindows() before cap.release(). Below I show working code.
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#do some ops
cap.release()
cv2.imshow("output", output)
cv2.waitKey(0)
cv2.destroyAllWindows()
This code works on El Captain.

Categories

Resources