Gesture recognition control led lights

Recently, the effect of using gesture recognition to control the LED light is as follows

 

The left hand controls the LED to turn on, and the right hand controls the LED to turn off. Two files are required

The code is as follows import time.py


import cv2
import serial
import serial.tools.list_ports
from handUtils import HandDetector



camera=cv2.VideoCapture(0)
hand_detector=HandDetector()

 



while True:
    success,img=camera.read()#返回里2个数值,一个是success,读取成功,另一个是每一帧的视频图片
    if success:
        img=cv2.flip(img,1)
        hand_detector.process(img)
        hand_detector.find_positions(img) 
        cv2.imshow('Video',img)
        #ser.write([0x42])
    k=cv2.waitKey(1)#等待按键1ms
    if k==ord('q'):
        break

camera.release()#不让程序占用
cv2.destroyAllWindows()

handUtils 

import mediapipe as mp
import cv2
import serial
import serial.tools.list_ports

#串口
ports_list = list(serial.tools.list_ports.comports())
if len(ports_list) <= 0:
    print("无串口设备。")
else:
    print("可用的串口设备如下:")
    for comport in ports_list:
        print(list(comport)[0], list(comport)[1])
# 方式1:调用函数接口打开串口时传入配置参数

 
ser = serial.Serial("COM3", 9600)    # 打开COM17,将波特率配置为115200,其余参数使用默认值
if ser.isOpen():                        # 判断串口是否成功打开
    print("打开串口成功。")
    print(ser.name)    # 输出串口号
   # data=bytearray([0x41])
   # ser.write([0x41])
else:
    print("打开串口失败。")  

class HandDetector():
    def __init__(self):
        self.hand_detector=mp.solutions.hands.Hands()
        self.draw_util=mp.solutions.drawing_utils

    def process(self,img):
        img_rgb=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        self.hands_data=self.hand_detector.process(img_rgb)
        if self.hands_data.multi_hand_landmarks:
            for handlms in self.hands_data.multi_hand_landmarks:
               self.draw_util.draw_landmarks(img,handlms,mp.solutions.hands.HAND_CONNECTIONS)

    def find_positions(self,img):
        position={'Left':{},'Right':{}}
        if  self.hands_data.multi_hand_landmarks:
            i=0
            for hand in self.hands_data.multi_handedness:
                print(hand)
                score=hand.classification[0].score
                if score>=0.8:
                    label=hand.classification[0].label
                    if label=='Left':
                       ser.write([0x41])
                    elif label=='Right':
                        ser.write([0x42])
                

For the specific learning process, you can refer to Python Programming Gesture Recognition_哔哩哔哩_bilibili which is  very good. Also, you can refer to my serial port transmission (I forgot who to refer to), anyway, first detect the serial port, then open the serial port, and finally send the serial port. As for the learning of SCM stm32, you can refer to the tutorial of Jiangke University Association, which contains serial port transmission and LED related knowledge. Finally, I wish you all good luck. Goodbye!

Guess you like

Origin blog.csdn.net/weixin_48433993/article/details/130890685