I'm running OpenCV 3 on Python 3.4 on a 2015 15" MacBook Pro. Below is a minimal example that illustrates my problem:
import cv2 as cv
import numpy as np
def mouse_callback(event, x, y, flags, param):
print("Callback!")
cv.namedWindow("Display")
cv.setMouseCallback("Display", mouse_callback)
cap = cv.VideoCapture(0)
while True:
ret, frame = cap.read()
cv.imshow("Display", frame)
if cv.waitKey(1) == ord("q"):
break
When I click on the screen, the text "Callback!" takes about 3 seconds to appear on the terminal screen. I'm not sure why I'm seeing this much lag--my laptop shouldn't be so bad that I can't even run this simple script.
Additionally, the problem persists when I reduce the webcam resolution, or even when I replace the webcam altogether with a still image. I rewrote a similar program in C++, and the C++ OpenCV libraries also suffer from this lag.
Any tips on how I can reduce or eliminate the lag?
Try this :
import cv2 as cv
import numpy as np
def mouse_callback(event, x, y, flags, param):
if event == cv.EVENT_LBUTTONDOWN:
print("Callback!")
cv.namedWindow("Display")
cv.setMouseCallback("Display", mouse_callback)
cap = cv.VideoCapture(0)
while True:
ret, frame = cap.read()
cv.imshow("Display", frame)
if cv.waitKey(1) == ord("q"):
break
Although its mostly a problem with something else. Maybe your webcam resolution is too high.
So try this also:
cap.set(3,640)
cap.set(4,480)
Put the above code above the while loop and check.
Related
import cv2
cap= cv2.VideoCapture(0)
while True:
ret,frame= cap.read()
cv2.imshow('Our live sketch',frame)
if cv2.waitKey(1)==13:
break
cap.release()
When I use cv2.VideoCapture(1), the programs shows error but the program works properly if I use cv2.VideoCapture(0)
That's the index of the camera it is used to select different cameras if you have more than one attached. By default 0 is your main one.
This is my program:
import numpy as np
import cv2
cap = cv2.VideoCapture(1)
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
cap.release()
cv2.destroyAllWindows()
And the result:
exit code -1073741819 (0xC0000005)
I am running my program in Windows 7. My Python version is 3.6 in Pycharm.
I got this code to run on my computer by changing the 1 to a 0 in the line cap = cv2.VideoCapture(1). This integer value determines which camera is used to capture an image. Passing 0 tells OpenCV to use the first camera on your device. If your device, like mine, only has one camera, then telling it to use the second camera by passing 1 will result in errors.
I have the code:
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
im = cv2.imread(r'C:\Users\james\OneDrive\Desktop\logos\normal.png')
bbox, label, conf = cv.detect_common_objects(im)
output_image = draw_bbox(im, bbox, label, conf)
plt.imshow(output_image)
plt.show()
That detects most objects in the picture, but I want to be able to make it from live video, I tried using cap = cv2.VideoCapture(0) but I could not get it to work.
A typical program executes every line until it's done, and then exits. Your code asks it to open a stream and show a frame. Then, because it's done everything that you've asked it to do, it exits.
In the code you provided in the comments, you are using matplotlib's .clf() immediately after .show(), which will clear the figure and potentially is the cause of your issue. You should be using cv2.imshow() instead.
If you want it to remain open, you need to put your displaying code inside a while True loop as shown in the getting started part of the docs. Combining their code with yours, you would get something like (however I haven't tested it):
import numpy as np
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Detect objects and draw on screen
bbox, label, conf = cv.detect_common_objects(frame)
output_image = draw_bbox(im, bbox, label, conf)
cv2.imshow('output',output_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
For a webcam, this will keep going until you press q to interrupt the program.
Be warned, this may not run smoothly because object detection models are typically fairly computationally expensive. But that is a challenge for another question.
I am playing around with OpenCV. I am following the documentation example (link)
I installed GTK webcam application on Ubuntu to validate that my webcam works. I am able to start the webcam and see the video feedback in GTK.
I added some print message in the tutorial code to see where I get.
I added a print before and after this line: cap = cv2.VideoCapture(0)
All I get, when running the Python file, is the print that I added before the cap = cv2.VideoCapture(0) and nothing else.
I tried increasing the waitKey to 20, 40, 100 but it didn't help.
Does anyone know why it does not get further and display the frame?
My code:
import numpy as np
import cv2
videoFeed = cv2.VideoCapture(0)
while (True):
ret, frame = videoFeed.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Feed', frame_gray)
if cv2.waitKey(10) & 0xFF = ord("q"):
break
videoFeed.release()
cv2.destroyAllWindows()
My setup:
Windows 10 host
Ubuntu 18.04 guest host
Integrated Webcam
Using PIP to install python module (numpy, scipi, pillow, open_cv, etc.)
Using venv python
You have a bug in your code at if cv2.waitKey(10) & 0xFF = ord("q"):. You should've gotten a syntax error here though.
import numpy as np
import cv2
videoFeed = cv2.VideoCapture(0)
while (True):
ret, frame = videoFeed.read()
if ret == False:
print("Failed to retrieve frame")
break
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Feed', frame_gray)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
videoFeed.release()
cv2.destroyAllWindows()
Tested your code. Works fine. Only other suggestion is to check whether your Ubuntu guest has permission to access your webcam. If you're using VirtualBox I remember seeing an option for this in the interface
When trying to capture frames from two cameras (using a single USB hub) simultaneously, only one camera can return a valid frame, the other one will return None. When using one external camera and the internal camera from the laptop, it works fine.
The exact same code and hardware work fine on a Ubuntu system. So it might be problem with Windows or its driver, or there is something wrong using the hub (maybe bandwidth problem, but not power problem because the hub has external power supply)
import cv2
import numpy as np
from multiprocessing import Process
def show(camera_id):
cap = cv2.VideoCapture(camera_id)
cap.set(3,640)
cap.set(4,480)
cap.set(cv2.CAP_PROP_FPS, 30)
while True:
ret, frame = cap.read()
cv2.imshow('test', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
p1 = Process(target=show, args=(0,))
p2 = Process(target=show, args=(1,))
p1.start()
p2.start()