最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊

pyserial串口讀取esp32-cam攝像頭

2020-08-26 13:28 作者:學(xué)的很雜的一個(gè)人  | 我要投稿

講解過程視頻訪問網(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板上的重啟重啟鍵即可看到輸出。



pyserial串口讀取esp32-cam攝像頭的評(píng)論 (共 條)

分享到微博請遵守國家法律
余干县| 枣强县| 抚宁县| 玛曲县| 方城县| 安阳市| 兰州市| 沁水县| 屏南县| 惠来县| 衢州市| 固镇县| 兰考县| 霍林郭勒市| 宣汉县| 华安县| 满洲里市| 奇台县| 綦江县| 铜陵市| 金湖县| 朝阳县| 眉山市| 陈巴尔虎旗| 台东市| 古蔺县| 绥化市| 浑源县| 太康县| 南阳市| 济源市| 登封市| 巴楚县| 辉南县| 湄潭县| 台东县| 芒康县| 哈巴河县| 金坛市| 乌鲁木齐县| 常熟市|