Python+OpenCV實戰(zhàn)之拖拽虛擬方塊的實現(xiàn)
一、項目效果

學(xué)校宿舍今天搬家,累麻了,突然發(fā)現(xiàn)展示處理的也很粗糙,就這樣吧嘿嘿~~~
二、核心流程
1、openCV讀取視頻流、在每一幀圖片上畫一個矩形。
2、使用mediapipe獲取手指關(guān)鍵點(diǎn)坐標(biāo)。
3、根據(jù)手指坐標(biāo)位置和矩形的坐標(biāo)位置,判斷手指點(diǎn)是否在矩形上,如果在則矩形跟隨手指移動。
三、代碼流程
環(huán)境準(zhǔn)備:
python: 3.8.8
opencv: 4.2.0.32
mediapipe: 0.8.10.1
注:
1、opencv版本過高或過低可能出現(xiàn)一些如攝像頭打不開、閃退等問題,python版本影響opencv可選擇的版本。
2、pip install mediapipe 后可能導(dǎo)致openCV無法正常使用,卸了重新下載,習(xí)慣了就好。
1. 讀取攝像頭視頻,畫矩形
import cv2
import time
import numpy as np
# 調(diào)用攝像頭 0 默認(rèn)攝像頭
cap = cv2.VideoCapture(0)
# 初始方塊數(shù)據(jù)
x = 100
y = 100
w = 100
h = 100
# 讀取一幀幀照片
while True:
# 返回frame圖片
rec,frame = cap.read()
# 鏡像
frame = cv2.flip(frame,1)
# 畫矩形
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1)
# 顯示畫面
cv2.imshow('frame',frame)
# 退出條件
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows() 這是很基礎(chǔ)的一步操作,此時我們運(yùn)行這段代碼,攝像頭打開,我們會驚訝地看到自己英俊的臉龐,且左上角有個100*100的紫色矩形。
2. 導(dǎo)入mediapipe處理手指坐標(biāo)
pip install mediapipe
此時可能出現(xiàn)一些問題,比如openCV突然用不了了,沒關(guān)系,卸載了重新下。
mediapipe詳細(xì)信息:Hands - mediapipe (google.github.io)


簡單來說,它會返回給我們21個手指關(guān)鍵點(diǎn)的坐標(biāo),即它在視頻畫面的位置比例( 0~1 ),我們乘以對應(yīng)畫面的寬高,就能得到手指對應(yīng)的坐標(biāo)了。
本次用到食指和中指指尖,也就是8號和12號。
2.1 配置一些基礎(chǔ)信息
import cv2
import time
import numpy as np
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.5)
2.2 在處理每一幀圖像時,加入
frame.flags.writeable = False
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 返回結(jié)果
results = hands.process(frame)
frame.flags.writeable = True
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
當(dāng)我們在視頻流中讀取每一幀圖片時,將其從BGR轉(zhuǎn)為RGB供給mediapipe生成的hands對象讀取,它會返回這張圖片中手指關(guān)鍵點(diǎn)的信息,我們只需要繼續(xù)對其作畫,畫在每一幀圖片上。
# 如果結(jié)果不為空
if results.multi_hand_landmarks:
# 遍歷雙手(根據(jù)讀取順序,一只只手遍歷、畫畫)
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
2.3 至此步驟完整代碼
import cv2
import time
import numpy as np
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.5)
# 調(diào)用攝像頭 0 默認(rèn)攝像頭
cap = cv2.VideoCapture(0)
# 方塊初始數(shù)組
x = 100
y = 100
w = 100
h = 100
# 讀取一幀幀照片
while True:
# 返回frame圖片
rec,frame = cap.read()
# 鏡像
frame = cv2.flip(frame,1)
frame.flags.writeable = False
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 返回結(jié)果
results = hands.process(frame)
frame.flags.writeable = True
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# 如果結(jié)果不為空
if results.multi_hand_landmarks:
# 遍歷雙手(根據(jù)讀取順序,一只只手遍歷、畫畫)
# results.multi_hand_landmarks n雙手
# hand_landmarks 每只手上21個點(diǎn)信息
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# 畫矩形
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 255), -1)
# 顯示畫面
cv2.imshow('frame',frame)
# 退出條件
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows() 此時我們運(yùn)行看一下還挺有意思的:

3. 位置計算
我們這個實驗要求拖動方塊,那肯定也有不拖動的時候,因此不妨根據(jù)上一步獲取食指(8)和中指(12)指尖的位置,如果這倆離得近,我們就在他與方塊重合的時候,根據(jù)手指的位置改變方塊的坐標(biāo)。

