hhhhhc12q3 2023-03-26 23:56 采纳率: 100%
浏览 103
已结题

串口连接改为TCP Server进行网络连接

我想将下列代码中的串口连接改为TCP Server进行网络连接,本机主机端口8266,本机的主机地址为192.168.1.1
类似网络调试助手发送的那样
刚刚开始学可能有地方表达的不对

img

#-*- coding : utf-8-*-
# coding:unicode_escape
import mediapipe as mp
import cv2
import numpy as np
import serial


def get_angle(v1, v2):
    angle = np.dot(v1, v2) / (np.sqrt(np.sum(v1 * v1)) * np.sqrt(np.sum(v2 * v2)))
    angle = np.arccos(angle) / 3.14 * 180

    return angle


# noinspection PyShadowingNames
def get_str_guester(up_fingers: object, list_lms: object) -> object:
    if len(up_fingers) == 1 and up_fingers[0] == 8:

        v1 = list_lms[6] - list_lms[7]
        v2 = list_lms[8] - list_lms[7]

        angle = get_angle(v1, v2)

        if angle < 160:
            str_guested = "9"
        else:
            str_guested = "1"
            serial.write("1".encode())  #此代码用于发送数据给单片机

    elif len(up_fingers) == 1 and up_fingers[0] == 4:
        str_guested = "Good"

    elif len(up_fingers) == 1 and up_fingers[0] == 20:
        str_guested = "Bad"

    elif len(up_fingers) == 1 and up_fingers[0] == 12:
        str_guested = "FXXX"

    elif len(up_fingers) == 2 and up_fingers[0] == 8 and up_fingers[1] == 12:
        str_guested = "2"
        serial.write("2".encode())
    elif len(up_fingers) == 2 and up_fingers[0] == 4 and up_fingers[1] == 20:
        str_guested = "6"
        serial.write("6".encode())
    elif len(up_fingers) == 2 and up_fingers[0] == 4 and up_fingers[1] == 8:
        str_guested = "8"

    elif len(up_fingers) == 3 and up_fingers[0] == 8 and up_fingers[1] == 12 and up_fingers[2] == 16:
        str_guested = "3"
        serial.write("3".encode())
    elif len(up_fingers) == 3 and up_fingers[0] == 4 and up_fingers[1] == 8 and up_fingers[2] == 12:

        dis_8_12 = list_lms[8, :] - list_lms[12, :]
        dis_8_12 = np.sqrt(np.dot(dis_8_12, dis_8_12))

        dis_4_12 = list_lms[4, :] - list_lms[12, :]
        dis_4_12 = np.sqrt(np.dot(dis_4_12, dis_4_12))

        if dis_4_12 / (dis_8_12 + 1) < 3:
            # noinspection PyShadowingNames
            str_guested = "7"

        elif dis_4_12 / (dis_8_12 + 1) > 5:
            str_guested = "Gun"
        else:
            str_guested = "7"

    elif len(up_fingers) == 3 and up_fingers[0] == 4 and up_fingers[1] == 8 and up_fingers[2] == 20:
        str_guested = "ROCK"

    elif len(up_fingers) == 4 and up_fingers[0] == 8 and up_fingers[1] == 12 and up_fingers[2] == 16 and up_fingers[
        3] == 20:
        str_guested = "4"
        serial.write("4".encode())
    elif len(up_fingers) == 5:
        str_guested = "5"
        serial.write("5".encode())
    elif len(up_fingers) == 0:
        str_guested = "0"
        serial.write("0".encode())
    else:
        str_guested = " "

    return str_guested


