|
|
|
@ -0,0 +1,58 @@
|
|
|
|
|
import cv2
|
|
|
|
|
import numpy as np
|
|
|
|
|
import mediapipe as mp
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
mpHands = mp.solutions.hands
|
|
|
|
|
hands = mpHands.Hands() # 设置参数,详见 hands.py 中的 __init__
|
|
|
|
|
mpDraw = mp.solutions.drawing_utils # 将检测出的手上的标记点连接起来
|
|
|
|
|
# 定义时间用于后边的fps计算
|
|
|
|
|
pTime = 0
|
|
|
|
|
cTime = 0
|
|
|
|
|
|
|
|
|
|
prototxt_path = "weights/deploy.prototxt.txt"
|
|
|
|
|
model_path = "weights/res10_300x300_ssd_iter_140000_fp16.caffemodel"
|
|
|
|
|
model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
|
|
|
|
|
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
|
|
|
|
|
while True:
|
|
|
|
|
_, image = cap.read()
|
|
|
|
|
h, w = image.shape[:2]
|
|
|
|
|
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), (104.0, 177.0, 123.0))
|
|
|
|
|
model.setInput(blob)
|
|
|
|
|
output = np.squeeze(model.forward())
|
|
|
|
|
font_scale = 1.0
|
|
|
|
|
for i in range(0, output.shape[0]):
|
|
|
|
|
confidence = output[i, 2]
|
|
|
|
|
if confidence > 0.5:
|
|
|
|
|
box = output[i, 3:7] * np.array([w, h, w, h])
|
|
|
|
|
start_x, start_y, end_x, end_y = box.astype(np.int)
|
|
|
|
|
cv2.rectangle(image, (start_x, start_y), (end_x, end_y), color=(255, 0, 0), thickness=2)
|
|
|
|
|
cv2.putText(image, f"{confidence*100:.2f}%", (start_x, start_y-5), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 0, 0), 2)
|
|
|
|
|
imgRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 将BGR格式图像转换为RGB
|
|
|
|
|
results = hands.process(imgRGB) # 对输入图像进行处理,探索图像中是否有手
|
|
|
|
|
# print(results.multi_hand_landmarks) # 如果有手,输出手所有0~20个标记点的比例坐标,如果没有,输出None
|
|
|
|
|
if results.multi_hand_landmarks:
|
|
|
|
|
for handLms in results.multi_hand_landmarks: # 捕捉画面中的每一只手
|
|
|
|
|
for id, lm in enumerate(handLms.landmark):
|
|
|
|
|
# print(id, lm)
|
|
|
|
|
h, w, c = image.shape
|
|
|
|
|
cx, cy = int(lm.x * w), int(lm.y * h) # 根据比例还原出每一个标记点的像素坐标
|
|
|
|
|
print(id, cx, cy) # 根据手上标记点的id打印出其相应所在图像中中的像素位置
|
|
|
|
|
if id == 4: # 可以根据手上标记点的id获得任意id对应的标记点的信息
|
|
|
|
|
cv2.circle(image, (cx, cy), 10, (255, 0, 255), cv2.FILLED) # 这里加粗强调了大拇指上的一个标记点
|
|
|
|
|
|
|
|
|
|
mpDraw.draw_landmarks(image, handLms, mpHands.HAND_CONNECTIONS) # 给画面中的每一只手进行标点、连线的操作
|
|
|
|
|
# 得到fps
|
|
|
|
|
cTime = time.time()
|
|
|
|
|
fps = 1/(cTime - pTime)
|
|
|
|
|
pTime = cTime
|
|
|
|
|
# 在画面上显示fps
|
|
|
|
|
cv2.putText(image, 'FPS:' + str(int(fps)), (10, 70), cv2.FONT_ITALIC, 1, (0, 0, 255), 3)
|
|
|
|
|
cv2.imshow("image", image)
|
|
|
|
|
if cv2.waitKey(1) == ord("q"):
|
|
|
|
|
break
|
|
|
|
|
# 点击窗口X按钮关闭窗口
|
|
|
|
|
if cv2.getWindowProperty('image',cv2.WND_PROP_VISIBLE) < 1:
|
|
|
|
|
break
|
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|
cap.release()
|