9685浏览
查看: 9685|回复: 20

[项目] 行空板——“AI手语翻译器”

[复制链接]
本帖最后由 云天 于 2023-3-13 09:27 编辑

【项目背景】
行空板——“AI手语翻译器”图4
    这是听障人士的日常手语表达之一,意为“最近好吗?”。然而,对于一个从未接受过专业训练的健听人士而言,理解手语几乎是不可能的。
    这种不被理解的窘境,正是听障人士的日常。全国7200万听障人士中,有2700万人需要通过手语进行日常沟通。但他们在公共场所与健听人士交流困难重重,怎么办?
【项目设计】
    AI手语翻译机,听障人士只要面对摄像头做手语,经过后台计算机高速运算,翻译机屏幕就能快速把手语转换成文字,让健听人士秒懂。
    本项目设计使用国产编程软件Mind+,使用开源机器学习TensorFlow框架、Mediapipe框架。
    本项目设计的AI手语翻译机:
    1.Python程序使用OpenCV库调用摄像头,使用Mediapipe识别手部关键点,提取手部关键点运动轨迹,将手语形成图片,如“你好”、“谢谢”、“晚安”、“我要睡觉”等。  
    2.将手语图片上传到“英荔AI训练平台”进行模型训练。
    3.Python程序利用TensorFlow库加载模型进行推理识别摄像头前的手语动作。
