def get_data(json_content): imgpath = json_content.get('imagePath') data.append(imgpath) flags = json_content.get('flags') values = list(flags. values()) label = 0 for i in range(len( values)): if values [i]: label= i+1 data = [] data.append(imgpath) data.append(label) return data temp_json=read_json(r'D:\data_1\flowers\1185.json') print(get_data(temp_json))
时间: 2024-02-20 21:58:42 浏览: 27
在这段代码中,第6行的`data`列表在函数中被重复初始化了。你需要把第6行移到第3行之前,以确保`data`列表只被初始化一次。此外,在第9行中,你应该使用`values()`方法而不是`values.`。修改后的代码应该是这样的:
```python
def get_data(json_content):
data = []
imgpath = json_content.get('imagePath')
data.append(imgpath)
flags = json_content.get('flags')
values = list(flags.values())
label = 0
for i in range(len(values)):
if values[i]:
label = i + 1
data.append(label)
return data
temp_json = read_json(r'D:\data_1\flowers\1185.json')
print(get_data(temp_json))
```
这个函数将返回一个包含图像路径和标签的列表。在这个例子中,我们打印了返回的数据,以确保函数正常工作。
相关问题
url="https://www.woyaogexing.com/tupian/weimei/2023/213014.html" headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"} res=requests.get(url,headers=headers) soup=bs4.BeautifulSoup(res.text,'html.parser') img_soup=soup.find_all('img',class_="lazy") for ench in img_soup: img_url=ench["src"] print(type(img_url)) imgname=os.path.basename(img_url) imgpath=os.path.join('oooo',imgname) with open (imgpath,'w') as f: img_data=requests.get(img_url).content print(img_data) f.write(img_data)
这段代码的作用是爬取网页 https://www.woyaogexing.com/tupian/weimei/2023/213014.html 中所有带有 class="lazy" 的 img 标签的图片,并将图片保存到本地文件夹 oooo 中。
但是代码中存在一个问题,就是在打开文件的时候使用了 'w' 模式,这会将文件以文本形式打开并写入数据,而图片是二进制数据,因此应该使用 'wb' 模式打开文件。另外,建议使用 try-except 语句来处理请求或保存文件时可能出现的异常。
可以参考以下代码:
```
import os
import requests
import bs4
url = "https://www.woyaogexing.com/tupian/weimei/2023/213014.html"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}
res = requests.get(url, headers=headers)
soup = bs4.BeautifulSoup(res.text, 'html.parser')
img_soup = soup.find_all('img', class_="lazy")
for ench in img_soup:
img_url = ench["src"]
print(type(img_url))
img_name = os.path.basename(img_url)
img_path = os.path.join('oooo', img_name)
try:
with open(img_path, 'wb') as f:
img_data = requests.get(img_url).content
f.write(img_data)
print(f"Saved {img_name} successfully!")
except Exception as e:
print(f"Failed to save {img_name} due to {e}")
```
import gzip import os import pickle import numpy as np def load_mnist(path, kind='train'): labels_path = os.path.join(path, '%s-labels-idx1-ubyte.gz' % kind) images_path = os.path.join(path, '%s-images-idx3-ubyte.gz' % kind) with gzip.open(labels_path, 'rb') as lbpath: labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8) with gzip.open(images_path, 'rb') as imgpath: images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(len(labels), 784) return images, labels def get_mnist_data(num_training=5000, num_validation=500, num_test=500): mnist_dir = r'D:\daima\mnist' # 修改为mnist数据集所在的目录 X_train, y_train = load_mnist(mnist_dir, kind='train') X_test, y_test = load_mnist(mnist_dir, kind='t10k') print(X_train.shape) mask = range(num_training, num_training + num_validation) X_val = X_train[mask] y_val = y_train[mask] mask = range(num_training) X_train = X_train[mask] y_train = y_train[mask] mask = range(num_test) X_test = X_test[mask] y_test = y_test[mask] X_train = X_train.astype('float32') / 255 X_val = X_val.astype('float32') / 255 X_test = X_test.astype('float32') / 255 return { 'X_train': X_train, 'y_train': y_train, 'X_val': X_val, 'y_val': y_val, 'X_test': X_test, 'y_test': y_test, },这是读取mnist的函数,如何把解包时给定的值数量从两个增加到4个
在这个函数中,load_mnist函数返回了两个值,即images和labels,但是这个函数只使用了一个返回值,所以只需要将这个函数的返回值改为一个元组即可。具体来说,将return images, labels改为return (images, labels)即可。这样get_mnist_data函数就可以使用解包时给定的值数量为4了。修改后的代码如下:
```
import gzip
import os
import pickle
import numpy as np
def load_mnist(path, kind='train'):
labels_path = os.path.join(path, '%s-labels-idx1-ubyte.gz' % kind)
images_path = os.path.join(path, '%s-images-idx3-ubyte.gz' % kind)
with gzip.open(labels_path, 'rb') as lbpath:
labels = np.frombuffer(lbpath.read(), dtype=np.uint8, offset=8)
with gzip.open(images_path, 'rb') as imgpath:
images = np.frombuffer(imgpath.read(), dtype=np.uint8, offset=16).reshape(len(labels), 784)
return images, labels
def get_mnist_data(num_training=5000, num_validation=500, num_test=500):
mnist_dir = r'D:\daima\mnist' # 修改为mnist数据集所在的目录
X_train, y_train = load_mnist(mnist_dir, kind='train')
X_test, y_test = load_mnist(mnist_dir, kind='t10k')
print(X_train.shape)
mask = range(num_training, num_training + num_validation)
X_val = X_train[mask]
y_val = y_train[mask]
mask = range(num_training)
X_train = X_train[mask]
y_train = y_train[mask]
mask = range(num_test)
X_test = X_test[mask]
y_test = y_test[mask]
X_train = X_train.astype('float32') / 255
X_val = X_val.astype('float32') / 255
X_test = X_test.astype('float32') / 255
return X_train, y_train, X_val, y_val, X_test, y_test
```
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)