点击上方“小白学视觉”,选择加"星标"或“置顶

重磅干货,第一时间送达

最近 ChatGPT 发行了,这是由 OpenAI 开发的AI聊天机器人,专门研究对话。它的目标是使AI系统更自然地与之互动,但是在编写代码时也可以为您提供帮助。

您可以让 ChatGPT 做你的编程助理,甚至更多!

在过去的几天里,我一直在玩 ChatGPT,并使用它完成了一些小任务。

任务1:绘制线性回归

如果要绘制线性回归,可以告诉 ChatGPT:

使用 Python 的 matplotlib 库绘制线性回归

84caa438e52c0cefcae066b45156355d.png

我们可以通过告诉ChatGPT应该使用的具体库。

这是 ChatGPT 创建的 Python 代码(包括注释!)

import matplotlib.pyplot as plt
import numpy as np


# Generate some random data
x = np.random.rand(100)
y = x + np.random.normal(0, 0.1, size=100)


# Fit a linear regression to the data
m, b = np.polyfit(x, y, 1)


# Plot the data and the fitted line
plt.plot(x, y, 'o')
plt.plot(x, m*x + b, '-')
plt.show()

以下是输出:

7d455932c1016584a654853060e32b2d.png

这很简单,所以让我们尝试一些更困难的事情。

任务2:与 PyWhatKit 上的 WhatsApp 发送消息

我很想知道 ChatGPT 是否可以帮助我们使用 PyWheKit 等第三方库。该库允许我们使用 WhatsApp 发送消息。

使用 Python和 pywhkit 在 whatsapp 上发送消息

a67669a91ff8fd1ca7bcbe367a9d7409.png

为了本文的目的,我没有告诉 ChatGPT 我的电话号码或我想发送的消息来(虽然我为Task3做了类似的告知)。

这是ChatGPT创建的Python代码。

import pywhatkit


# Set the time in 24-hour format (HH:MM:SS) when you want the message to be sent
time = "HH:MM:SS"
# Set the phone number that you want to send the message to
phone_number = "1234567890"
# Set the message that you want to send
message = "Your message goes here"
# Use the sendwhatmsg method to send the message at the specified time
pywhatkit.sendwhatmsg(time, phone_number, message)

除了ChatGPT生成的代码外,您还必须在浏览器中打开 WhatsApp 会话,并安装 PyWhekit

任务3:使用 Python 发送电子邮件

互联网上有数百个代码段向您展示如何使用 Python 一起发送电子邮件。您也可以使用 ChatGPT 做到:

将来自“ email_1”的电子邮件发送到“ email_2”,并带有“ ChatGPT发送的电子邮件”和“ ChatGPT rocks!”的主题。

31eb11c00f3fce24b1e2f1c896fe0eaa.png

以下是代码:

import smtplib


# create an SMTP object
smtp_obj = smtplib.SMTP('smtp.gmail.com', 587)


# start TLS encryption
smtp_obj.starttls()


# login to the email account
smtp_obj.login('codelessearnmore@gmail.com', 'YOUR_PASSWORD')


# construct the email message
message = "Subject: Email sent by ChatGPT\n\nChatGPT rocks!"


# send the email
smtp_obj.sendmail('codelessearnmore@gmail.com', 'frank@codelessearnmore.com', message)


# close the SMTP connection
smtp_obj.quit()

输入代码后,ChatGPT 告诉我们,我们应该在“ YOUR_PASSWORD”占位符中输入我们的密码。

ChatGPT 这次没有提示我们的(有时确实如此)是,我们必须打开2步验证才能获取16个字符的密码,以防我们使用Gmail

无论如何,ChatGPT 已经做得很棒了。输入密码后,我收到了电子邮件:

f99cd3fc9ca4bf77c7000091e8a55d64.png

任务4:从“Books to Scrape”中爬取数据

我给 ChatGPT 的最具挑战性的任务是爬取一个网站。如您所知,网站具有不同的HTML,因此爬取网站的步骤随着网站的变化而变化。

ChatGPT 从 https://books.toscrape.com/ 网站上爬取书的名称和价格

7fb2070c36967883ec6dfba99c2fd805.png

以下是代码:

import requests
from bs4 import BeautifulSoup


# Make a GET request to the website
response = requests.get("https://books.toscrape.com/")


# Parse the HTML content of the website
soup = BeautifulSoup(response.text, "html.parser")


# Extract the data you want using Beautiful Soup's various methods and attributes
titles = soup.find_all("h3")
prices = soup.find_all(class_="price_color")


# Save the extracted data to a file or database, or use it for some other purpose
with open("book_data.txt", "w") as file:
    for title, price in zip(titles, prices):
        file.write(f"{title.text}: {price.text}\n")

这是爬取的数据:

0b6de03005158c032d1c7876d68c8066.png

爬取数据而无需自己编写代码……令人惊讶!

附加:使用 Python 生成 Excel 表格

我还尝试使用 ChatGPT 生成 Excel 表格

我无法想出一个好主意来帮助我自动化 Excel 表格,所以我只尝试了一下。

使用 Python 生成 Excel 表格

320e2d9077942735aeaad7dcf50d039c.png

以下是生成的代码:

# Import the `openpyxl` library
import openpyxl
# Create a new Excel workbook
workbook = openpyxl.Workbook()
# Get the active sheet in the workbook (which will be the only sheet at this point)
sheet = workbook.active
# Add some data to the sheet
sheet["A1"] = "Product"
sheet["B1"] = "Sales"
# Add some more data to the sheet
products = ["Product A", "Product B", "Product C"]
sales = [100, 200, 300]
for i in range(len(products)):
    sheet.cell(row=i+2, column=1).value = products[i]
    sheet.cell(row=i+2, column=2).value = sales[i]
# Save the workbook to a file
workbook.save("sales_report.xlsx")

以下是输出:

4b057a79a1ed83f5a314263e1181cf6f.png

如您所见,我只有一个简单的模板,但我相信您只需要给 ChatGPT 提供正确的命令即可获得令人惊讶的结果。

 
 

好消息!

小白学视觉知识星球

开始面向外开放啦👇👇👇

 
 

52cd99d08fcd3d5acd563466889fe72d.jpeg

下载1:OpenCV-Contrib扩展模块中文版教程

在「小白学视觉」公众号后台回复:扩展模块中文教程,即可下载全网第一份OpenCV扩展模块教程中文版,涵盖扩展模块安装、SFM算法、立体视觉、目标跟踪、生物视觉、超分辨率处理等二十多章内容。


下载2:Python视觉实战项目52讲
在「小白学视觉」公众号后台回复:Python视觉实战项目,即可下载包括图像分割、口罩检测、车道线检测、车辆计数、添加眼线、车牌识别、字符识别、情绪检测、文本内容提取、面部识别等31个视觉实战项目,助力快速学校计算机视觉。


下载3:OpenCV实战项目20讲
在「小白学视觉」公众号后台回复:OpenCV实战项目20讲,即可下载含有20个基于OpenCV实现20个实战项目,实现OpenCV学习进阶。


交流群

欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器、自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~
Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