【演示视频】
(可信度显示时,数字忘记乘100了
【Mediapipe】
    Mediapipe是google的一个开源项目,可以提供开源的、跨平台的常用机器学习(machine learning)方案。Mediapipe实际上是一个集成的机器学习视觉算法的工具库,包含了人脸检测、人脸关键点、手势识别、头像分割和姿态识别等各种模型。官网地址:https://mediapipe.dev/
行空板——“AI手语翻译器”图1

行空板——“AI手语翻译器”图2
【采集手语图片】
  
    利用手部关键点的Z轴坐标大小调整描绘关键点的圆形大小,表示手部距离摄像头的远近, 将手部关键点运动轨迹形成图片程序。
  1. import cv2
  2. import mediapipe as mp
  3. import time
  4. import numpy as np
  5. mp_drawing = mp.solutions.drawing_utils
  6. mp_hands = mp.solutions.hands
  7. hands = mp_hands.Hands(
  8.         static_image_mode=False,
  9.         max_num_hands=2,
  10.         min_detection_confidence=0.75,
  11.         min_tracking_confidence=0.75)
  12. cap = cv2.VideoCapture(0)
  13. h, w=480,640
  14. r=2
  15. img=np.zeros((480,640,3),np.uint8)
  16. temptime=time.time()
  17. i=0
  18. j=0
  19. while True:
  20.    
  21.     if time.time()-temptime>3:
  22.        j=0
  23.        img=cv2.resize(img,(224,224))
  24.        cv2.imwrite('three/three'+str(i)+'.jpg', img)
  25.        i=i+1
  26.        time.sleep(3)
  27.        temptime=time.time()
  28.        img=np.zeros((480,640,3),np.uint8)
  29.     ret,frame = cap.read()
  30.     #h, w, c = frame.shape
  31.     #print(h,w)
  32.     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  33.     # 因为摄像头是镜像的,所以将摄像头水平翻转
  34.     # 不是镜像的可以不翻转
  35.     frame= cv2.flip(frame,1)
  36.     results = hands.process(frame)
  37.     frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  38.     if results.multi_handedness:
  39.         for hand_label in results.multi_handedness:
  40.           #print(hand_label)
  41.           pass
  42.     if results.multi_hand_landmarks:
  43.       for hand_landmarks in results.multi_hand_landmarks:
  44.               
  45.         for id, lm in enumerate(hand_landmarks.landmark):
  46.               
  47.               px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w)
  48.               #利用Z轴坐标,实现描绘关键点近大远小(离摄像头)
  49.               if pz>100:
  50.                   pz=100
  51.               cv2.circle(img, (px, py), r-int(r*pz/101), (255, 255, 255), cv2.FILLED)
  52.               
  53.         # 关键点可视化
  54.         #mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
  55.     cv2.putText(frame, str(j), (int(w/2), int(h/2)),cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
  56.     cv2.imshow('MediaPipe Hands', frame)
  57.     cv2.imshow('Hands Points', img)
  58.     j=j+1
  59.     if cv2.waitKey(1) & 0xFF == 27:
  60.         break
  61. cap.release()
复制代码
    1、静态手语:“3”
行空板——“AI手语翻译器”图3


    2、动态手语:“你好”


行空板——“AI手语翻译器”图11行空板——“AI手语翻译器”图12



    3、“晚安”
行空板——“AI手语翻译器”图7行空板——“AI手语翻译器”图8



    4、“谢谢”
行空板——“AI手语翻译器”图5行空板——“AI手语翻译器”图6


    5、“我要睡觉”
行空板——“AI手语翻译器”图9行空板——“AI手语翻译器”图10

【模型训练】
    利用AI训练平台进行手语模型训练。英荔AI 训练平台 | Teachable Machine,网址:https://train.aimaker.space/train
行空板——“AI手语翻译器”图13

模型下载

行空板——“AI手语翻译器”图14

【手语推理】
    Python程序利用TensorFlow库加载模型进行推理识别摄像头前的手语动作。
  1. import cv2
  2. import mediapipe as mp
  3. import time
  4. import numpy as np
  5. import tensorflow.keras
  6. mp_drawing = mp.solutions.drawing_utils
  7. mp_hands = mp.solutions.hands
  8. hands = mp_hands.Hands(
  9.         static_image_mode=False,
  10.         max_num_hands=2,
  11.         min_detection_confidence=0.75,
  12.         min_tracking_confidence=0.75)
  13. cap = cv2.VideoCapture(0)
  14. h, w=480,640
  15. r=2
  16. img=np.zeros((480,640,3),np.uint8)
  17. temptime=time.time()
  18. i=0
  19. model = tensorflow.keras.models.load_model('keras_model.h5')
  20. labels=['Back','Bye','Good','Morning','One','Two','Three']
  21. while True:
  22.     if time.time()-temptime>3:
  23.       
  24.        img=cv2.resize(img,(224,224))
  25.        img = np.array(img,dtype=np.float32)
  26.        img = np.expand_dims(img,axis=0)
  27.        img = img/255
  28.        prediction = model.predict(img)
  29.        predicted_class = labels[np.argmax(prediction)]
  30.        print(predicted_class)
  31.        time.sleep(3)
  32.        temptime=time.time()
  33.        img=np.zeros((480,640,3),np.uint8)
  34.     ret,frame = cap.read()
  35.     #h, w, c = frame.shape
  36.     #print(h,w)
  37.     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  38.     # 因为摄像头是镜像的,所以将摄像头水平翻转
  39.     # 不是镜像的可以不翻转
  40.     frame= cv2.flip(frame,1)
  41.     results = hands.process(frame)
  42.     frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  43.     if results.multi_handedness:
  44.         for hand_label in results.multi_handedness:
  45.           #print(hand_label)
  46.           pass
  47.     if results.multi_hand_landmarks:
  48.       for hand_landmarks in results.multi_hand_landmarks:
  49.               
  50.         for id, lm in enumerate(hand_landmarks.landmark):
  51.               
  52.               px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w)
  53.               #利用Z轴坐标,实现描绘关键点近大远小(离摄像头)
  54.               if pz>100:
  55.                   pz=100
  56.               cv2.circle(img, (px, py), r-int(r*pz/101), (255, 255, 255), cv2.FILLED)
  57.               
  58.         # 关键点可视化
  59.         #mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
  60.     cv2.imshow('MediaPipe Hands', frame)
  61.     cv2.imshow('Hands Points', img)
  62.     if cv2.waitKey(1) & 0xFF == 27:
  63.         break
  64. cap.release()
复制代码

