本篇介绍ESP8266在OLED上播放视频:
ESP8266作为socket客户端,接收服务端发送的图像帧数据,进行显示。
ESP8266使用Arduino IDE环境进行开发。
#define IP "192.168.5.100"
#define PORT 8080
void initWiFi()
{
Serial.print("Connecting WiFi...");
WiFi.mode(WIFI_STA); //配置WIFI为Station模式
WiFi.begin(ssid, pass); //传入WIFI热点的ssid和密码
while (WiFi.status() != WL_CONNECTED) //等待连接成功
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //打印自己的IP地址
client.connect(IP, PORT);
}
uint8_t testb[1024] = {};
void loop()
{
//Serial.println("hello esp8266");
delay(10);
while (client.available())
{
delay(12);
client.read(testb, 1024);
u8g2.firstPage();
do {
u8g2.drawXBM(0, 0, 128, 64, testb);
} while (u8g2.nextPage());
}
}
电脑端作为socket服务器端,将视频帧数据发送出去。
电脑端采用Python3编程,借助OpenCV实现视频的读取与图像数据格式转换。
程序的基本思想是:
video_path="badapple_320240_xvid.mp4"
def PlayVideo(video_path, client):
endian = 'L'
color_reverse = 'false'
c = 0#累计帧数
timeF = 8#隔x帧截一次图
video = cv2.VideoCapture(video_path) #打开视频
player = MediaPlayer(video_path) #打开音频
while True:
grabbed, frame= video.read()
audio_frame, val = player.get_frame()
if not grabbed:
print("End of video")
break
if cv2.waitKey(28) & 0xFF == ord("q"):
break
cv2.imshow("Video", frame)
if val != 'eof' and audio_frame is not None:
img, t = audio_frame
if (c % timeF == 0): # 每隔timeF帧进行存储操作
frame = cv2.resize(frame,(128,64))#调整尺寸
frame = binary_image(frame)#二值化
matrix = img_to_matrix(frame, endian, color_reverse)
data = bytes(matrix)
client.send(data)
c = c + 1
#time.sleep(0.2)
#cv2.waitKey(1)
video.release()
cv2.destroyAllWindows()
###############
client = 0
client = socket_start()
PlayVideo(video_path, client)
利用OpenCV,将图像转为黑白图像:
def binary_image(image):#将图像处理为二值化的程序
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) #把输入图像灰度化
h, w =gray.shape[:2]
m = np.reshape(gray, [1,w*h])
mean = m.sum()/(w*h)
ret, binary = cv2.threshold(gray, mean, 255, cv2.THRESH_OTSU)
return binary
然后再转为数组:
def img_to_matrix(frame, endian, color_reverse):
width = frame.shape[1] #128
height = frame.shape[0] #64
if endian == 'B':
byte_reverse = True
else:
byte_reverse = False
if color_reverse == 'true':
color_reverse = True
else:
color_reverse = False
unalign = 0
matrix = list()
if (width%8) != 0:
unalign = 1
for i in range(0, height): #64
for j in range(0, (width//8)+unalign): #128/8=16
v = 0x00
rs = 8*j
re = 8*(j+1)
if re > width:
re = width
for k in range(rs, re):
if frame[i, k] != 0:
if not byte_reverse:
v |= (0x01 << (k%8))
else:
v |= (0x01 << (7-(k%8)))
if color_reverse:
v ^= 0xff
matrix.append(v)
return matrix
def socket_start():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 8080
host = '192.168.5.100'
print(host)
print(port)
s.bind((host,port))
s.listen(5)
print('等待客户端连接中…')
client,client_address = s.accept()
print('新连接')
client_IP = str(client_address[0])
print('IP:'+client_IP)
client_port = str(client_address[1])
print('Port:' + client_port)
return client
http://mpvideo.qpic.cn/0bc3mmabsaaaouap7euryjrfay6ddfrqagia.f10002.mp4?
本篇介绍ESP8266在OLED上播放视频,包括ESP8266端的图像接收与显示程序,与电脑端的Python读取视频并进行编码与数据发送程序。