Python mediapipe handdetector error: create_int(): incompatible function arguments - python

HandTracking.py (Main Python File) this is my main Python File where I run the code using VSCODE
I'm just following the Youtube Video from
Murtaza's Workshop - Robotics and AI
import cv2
import numpy as np
import HandTrackingModule as htm
import time
import autopy
##########################
wCam, hCam = 640, 480
##########################
cap = cv2.VideoCapture(0)
cap.set(3, wCam)
cap.set(4, hCam)
pTime = 0
detector = htm.handDetector(maxHands=1)
while True:
# 1. Find Hnd Landmarks
success, img = cap.read()
img = detector.findHands(img)
lmList, bbox = detector.findPosition(img)
# 2. Get the tip of the index and middle fingers
# 3. Check wwhich fingers are up
# 4. Only Index Finger : Moving Mode
# 5. Convert Coordinates
# 6. Smoothen the values for smooth cursor
# 7. Move the MOUSE CURSOR!
# 8. Both Index and middle fingers are up : CLICK MODE
# 9. Find Distance between fingers
# 10. Click mouse if distance short
# 11. Frame rate
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10,50), cv2.FONT_HERSHEY_PLAIN, 2,
(255, 0, 0), 2)
# 12. Display
cv2.imshow("Image", img)
k = cv2.waitKey(1)
if k==ord('1'):
break
This is the other python file named "HandTrackingModule.py"
Hand Tracing Module
By: Murtaza Hassan
Youtube: http://www.youtube.com/c/MurtazasWorkshopRoboticsandAI
Website: https://www.computervision.zone/
"""
import cv2
import mediapipe as mp
import time
import math
import numpy as np
class handDetector():
def __init__(self, mode=False, maxHands=2, model_complexity=1, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
self.tipIds = [4, 8, 12, 16, 20]
def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
# print(results.multi_hand_landmarks)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms,
self.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self, img, handNo=0, draw=True):
xList = []
yList = []
bbox = []
self.lmList = []
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(myHand.landmark):
# print(id, lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
xList.append(cx)
yList.append(cy)
# print(id, cx, cy)
self.lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
xmin, xmax = min(xList), max(xList)
ymin, ymax = min(yList), max(yList)
bbox = xmin, ymin, xmax, ymax
if draw:
cv2.rectangle(img, (xmin - 20, ymin - 20), (xmax + 20, ymax + 20),
(0, 255, 0), 2)
return self.lmList, bbox
def fingersUp(self):
fingers = []
# Thumb
if self.lmList[self.tipIds[0]][1] > self.lmList[self.tipIds[0] - 1][1]:
fingers.append(1)
else:
fingers.append(0)
# Fingers
for id in range(1, 5):
if self.lmList[self.tipIds[id]][2] < self.lmList[self.tipIds[id] - 2][2]:
fingers.append(1)
else:
fingers.append(0)
# totalFingers = fingers.count(1)
return fingers
def findDistance(self, p1, p2, img, draw=True,r=15, t=3):
x1, y1 = self.lmList[p1][1:]
x2, y2 = self.lmList[p2][1:]
cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
if draw:
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 255), t)
cv2.circle(img, (x1, y1), r, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (x2, y2), r, (255, 0, 255), cv2.FILLED)
cv2.circle(img, (cx, cy), r, (0, 0, 255), cv2.FILLED)
length = math.hypot(x2 - x1, y2 - y1)
return length, img, [x1, y1, x2, y2, cx, cy]
def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(1)
detector = handDetector()
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList, bbox = detector.findPosition(img)
if len(lmList) != 0:
print(lmList[4])
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
(255, 0, 255), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()
I'm just following the code from youtube and it seems that I have this error even though its perfectly working from the video.
THE ERROR:
Traceback (most recent call last):
File "d:\Users\Romwald\Desktop\OpenCV Code tutorial\HANDGESTURE\HandTracking.py", line 15, in <module>
detector = htm.handDetector(maxHands=1)
File "d:\Users\Romwald\Desktop\OpenCV Code tutorial\HANDGESTURE\HandTrackingModule.py", line 24, in __init__
self.detectionCon, self.trackCon)
File "C:\Users\Romwald\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solutions\hands.py", line 129, in __init__
'multi_handedness'
File "C:\Users\Romwald\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solution_base.py", line 260, in __init__
for name, data in (side_inputs or {}).items()
File "C:\Users\Romwald\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solution_base.py", line 260, in <dictcomp>
for name, data in (side_inputs or {}).items()
File "C:\Users\Romwald\AppData\Local\Programs\Python\Python37\lib\site-packages\mediapipe\python\solution_base.py", line 513, in _make_packet
return getattr(packet_creator, 'create_' + packet_data_type.value)(data)
TypeError: create_int(): incompatible function arguments. The following argument types are supported:
1. (arg0: int) -> mediapipe.python._framework_bindings.packet.Packet
Invoked with: 0.5
[ WARN:0#2.743] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

Nvm fixed it.
Just did everything here:
Installed python Python 3.7.4
python -m pip install --upgrade pip
pip install opencv-python
pip install mediapipe or use or py -m pip install mediapipe
pip install mediapipe==0.8.8 (for Hands())
pip install autopy
pip install --upgrade virtualenv
pip install tensorflow

You missed some code. Just replace your constructor code with this one:
def __init__(self, mode=False, maxHands=2, model_complexity=1, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.model_complexity = model_complexity
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(
self.mode, self.maxHands, self.model_complexity, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils

Related

i = i[0] leads to IndexError: invalid index to scalar variable [duplicate]

This question already has answers here:
How to fix IndexError: invalid index to scalar variable
(6 answers)
Closed 2 months ago.
Im getting an error every time i run this script in my environment
Traceback (most recent call last):
File "FaceMark.py", line 55, in <module>
i = i[0]
IndexError: invalid index to scalar variable.
The camera does turns on but as soon it detects my hand or face the camera windows shuts down and it throws me the error posted above.
here's the entire script:
import cv2
import mediapipe as mp
import time
import numpy as np
thres = 0.45 # Threshold to detect object
nms_threshold = 0.2`your text`
cap = cv2.VideoCapture()
cap.set(3, 1280)
cap.set(4, 720)
cap.set(10, 150)
classNames = []
classFile = 'coco.names'
with open(classFile, 'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath, configPath)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
cap = cv2.VideoCapture(0)
pTime = 0
cTime = 0
mpDraw = mp.solutions.drawing_utils
mpFaceMesh = mp.solutions.face_mesh
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2)
drawSpec = mpDraw.DrawingSpec(thickness=1, circle_radius=2)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDrawHand = mp.solutions.drawing_utils
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
classIds, confs, bbox = net.detect(img, confThreshold=thres)
bbox = list(bbox)
confs = list(np.array(confs).reshape(1, -1)[0])
confs = list(map(float, confs))
indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold)
results = faceMesh.process(imgRGB)
resultsHand = hands.process(imgRGB)
for i in indices:
i = i[0]
box = bbox[i]
# colors = np.random.uniform(0, 255, size=(len(box), 3))
x, y, w, h = box[0], box[1], box[2], box[3]
cv2.rectangle(img, (x, y), (x + w, h + y), color=(0, 255, 0), thickness=2)
cv2.putText(img, classNames[classIds[i][0] - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 2)
print("Objects Ids: ", classIds)
if resultsHand.multi_hand_landmarks:
for handLms in resultsHand.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
print(id, lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
mpDrawHand.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
if results.multi_face_landmarks:
for faceLms in results.multi_face_landmarks:
mpDraw.draw_landmarks(img, faceLms, mpFaceMesh.FACE_CONNECTIONS,
drawSpec, drawSpec)
for id, lm in enumerate(faceLms.landmark):`
# print(lm)
ih, iw, ic = img.shape
x, y = int(lm.x * iw), int(lm.y * ih)
print("Face id: ", id, x, y)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN,
3, (255, 0, 0), 3)
cv2.imshow('image', img)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
how can i solve the problem around the indexes loop? i have tried other solutions here in similar questions, but i havent had any luck.
The variable i returns an integer value. This is not a list or tuple. If you change the for loop as follows, it will likely work.
for i in indices:
box = bbox[i]
# colors = np.random.uniform(0, 255, size=(len(box), 3))
x, y, w, h = box[0], box[1], box[2], box[3]
cv2.rectangle(img, (x, y), (x + w, h + y), color=(0, 255, 0), thickness=2)
cv2.putText(img, classNames[classIds[i] - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 2)
print("Objects Ids: ", classIds)

How to fix an OpenCV AI Hands critical code error in Python?

So I have been watching a video online and was trying to replicate the same project with the same code. It worked in the beginning but after I added another script to my VS Code project folder it completely broke and throws an error every time I run the program. I tried reinstalling and upgrading all of the packages I used but still the same error. Can anyone please suggest what may be the result of this error?
import cv2
import mediapipe as mp
import time
class handDetector():
def __init__(self, mode=False, maxHands=2, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils
def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
# print(results.multi_hand_landmarks)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms,
self.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self, img, handNo=0, draw=True):
lmList = []
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(myHand.landmark):
# print(id, lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
# print(id, cx, cy)
lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
return lmList
def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(1)
detector = handDetector()
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img)
if len(lmList) != 0:
print(lmList[4])
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
(255, 0, 255), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()
Error Message:
Traceback (most recent call last):
File "d:\Hand Gensture Tracking AI\HandTrackingModule.py", line 71, in <module>
main()
File "d:\Hand Gensture Tracking AI\HandTrackingModule.py", line 51, in main
detector = handDetector()
File "d:\Hand Gensture Tracking AI\HandTrackingModule.py", line 14, in __init__
self.hands = self.mpHands.Hands(self.mode, self.maxHands,
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mediapipe\python\solutions\hands.py", line 114, in __init__
super().__init__(
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mediapipe\python\solution_base.py", line 288, in __init__
self._input_side_packets = {
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mediapipe\python\solution_base.py", line 289, in <dictcomp>
name: self._make_packet(self._side_input_type_info[name], data)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mediapipe\python\solution_base.py", line 591, in _make_packet
return getattr(packet_creator, 'create_' + packet_data_type.value)(data)
TypeError: create_int(): incompatible function arguments. The following argument types are supported:
1. (arg0: int) -> mediapipe.python._framework_bindings.packet.Packet
Invoked with: 0.5
The problem with your code lies in line 11-12:
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.detectionCon, self.trackCon)
The class Hands() takes in 5 arguments, but it seems like you have missed the "complexity" parameter.
Your modified code:
import cv2
import mediapipe as mp
import time
class handDetector():
def __init__(self, mode=False, maxHands=2, complexity=1, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.detectionCon = detectionCon
self.trackCon = trackCon
self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(mode, maxHands, complexity, detectionCon, trackCon)
self.mpDraw = mp.solutions.drawing_utils
def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
# print(results.multi_hand_landmarks)
if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms,
self.mpHands.HAND_CONNECTIONS)
return img
def findPosition(self, img, handNo=0, draw=True):
lmList = []
if self.results.multi_hand_landmarks:
myHand = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(myHand.landmark):
# print(id, lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
# print(id, cx, cy)
lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)
return lmList
def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(1)
detector = handDetector()
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img)
if len(lmList) != 0:
print(lmList[4])
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
(255, 0, 255), 3)
cv2.imshow("Image", img)
cv2.waitKey(1)
if __name__ == "__main__":
main()
If this still doesn't solve your problem, then take note of the following things:
If you are using a virtual environment in vscode and try installing mediapipe or opencv-python, you may face a "numpy" version error. However, if you try running this code in PyCharm and installing these packages in the python interpreter, you won't face any errors.
You must be using python 3.7.0 or below as mediapipe doesn't support the newer versions.
Another tip to keep things simpler would be to use the "pydetection" module. You will need to install these packages:
pip install mediapipe
pip install opencv-python
pip install pydetetcion
The pydetection module will simplify the code a lot!
import pydetection as hand # The pydetection module
import cv2 # We will use this for reading and displaying webcam frames
detector = hand.HandRecogniser()
webcam = cv2.VideoCapture(0) # Change the number for a different camera. 0 is the default one.
while True:
_, frame = webcam.read() # Gets a frame of the current webcam feed
processed_image, hand_landmark_position = detector.findHands(frame, draw=True)
cv2.imshow(processed_image) # Show the image with the hands outlined
# Add a delay of 1 millisecond and check for the keystroke "q"
key = cv2.waitKey(1)
if key == ord("q"):
break
Github page to pydetection: https://github.com/Ayaan-Imran/pydetection

How do I use MILTracker tracking in my computer screen mss sct.grab?

I am a junior high school student, I am not living in English-speaking countries.
So My english not good. My dad said I can need to asking about coding questions in this community.
I know asking questions is very important,
Please help me solve this question. I interesting about image processing and I still grope this
field.
At the same time, I hope you can give me some suggestions for the software engineering.
Error message :
Traceback (most recent call last):
File "C:/Users/user/Desktop/PythonTrueTest/TrackingTesT1.py", line 88, in
ok, frame = cap.read()
AttributeError: 'numpy.ndarray' object has no attribute 'read'
import time
import mss
import numpy
from yoloOpencv import opencvYOLO
from PIL import ImageFont, ImageDraw, Image
from urllib.request import urlopen
import cv2
import imutils
tracker = cv2.TrackerMIL_create()
yolo = opencvYOLO(modeltype="yolov3-tiny", \
objnames="mask_face_outsource_yolov3-tiny/obj.names", \
weights="mask_face_outsource_yolov3-tiny/yolov3-tiny_final.weights", \
cfg="mask_face_outsource_yolov3-tiny/yolov3-tiny.cfg")
labels_tw = {"none": "沒有戴好", "good": "正確配戴口罩", "bad": "戴上口罩"}
def printText(bg, txt, color=(0, 255, 0, 0), size=0.7, pos=(0, 0), type="Chinese"):
(b, g, r, a) = color
if (type == "English"):
cv2.putText(bg, txt, pos, cv2.FONT_HERSHEY_SIMPLEX, size, (b, g, r, a), 2, cv2.LINE_AA)
else:
## Use simsum.ttf to write Chinese.
fontpath = "fonts/wt009.ttf"
font = ImageFont.truetype(fontpath, int(size * 10 * 4))
img_pil = Image.fromarray(bg)
draw = ImageDraw.Draw(img_pil)
draw.text(pos, txt, font=font, fill=(b, g, r, a))
bg = numpy.array(img_pil)
return bg
with mss.mss() as sct:
# Part of the screen to capture
monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
write_video = True
while "Screen capturing":
last_time = time.time()
#print(pyautogui.position())
# Get raw pixels from the screen, save it to a Numpy array
img = numpy.array(sct.grab(monitor))
cap = img
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
fps = (1 / (time.time() - last_time))
print("fps: {}".format(1 / (time.time() - last_time)))
# Display the picture
yolo.getObject(img, labelWant="", drawBox=True, bold=2, textsize=0.95, bcolor=(0, 0, 255),
tcolor=(255, 255, 255))
for id, label in enumerate(yolo.labelNames):
x = yolo.bbox[id][0]
y = yolo.bbox[id][1]
w = yolo.bbox[id][2]
h = yolo.bbox[id][3]
cx = int(x)
#if (cx > width): cx = width - 60
cy = int(y - h / 3)
if (cy < 0): cy = 0
if (label == "bad"):
txt_color = (0, 0, 255, 0)
elif (label == "none"):
txt_color = (255, 255, 0, 0)
else:
txt_color = (0, 255, 0, 0)
txt_size = round(w / 250, 1)
print(labels_tw[label], (w, h))
img = printText(bg=img, txt=labels_tw[label], color=txt_color, size=txt_size, pos=(cx, cy), type="Chinese")
cv2.putText(img, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
cv2.imshow("Frame", img)
k = cv2.waitKey(1)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
bbox = cv2.selectROI(img, False)
ok = tracker.init(img, bbox)
cv2.destroyWindow("ROI selector")
while True:
ok, frame = cap.read()
ok, bbox = tracker.update(frame)
if ok:
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]),
int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (0, 0, 255), 2, 2)
print(bbox[0])
cv2.imshow("Tracking", frame)
k = cv2.waitKey(1) & 0xff
if k == 27: break`

how to stop the execution of a cv2 program

i am currently workingon a project which requires body detection, so i created a file for it and when i tried to use it my main file but the body detection keeps on running and never stops making my rest of the program non executabale . I know it is because of the infinite for loop. But do i have any other ways where i could use in my main file
i have attached the body detection program below
kindly help
import cv2
import mediapipe as mp
import time
mpDraw = mp.solutions.drawing_utils
mppose = mp.solutions.pose
pose = mppose.Pose()
cap = cv2.VideoCapture('open cv/squidgamee/3.mp4')
cap.set(3, 400)
cap.set(4, 800)
ptime =0
while True:
succ, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = pose.process(imgRGB)
#print(results.pose_landmarks)
if results.pose_landmarks:
mpDraw.draw_landmarks(img, results.pose_landmarks, mppose.POSE_CONNECTIONS)
lmlist= []
if results.pose_landmarks:
for id, lm in enumerate(results.pose_landmarks.landmark):
h, w, c = img.shape
cx , cy = int(lm.x*w), int(lm.y*h)
lmlist.append([id, cx, cy])
cv2.circle(img, (lmlist[0][1], lmlist[0][2]), 15 , (255, 0, 255), cv2.FILLED)
print(lmlist[0])
ctime = time.time()
fps = 1/(ctime-ptime)
ptime = ctime
cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_COMPLEX, 3, (255,255,0), 3)
cv2.imshow("image", img)
key= cv2.waitKey(1)
if key == ord('q'):
break
You can call the content in the file in function on a separate thread.
import cv2
import mediapipe as mp
import time
def func():
mpDraw = mp.solutions.drawing_utils
mppose = mp.solutions.pose
pose = mppose.Pose()
cap = cv2.VideoCapture('open cv/squidgamee/3.mp4')
cap.set(3, 400)
cap.set(4, 800)
ptime =0
while True:
succ, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = pose.process(imgRGB)
if results.pose_landmarks:
mpDraw.draw_landmarks(img, results.pose_landmarks, mppose.POSE_CONNECTIONS)
lmlist= []
if results.pose_landmarks:
for id, lm in enumerate(results.pose_landmarks.landmark):
h, w, c = img.shape
cx, cy = int(lm.x*w), int(lm.y*h)
lmlist.append([id, cx, cy])
cv2.circle(img, (lmlist[0][1], lmlist[0][2]), 15 , (255, 0, 255), cv2.FILLED)
print(lmlist[0])
ctime = time.time()
fps = 1/(ctime-ptime)
ptime = ctime
cv2.putText(img, str(int(fps)), (70, 50), cv2.FONT_HERSHEY_COMPLEX, 3, (255,255,0), 3)
cv2.imshow("image", img)
key= cv2.waitKey(1)
if key == ord('q'):
break
Main:
import threading
from file import func
x = threading.Thread(target=func, args=())
x.start()

trouble in face detection program using python

I am working on this face detection project ,as i have run your code also, the one you had given for reference. Whenever i run it i am getting this error-'module' object has no attribute 'CascadeClassifier'. What should i do ?
Code-
from __future__ import print_function
import numpy as np
import cv2
# local modules
from video import create_capture
from common import clock, draw_str
def detect(img, cascade):
rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(rects) == 0:
return []
rects[:,2:] += rects[:,:2]
return rects
def draw_rects(img, rects, color):
for x1, y1, x2, y2 in rects:
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
if __name__ == '__main__':
import sys, getopt
print(__doc__)
args, video_src = getopt.getopt(sys.argv[1:], '', ['cascade=', 'nested-cascade='])
try:
video_src = video_src[0]
except:
video_src = 0
args = dict(args)
cascade_fn = args.get('--cascade', "../../data/haarcascades/haarcascade_frontalface_alt.xml")
nested_fn = args.get('--nested-cascade', "../../data/haarcascades/haarcascade_eye.xml")
cascade = cv2.CascadeClassifier(cascade_fn)
nested = cv2.CascadeClassifier(nested_fn)
cam = create_capture(video_src, fallback='synth:bg=../data/lena.jpg:noise=0.05')
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
t = clock()
rects = detect(gray, cascade)
vis = img.copy()
draw_rects(vis, rects, (0, 255, 0))
if not nested.empty():
for x1, y1, x2, y2 in rects:
roi = gray[y1:y2, x1:x2]
vis_roi = vis[y1:y2, x1:x2]
subrects = detect(roi.copy(), nested)
draw_rects(vis_roi, subrects, (255, 0, 0))
dt = clock() - t
draw_str(vis, (20, 20), 'time: %.1f ms' % (dt*1000))
cv2.imshow('facedetect', vis)
if cv2.waitKey(5) == 27:
break
cv2.destroyAllWindows()

Categories