【行空板】
  1. import cv2
  2. import mediapipe as mp
  3. import time
  4. import numpy as np
  5. import tensorflow.keras
  6. from unihiker import GUI
  7. u_gui=GUI()
  8. 图片=u_gui.draw_image(image="img.jpg",x=0,y=0)
  9. 提示=u_gui.draw_text(text="手语识别",x=100,y=260,font_size=20, color="#0000FF")
  10. mp_drawing = mp.solutions.drawing_utils
  11. mp_hands = mp.solutions.hands
  12. hands = mp_hands.Hands(
  13.         static_image_mode=False,
  14.         max_num_hands=2,
  15.         min_detection_confidence=0.75,
  16.         min_tracking_confidence=0.75)
  17. cap = cv2.VideoCapture(0)
  18. h, w=480,640
  19. r=2
  20. img=np.zeros((480,640,3),np.uint8)
  21. temptime=time.time()
  22. i=0
  23. model = tensorflow.keras.models.load_model('keras_model.h5')
  24. labels=['back','Good Moning','You Good']
  25. while True:
  26.     if time.time()-temptime>3:
  27.       
  28.       
  29.        img = np.array(img,dtype=np.float32)
  30.        img = np.expand_dims(img,axis=0)
  31.        img = img/255
  32.        prediction = model.predict(img)
  33.        predicted_class = labels[np.argmax(prediction)]
  34.        print(predicted_class)
  35.       
  36.        提示.config(text=predicted_class)
  37.        time.sleep(3)
  38.        temptime=time.time()
  39.        img=np.zeros((480,640,3),np.uint8)
  40.     ret,frame = cap.read()
  41.     #h, w, c = frame.shape
  42.     #print(h,w)
  43.     frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  44.     # 因为摄像头是镜像的,所以将摄像头水平翻转
  45.     # 不是镜像的可以不翻转
  46.     frame= cv2.flip(frame,1)
  47.     results = hands.process(frame)
  48.     frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  49.     if results.multi_handedness:
  50.         for hand_label in results.multi_handedness:
  51.           #print(hand_label)
  52.           pass
  53.     if results.multi_hand_landmarks:
  54.       for hand_landmarks in results.multi_hand_landmarks:
  55.               
  56.         for id, lm in enumerate(hand_landmarks.landmark):
  57.               
  58.               px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w)
  59.               #利用Z轴坐标,实现描绘关键点近大远小(离摄像头)
  60.               if pz>100:
  61.                   pz=100
  62.               cv2.circle(img, (px, py), r-int(r*pz/101), (255, 255, 255), cv2.FILLED)
  63.               
  64.         # 关键点可视化
  65.         #mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
  66.     #cv2.imshow('MediaPipe Hands', frame)
  67.     #cv2.imshow('Hands Points', img)
  68.     img=cv2.resize(img,(224,224))
  69.     图片.config(image="img2.png")
  70.     if cv2.waitKey(1) & 0xFF == 27:
  71.         break
  72. cap.release()
复制代码
LattePanda
使用LattePanda(拿铁熊猫)提升识别速度。LattePanda Delta,Delta 系列采用Intel第8代赛扬N4100处理器作为主要的机器人控制器、交互项目核心、物联网边缘设备或人工智能大脑,在功能和价格上都是完美的选择。所以LattePanda Delta仍然是基于x86的SBC设计。把这个计算机缩小了,从笔记本电脑的大小缩小到手机的大小。因OpenCV2.x的putText是无法处理中文,可将cv2图片转为pil,然后再添加汉字后,再转成cv2格式。


  1. import cv2
  2. import mediapipe as mp
  3. import time
  4. import numpy as np
  5. import tensorflow.keras
  6. from PIL import Image,ImageDraw,ImageFont
  7. mp_drawing = mp.solutions.drawing_utils
  8. mp_hands = mp.solutions.hands
  9. hands = mp_hands.Hands(
  10.         static_image_mode=False,
  11.         max_num_hands=2,
  12.         min_detection_confidence=0.75,
  13.         min_tracking_confidence=0.75)
  14. cap = cv2.VideoCapture(0)
  15. h, w=480,640
  16. r=2
  17. img=np.zeros((480,640,3),np.uint8)
  18. temptime=time.time()
  19. i=0
  20. model = tensorflow.keras.models.load_model('keras_model.h5')
  21. labels=['未识别到内容','你好','晚安','我要睡觉','谢谢']
  22. predicted_class="未识别到内容"
  23. prediction_max=0
  24. while True:
  25.     if time.time()-temptime>3:
  26.        h, w, c = img.shape
  27.        temimg=cv2.resize(img,(224,224))
  28.        temimg = np.array(temimg,dtype=np.float32)
  29.        temimg = np.expand_dims(temimg,axis=0)
  30.        temimg = temimg/255
  31.        prediction = model.predict(temimg)
  32.        predicted_class = labels[np.argmax(prediction)]
  33.        prediction_max=np.max(prediction)
  34.       
  35.       
  36.        time.sleep(3)
  37.        temptime=time.time()
  38.        img=np.zeros((480,640,3),np.uint8)
  39.     ret,frame = cap.read()
  40.     #h, w, c = frame.shape
  41.     #print(h,w)
  42.     if ret:
  43.       frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  44.       # 因为摄像头是镜像的,所以将摄像头水平翻转
  45.       # 不是镜像的可以不翻转
  46.       frame= cv2.flip(frame,1)
  47.       results = hands.process(frame)
  48.       frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  49.       if results.multi_handedness:
  50.           for hand_label in results.multi_handedness:
  51.             #print(hand_label)
  52.             pass
  53.       if results.multi_hand_landmarks:
  54.         for hand_landmarks in results.multi_hand_landmarks:
  55.               
  56.           for id, lm in enumerate(hand_landmarks.landmark):
  57.               
  58.                 px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w)
  59.                 #利用Z轴坐标,实现描绘关键点近大远小(离摄像头)
  60.                 if pz>100:
  61.                     pz=100
  62.                 cv2.circle(img, (px, py), r-int(r*pz/101), (255, 255, 255), cv2.FILLED)
  63.               
  64.         # 关键点可视化
  65.         #mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
  66.     #cv2.imshow('MediaPipe Hands', frame)
  67.     cv2.imshow('cap', img)
  68.       
  69.     #显示中文
  70.     pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
  71.     draw = ImageDraw.Draw(pil_img)
  72.     # 第一个参数是字体文件的路径,第二个是字体大小
  73.     font = ImageFont.truetype('Alibaba-PuHuiTi-Medium.ttf',30,encoding='utf-8')
  74.     # 第一个参数是文字的起始坐标,第二个需要输出的文字,第三个是字体颜色,第四个是字体类型
  75.     if prediction_max>0.8:
  76.         draw.text((240,420),predicted_class+str(round(prediction_max,2))+"%",(0,255,255),font=font)
  77.     else:
  78.         draw.text((240,420),"未识别到内容",(0,255,255),font=font)
  79.     # PIL图片转cv2
  80.     frame = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
  81.     cv2.imshow('Hands Points', frame)
  82.     if cv2.waitKey(1) & 0xFF == 27:
  83.         break
  84. cap.release()
