554 views|0 replies

279

Posts

0

Resources
The OP
 

Visual and interactive! Use OpenCV to read images on H618 and display them on a PyQt5 window [Copy link]

 

Here is a code that uses the image processing library opencv to obtain data from the camera and displays it in the pyqt5 window after scaling.

Install opencv

Create a pyqt5 window

1. Draw a window with Qt Designer
Here I use the designer software on my computer to create a Main Window type window. Drag a label from the component bar on the left and drop it in the middle of the window.

Click on the label in the window, and you can set many things in the property editor in the lower right corner of the software. I won't go into details here. Here I set QFrame to enable the border, the texte property in QLabel controls the displayed text, and the alignment property in QLabel controls the text alignment.

Then save it as a file ending with .ui

2. Convert the UI file drawn by designer into a py file

3. Write the main.py program and call the window just drawn to display
First import the ui_main.py and some qt libraries

Add some auxiliary code, one is to realize the display of Qt program on the desktop when running from the remote command line, and the other is to press ctrl+c shortcut key in the command line to force exit Qt program

Define the window class and rewrite some trigger events of the window. Here I modified the mousePressEvent that will be automatically called after the mouse is clicked and the paintEvent that will be called when the window is drawn.

Adding the part of calling the function for display, the basic program for displaying the pyqt5 window is complete

Effect display on Walnut Pie LCD screen

EEWORLDLINKTK2

How to read camera in opencv

Call the header file, opencv header file only needs this one

Open the camera, where the parameter 1 passed in is the camera number, usually starting from 0 and going backwards

Read a frame of image from the camera, ret is the reading status, frame is the image data

How to display opencv image data to qt label

The cap.read function reads in bgr format and needs to be converted to rgb format first

Convert the image to a QImage used to represent the image in Qt

The label's setPixmap method can overwrite the label with image data

Threads, signals and slots

Here we use the multithreading function that comes with Qt. It is very simple to use. You only need to create a class and inherit from QThread, and then write the things to be run under the run method in the class. After instantiating an object, call the start method to create a new thread

Directly calling functions in the thread to modify the contents of the Qt window cannot meet thread safety requirements.

We need to create a signal, write the statement to modify the Qt window into a slot, connect them, send a signal when we want to modify the window, and let Qt schedule it internally to prevent conflicts with other threads inside Qt.

Because our thread class inherits from QThread, we can define signals in the class. We just need to instantiate a pyqtSignal object. The parameters in the brackets when calling determine what type of parameters the slot function must have and what parameters need to be passed in when sending the signal.

signal_update_label = pyqtSignal(QPixmap)

To connect signals and slots, use the connect method

Use the emit method to send a signal. Qt will schedule all functions connected to this signal and pass the parameters to them. This is an important mechanism for Qt to achieve thread safety.

Final code

This post is from Domestic Chip Exchange
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list