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.
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'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 64bit, ubuntu system. Im running the code in idel.
I was facing opencv hang issue , the image shows up but I have to force kill the image window. So reffered to this thread -- Using other keys for the waitKey() function of opencv
import cv2
img = cv2.imread('sof.jpg') # load a dummy image
while(1):
cv2.imshow('img',img)
k = cv2.waitKey(3000) & 0xff
if k==32: # SpaceBar key to stop
break
elif k==-1: # normally -1 returned,so don't print it
continue
else:
print k # else print its value
Still its not working image hangs and I have to close it manually.
try :
k = cv2.waitKey(3000) & 0xff
Add cv2.waitkey(0) and cv2.destroyallwindows() but If you have used python notebooks then there is a problem in Unix based system to run a program of opencv. It will cause system freeze so you will need to restart kernel everytime when you try to execute code.
I have an alternative method which would prevent from freezing your system
Steps:
-Copy the code from python notebooks and create new filename.py and paste it
Open terminal
cd path/to/file
source activate VirtualEnvironment
python filename.py
This will run code directly from terminal.
Hope this helps you.
Example Link: https://youtu.be/8O-FW4Wm10s
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.