Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What high-end operations can be achieved by 10 lines of Python code?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

10 lines of Python code can achieve what high-end operations, in response to this question, this article describes in detail the corresponding analysis and solutions, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Python has won the favor of many developers for its concise code. As a result, it encourages more developers to develop new modules with Python, thus forming a virtuous circle, and Python can implement a lot of interesting operations with shorter code. Let's see what interesting features we can achieve in no more than 10 lines of code.

1. Generate a QR code

As a tool of information transmission, QR code plays an important role in today's society. It is also very easy to generate a QR code. In Python, we can generate a QR code through the MyQR module, but we only need 2 lines of code to generate a QR code. We first install the MyQR module. Here we choose the domestic source to download:

Pip install-I https://pypi.tuna.tsinghua.edu.cn/simple/ myqr

After 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 jump automatically. The text will be displayed directly. Chinese is not supported.

After we execute the code, we will generate a QR code under the project. Of course, we can also enrich the QR code:

From MyQR import myqrmyqr.run (words=' http://www.baidu.com', # contains information picture='lbxx.jpg', # background picture colorized=True, whether # has color, if False, black and white save_name='code.png' # output file name)

The effect picture is as follows:

In addition, MyQR also supports dynamic pictures.

two。 Generate word cloud

Word cloud is a very beautiful way of data visualization, through which we can directly see the frequency of some words. Using Python, we can generate word clouds through the wordcloud module. Let's install the wordcloud module first:

Pip install-I https://pypi.tuna.tsinghua.edu.cn/simple/ wordcloud

Then we can write code:

From wordcloud import WordCloudWordCloudwc = WordCloud # create word cloud object wc.generate ('Do not go gentle into that good night') # generate word cloud wc.to _ file ('wc.png') # save word cloud

After executing the code, the following words are generated:

Of course, this is only the simplest word cloud, and for more detailed operations, see WordCloud to generate Kakashi ninjitsu word cloud [1].

3. Batch matting

Matting needs the help of Baidu Flying Propeller's deep learning tool paddlepaddle. We need to install two modules to quickly realize batch matting. The first is PaddlePaddle:

Python-m pip install paddlepaddle-I https://mirror.baidu.com/pypi/simple

Another is the paddlehub model library:

Pip install-I https://mirror.baidu.com/pypi/simple paddlehub

Next, we only need 5 lines of code to achieve batch matting:

Import os, paddlehub as hubhubhumanseg = hub.Module (name='deeplabv3p_xception65_humanseg') # load model path = 'DGV _ humanseg.segmentation _ GrapImage _' # file directory files = [path + i for i in os.listdir (path)] # get file list results = humanseg.segmentation (data= {'image':files}) # matting

The matting effect is as follows:

The left side is the original image, and the right side is the yellow background image filled with matting.

4. Text emotion recognition

In the face of paddlepaddle, natural language processing has also become very simple. To achieve text emotion recognition, we also need to install PaddlePaddle and Paddlehub. For specific installation, please see part 3. And then there's the part of our code:

Import paddlehub as hub senta = hub.Module (name='senta_lstm') # load model sentence = [# words to be identified: 'you are so beautiful', 'you are so ugly','I'm so sad','I'm not happy', 'have a good time this game', 'what junk game',] results = senta.sentiment_classify (data= {"text": sentence}) # emotion recognition # output recognition result for result in results: print (result)

The result of recognition is a list of dictionaries:

{'text':' you are so beautiful, 'sentiment_label': 1,' sentiment_key': 'positive',' positive_probs': 0.9602, 'negative_probs': 0.0398} {' text': 'you are ugly', 'sentiment_label': 0,' sentiment_key': 'negative',' positive_probs': 0.0033, 'negative_probs': 0.9967} {' text':'I'm so sad', 'sentiment_label': 1 'sentiment_key':' positive', 'positive_probs': 0.5324,' negative_probs': 0.4676} {'text':' I'm not happy', 'sentiment_label': 0,' sentiment_key': 'negative',' positive_probs': 0.1936, 'negative_probs': 0.8064} {' text':'is a good game, 'sentiment_label': 1,' sentiment_key': 'positive' 'positive_probs': 0.9933, 'negative_probs': 0.0067} {' text': 'what junk game', 'sentiment_label': 0,' sentiment_key': 'negative',' positive_probs': 0.0108, 'negative_probs': 0.9892}

The sentiment_key field contains emotional information, and for a detailed analysis, see Python Natural language processing requires only 5 lines of code [2].

5. Identify if you are wearing a mask

Here are the same products that use PaddlePaddle. We install PaddlePaddle and Paddlehub according to the above steps, and then start writing code:

Import paddlehub as hub# load model module = hub.Module (name='pyramidbox_lite_mobile_mask') # picture list image_list = ['face.jpg'] # get picture dictionary input_dict = {' image':image_list} # check if you have a mask module.face_detection (data=input_dict)

After executing the above program, a detection_result folder will be generated under the project, and the identification results will be in it. The recognition results are as follows:

6. Simple information bombing

There are many ways for Python to control input devices, and we can use win32 or pynput modules. We can achieve the effect of information bombardment through a simple loop operation. Here, taking pynput as an example, we need to install the module first:

Pip install-I https://pypi.tuna.tsinghua.edu.cn/simple/ pynput

We need to manually get the coordinates of the input box before writing the code:

From pynput import mouse# create a mouse m_mouse = mouse.Controller# output mouse position print (m_mouse.position)

There may be a more efficient way, but I won't.

After we get it, we can record this coordinate, and don't move the message window. Then we execute the following code and switch the window to the message page:

Import timefrom pynput import mouse, keyboardtime.sleep (5) m_mouse = mouse.Controller # create a mouse m_keyboard = keyboard.Controller # create a keyboard m_mouse.position = # move the mouse to the specified location 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) # release enter time.sleep (0.5) # wait 0.5 seconds

I admit, this is more than 10 lines of code, and it's not high-end. The effect of sending a message to the secondary account before using QQ is as follows:

7. Identify the text in the picture

We can identify the text in the image through Tesseract, which is very easy to implement in Python, but downloading files and configuring environment variables are a little cumbersome, so this article only shows the code:

Import pytesseractfrom PIL import ImageImageimg = Image.open ('text.jpg') text = pytesseract.image_to_string (img) print (text)

Where text is the recognized text. If you are not satisfied with the accuracy, you can also use Baidu's general text interface.

8. Draw function image

Icons are important tools for data visualization. Matplotlib plays an important role in data visualization in Python. Let's take a look at how to draw a function image using matplotlib:

Import numpy as np from matplotlib import pyplot as plt x = np.arange (1Magazine 11) # x-axis data y=x*x+5 # functional relationship plt.title ("y=x*x+5") # Image title plt.xlabel ("x") # x-axis label plt.ylabel ("y") # y-axis label plt.plot (x-ray) # generate image plt.show # display image

The generated image is as follows:

9. artificial intelligence

The following is an introduction to the exclusive AI artificial intelligence, which is generally not spread out. This artificial intelligence can answer many questions. of course, artificial intelligence is still in the stage of development, and it is still a long way from understanding human language. Needless to say, let's take a look at our artificial intelligence Fdj:

While (True): question = input answer = question.replace (?) answeranswer = answer.replace (? ,'!') Print (answer)

Let's take a look at a simple test:

How are you? I'm fine! Did you eat? I'm eating! Are you going to bed? I'm going to bed!

It seems that our "Xiaofu" is still quite intelligent.

The answer to the question about what high-end operation 10 lines of Python code can achieve is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report