- preface
- PyQt5 interface design
- Use Qt Designer to draw the interface
- Video component insertion tips
- Decoder download
- Function implementation
- Trace. MOE API introduction and video
- Use Nuitka to package as an exe file
preface
Friends who like watching cartoons can probably experience a uncomfortable thing, is in the forum or group chat inside to see a cartoon screenshot, want to know its origin, but Baidu search a circle but there is no reliable results, it is very depressed. Today we will take you to use Python to do a simple “search by image” small application. The implementation of the application itself is not very difficult, in fact, is to call other people’s API interface to achieve, the main purpose or through this case to learn the following content:
- “Learn how to make user interface (UI) with PyQt5”;
- “Learn how to use Nuitka to package a program as an EXE file”;
PyQt5 interface design
When developing a “cross-platform” graphical application in Python, there are three main options:
-
Tkinter: A Python library based on Tk. It is the official Standard library of Python. It has the advantages of being stable as the Python standard library and small distribution programs, but has the disadvantages of relatively few controls.
-
“WxPython” : A Python library based on wxWidgets. The advantages are rich controls, but the disadvantages are relatively poor stability, less documentation, and fewer users.
-
“PyQt5” (or PySide2) : A Qt based Python library with rich controls, good cross-platform experience, complete documentation, and a large number of users. The disadvantage is that the library is relatively large, released the program is relatively large.
This tutorial uses PyQt5, a combination of Digia’s Qt5 application framework and Python that supports both 2.x and 3.x. This tutorial uses 3.x. Qt libraries by the Riverbank Computing development, is one of the most powerful GUI library’s official website: https://www.riverbankcomputing.com/software/pyqt/. PyQt5 is a series of Python modules. More than 620 classes, 6000 functions and methods. It runs on major operating systems such as Unix, Windows and Mac OS. PyQt5 comes in two types of certificates, GPL and commercial certificates. PIP install PyQt5
Use Qt Designer to draw the interface
PyQt5 Qt Designer application, its file name is Designer.exe, can not find the location can be searched in the local file: PyQt5 Qt Designer application: PyQt5 Qt Designer application: PyQt5 Qt Designer
The following figure shows a preliminary interface design of this application. A UI design can be completed through the coordination of the four marked areas, and the project will be saved as one.ui
UI files with suffixed names are best placed alongside Python code files. The interface design is not complex, two buttons, one to open the picture, another click after the query; A QComboBox widget for selecting alternative outcomes; A picture display area to display the opened query picture, a result display area to display the specific content of the query result. Below the interface is the video display area, that is, the video clip in which the picture appears in the original animation.
Concrete basic usage of Qt Designer I had, is not here if you are using for the first time, you can refer to this introductory video tutorial: https://www.bilibili.com/video/BV1cJ411R7bP, to be also quite good, I just follow this video introduction of learning. The text version of the tutorial here: http://www.python3.vip/tut/py/gui/qt_01/
PYQT Integration is a plugin that allows you to preview the UI and edit the UI at any time. It is also easy to convert UI files into Python code.
Video component insertion tips
Adding a video display section to the UI I spent a lot of time researching, mainly“There are no widgets for video players in Qt Designer“I can edit the Python code directly, but I want to do it in Qt Designer to unify the process. After some research, the following methods are summarized:“The first step“: Drag a container Widget to the edit interface;“The second step“: Right-click the Widget and choose Promote to… (Promote to…) “; “The third step“: Set promoted class name toQVideoWidget
, “Header file” isPyQt5.QtMultimediaWidgets
“The fourth step“: Click Add, then click Promote. When reflected in compiled Python code, you add a sentencefrom PyQt5.QtMultimediaWidgets import QVideoWidget
In use, you can refer to the following code (just an example), here only play the video, there is no pause, show the progress bar and other functions, if you need to add, you can refer to this tutorial: https://stackoverflow.com/questions/57842104/how-to-play-videos-in-pyqt
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
Initialize a mediaPlayer first
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.mediaPlayer.setVideoOutput(self.ui.VideoDisplay)
# In the function that needs to use this mediaPlayer, open the video locally and play it self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(file_name))) self.mediaPlayer.play() Copy the code
Decoder download
In addition, you need to download a decoder, otherwise when playing mp4 video, you will get an error as shown below. LAV decoder to download and install, download address:LAV 0.74.1: Installer (both x86/ X64)
Function implementation
Trace. MOE API introduction and video
The implementation behind it relies on “Big Data + Content-based Image Retrieval (CBIR),” which means searching and analyzing the Content of an image, rather than image-related metadata such as keywords, tags, or descriptions. The term “content” in this context may refer to color, shape, texture, or any other information that can be derived from the image itself. CBIR is easier to use because it does not require annotation information, whereas a purely metadata-dependent search relies on the quality and completeness of the annotation. Wikipedia lists many CBIR engines, and Trace.moe uses Lire. For image description, trace. MOE only uses Color Layout. On the other hand is big data, which supports 30,096 hours of video content (about 2.6 billion frames) from 3,194 animations, about 18.1 TB in size. 746 million frame index (after deduplication), database size is 140 GB. For implementation details, see the Trace. MOE Slide and the Trace. MOE Github project.
If you don’t care about implementation details, you can just look at the following API interface.
import requests
img_path = 'xxx.png'
traceMoe_api = "https://trace.moe/api/search"
files = {"image": ('anime.png', open(img_path, 'rb'))}
res = requests.post(traceMoe_api, files=files) Copy the code
Returns a JSON result that looks like this:Docs contains the possible results, which look like this:
Once you have these, you can download the corresponding video clip:
url = f"https://trace.moe/preview.php? anilist_id={item['anilist_id']}&file={item['filename']}&t={item['at']}&token={item['tokenthumb']}"
video = requests.get(url)
with open(item['filename'].'wb') as f:
f.write(video.content)
Copy the code
Of course, there are not unlimited requests per day, the average user can only query 150 times per day
Use Nuitka to package as an exe file
Nuitka is used to convert Python programs into executable ELF files in C. In this way, at runtime, you can enjoy the optimization of C language processing and improve speed. PyQT5 UI files converted to PY files converted to C language, the interface will open in seconds.“The first step“:Download MinGW64 8.1, decompress the file to the root directory of drive C, and add the bin path to the environment variable. Then install Nuitka:pip install nuitka
; This means success:
❝
Import system library, using PYTHon3x.dll to execute, other self-implemented UI interface and database connection and function and function implementation, need encryption (decompilation) and quick response, the user experience is here, this part with Nuitka to achieve. Here are Nuitka’s key command segments: “–nofollow-imports # all imports are discarded and python3x. DLL is executed –follow-import-to=need #need Name the py folder you need to compile into C/C++” — quotes from: Python package exe(32/64 bit)-Nuitka next city
❞
“The second step“: Debug phase, add the required wheel file one by one: run firstnuitka --standalone --mingw64 --show-memory --show-progress --nofollow-imports --plugin-enable=qt-plugins --follow-import-to=need --output-dir=output app_main.py
Then run app_main.exe in output\app_main.dist to find the files of missing wheels one by one and add them to output\app_main.dist as shown below, indicating that they are missingrequests
And other software, such as Everything and XSearch, can be used to quickly locate the location of the file (follow the public account: Ye Feng, reply to “file search” to obtain the software). Add until it is ready to run.“Remember to put these library files in a separate folder and then copy them to the official output folder“. “The third step“: Generation phase, runnuitka --standalone --windows-disable-console --mingw64 --nofollow-imports --show-memory --show-progress --plugin-enable=qt-plugins --follow-import-to=need --recurse-all --output-dir=output app_main.py
Then copy the library file you just found into the app_main.dist folder. App_main. dist, click app_main.exe to run (provided LAV decoder is installed, otherwise the video will not be displayed!). .
Act out the following: https://zhuanlan.zhihu.com/p/156851623
This article Python source code and exe packaged software, follow my public account “wild wind students”, reply “yipan” can be obtained. As a programmer, I continue to share technical articles on machine learning basics and applications, LeetCode interviewing algorithms, and Python Basics and Applications, as well as frequently recommend high quality software tools, websites, and books.