234浏览
查看: 234|回复: 0

【行空板】视频车

[复制链接]
SunMay-202205223051..png SunMay-202205228988..png SunMay-202205225081..png
【行空板】
行空板有一个USB TYPE-A接口用于外接USB外设,可用于连接摄像头,在CV2进行调用,通过socket将视频传给电脑。

SunMay-202205228572..png
SunMay-202205226011..png

SunMay-202205221186..png

【行空板服务端】

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-

  3. import socket
  4. import numpy as np
  5. import urllib
  6. import cv2 as cv
  7. import threading
  8. import time


  9. print('this is Server')

  10. cap = cv.VideoCapture(0)
  11. cap.set(cv.CAP_PROP_FRAME_WIDTH, 320)
  12. cap.set(cv.CAP_PROP_FRAME_HEIGHT, 240)

  13. def socket_service():
  14.     try:
  15.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16.         # 防止socket server重启后端口被占用(socket.error: [Errno 98] Address already in use)
  17.         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  18.         #s.bind(('127.0.0.1', 6666))
  19.         s.bind(('192.168.31.25', 6666))#这个是服务端机器的ip
  20.         s.listen(10)
  21.     except socket.error as msg:
  22.         print (msg)
  23.         sys.exit(1)
  24.     print ('Waiting connection...')

  25.     while True:
  26.         conn, addr = s.accept()
  27.         t = threading.Thread(target=deal_data, args=(conn, addr))
  28.         t.start()

  29. def deal_data(conn, addr):
  30.     print ('Accept new connection from {0}'.format(addr))
  31.     while True:
  32.         # get a frame
  33.         ret, frame = cap.read()
  34.         # '.jpg'表示把当前图片img按照jpg格式编码,按照不同格式编码的结果不一样
  35.         img_encode = cv.imencode('.jpg', frame)[1]
  36.         data_encode = np.array(img_encode)
  37.         str_encode = data_encode.tostring()
  38.         encode_len = str(len(str_encode))
  39.         print('img size : %s'%encode_len)
  40.         try:
  41.             conn.send(str_encode)#发送图片的encode码
  42.         except Exception as e:
  43.             print(e)
  44.         time.sleep(0.1)
  45.     conn.close()

  46. socket_service()

复制代码
【电脑接收端程序】

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-

  3. import socket
  4. import numpy as np
  5. import urllib
  6. import cv2
  7. import threading
  8. import time,sys

  9. def socket_client():
  10.     try:
  11.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12.         s.connect(('192.168.31.25', 6666))#连接服务端
  13.     except socket.error as msg:
  14.         print (msg)
  15.         #sys.exit(1)
  16.     #c_sock, c_addr =s.accept()
  17.     print('this is Client')
  18.     while True:
  19.         try:
  20.             receive_encode = s.recv(77777)#接收的字节数 最大值 2147483647 (31位的二进制)
  21.             nparr = np.fromstring(receive_encode, dtype='uint8')
  22.             img_decode = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  23.          
  24.             #产生标签文本背景透明效果

  25.             cv2.imshow("Client_show", img_decode)

  26.             cv2.waitKey(1)
  27.             
  28.         except Exception as e:
  29.             print(e)
  30.         

  31. socket_client()

复制代码

