Python Mediapipe module call - python

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.

Related

raspberry pi pico (micropython) throws ImportError

if i run this code in VsCode with python it works just fine but if i run it with MicroPython i get the error ImportError: no module named 'cv2'
#from machine import ADC,PWM,Pin
import cv2
import mediapipe as mp
LandMarks = mp.solutions.drawing_utils
HandsModule = mp.solutions.hands
cam = cv2.VideoCapture(0)
forcc = cv2.VideoWriter_fourcc('m','p','4','v')
with HandsModule.Hands(static_image_mode = False,min_detection_confidence=0.7,min_tracking_confidence= 0.7,max_num_hands=2) as hands:
while True:
ret,frame = cam.read()
frame1 = cv2.resize(frame,(640,480))
results = hands.process(cv2.cvtColor(frame1,cv2.COLOR_BGR2RGB))
if results.multi_hand_landmarks != None:
for handlandmarks in results.multi_hand_landmarks:
LandMarks.draw_landmarks(frame1,handlandmarks,HandsModule.HAND_CONNECTIONS)
cv2.imshow('img',frame1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
note: i just commented import machine because python doesn't recognize it as MicroPython
As #Klaus D. said, a Pico is a microcontroller, not a computer. As such, it cannot run such python programs. It can however, run micropython. If you wish to run the script you specified, you need to use a different Raspberry pi. If you wish to use a Pico W (which I would not recommend), As Mark Setchell said, you will need to do extra processing (on your laptop), which adds additional complexity.
The easiest would be to just use a Raspberry Pi Zero W or Raspberry Pi 4 (and not use micropython), which would allow you to run the specified script pretty straightforwardly.

Using value pointer is unsafe and deprecated error in Opencv Python

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 ;(

read data matrix with python in raspberry pi using pylibdmtx package

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)

Process finished with exit code 134 (interrupted by signal 6: SIGABRT) in Python and OpenCV

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>

Python CV Webcam - Null Argument To Internal Routine

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.

Categories