完整代碼

import cv2
import time
import math
import numpy as np
import mediapipe as mp
# mediapipe配置
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.5)
# 調(diào)用攝像頭 0 默認(rèn)攝像頭
cap = cv2.VideoCapture(0)
# cv2.namedWindow("frame", 0)
# cv2.resizeWindow("frame", 960, 640)
# 獲取畫面寬度、高度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 方塊初始數(shù)組
x = 100
y = 100
w = 100
h = 100
L1 = 0
L2 = 0
on_square = False
square_color = (0, 255, 0)
# 讀取一幀幀照片
while True:
# 返回frame圖片
rec,frame = cap.read()
# 鏡像
frame = cv2.flip(frame,1)
frame.flags.writeable = False
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 返回結(jié)果
results = hands.process(frame)
frame.flags.writeable = True
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# 如果結(jié)果不為空
if results.multi_hand_landmarks:
# 遍歷雙手(根據(jù)讀取順序,一只只手遍歷、畫畫)
# results.multi_hand_landmarks n雙手
# hand_landmarks 每只手上21個點(diǎn)信息
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
frame,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
# 記錄手指每個點(diǎn)的x y 坐標(biāo)
x_list = []
y_list = []
for landmark in hand_landmarks.landmark:
x_list.append(landmark.x)
y_list.append(landmark.y)
# 獲取食指指尖
index_finger_x, index_finger_y = int(x_list[8] * width),int(y_list[8] * height)
# 獲取中指
middle_finger_x,middle_finger_y = int(x_list[12] * width), int(y_list[12] * height)
# 計算兩指尖距離
finger_distance = math.hypot((middle_finger_x - index_finger_x), (middle_finger_y - index_finger_y))
# 如果雙指合并(兩之間距離近)
if finger_distance < 60:
# X坐標(biāo)范圍 Y坐標(biāo)范圍
if (index_finger_x > x and index_finger_x < (x + w)) and (
index_finger_y > y and index_finger_y < (y + h)):
if on_square == False:
L1 = index_finger_x - x
L2 = index_finger_y - y
square_color = (255, 0, 255)
on_square = True
else:
# 雙指不合并/分開
on_square = False
square_color = (0, 255, 0)
# 更新坐標(biāo)
if on_square:
x = index_finger_x - L1
y = index_finger_y - L2
# 圖像融合 使方塊不遮擋視頻圖片
overlay = frame.copy()
cv2.rectangle(frame, (x, y), (x + w, y + h), square_color, -1)
frame = cv2.addWeighted(overlay, 0.5, frame, 1 - 0.5, 0)
# 顯示畫面
cv2.imshow('frame',frame)
# 退出條件
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows() 到此這篇關(guān)于Python+OpenCV實戰(zhàn)之拖拽虛擬方塊的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python OpenCV拖拽虛擬方塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 申請內(nèi)存空間,用于創(chuàng)建多維數(shù)組的實例
今天小編就為大家分享一篇python 申請內(nèi)存空間,用于創(chuàng)建多維數(shù)組的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python中JSON數(shù)據(jù)的相互轉(zhuǎn)化詳解
這篇文章主要介紹了Python中JSON數(shù)據(jù)的相互轉(zhuǎn)化詳解,JSON 是一種輕量級的數(shù)據(jù)交互格式,可以按照 JSON 指定的格式去組織和封裝數(shù)據(jù),2023-12-12
JSON 本質(zhì)是一個帶有特定格式的字符串,需要的朋友可以參考下
python pip安裝的包放在哪個目錄(site-packages目錄的位置)
這篇文章主要介紹了python pip安裝的包放在哪個目錄(site-packages目錄的位置),通常安裝在python安裝目錄下的lib/site-packages目錄下,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
詳解使用Python寫一個向數(shù)據(jù)庫填充數(shù)據(jù)的小工具(推薦)
這篇文章主要介紹了用Python寫一個向數(shù)據(jù)庫填充數(shù)據(jù)的小工具,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python3實現(xiàn)的爬蟲爬取數(shù)據(jù)并存入mysql數(shù)據(jù)庫操作,涉及Python正則爬取數(shù)據(jù)及針對mysql數(shù)據(jù)庫的存儲操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-06-06
Python-numpy實現(xiàn)灰度圖像的分塊和合并方式
今天小編就為大家分享一篇Python-numpy實現(xiàn)灰度圖像的分塊和合并方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