【物联网——行空板】


  1. # -*- coding: utf-8 -*-
  2. import time

  3. import numpy as np
  4. from pinpong.board import Board
  5. from microbit_motor import Microbit_Motor #导入Microbit_Motor库
  6. import siot
  7. from pinpong.extension.unihiker import *

  8. from unihiker import GUI

  9. import socket

  10. import urllib
  11. import cv2 as cv
  12. import threading



  13. print('this is Server')

  14. cap = cv.VideoCapture(0)
  15. cap.set(cv.CAP_PROP_FRAME_WIDTH, 320)
  16. cap.set(cv.CAP_PROP_FRAME_HEIGHT, 240)
  17. u_gui=GUI()
  18. xianshi=u_gui.draw_text(text="开始",x=50,y=100,font_size=50, color="#0000FF")
  19. # 事件回调函数
  20. def on_message_callback(client, userdata, msg):
  21.   
  22.   if (msg.payload.decode("utf-8")  == 'G'):
  23.       forward(200)
  24.       xianshi.config(text="前进")
  25.   if (msg.payload.decode("utf-8")== 'S'):
  26.       stop()
  27.       xianshi.config(text="停止")   
  28.       
  29.   if (msg.payload.decode("utf-8") == 'B'):
  30.       back(200)
  31.       xianshi.config(text="后退")  
  32.       
  33.   if (msg.payload.decode("utf-8") == 'L'):
  34.       left(200)
  35.       xianshi.config(text="向左")  
  36.       
  37.   if (msg.payload.decode("utf-8") == 'R'):
  38.       right(200)
  39.       xianshi.config(text="向右")
  40.       

  41. siot.init(client_id="siot_192",server="192.168.31.25",port=1883,user="siot",password="dfrobot")
  42. siot.connect()
  43. siot.loop()
  44. siot.set_callback(on_message_callback)
  45. siot.getsubscribe(topic="car/control")

  46. Board("microbit").begin()
  47. motorbit = Microbit_Motor()
  48. def forward(speed):
  49.   #前进
  50.   #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
  51.   if speed>255:
  52.       speed=255
  53.   motorbit.motor_run(motorbit.M1, motorbit.CW, speed)
  54.   motorbit.motor_run(motorbit.M2, motorbit.CW, speed)
  55.   motorbit.motor_run(motorbit.M3, motorbit.CCW, speed)
  56.   motorbit.motor_run(motorbit.M4, motorbit.CCW, speed)
  57. def back(speed):
  58.   #后退
  59.   #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
  60.   if speed>255:
  61.       speed=255
  62.   motorbit.motor_run(motorbit.M1, motorbit.CCW, speed)
  63.   motorbit.motor_run(motorbit.M2, motorbit.CCW, speed)
  64.   motorbit.motor_run(motorbit.M3, motorbit.CW, speed)
  65.   motorbit.motor_run(motorbit.M4, motorbit.CW, speed)
  66. def left_turn(speed):
  67.   #向左转
  68.   #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
  69.   if speed>255:
  70.       speed=255
  71.   motorbit.motor_run(motorbit.M1, motorbit.CW, speed)
  72.   motorbit.motor_run(motorbit.M2, motorbit.CW, speed)
  73.   motorbit.motor_run(motorbit.M3, motorbit.CW, speed)
  74.   motorbit.motor_run(motorbit.M4, motorbit.CW, speed)
  75. def right_turn(speed):
  76.   #向右转
  77.   #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
  78.   if speed>255:
  79.       speed=255
  80.   motorbit.motor_run(motorbit.M1, motorbit.CCW, speed)
  81.   motorbit.motor_run(motorbit.M2, motorbit.CCW, speed)
  82.   motorbit.motor_run(motorbit.M3, motorbit.CCW, speed)
  83.   motorbit.motor_run(motorbit.M4, motorbit.CCW, speed)
  84. def left(speed):
  85.   #向左
  86.   #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
  87.   if speed>255:
  88.       speed=255
  89.   motorbit.motor_run(motorbit.M1, motorbit.CW, speed)
  90.   motorbit.motor_run(motorbit.M2, motorbit.CCW, speed)
  91.   motorbit.motor_run(motorbit.M3, motorbit.CW, speed)
  92.   motorbit.motor_run(motorbit.M4, motorbit.CCW, speed)
  93. def right(speed):
  94.   #向右
  95.   #电机有M1,M2,M3,M4, CW代表正转,CCW代表反转,255是速度,范围0-255
  96.   if speed>255:
  97.       speed=255
  98.   motorbit.motor_run(motorbit.M1, motorbit.CCW, speed)
  99.   motorbit.motor_run(motorbit.M2, motorbit.CW, speed)
  100.   motorbit.motor_run(motorbit.M3, motorbit.CCW, speed)
  101.   motorbit.motor_run(motorbit.M4, motorbit.CW, speed)
  102. def stop():
  103.   motorbit.motor_stop(motorbit.M1)
  104.   motorbit.motor_stop(motorbit.M2)
  105.   motorbit.motor_stop(motorbit.M3)
  106.   motorbit.motor_stop(motorbit.M4)
  107. def socket_service():
  108.     try:
  109.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  110.         # 防止socket server重启后端口被占用(socket.error: [Errno 98] Address already in use)
  111.         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  112.         #s.bind(('127.0.0.1', 6666))
  113.         s.bind(('192.168.31.25', 6666))#这个是服务端机器的ip
  114.         s.listen(10)
  115.     except socket.error as msg:
  116.         print (msg)
  117.         sys.exit(1)
  118.     print ('Waiting connection...')

  119.     while True:
  120.         conn, addr = s.accept()
  121.         t = threading.Thread(target=deal_data, args=(conn, addr))
  122.         t.start()

  123. def deal_data(conn, addr):
  124.     print ('Accept new connection from {0}'.format(addr))
  125.     while True:
  126.         # get a frame
  127.         ret, frame = cap.read()
  128.         # '.jpg'表示把当前图片img按照jpg格式编码,按照不同格式编码的结果不一样
  129.         img_encode = cv.imencode('.jpg', frame)[1]
  130.         data_encode = np.array(img_encode)
  131.         str_encode = data_encode.tostring()
  132.         encode_len = str(len(str_encode))
  133.         print('img size : %s'%encode_len)
  134.         try:
  135.             conn.send(str_encode)#发送图片的encode码
  136.         except Exception as e:
  137.             print(e)
  138.         time.sleep(0.1)
  139.     conn.close()
  140. def trace_fun():

  141.     while True:
  142.       socket_service()
  143.            


  144. if __name__ == '__main__':
  145.     trace_fun()
