In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Python code to achieve a variety of cool functions of the example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Generate a QR code
QR code is also called 2D barcode. The common QR code is the full name Quick Response of QR Code,QR, which is a very popular coding method on mobile devices in recent years, and it is very easy to generate a QR code. In Python, we can generate a QR code through the MyQR module, while 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 qrcode
After the installation is complete, we can start writing code:
Import qrcodetext = input (enter text or URL:) # set URL must add http://img = qrcode.make (text) img.save () # Save the picture to the local directory, you can set the path img.show ()
After we execute the code, we will generate a QR code under the project. Of course, we can also enrich the QR code:
Let's install the MyQR module first
Pip install myqrdef gakki_code (): version, level, qr_name = myqr.run (words= https://520mg.com/it/#/main/2, # can be a string or URL (preceded by http (s): / /) version=1, # set fault tolerance to the highest level='H', # control error correction level, with the range of L, M, Q, H. Increase the picture=gakki.gif from left to right, # combine the QR code and the picture into colorized=True, # the color QR code contrast=1.0, # to adjust the contrast of the picture, 1. 0 represents the original picture, a smaller value indicates lower contrast, and vice versa. The default is 1.0 brightness=1.0. # is used to adjust the brightness of the picture. The other usage and values are the same as the above save_name=gakki_code.gif. # saves the name of the file. The format can be jpg,png,bmp,gif save_dir=os.getcwd () # control location) gakki_code ()
In addition, MyQR also supports dynamic pictures.
Second, generate word clouds
The word cloud, also known as the word cloud, is the visual prominent presentation of the "keywords" with high frequency in the text data, forming the rendering of the keywords to form a color picture similar to the cloud, so that you can appreciate the main expression meaning of the text data at a glance.
But as an old code farmer, I still like to use my own code to generate my own word cloud. Is it complicated? Will it take a long time? A lot of text describes a variety of methods, but in fact it only takes 10 lines of python code.
Install the necessary libraries first
Pip install wordcloudpip install jiebapip install matplotlibimport matplotlib.pyplot as pltfrom wordcloud import WordCloudimport jiebatext_from_file_with_apath = open ('/ Users/linuxmi/linuxmi.txt'). Read () wordlist_after_jieba = jieba.cut (text_from_file_with_apath, cut_all = True) wl_space_split = .join (wordlist_after_jieba) my_wordcloud = WordCloud () .generate (wl_space_split) plt.imshow (my_wordcloud) plt.axis (off) plt.show ()
That's all, a word cloud generated goes like this:
Read these 10 lines of code:
1x 3 lines, imported the library of drawing matplotlib, the word cloud generation library wordcloud and the lexicon of jieba, respectively
4 lines, is to read the local file
Line 5: 6, use jieba for word segmentation, and separate the results of the word segmentation with spaces
7 lines to generate word clouds for the text after word segmentation
Line 8: 10, use pyplot to show the word cloud picture.
This is one of the reasons why I like python, concise and lively.
Third, 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 hubhumanseg = 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 IV, 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 = [# prepare the sentences to be identified: 'you are so beautiful', 'you are so ugly','I'm so sad','I'm not happy', 'have a good time in 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 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 am unhappy', '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 games', '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 five lines of code.
Identify whether you are wearing a mask or not
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, the detection_result folder will be generated under the project, and the identification results will be in it.
VI. 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 = 670) # 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 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 Imageimg = 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.
A simple Mini Game
It feels very efficient to start with some small examples.
Import randomprint (1-100 number guessing game!) Num = random.randint (1100) guess = guessi = 0while guess! = num: I + = 1 guess = int (input (please enter the number you guessed:) if guess = = num: print (congratulations, you guessed it!) Elif guess < num: print (your guess is too small.) Else: print (you guessed a lot...) print (you guessed a total of% d% I + times) Thank you for reading this article carefully. I hope the article "sample Analysis of Python Code implementing various cool functions" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.