十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇內(nèi)容主要講解“如何使用Python+OpenCV實(shí)現(xiàn)拖拽虛擬方塊效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何使用Python+OpenCV實(shí)現(xiàn)拖拽虛擬方塊效果”吧!
公司主營業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出閻良免費(fèi)做網(wǎng)站回饋大家。
1、openCV讀取視頻流、在每一幀圖片上畫一個(gè)矩形。
2、使用mediapipe獲取手指關(guān)鍵點(diǎn)坐標(biāo)。
3、根據(jù)手指坐標(biāo)位置和矩形的坐標(biāo)位置,判斷手指點(diǎn)是否在矩形上,如果在則矩形跟隨手指移動(dòng)。
環(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í)慣了就好。
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ǔ)的一步操作,此時(shí)我們運(yùn)行這段代碼,攝像頭打開,我們會(huì)驚訝地看到自己英俊的臉龐,且左上角有個(gè)100*100的紫色矩形。
pip install mediapipe
此時(shí)可能出現(xiàn)一些問題,比如openCV突然用不了了,沒關(guān)系,卸載了重新下。
mediapipe詳細(xì)信息:Hands - mediapipe (google.github.io)
簡單來說,它會(huì)返回給我們21個(gè)手指關(guān)鍵點(diǎn)的坐標(biāo),即它在視頻畫面的位置比例( 0~1 ),我們乘以對應(yīng)畫面的寬高,就能得到手指對應(yīng)的坐標(biāo)了。
本次用到食指和中指指尖,也就是8號(hào)和12號(hào)。
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 在處理每一幀圖像時(shí),加入
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)我們在視頻流中讀取每一幀圖片時(shí),將其從BGR轉(zhuǎn)為RGB供給mediapipe生成的hands對象讀取,它會(huì)返回這張圖片中手指關(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個(gè)點(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()
我們這個(gè)實(shí)驗(yàn)要求拖動(dòng)方塊,那肯定也有不拖動(dòng)的時(shí)候,因此不妨根據(jù)上一步獲取食指(8)和中指(12)指尖的位置,如果這倆離得近,我們就在他與方塊重合的時(shí)候,根據(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個(gè)點(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()) # 記錄手指每個(gè)點(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) # 計(jì)算兩指尖距離 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()
到此,相信大家對“如何使用Python+OpenCV實(shí)現(xiàn)拖拽虛擬方塊效果”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!