复制代码
【物联网——电脑端】

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-

  3. import socket
  4. import numpy as np
  5. import urllib
  6. import cv2
  7. import threading
  8. import time,sys
  9. import tkinter as tk
  10. from tkinter import *
  11. from PIL import Image, ImageTk
  12. import siot
  13. siot.init(client_id="siot_192",server="192.168.31.25",port=1883,user="siot",password="dfrobot")
  14. siot.connect()
  15. siot.loop()

  16. #生成窗体,布置相应文本框和按钮
  17. def forward():
  18.     siot.publish('car/control', 'G')
  19. def left():
  20.     siot.publish('car/control', 'L')
  21. def right():
  22.     siot.publish('car/control', 'R')
  23. def stop():
  24.     siot.publish('car/control', 'S')
  25. def back():
  26.     siot.publish('car/control', 'B')
  27. top = tk.Tk()
  28. top.title('控制窗口')
  29. top.geometry('420x300')
  30. image_width = 425
  31. image_height = 300

  32. canvas = Canvas(top,bg = 'white',width = image_width,height = image_height )#绘制画布
  33. canvas.pack()
  34. img = Image.open('back.jpg')
  35. bg = ImageTk.PhotoImage(img)
  36. bgid = canvas.create_image(0, 0, image=bg, anchor='nw')
  37. canvas.place(x = 0,y = 0)
  38. #产生标签文本背景透明效果
  39. txtid=canvas.create_text(5,20, fill = 'red',font=("黑体", 15),anchor="nw")
  40. canvas.insert(txtid,1,"行空板四驱车")
  41. wx=5
  42. hy=20

  43. #生成按钮,并指定相应功能
  44. left = tk.Button(top,text='左转',height=2,width=15,command=left)
  45. left.place(x=wx,y=hy+100)
  46. forward = tk.Button(top,text='前进',height=2,width=15,command=forward)
  47. forward.place(x=wx+150,y=hy)
  48. right = tk.Button(top,text='右转',height=2,width=15,command=right)
  49. right.place(x=wx+300,y=hy+100)
  50. back = tk.Button(top,text='后退',height=2,width=15,command=back)
  51. back.place(x=wx+150,y=hy+200)
  52. stop = tk.Button(top,text='停止',height=2,width=15,command=stop)
  53. stop.place(x=wx+150,y=hy+100)
  54. def socket_client():
  55.     try:
  56.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  57.         s.connect(('192.168.31.25', 6666))#连接服务端
  58.     except socket.error as msg:
  59.         print (msg)
  60.         #sys.exit(1)
  61.     #c_sock, c_addr =s.accept()
  62.     print('this is Client')
  63.     while True:
  64.         try:
  65.             receive_encode = s.recv(77777)#接收的字节数 最大值 2147483647 (31位的二进制)
  66.             nparr = np.fromstring(receive_encode, dtype='uint8')
  67.             img_decode = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
  68.          
  69.             #产生标签文本背景透明效果

  70.             cv2.imshow("Client_show", img_decode)

  71.             cv2.waitKey(1)
  72.             top.update()
  73.             top.after(100)
  74.         except Exception as e:
  75.             print(e)
  76.         

  77. socket_client()

复制代码
SunMay-202205224177..png

【演示视频】



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

本版积分规则

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

硬件清单

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

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

mail