if __name__ == "__main__":
    serial = serial.Serial('COM3', 9600, timeout=0.5) #此com口根据自己的电脑更改com端口和波特率
    if serial.isOpen():
        print("open success")
    else:
        print("open failed")
    cap = cv2.VideoCapture(0)
    # 定义手 检测对象
    mpHands = mp.solutions.hands
    hands = mpHands.Hands()
    mpDraw = mp.solutions.drawing_utils

    while True:
        # 读取一帧图像
        success, img = cap.read()
        if not success:
            continue
        image_height, image_width, _ = np.shape(img)

        # 转换为RGB
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        # 得到检测结果
        results = hands.process(imgRGB)

        if results.multi_hand_landmarks:
            hand = results.multi_hand_landmarks[0]

            mpDraw.draw_landmarks(img, hand, mpHands.HAND_CONNECTIONS)

            # 采集所有关键点的坐标
            list_lms = []
            for i in range(21):
                pos_x = hand.landmark[i].x * image_width
                pos_y = hand.landmark[i].y * image_height
                list_lms.append([int(pos_x), int(pos_y)])

            # 构造凸包点
            list_lms = np.array(list_lms, dtype=np.int32)
            hull_index = [0, 1, 2, 3, 6, 10, 14, 19, 18, 17, 10]
            hull = cv2.convexHull(list_lms[hull_index, :])
            # 绘制凸包
            cv2.polylines(img, [hull], True, (0, 255, 0), 2)

            # 查找外部的点数
            n_fig = -1
            ll = [4, 8, 12, 16, 20]
            up_fingers = []

            for i in ll:
                pt = (int(list_lms[i][0]), int(list_lms[i][1]))
                dist = cv2.pointPolygonTest(hull, pt, True)
                if dist < 0:
                    up_fingers.append(i)

            # print(up_fingers)
            # print(list_lms)
            # print(np.shape(list_lms))
            str_guester = get_str_guester(up_fingers, list_lms)

            cv2.putText(img, ' %s' % str_guester, (90, 90), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 255, 0), 4,
                        cv2.LINE_AA)

            for i in ll:
                pos_x = hand.landmark[i].x * image_width
                pos_y = hand.landmark[i].y * image_height
                # 画点
                cv2.circle(img, (int(pos_x), int(pos_y)), 3, (0, 255, 255), -1)

        cv2.imshow("hands", img)

        key = cv2.waitKey(1) & 0xFF
         
        # 按键 "q" 退出
        if key == ord('q'):
            break
    cap.release()

  • 写回答

