having recently learned the basics of python, I thought I would dive in with a small project to build on.
A webcam application. I would then add tools as I developed my skills.
I have installed matplotlib, CV, numpy, and various others,the code I found:
import cv
cv.namedWindow("lll")
cap = cv.VideoCapture(0)
while( cap.isOpened() ) :
ret,img = cap.read()
cv.imshow("lll",img)
k = cv.waitKey(10)
if k == 27:
break
Now the initial "video Source" dialogue comes up, I select my webcam and press OK.
Then I get an error:
while(cap.isOpened()):
SystemError: null argument to internal routine
Done a bit of googling. Found others with the same issue but no resolution...
Any tips?
Sorry for not addressing your specific problem, but you can always use the newer cv2 module:
import cv2
cv2.namedWindow("lll")
cap = cv2.VideoCapture(0)
while True:
ret,img = cap.read()
cv2.imshow("lll",img)
k = cv2.waitKey(10)
if k == 27:
break
Sounds like something that would require digging into the source of PyCV (or whatever the interface to OpenCV is called) to fix. My tip to you would be to go to the OpenCV IRC-channel and ask or/and file a bug report.
Related
mediapipe==0.8.10.1
I'm trying to make a project based on mediapipe, and I've installed the package normally using
pip install mediapipe
I'm running this in the VS Code editor within a venv which has access to global packages,
import cv2
import mediapipe as mp
# why do I need to do this, instead of simply
# "mpHands = mp.solutions.hands"
# to access the hands function because otherwise it doesn't recognize the module
mpHands = mp.solutions.mediapipe.python.solutions.hands
Hands = mpHands.Hands() # doesn't recognize unless I do the entire line above
capture = cv2.VideoCapture(0)
while True:
ret, img = capture.read()
cv2.imshow("res", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
From the google github and other tutorials, "mp.solutions.hands" would be the usual way to call the module, but after some head scratching I've noticed that I need to use "mp.solutions.mediapipe.python.solutions.hands"
to access the "Hands()" module, otherwise it's unrecognized.
Moreover this only happens with mediapipe and not with, for example opencv.
I'm really not sure why this happens; I am new to package handling so any advice would be appreciated,
thank you.
I am new to opencv and python and was learning the basics from the docs. I was learning about the trackbar in opencv (python).
I wrote the same code as shown in the docs here. (Note the version is 4.5.3)
Here's my code:
import numpy as np
import cv2 as cv
def nothing(x):
pass
# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv.namedWindow('image')
# create trackbars for color change
cv.createTrackbar('R','image',0,255,nothing)
cv.createTrackbar('G','image',0,255,nothing)
cv.createTrackbar('B','image',0,255,nothing)
# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch, 'image',0,1,nothing)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
# get current positions of four trackbars
r = cv.getTrackbarPos('R','image')
g = cv.getTrackbarPos('G','image')
b = cv.getTrackbarPos('B','image')
s = cv.getTrackbarPos(switch,'image')
if s == 0:
img[:] = 0
else:
img[:] = [b,g,r]
cv.destroyAllWindows()
When I run this code, I get the following annoying long warning:
Using 'value' pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.
This is the full warning message if anyone wants to refer:
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-q3d_8t8e\opencv\modules\highgui\src\window.cpp (704) cv::createTrackbar UI/Trackbar(R#image): Using 'value' pointer is unsafe and deprecated. Use NULL as value pointer. To fetch trackbar value setup callback.
I tried to understand what it is trying to say, but couldn't understand much. AFAIU, the error is in the line (and subsequent):
r = cv.getTrackbarPos('R','image')
Even thought it is a warning, I would like to get rid of it, as it uses the word unsafe and deprecated.
I have opencv-python of version 4.5.3.56. I've tried getting through the docs, but seems they are the same as for older versions, and this particular feature is deprecated in my version.
Can anyone suggest me what should be done to avoid this?
this bug in 4.5.3 is already reported and fixed (at least for python)
however, unless you rebuild it locally, you will have to wait for the next pypi release ;(
I'm using libdmtx to decode data matrix with rasbian OS. when i run my code in windows, i get perfect results but in linux it does'nt work.
here is my simple code, when i run it in linux the variable "code" is always empty. i dont have any error. what is the problem?
import cv2
import time
from pylibdmtx.pylibdmtx import decode
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
while True:
ret,img = cap.read()
cv2.waitKey(1)
t0 = time.time()
code = decode(img, timeout=100, max_count=1, corrections=3)
if(code):
print((time.time() - t0)*1000)
print(code)
print(code[0].data.decode('utf-8'))
print(code[0].rect)
#x,y,w,h = code[0].rect
#cv2.rectangle(img,(x,y),(x+w,480-y-h),(255,0,255),2)
cv2.imshow('Result',img)
if cv2.waitKey(1) == 27:
break
Check to make sure you've installed libdmtx on Linux:
From the repository's readme:
"The libdmtx DLLs are included with the Windows Python wheels. On other operating systems, you will need to install the libdmtx shared library."
That'd be my first guess.
code = decode(img, timeout=100, max_count=1, corrections=3)
the timeout is to low. Try without timeout
code = decode(img, max_count=1, corrections=3)
Consider:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I'm using Python and OpenCV in the PyCharm IDE. When I try to open the webcam using OpenCV, it gives the following error:
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
Is this happening because I'm running out of memory?
What are the solutions for this?
I'm using PyCharm on MacBook Pro (OS: macOS v10.14 (Mojave)).
Open /Applications/PyCharm.app/Contents/info.plist,
Insert a new line:
Key: Privacy - Camera Usage
Type: String
Value: An application in PyCharm wants to use the camera.
Save it.
Reopen PyCharm.
Run your code.
I have opened an issue at JetBrains because of this. But here is a workaround:
Run PyCharm or IntelliJ IDEA (whatever JetBrains application) from an application which already has been approved for camera access. For example, I use the Hyper terminal to run the IDE and everything works.
By running the Python script in Visual Studio Code, it will execute the program without any warnings or bugs.
Open file /Applications/PyCharm.app/Contents/info.plist in any text editor.
Add these two lines before the dict and plist tags:
<key>Privacy - Camera Usage</key>
<string>An application in PyCharm wants to use the camera.</string>
</dict>
</plist>
I have searched everywhere and I find it truly amazing that there is no reference on the chamerMatching function, especially in Python. Someone else also had the same problem with no answer:
I don't really want to know about the algorithm - I know how the algorithm works - I want to know how to call it in Python and retrieve the costs, results and bestfit. I have tried the following code.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
frame = cv2.GaussianBlur(frame, (13, 13), 0)
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame = cv2.adaptiveThreshold(frame,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
templ = cv2.imread("template.png",cv2.CV_LOAD_IMAGE_GRAYSCALE)
cannyframe = cv2.Canny(frame,5,50,apertureSize=3)
cannytempl = cv2.Canny(templ,5,50,apertureSize=3)
cv2.imshow("cannyframe",cannyframe)
cv2.imshow("cannytempl", cannytempl)
cv2.waitKey(0)
#The line below, and NOT any other line, crashes the program
cv2.chamerMatching(cannytempl,cannyframe)
All of it runs fine except the final call to the chamerMatching function which causes the python interpreter to crash and stop working for some reason with a message that looks like this:
With absolutely zero documentation on the function, I can't figure out why.
EDIT:
The code above now includes all the required lines to run and below is template.png.
i don't know if you still need this information. But I also needed chamfer matching for my research. And here's what I found after encountering the same dilemma as you've had.
http://code.opencv.org/issues/3602
:)