pyserial串口讀取esp32-cam攝像頭
講解過程視頻訪問網(wǎng)址:https://www.bilibili.com/video/BV13A411n78H/
esp32-cam的引腳連接表:

直接上代碼:
1、esp32-cam(arduino下位機(jī)代碼)
#////////////////////////////////////////////////////////////////////////////////////////////
#include "esp_camera.h"
void setup() {
Serial.begin(115200);//傳口速率
Serial.println();//僅輸出一個(gè)回車和換行符
camera_config_t config1;
//config1.ledc_channel = 4;
config1.pin_d0 = 5;
config1.pin_d1 = 18;
config1.pin_d2 = 19;
config1.pin_d3 = 21;
config1.pin_d4 = 36;
config1.pin_d5 = 39;
config1.pin_d6 = 34;
config1.pin_d7 = 35;
config1.pin_xclk = 0;
config1.pin_pclk = 22;
config1.pin_vsync = 25;
config1.pin_href = 23;
config1.pin_sscb_sda = 26;
config1.pin_sscb_scl = 27;
config1.pin_pwdn = 32;
config1.pin_reset = 15;
config1.xclk_freq_hz = 20000000;
config1.pixel_format = PIXFORMAT_JPEG;
?
// if PSRAM IC present, init with UXGA resolution and higher JPEG quality
//????????????????????? for larger pre-allocated frame buffer.
if(psramFound()){
? config1.frame_size = FRAMESIZE_UXGA;
? config1.jpeg_quality = 10;
? config1.fb_count = 2;
} else {
? config1.frame_size = FRAMESIZE_SVGA;
? config1.jpeg_quality = 12;
? config1.fb_count = 1;
}
// camera init
esp_err_t err = esp_camera_init(&config1);//攝像頭初始化
if (err != ESP_OK) {//攝像頭初始化失敗,則打印消息并終止程序
? Serial.printf("Camera init failed with error 0x%x", err);
? return;
}
?? Serial.printf("camera is runing\n");
sensor_t *s = esp_camera_sensor_get();//獲取調(diào)整圖像接口
s->set_framesize(s, FRAMESIZE_VGA);//更改幀尺寸
//s->set_quality(s, 10);//設(shè)置品質(zhì)10
//acquire a frame獲取圖像,查看是否改變
camera_fb_t * fb = esp_camera_fb_get();
if (!fb)
{
??? Serial.print( "Camera capture failed");
}
else
{
??? Serial.printf("width:%d, height:%d, format:%d, len:%d\r\n", fb->width, fb->height, fb->format, fb->len);//輸出width:640, height:480, format:3, len:30805;尺寸小了,但len怎么更長了?
??? delay(500);
??? Serial.write(fb->buf, fb->len);//輸出圖像數(shù)據(jù)
}
}
void loop() {
}
2、上位機(jī)python代碼:
#////////////////////////////////////////////////////////////////////////////////////////////
#串口圖片通訊
#usr/bin/python3
# -*- coding: utf-8 -*-
import serial #python自帶串行通訊模塊
import matplotlib.pyplot as plt # plt 用于顯示圖片
import numpy as np
import CV2 #opencv
from time import sleep
ser = serial.Serial('com3', 115200, timeout=0.1) #設(shè)置串口名稱,波特率,超時(shí)時(shí)間
?
?
def recv(serial):
??????? while True:
??????????????? data = serial.read(1024)#設(shè)置1024緩沖區(qū)
??????????????? if data == '':
??????????????????????? continue
??????????????? else:
??????????????????????? break
??????????????? sleep(0.02)
??????? return data
img = b'' #字節(jié)連接,定義全局字節(jié)變量以備使用
recev = 0 #接收標(biāo)志位
while True:
??????? data = recv(ser)
#???????? print(len(data))#圖片數(shù)據(jù)按1024分段接收
#???????? print(type(data))#output:<class 'bytes'>
??????? if data:
??????????? try:
??????????????? print(data.decode())
??????????? except:
??????????????? if (b'\xff\xd8' in data) or (recev):#jpg圖片開始字節(jié)為b'\xff\xd8',若存在則開始接受數(shù)據(jù),接下來用標(biāo)志位保持接收
??????????????????? recev = 1 #接收標(biāo)志位置1
??????????????????? img += data #圖片接收后分段連接
??????????????????? print('開始接收圖片:',len(img))#圖片數(shù)據(jù)按1024分段接收
??????????????????? #sleep(0.02)#不加延時(shí)下面的判斷會(huì)出錯(cuò)
??????????????????? if b'\xff\xd9' in data: #jpg圖片開始字節(jié)為b'\xff\xd9,若存在則停止接受數(shù)據(jù)
??????????????????????? print('接收圖片完成:',len(img))#圖片數(shù)據(jù)按1024分段接收
??????????????????????? nparr = np.frombuffer(img, np.uint8)#圖片字節(jié)數(shù)據(jù)轉(zhuǎn)換np.uint8
??????????????????????? img_np = CV2.imdecode(nparr, CV2.IMREAD_COLOR) #利用opencv轉(zhuǎn)換成圖片格式
??????????????????????? print(np.shape(img_np))
??????????????????????? plt.imshow(img_np[:,:,::-1]) # 顯示圖片,opencv的bgr轉(zhuǎn)換用[:,:,::-1]操作即可
??????????????????????? #plt.axis('off') # 不顯示坐標(biāo)軸
??????????????????????? plt.show()
??????????????????????? img = b'' #完成圖片顯示,清空圖片緩沖區(qū)內(nèi)存
??????????????????????? recev = 0 #接收標(biāo)志位置0
#////////////////////////////////////////////////////////////////////////////////////////////
3、使用方法:
燒寫esp32-cam代碼完成后,運(yùn)行上位機(jī)python代碼,按esp32-cam板上的重啟重啟鍵即可看到輸出。