3条回答 默认 最新

  • hhhhhc12q3 2023-03-27 20:52
    关注

    '

    import mediapipe as mp
    import cv2
    import numpy as np
    import socket
    
    def get_angle(v1, v2):
        angle = np.dot(v1, v2) / (np.sqrt(np.sum(v1 * v1)) * np.sqrt(np.sum(v2 * v2)))
        angle = np.arccos(angle) / 3.14 * 180
    
        return angle
    
    
    # noinspection PyShadowingNames
    def get_str_guester(up_fingers: object, list_lms: object) -> object:
        if len(up_fingers) == 1 and up_fingers[0] == 8:
    
            v1 = list_lms[6] - list_lms[7]
            v2 = list_lms[8] - list_lms[7]
    
            angle = get_angle(v1, v2)
    
            if angle < 160:
                str_guested = "9"
            else:
                str_guested = "1"
                conn.sendall(b'1')  #此代码用于发送数据给单片机
    
        elif len(up_fingers) == 1 and up_fingers[0] == 4:
            str_guested = "Good"
    
        elif len(up_fingers) == 1 and up_fingers[0] == 20:
            str_guested = "Bad"
    
        elif len(up_fingers) == 1 and up_fingers[0] == 12:
            str_guested = "FXXX"
    
        elif len(up_fingers) == 2 and up_fingers[0] == 8 and up_fingers[1] == 12:
            str_guested = "2"
            
        elif len(up_fingers) == 2 and up_fingers[0] == 4 and up_fingers[1] == 20:
            str_guested = "6"
            
        elif len(up_fingers) == 2 and up_fingers[0] == 4 and up_fingers[1] == 8:
            str_guested = "8"
    
        elif len(up_fingers) == 3 and up_fingers[0] == 8 and up_fingers[1] == 12 and up_fingers[2] == 16:
            str_guested = "3"
            
        elif len(up_fingers) == 3 and up_fingers[0] == 4 and up_fingers[1] == 8 and up_fingers[2] == 12:
    
            dis_8_12 = list_lms[8, :] - list_lms[12, :]
            dis_8_12 = np.sqrt(np.dot(dis_8_12, dis_8_12))
    
            dis_4_12 = list_lms[4, :] - list_lms[12, :]
            dis_4_12 = np.sqrt(np.dot(dis_4_12, dis_4_12))
    
            if dis_4_12 / (dis_8_12 + 1) < 3:
                # noinspection PyShadowingNames
                str_guested = "7"
    
            elif dis_4_12 / (dis_8_12 + 1) > 5:
                str_guested = "Gun"
            else:
                str_guested = "7"
    
        elif len(up_fingers) == 3 and up_fingers[0] == 4 and up_fingers[1] == 8 and up_fingers[2] == 20:
            str_guested = "ROCK"
    
        elif len(up_fingers) == 4 and up_fingers[0] == 8 and up_fingers[1] == 12 and up_fingers[2] == 16 and up_fingers[
            3] == 20:
            str_guested = "4"
            
        elif len(up_fingers) == 5:
            str_guested = "5"
            
        elif len(up_fingers) == 0:
            str_guested = "0"
            
        else:
            str_guested = " "
    
        return str_guested
    
    
    if __name__ == "__main__":
        cap = cv2.VideoCapture(0)
        # 定义手 检测对象
        mpHands = mp.solutions.hands
        hands = mpHands.Hands()
        mpDraw = mp.solutions.drawing_utils
    
        HOST = '192.168.45.44'  # 服务器 IP 地址
        PORT = 8266             # 服务器端口号
        # 创建 TCP 服务器套接字
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        # 将套接字绑定到指定的 IP 地址和端口号
        s.bind((HOST, PORT))
        # 开始监听传入连接
        s.listen()
        print('服务器已启动,等待客户端连接...')
    
         # 接受一个新连接
        conn, addr = s.accept()
        print(f'已连接到客户端:{addr}')
    
        while True:
            # 读取一帧图像
            success, img = cap.read()
            if not success:
                continue
            image_height, image_width, _ = np.shape(img)
    
            # 转换为RGB
            imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    
            # 得到检测结果
            results = hands.process(imgRGB)
    
            if results.multi_hand_landmarks:
                hand = results.multi_hand_landmarks[0]
    
                mpDraw.draw_landmarks(img, hand, mpHands.HAND_CONNECTIONS)
    
                # 采集所有关键点的坐标
                list_lms = []
                for i in range(21):
                    pos_x = hand.landmark[i].x * image_width
                    pos_y = hand.landmark[i].y * image_height
                    list_lms.append([int(pos_x), int(pos_y)])
    
                # 构造凸包点
                list_lms = np.array(list_lms, dtype=np.int32)
                hull_index = [0, 1, 2, 3, 6, 10, 14, 19, 18, 17, 10]
                hull = cv2.convexHull(list_lms[hull_index, :])
                # 绘制凸包
                cv2.polylines(img, [hull], True, (0, 255, 0), 2)
    
                # 查找外部的点数
                n_fig = -1
                ll = [4, 8, 12, 16, 20]
                up_fingers = []
    
                for i in ll:
                    pt = (int(list_lms[i][0]), int(list_lms[i][1]))
                    dist = cv2.pointPolygonTest(hull, pt, True)
                    if dist < 0:
                        up_fingers.append(i)
    
                # print(up_fingers)
                # print(list_lms)
                # print(np.shape(list_lms))
                str_guester = get_str_guester(up_fingers, list_lms)
    
                cv2.putText(img, ' %s' % str_guester, (90, 90), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 255, 0), 4,
                            cv2.LINE_AA)
    
                for i in ll:
                    pos_x = hand.landmark[i].x * image_width
                    pos_y = hand.landmark[i].y * image_height
                    # 画点
                    cv2.circle(img, (int(pos_x), int(pos_y)), 3, (0, 255, 255), -1)
    
            cv2.imshow("hands", img)
    
            key = cv2.waitKey(1) & 0xFF
             
            # 按键 "q" 退出
            if key == ord('q'):
                break
        cap.release()
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月4日
  • 已采纳回答 3月27日
  • 创建了问题 3月26日

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么