STM32连接OV2640 视频源接入到PyQt5界面中
作为AI模型的输入,视频流是非常重要的。在本文中,我们将介绍如何将STM32连接到OV2640摄像头,并将视频流接入到PyQt5界面中。
硬件连接
首先,我们需要将OV2640摄像头连接到STM32上。我们可以使用以下连接方式:
OV2640 | STM32 ------|------ SIOC | PB8 SIOD | PB9 XCLK | PA8 PCLK | PA6 VSYNC | PA4 HREF | PA7 D0 | PC0 D1 | PC1 D2 | PC2 D3 | PC3 D4 | PC4 D5 | PB6 D6 | PB7 D7 | PA9
STM32代码
接下来,我们需要编写STM32代码来控制OV2640摄像头并将视频流传输到PC。我们可以使用以下代码:
#include “stm32f4xx.h”
#include “stm32f4xx_gpio.h”
#include “stm32f4xx_rcc.h”
#include “stm32f4xx_dma.h”
#include “stm32f4xx_dcmi.h”
#define BUFFER_SIZE 3202402
uint8_t buffer[BUFFER_SIZE];
void DCMI_Config(void)
{
DCMI_InitTypeDef DCMI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_DCMI);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_Continuous;
DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Falling;
DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High;
DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_Low;
DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame;
DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b;
DCMI_Init(&DCMI_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&DCMI->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
DCMI_Cmd(ENABLE);
DMA_Cmd(DMA2_Stream1, ENABLE);
DCMI_CaptureCmd(ENABLE);
}
int main(void)
{
DCMI_Config();
while (1)
{
}
}
PyQt5代码
最后,我们需要编写PyQt5代码来接收STM32传输的视频流并将其显示在界面上。我们可以使用以下代码:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
import serial
ser = serial.Serial(‘COM3’, 115200)
class App(QWidget):
def init(self):
super().init()
self.title = ‘STM32 Video Stream’
self.left = 100
self.top = 100
self.width = 320
self.height = 240
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.label = QLabel(self)
self.label.setGeometry(0, 0, self.width, self.height)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(1)
self.show()
def update_frame(self):
if ser.in_waiting > 0:
data = ser.read(320*240*2)
img = QImage(data, 320, 240, QImage.Format_RGB16)
pixmap = QPixmap.fromImage(img)
self.label.setPixmap(pixmap)
if name == ‘main’:
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
在这个例子中,我们使用了PyQt5的QLabel控件来显示视频流。我们还使用了一个定时器来定期更新视频流。最后,我们使用串口连接STM32并接收视频流。
总结文章来源:https://www.toymoban.com/news/detail-460413.html
在本文中,我们介绍了如何将STM32连接到OV2640摄像头,并将视频流接入到PyQt5界面中。这个例子可以帮助你了解如何使用STM32控制外设,并将数据传输到PC。
如果使用MQTT的方式进行视频流的传输
如果使用MQTT的方式进行视频流的传输,可以使用MQTT协议的发布/订阅模式。具体步骤如下:
在STM32上编写代码,将视频流转换为二进制数据,并使用MQTT协议将数据发布到MQTT服务器上。
在PC端编写代码,使用MQTT协议从MQTT服务器上订阅视频流数据,并将数据转换为视频流进行显示。
下面是一个简单的示例代码:
STM32代码:
#include “stm32f4xx.h”
#include “stm32f4xx_gpio.h”
#include “stm32f4xx_rcc.h”
#include “stm32f4xx_dma.h”
#include “stm32f4xx_dcmi.h”
#include “MQTTClient.h”
#define BUFFER_SIZE 3202402
uint8_t buffer[BUFFER_SIZE];
void DCMI_Config(void)
{
DCMI_InitTypeDef DCMI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_DCMI);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOB, &GPIO_InitStructure);
DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_Continuous;
DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Falling;
DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High;
DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_Low;
DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_All_Frame;
DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b;
DCMI_Init(&DCMI_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&DCMI->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = BUFFER_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream1, &DMA_InitStructure);
DCMI_Cmd(ENABLE);
DMA_Cmd(DMA2_Stream1, ENABLE);
DCMI_CaptureCmd(ENABLE);
}
int main(void)
{
DCMI_Config();
Network network;
MQTTClient client;
NetworkInit(&network);
MQTTClientInit(&client, &network, 30000, NULL, 0, NULL, 0);
char* address = "tcp://localhost:1883";
int rc = NetworkConnect(&network, address, 1883);
if (rc != 0) {
printf("Failed to connect to MQTT broker: %d
", rc);
return -1;
}
MQTTMessage message;
message.qos = QOS0;
message.retained = 0;
message.payload = buffer;
message.payloadlen = BUFFER_SIZE;
while (1)
{
rc = MQTTClientPublish(&client, "video_stream", &message);
if (rc != 0) {
printf("Failed to publish video stream: %d
", rc);
}
}
}
PC端代码:
import paho.mqtt.client as mqtt
import cv2
import numpy as np
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(“video_stream”)
def on_message(client, userdata, msg):
data = np.frombuffer(msg.payload, dtype=np.uint8)
img = cv2.imdecode(data, cv2.IMREAD_COLOR)
cv2.imshow(“Video Stream”, img)
cv2.waitKey(1)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(“localhost”, 1883, 60)
while True:
client.loop_forever()
在这个例子中,我们使用了paho-mqtt库来实现MQTT协议的发布/订阅功能。在STM32代码中,我们使用了MQTTClient库来将视频流数据发布到MQTT服务器上。在PC端代码中,我们使用了OpenCV库来将二进制数据转换为视频流,并使用MQTT协议从MQTT服务器上订阅视频流数据。
总结
在本文中,我们介绍了如何使用MQTT协议进行视频流的传输。这个例子可以帮助你了解如何使用MQTT协议实现发布/订阅模式,并将视频流数据传输到PC端进行显示。文章来源地址https://www.toymoban.com/news/detail-460413.html
到了这里,关于STM32连接OV2640摄像头串口方式在PyQt5界面上展示视频界面 以及使用MQTT方式进行展示的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!