复制代码

【中文播报识别结果】

    pyttsx3是一个文本朗读库,通过调用此库,很容易就可以让程序“开口说话”。
  1. #导入pyttsx3库
  2. import pyttsx3
  3. engine = pyttsx3.init() #创建engine并初始化
  4. engine.say('有志者,事竟成。') #开始朗读
  5. engine.runAndWait() #等待语音播报完毕
复制代码

    完整程序
  1. import cv2
  2. import mediapipe as mp
  3. import time
  4. import numpy as np
  5. import tensorflow.keras
  6. from PIL import Image,ImageDraw,ImageFont
  7. import pyttsx3
  8. engine = pyttsx3.init() #创建engine并初始化
  9. mp_drawing = mp.solutions.drawing_utils
  10. mp_hands = mp.solutions.hands
  11. hands = mp_hands.Hands(
  12.         static_image_mode=False,
  13.         max_num_hands=2,
  14.         min_detection_confidence=0.75,
  15.         min_tracking_confidence=0.75)
  16. cap = cv2.VideoCapture(0)
  17. h, w=480,640
  18. r=2
  19. img=np.zeros((480,640,3),np.uint8)
  20. temptime=time.time()
  21. i=0
  22. model = tensorflow.keras.models.load_model('keras_model.h5')
  23. labels=['未识别到内容','你好','晚安','我要睡觉','谢谢']
  24. predicted_class="未识别到内容"
  25. prediction_max=0
  26. while True:
  27.     if time.time()-temptime>3:
  28.        h, w, c = img.shape
  29.        temimg=cv2.resize(img,(224,224))
  30.        temimg = np.array(temimg,dtype=np.float32)
  31.        temimg = np.expand_dims(temimg,axis=0)
  32.        temimg = temimg/255
  33.        prediction = model.predict(temimg)
  34.        predicted_class = labels[np.argmax(prediction)]
  35.        prediction_max=np.max(prediction)
  36.        if prediction_max>0.8:
  37.            engine.say(predicted_class) #开始朗读
  38.        else:
  39.            engine.say('未识别到内容') #开始朗读
  40.        engine.runAndWait() #等待语音播报完毕
  41.       
  42.        time.sleep(3)
  43.        temptime=time.time()
  44.        img=np.zeros((480,640,3),np.uint8)
  45.     ret,frame = cap.read()
  46.     #h, w, c = frame.shape
  47.     #print(h,w)
  48.     if ret:
  49.       frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  50.       # 因为摄像头是镜像的,所以将摄像头水平翻转
  51.       # 不是镜像的可以不翻转
  52.       frame= cv2.flip(frame,1)
  53.       results = hands.process(frame)
  54.       frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  55.       if results.multi_handedness:
  56.           for hand_label in results.multi_handedness:
  57.             #print(hand_label)
  58.             pass
  59.       if results.multi_hand_landmarks:
  60.         for hand_landmarks in results.multi_hand_landmarks:
  61.               
  62.           for id, lm in enumerate(hand_landmarks.landmark):
  63.               
  64.                 px, py, pz = int(lm.x * w), int(lm.y * h), int(lm.z * w)
  65.                 #利用Z轴坐标,实现描绘关键点近大远小(离摄像头)
  66.                 if pz>100:
  67.                     pz=100
  68.                 cv2.circle(img, (px, py), r-int(r*pz/101), (255, 255, 255), cv2.FILLED)
  69.               
  70.         # 关键点可视化
  71.         #mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
  72.     #cv2.imshow('MediaPipe Hands', frame)
  73.     cv2.imshow('cap', img)
  74.       
  75.     #显示中文
  76.     pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
  77.     draw = ImageDraw.Draw(pil_img)
  78.     # 第一个参数是字体文件的路径,第二个是字体大小
  79.     font = ImageFont.truetype('Alibaba-PuHuiTi-Medium.ttf',30,encoding='utf-8')
  80.     # 第一个参数是文字的起始坐标,第二个需要输出的文字,第三个是字体颜色,第四个是字体类型
  81.     if prediction_max>0.8:
  82.         draw.text((240,420),predicted_class+str(round(prediction_max,2))+"%",(0,255,255),font=font)
  83.     else:
  84.         draw.text((240,420),"未识别到内容",(0,255,255),font=font)
  85.     # PIL图片转cv2
  86.     frame = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
  87.     cv2.imshow('Hands Points', frame)
  88.     if cv2.waitKey(1) & 0xFF == 27:
  89.         break
  90. cap.release()
