I am trying to use mediapipe to track hands. I am using Python 3.7.9 on Windows 10, my code is below:
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
while (True):
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = mp_hands.Hands.process(imgRGB)
print(result)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
I'm getting this error:
Traceback (most recent call last):
File "C:/Users/Tomáš/PycharmProjects/pythonProject/hand_detect.py", line 11, in <module>
results = mp_hands.Hands.process(imgRGB)
TypeError: process() missing 1 required positional argument: 'image'
[ WARN:1] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Error says that I need to pass one more argument 'self' before I pass argument 'image'. I've been browsing a lot and every related code doesnt use first argument in the process() function. Could anyone help me solve this error?
The problem is that you do not create an object of mp_hands.Hands before you want to process it. The following code solves it and prints some results. By the way, this was well documentated in the documentation link i commented before.. :
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # i had problems before reading webcam feeds, so i added cv2.CAP_DSHOW here
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# you have to create an object of mp_hands.Hands to get results
# alternatively you could do: results = mp_hands.Hands().process(imgRGB)
with mp_hands.Hands() as hands:
results = hands.process(imgRGB)
# continue loop if no results were found
if not results.multi_hand_landmarks:
continue
# print some results
for hand_landmarks in results.multi_hand_landmarks:
print(
f'Index finger tip coordinates: (',
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x}, '
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y})'
)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
Edit:
This is more or less the same code from here:
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
# initialize webcam
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
with mp_hands.Hands(model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# Flip the image horizontally for a selfie-view display.
cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
Related
I'm trying to run motion saliency on a video and see the results. However, I keep getting this particular error:(ret, img_sal) = saliency.computeSaliency(gray)
cv2.error: Unknown C++ exception from OpenCV code
Here is the code below:
import cv2
vid = cv2.VideoCapture('vids/test.mp4')
saliency = cv2.saliency.MotionSaliencyBinWangApr2014_create()
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = vid.get(cv2.CAP_PROP_FPS)
print(fps)
while True:
success, frame = vid.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(ret, img_sal) = saliency.computeSaliency(gray)
saliencyMap = (img_sal * 255).astype('uint8')
cv2.imshow('Map', saliencyMap)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
vid.release()
I would also appreciate if any other errors could be pointed out. I'm trying to save the video after applying motion saliency, so any help to that would be highly appreciated. Thank you.
I don't know anything about Python, but I have a job that requires processing, so I need to implement some functions.
Below is my code.
When I ran Python,
Traceback (most recent call last): File "C:\Users\user\Desktop\bodypix\bodypix.py", line 80, in
frame.shape
NameError: name 'frame' is not defined
This error is checked.
Why does this error appear?
If you know how to solve this problem, I would really appreciate your help.
import tensorflow as tf
from tf_bodypix.api import download_model, load_model, BodyPixModelPaths
import cv2
from matplotlib import pyplot as plt
import numpy as np
# # 2. Detections
# In[2]:
load_model(download_model(BodyPixModelPaths.MOBILENET_FLOAT_50_STRIDE_16))
# In[3]:
bodypix_model = load_model(download_model(BodyPixModelPaths.MOBILENET_FLOAT_50_STRIDE_16))
# In[27]:
# get vid cap device
cap = cv2.VideoCapture(0)
# loop through frame
while cap.isOpened():
ret, frame = cap.read()
# BodyPix Detections
result = bodypix_model.predict_single(frame)
mask = result.get_mask(threshold=0.5).numpy().astype(np.uint8)
masked_image = cv2.bitwise_and(frame, frame, mask=mask)
# Show result to user on desktop
cv2.imshow('BodyPix', masked_image)
# Break loop outcome
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release() # Releases webcam or capture device
cv2.destroyAllWindows() # Closes imshow frames
# # 3. Add Virtual Background
# In[12]:
img = cv2.imread('beach.jpg')
img = img[:480, :640, :]
# In[13]:
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# In[14]:
img.shape
# In[15]:
frame.shape
# In[24]:
plt.imshow(mask)
# In[23]:
plt.imshow(np.where(np.add(mask, -1) == -1, 1, np.add(mask, -1)))
# In[ ]:
# get vid cap device
cap = cv2.VideoCapture(0)
# loop through frame
while cap.isOpened():
ret, frame = cap.read()
# BodyPix Detections
result = bodypix_model.predict_single(frame)
mask = result.get_mask(threshold=0.5).numpy().astype(np.uint8)
masked_image = cv2.bitwise_and(frame, frame, mask=mask)
# Apply virtual background
neg = np.add(mask, -1)
inverse = np.where(neg==-1, 1, neg).astype(np.uint8)
masked_background = cv2.bitwise_and(img, img, mask=inverse)
final = cv2.add(masked_image, masked_background)
# Show result to user on desktop
cv2.imshow('BodyPix', final)
# Break loop outcome
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release() # Releases webcam or capture device
cv2.destroyAllWindows() # Closes imshow frames
You're referencing frame.shape prior to frame being assigned. Notice how img.shape is allowed since before this there is an assignment: img = cv2.imread('beach.jpg').
Read the documentation to see what "frame" should be assigned to.
happy day, I am trying to synchronize two cameras
(my local computer) and the camera of my cell phone with IP Web cam,
however when running the code it generates an error and the two
cameras that I have previously opened are closed (see photo ). What
would this be due to? Thanks for your help!
ERROR -->
Traceback (most recent call last):
File "C:\Users\JUCABALL\Desktop\camera_stream_openCV-master\main.py", line 20, in <module>
cv2.imshow ('cam1', frame1)
cv2.error: OpenCV (4.5.4-dev) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp: 182:
error: (-215: Assertion failed)! _src .empty () in function 'cv :: cvtColor'
This is the tabs - after fews seconds tabs close
[Tabs, 2 cameras][1]
This is my code
import numpy as np
import cv2
# capture the webcam
vid1 = cv2.VideoCapture(0, cv2.CAP_DSHOW)
vid2 = cv2.VideoCapture(1, cv2.CAP_DSHOW)
vid3 = cv2.VideoCapture(
"http://192.168.0.11:8080/video", cv2.CAP_DSHOW
) # ipwebcam address
while True: # while true, read the camera
ret, frame = vid1.read()
ret1, frame1 = vid2.read()
ret2, frame2 = vid3.read()
if ret:
cv2.imshow("cam0", frame) # frame with name and variable of the camera
cv2.imshow("cam1", frame1)
cv2.imshow("cam3", frame2)
# to break the loop and terminate the program
if cv2.waitKey(1) & 0xFF == ord("q"):
break
vid1.release()
vid2.release()
vid3.release()
The problem is you're not checking the value of ret1 and ret2.
If those are False, the images will not have been captured, and you're still trying to show them, and OpenCV crashes because there's no way to show an image that doesn't exist.
Try
if ret:
cv2.imshow("cam0", frame)
if ret1:
cv2.imshow("cam1", frame1)
if ret2:
cv2.imshow("cam3", frame2)
instead.
You could also store all of the capture devices in a list, and all the captured images equally so, to make the code a little simpler to work with:
import numpy as np
import cv2
captures = [
cv2.VideoCapture(0, cv2.CAP_DSHOW),
cv2.VideoCapture(1, cv2.CAP_DSHOW),
cv2.VideoCapture("http://192.168.0.11:8080/video", cv2.CAP_DSHOW), # ipwebcam address
]
while True: # while true, read the camera
frames = []
for cap in captures:
ret, frame = cap.read()
frames.append((frame if ret else None))
for i, frame in enumerate(frames):
if frame is not None: # None if not captured
cv2.imshow(f"cam{i}", frame)
# to break the loop and terminate the program
if cv2.waitKey(1) & 0xFF == ord("q"):
break
for cap in captures:
cap.release()
The code below shows the face and produces an output using voices. The problem is I'm unable to stop the voices, I want it to say it only once not for each frame taken
P.S I've tried using a timer but it didn't work.
import cv2
import pyttsx3
cap = cv2.VideoCapture(0)
voiceEngine = pyttsx3.init()
while(True):
# Capture frame-by-frame
success, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if success:
voiceEngine.say("hello there")
voiceEngine.runAndWait()
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == 27:
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
This is what flags are for.
said = False
while True:
...
if success and not said:
voiceEngine.say("hello there")
voiceEngine.runAndWait()
said = True
I tried playing a video from a file, as given in the tutorials. My program was as follows:
import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi')
while(cap.isOpened()):
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('outVideo',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
But I got the following error:
Traceback (most recent call last):
File "playVideo.py", line 8, in <module>
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.error: /home/hp/opencv/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor
I checked ret and it turned out to be False.
So the actual problem is with saving video. I used the following code to save 'output.avi' using VideoWriter function:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourCc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourCc,20.0,(640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
I am not able to open 'output.avi', even using VLC
First :
check ret value with: ret==True
Second as tutorials said:
Make sure proper versions of ffmpeg or gstreamer is installed. Sometimes, it is a headache to work with Video Capture mostly due to wrong installation of ffmpeg/gstreamer.
from:
http://docs.opencv.org/3.1.0/dd/d43/tutorial_py_video_display.html#gsc.tab=0
Finally check the video codec:
Can't open video with opencv2
Change the "while"- loop parameter to "ret" - and the order of the cap.read()
- ret is True if there is a valid next frame in the video/file stream.
import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi')
ret, frame = cap.read()
while(ret):
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('outVideo',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
ret, frame = cap.read()
cap.release()
cv2.destroyAllWindows()
I had faced the same error. But the issue was due to a missing package. It wasn't detected while using a jupyter notebook, but it showed up when I run the .py through terminal.
sudo apt-get install python-tk
This solved the error for me, hope it helps somebody else too :)