This is the 16th day of my participation in the August More Text Challenge. For details, see:August is more challenging
preface
Python has won the hearts of many developers because of its clean code. This has encouraged more developers to develop new modules in Python, creating a virtuous circle in which Python can do a lot of interesting things with shorter code. Let’s see what interesting things we can do with less than 10 lines of code.
First, generate two-dimensional code
As a tool of information transmission, TWO-DIMENSIONAL code plays an important role in today’s society. And generate a TWO-DIMENSIONAL code is also very simple, in Python we can generate two-dimensional code through MyQR module, and generate a two-dimensional code we only need 2 lines of code, we first install MyQR module, here select the source download:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ myqr
Copy the code
Once the installation is complete we can start writing code:
from MyQR import myqr # Note case
myqr.run(words='http://www.baidu.com') If it is a website, it will automatically jump, the text will be displayed directly, do not support Chinese
Copy the code
After we execute the code, we will generate a QR code under the project. Of course, we can also enrich the TWO-DIMENSIONAL code:
from MyQR import myqr
myqr.run(
words='http://www.baidu.com'.# Include information
picture='lbxx.jpg'.# Background Image
colorized=True.# Whether there is a color, if False, black and white
save_name='code.png' # output file name
)
Copy the code
The renderings are as follows:In addition, MyQR also supports dynamic pictures.
2. Generate word cloud
Word cloud is a very beautiful way of data visualization, through which we can intuitively see the frequency of some words. Using Python we can generate word clouds from the WordCloud module. Let’s install the WordCloud module first:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ wordcloud
Copy the code
Then we can write the code:
from wordcloud import WordCloud
wc = WordCloud() Create a word cloud object
wc.generate('Do not go gentle into that good night') # Generate word clouds
wc.to_file('wc.png') # Save the word cloud
Copy the code
After executing the code, the following word cloud is generated:Of course, this is only the simplest word cloud. For more detailed operations on word clouds, seeWordCloud generates a kakashi ninjutsu WordCloud.
Three, batch cutout
The realization of matting needs to use the deep learning tool paddlePaddle of Baidu paddle, we need to install two modules to quickly realize batch matting, the first one is Paddlepaddle:
python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
Copy the code
Another is the PaddleHub model library:
pip install -i https://mirror.baidu.com/pypi/simple paddlehub
Copy the code
For more detailed installation, please refer to the official website of the flying paddle: www.paddlepaddle.org.cn/
Next, we only need 5 lines of code to implement batch matting:
import os, paddlehub as hub
humanseg = hub.Module(name='deeplabv3p_xception65_humanseg') # Load model
path = 'D:/CodeField/Workplace/PythonWorkplace/GrapImage/' # File directory
files = [path + i for i in os.listdir(path)] # Get the list of files
results = humanseg.segmentation(data={'image':files}) # cutout
Copy the code
Image matting effect is as follows:The left is the original image, and the right is the image with yellow background after matting.
Four, word emotion recognition
Natural language processing also becomes very simple in front of Paddlepaddle. We also need to install PaddlePaddle and Paddlehub to realize word emotion recognition. For details, see Section 3. Then there is our code:
import paddlehub as hub
senta = hub.Module(name='senta_lstm') # Load model
sentence = [ Prepare the statement to be identified
'You are so beautiful'.'You're ugly'.'I'm so sad'.'I'm not happy'.'This is a great game.'.'What a game!',
]
results = senta.sentiment_classify(data={"text":sentence}) # Emotion recognition
# Output identification results
for result in results:
print(result)
Copy the code
The result of the recognition is a list of dictionaries:
{'text': 'you're beautiful ', 'sentiment_label': 1, 'sentiment_key': 'positive', 'positive_probs': 0.9602, 'negative_probs': {sentiment_label': 0, 'sentiment_key': 'negative', 'positive_probs': 0.0033, 'negative_probs': 0.0033; } {'text': 'I'm so sad ', 'sentiment_label': 1, 'positive_probs': 0.5324, 'negative_probs': } {'text': 'I'm not happy ', 'sentiment_label': 0, 'sentiment_key': 'negative', 'positive_probs': 0.1936, 'negative_probs': Sentiment_label ': 1, 'sentiment_key': 'positive', 'positive_probs': Sentiment_label: 0, 'sentiment_key': 0, 'negative_probs': 0.0067 'negative', 'positive_probs': 0.0108, 'negative_probs': 0.9892}Copy the code
The sentiment_key field contains the emotional information, which can be analyzed in detail in Python Natural Language Processing, which requires only 5 lines of code.
Five, identify whether you are wearing a mask
Here is the same product that uses PaddlePaddle, we install PaddlePaddle and Paddlehub in the steps above, and then start writing code:
import paddlehub as hub
# Load model
module = hub.Module(name='pyramidbox_lite_mobile_mask')
# Image list
image_list = ['face.jpg']
# Get the picture dictionary
input_dict = {'image':image_list}
# Check if you are wearing a mask
module.face_detection(data=input_dict)
Copy the code
After executing the above program, the detection_RESULT folder will be generated under the project, and the identification results will be in it. The identification effect is as follows:
Six, simple information bombing
There are many ways that Python can control input devices. We can use win32 or PyNput modules. In this case, we need to install the module first:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pynput
Copy the code
Before writing code we need to manually retrieve the coordinates of the input box:
from pynput import mouse
Create a mouse
m_mouse = mouse.Controller()
# Output mouse position
print(m_mouse.position)
Copy the code
There may be more efficient ways, but I won’t.
After that we can record the coordinates, the message window does not move. Then we execute the following code and switch the window to the messages page:
import time
from pynput import mouse, keyboard
time.sleep(5)
m_mouse = mouse.Controller() Create a mouse
m_keyboard = keyboard.Controller() Create a keyboard
m_mouse.position = (850.670) Move the mouse to the specified position
m_mouse.click(mouse.Button.left) Click the left mouse button
while(True):
m_keyboard.type('hello') # typing
m_keyboard.press(keyboard.Key.enter) # press enter
m_keyboard.release(keyboard.Key.enter) # to loosen the enter
time.sleep(0.5) Wait 0.5 seconds
Copy the code
I admit, this is more than 10 lines of code, and it’s not high-end. Before using QQ to small message effect is as follows:
7. Identify the text in the picture
We can use Tesseract to identify text in images, which is quite simple to implement in Python, but a bit cumbersome to download files, configure environment variables, etc., so this article will only show the code:
import pytesseract
from PIL import Image
img = Image.open('text.jpg')
text = pytesseract.image_to_string(img)
print(text)
Copy the code
Where text is the recognized text. If you are not satisfied with the accuracy, you can also use Baidu’s general text interface.
Viii. Drawing function images
ICONS are an important tool for data visualization. Matplotlib plays an important role in data visualization in Python. Let’s look at how to draw a function image using Matplotlib:
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(1.11) # X-axis data
y = x * x + 5 # Function relation
plt.title("y=x*x+5") # Image title
plt.xlabel("x") # X-axis tag
plt.ylabel("y") # Y-axis tag
plt.plot(x,y) # Generate image
plt.show() # Display image
Copy the code
The generated images are as follows:
Artificial intelligence
The following is an exclusive INTRODUCTION of AI artificial intelligence, generally not outside. This ai can answer a lot of questions, of course ai is still in the development stage, it is far from understanding human language. Without further ado, let’s take a look at our artificial intelligence Fdj:
while(True):
question = input()
answer = question.replace('it'.'it')
answer = answer.replace('? '.'! ')
print(answer)
Copy the code
Here’s a simple test:
How are you? I'm good! Have you eaten yet? I ate! Are you going to bed? I'm trying to sleep!Copy the code
It seems that we “small” or more intelligent.