复制代码


    解决pyttsx3不发声问题:为pyttsx3调用的本地语音包实现文本转音频, 首先确认环境中是否有微软语音包。
  1. import pyttsx3
  2. tts = pyttsx3.init()
  3. voices = tts.getProperty('voices')
  4. for voice in voices:
  5.     print('id = {} \n name = {} \n'.format(voice.id, voice.name))
复制代码

    如果结果有输出, 说明存在语音包.

    如果结果没有任何输出, 则需要下载Microsoft Speech Platform - Runtime Languages中的MSSpeech_TTS_zh-CN_HuiHui.msi 并安装。



木子呢  管理员

发表于 2023-2-15 11:09:22

大神!!!!!!
回复

使用道具 举报

腿毛利小五郎  高级技师

发表于 2023-2-17 14:02:33

牛蛙!!
回复

使用道具 举报

gray6666  初级技神

发表于 2023-2-18 17:52:42

很棒,赞一个
回复

使用道具 举报

aYYSW8AepLLd  高级技师

发表于 2023-2-18 19:38:12

厉害厉害不错不错
回复

使用道具 举报

怀若谷  高级技匠

发表于 2023-2-28 12:19:22

云天老师出品,必属精品
回复

使用道具 举报

志中  见习技师

发表于 2023-3-5 22:13:39

是真的6
回复

使用道具 举报

Mr-k  初级技匠

发表于 2023-3-6 20:47:01

厉害厉害
回复

使用道具 举报

onlyone  学徒

发表于 2023-3-9 10:41:55

如果数据集更多就好啦
回复

使用道具 举报

云天  初级技神
 楼主|

发表于 2023-3-13 09:25:26

onlyone 发表于 2023-3-9 10:41
如果数据集更多就好啦

说得对,这是一项大工程
回复

使用道具 举报

kkkw  学徒

发表于 2023-3-16 15:18:49

你好请问没有名称为 'keras' 的模块 是什么原因
回复

使用道具 举报

三春牛-创客  初级技神

发表于 2023-3-28 16:44:38

厉害厉害
回复

使用道具 举报

三春牛-创客  初级技神

发表于 2023-3-28 16:46:07

赞!这个挺不错的
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-3-28 16:47:35

厉害厉害
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-3-28 16:48:52

赞!!!
回复

使用道具 举报

irismin  学徒

发表于 2023-4-7 21:58:27

你好,云天老师,看了你的AI手语翻译器,很厉害,想学习,但不是很懂python编程,不知道这个可以用模块编程吗?
回复

使用道具 举报

111damn  学徒

发表于 2023-5-10 10:12:49

老师你好,请问可以分享一下权重文件么,单纯就是想看下效果,不会有其他多余用途的
回复

使用道具 举报

派大星ym  初级技匠

发表于 2023-8-22 00:05:10

厉害厉害了
回复

使用道具 举报

派大星ym  初级技匠

发表于 2023-8-22 00:07:26

6666666666
回复

使用道具 举报

_深蓝_  中级技师

发表于 2023-9-21 18:13:51

云天老师出品,必属精品!必属精品!
回复

使用道具 举报

feng0539  初级技匠

发表于 2023-9-22 07:12:53

神仙啊,惊艳之作
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail